1 /* logname -- print user's login name
2    Copyright (C) 1990-2023 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
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/>.  */
16 
17 #include <config.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20 
21 #include "system.h"
22 #include "long-options.h"
23 #include "quote.h"
24 
25 /* The official name of this program (e.g., no 'g' prefix).  */
26 #define PROGRAM_NAME "logname"
27 
28 #define AUTHORS proper_name ("FIXME: unknown")
29 
30 void
usage(int status)31 usage (int status)
32 {
33   if (status != EXIT_SUCCESS)
34     emit_try_help ();
35   else
36     {
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);
45     }
46   exit (status);
47 }
48 
49 int
main(int argc,char ** argv)50 main (int argc, char **argv)
51 {
52   char *cp;
53 
54   initialize_main (&argc, &argv);
55   set_program_name (argv[0]);
56   setlocale (LC_ALL, "");
57   bindtextdomain (PACKAGE, LOCALEDIR);
58   textdomain (PACKAGE);
59 
60   atexit (close_stdout);
61 
62   parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
63                                    Version, true, usage, AUTHORS,
64                                    (char const *) nullptr);
65 
66   if (optind < argc)
67     {
68       error (0, 0, _("extra operand %s"), quote (argv[optind]));
69       usage (EXIT_FAILURE);
70     }
71 
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"));
77 
78   puts (cp);
79   return EXIT_SUCCESS;
80 }
81