test: avoid false failure with setgid directories
[coreutils.git] / src / logname.c
blobb2cabc9fbbcdaef20b8871c168fecbc16e4156ea
1 /* logname -- print user's login name
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
18 #include <stdio.h>
19 #include <sys/types.h>
21 #include "system.h"
22 #include "long-options.h"
23 #include "quote.h"
25 /* The official name of this program (e.g., no 'g' prefix). */
26 #define PROGRAM_NAME "logname"
28 #define AUTHORS proper_name ("FIXME: unknown")
30 void
31 usage (int status)
33 if (status != EXIT_SUCCESS)
34 emit_try_help ();
35 else
37 printf (_("Usage: %s [OPTION]\n"), program_name);
38 fputs (_("\
39 Print the user's login name.\n\
40 \n\
41 "), stdout);
42 fputs (HELP_OPTION_DESCRIPTION, stdout);
43 fputs (VERSION_OPTION_DESCRIPTION, stdout);
44 emit_ancillary_info (PROGRAM_NAME);
46 exit (status);
49 int
50 main (int argc, char **argv)
52 char *cp;
54 initialize_main (&argc, &argv);
55 set_program_name (argv[0]);
56 setlocale (LC_ALL, "");
57 bindtextdomain (PACKAGE, LOCALEDIR);
58 textdomain (PACKAGE);
60 atexit (close_stdout);
62 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
63 Version, true, usage, AUTHORS,
64 (char const *) nullptr);
66 if (optind < argc)
68 error (0, 0, _("extra operand %s"), quote (argv[optind]));
69 usage (EXIT_FAILURE);
72 /* POSIX requires using getlogin (or equivalent code) and prohibits
73 using a fallback technique. */
74 cp = getlogin ();
75 if (! cp)
76 error (EXIT_FAILURE, 0, _("no login name"));
78 puts (cp);
79 return EXIT_SUCCESS;