home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / sh-utils-1.12-src.tgz / tar.out / fsf / sh-utils / src / logname.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  105 lines

  1. /* logname -- print user's login name
  2.    Copyright (C) 90, 91, 92, 93, 1994 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
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <getopt.h>
  22.  
  23. #include "version.h"
  24. #include "system.h"
  25.  
  26. /* The name this program was run with. */
  27. char *program_name;
  28.  
  29. /* If non-zero, display usage information and exit.  */
  30. static int show_help;
  31.  
  32. /* If non-zero, print the version on standard output and exit.  */
  33. static int show_version;
  34.  
  35. static struct option const long_options[] =
  36. {
  37.   {"help", no_argument, &show_help, 1},
  38.   {"version", no_argument, &show_version, 1},
  39.   {0, 0, 0, 0}
  40. };
  41.  
  42. static void
  43. usage (status)
  44.      int status;
  45. {
  46.   if (status != 0)
  47.     fprintf (stderr, "Try `%s --help' for more information.\n",
  48.          program_name);
  49.   else
  50.     {
  51.       printf ("Usage: %s [OPTION]...\n", program_name);
  52.       printf ("\
  53. \n\
  54.   --help      display this help and exit\n\
  55.   --version   output version information and exit\n\
  56. ");
  57.     }
  58.   exit (status);
  59. }
  60.  
  61. main (argc, argv)
  62.      int argc;
  63.      char **argv;
  64. {
  65.   register char *cp;
  66.   int c;
  67.  
  68.   program_name = argv[0];
  69.  
  70.   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
  71.     {
  72.       switch (c)
  73.     {
  74.     case 0:
  75.       break;
  76.  
  77.     default:
  78.       usage (1);
  79.     }
  80.     }
  81.  
  82.   if (show_version)
  83.     {
  84.       printf ("logname - %s\n", version_string);
  85.       exit (0);
  86.     }
  87.  
  88.   if (show_help)
  89.     usage (0);
  90.  
  91.   if (argc - optind != 0)
  92.     usage (1);
  93.  
  94.   /* POSIX.2 requires using getlogin (or equivalent code).  */
  95.   cp = getlogin ();
  96.   if (cp)
  97.     {
  98.       puts (cp);
  99.       exit (0);
  100.     }
  101.   /* POSIX.2 prohibits using a fallback technique.  */
  102.   fprintf (stderr,"%s: no login name\n", argv[0]);
  103.   exit (1);
  104. }
  105.