LCOV - code coverage report
Current view: top level - src - whoami.c (source / functions) Hit Total Coverage
Test: coreutils.info Lines: 24 26 92.3 %
Date: 2018-01-30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* whoami -- print effective userid
       2             : 
       3             :    Copyright (C) 89,90, 1991-1997, 1999-2002, 2004, 2005, 2007 Free Software
       4             :    Foundation, Inc.
       5             : 
       6             :    This program is free software: you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation, either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18             : 
      19             : /* Equivalent to `id -un'. */
      20             : /* Written by Richard Mlynarik. */
      21             : 
      22             : #include <config.h>
      23             : #include <stdio.h>
      24             : #include <sys/types.h>
      25             : #include <pwd.h>
      26             : #include <getopt.h>
      27             : 
      28             : #include "system.h"
      29             : #include "error.h"
      30             : #include "long-options.h"
      31             : #include "quote.h"
      32             : 
      33             : /* The official name of this program (e.g., no `g' prefix).  */
      34             : #define PROGRAM_NAME "whoami"
      35             : 
      36             : #define AUTHORS "Richard Mlynarik"
      37             : 
      38             : /* The name this program was run with. */
      39             : char *program_name;
      40             : 
      41             : void
      42          30 : usage (int status)
      43             : {
      44          30 :   if (status != EXIT_SUCCESS)
      45          28 :     fprintf (stderr, _("Try `%s --help' for more information.\n"),
      46             :              program_name);
      47             :   else
      48             :     {
      49           2 :       printf (_("Usage: %s [OPTION]...\n"), program_name);
      50           2 :       fputs (_("\
      51             : Print the user name associated with the current effective user ID.\n\
      52             : Same as id -un.\n\
      53             : \n\
      54             : "), stdout);
      55           2 :       fputs (HELP_OPTION_DESCRIPTION, stdout);
      56           2 :       fputs (VERSION_OPTION_DESCRIPTION, stdout);
      57           2 :       emit_bug_reporting_address ();
      58             :     }
      59          30 :   exit (status);
      60             : }
      61             : 
      62             : int
      63          33 : main (int argc, char **argv)
      64             : {
      65             :   struct passwd *pw;
      66             :   uid_t uid;
      67             : 
      68             :   initialize_main (&argc, &argv);
      69          33 :   program_name = argv[0];
      70          33 :   setlocale (LC_ALL, "");
      71             :   bindtextdomain (PACKAGE, LOCALEDIR);
      72             :   textdomain (PACKAGE);
      73             : 
      74          33 :   atexit (close_stdout);
      75             : 
      76          33 :   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
      77             :                       usage, AUTHORS, (char const *) NULL);
      78          30 :   if (getopt_long (argc, argv, "", NULL, NULL) != -1)
      79           6 :     usage (EXIT_FAILURE);
      80             : 
      81          24 :   if (optind != argc)
      82             :     {
      83          22 :       error (0, 0, _("extra operand %s"), quote (argv[optind]));
      84          22 :       usage (EXIT_FAILURE);
      85             :     }
      86             : 
      87           2 :   uid = geteuid ();
      88           2 :   pw = getpwuid (uid);
      89           2 :   if (pw)
      90             :     {
      91           2 :       puts (pw->pw_name);
      92           2 :       exit (EXIT_SUCCESS);
      93             :     }
      94           0 :   fprintf (stderr, _("%s: cannot find name for user ID %lu\n"),
      95             :            program_name, (unsigned long int) uid);
      96           0 :   exit (EXIT_FAILURE);
      97             : }

Generated by: LCOV version 1.10