Tizen 2.0 Release
[external/tizen-coreutils.git] / src / logname.c
1 /* logname -- print user's login name
2    Copyright (C) 1990-1997, 1999-2005 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <getopt.h>
22
23 #include "system.h"
24 #include "error.h"
25 #include "long-options.h"
26 #include "quote.h"
27
28 /* The official name of this program (e.g., no `g' prefix).  */
29 #define PROGRAM_NAME "logname"
30
31 #define AUTHORS "FIXME: unknown"
32
33 /* The name this program was run with. */
34 char *program_name;
35
36 void
37 usage (int status)
38 {
39   if (status != EXIT_SUCCESS)
40     fprintf (stderr, _("Try `%s --help' for more information.\n"),
41              program_name);
42   else
43     {
44       printf (_("Usage: %s [OPTION]\n"), program_name);
45       fputs (_("\
46 Print the name of the current user.\n\
47 \n\
48 "), stdout);
49       fputs (HELP_OPTION_DESCRIPTION, stdout);
50       fputs (VERSION_OPTION_DESCRIPTION, stdout);
51       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
52     }
53   exit (status);
54 }
55
56 int
57 main (int argc, char **argv)
58 {
59   char *cp;
60
61   initialize_main (&argc, &argv);
62   program_name = argv[0];
63   setlocale (LC_ALL, "");
64   bindtextdomain (PACKAGE, LOCALEDIR);
65   textdomain (PACKAGE);
66
67   atexit (close_stdout);
68
69   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
70                       usage, AUTHORS, (char const *) NULL);
71   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
72     usage (EXIT_FAILURE);
73
74   if (optind < argc)
75     {
76       error (0, 0, _("extra operand %s"), quote (argv[optind]));
77       usage (EXIT_FAILURE);
78     }
79
80   /* POSIX requires using getlogin (or equivalent code).  */
81   cp = getlogin ();
82   if (cp)
83     {
84       puts (cp);
85       exit (EXIT_SUCCESS);
86     }
87   /* POSIX prohibits using a fallback technique.  */
88
89   error (0, 0, _("no login name"));
90   exit (EXIT_FAILURE);
91 }