home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / sharutils-4.1-src.tgz / tar.out / fsf / sharutils / whoami.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  119 lines

  1. /* Find out who I am & where I am.
  2.    Copyright (C) 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.  
  19. #include "system.h"
  20.  
  21. /* Size of buffers holding strings.  */
  22. #define STRING_BUFFER_SIZE 64
  23.  
  24. #include <pwd.h>
  25.  
  26. /* On Ultrix 2.x, <utsname.h> uses SYS_NMLEN, which is only defined in
  27.    <limits.h>.  Sigh!  */
  28.  
  29. #ifdef HAVE_UNAME
  30. # ifdef HAVE_LIMITS_H
  31. #  include <limits.h>
  32. # endif
  33. # include <sys/utsname.h>
  34. # include <time.h>
  35. #else
  36. # include <sys/time.h>
  37. #endif
  38.  
  39. #ifdef HAVE_UNISTD_H
  40. # include <unistd.h>
  41. #endif
  42.  
  43. #ifndef _POSIX_VERSION
  44. struct passwd *getpwuid ();
  45. #endif
  46.  
  47. /*----------------.
  48. | Get user name.  |
  49. `----------------*/
  50.  
  51. static const char *
  52. get_username (void)
  53. {
  54.   struct passwd *passwd;
  55.  
  56.   passwd = getpwuid (getuid ());
  57.   endpwent ();
  58.   if (passwd == NULL)
  59.     return "???";
  60.   return passwd->pw_name;
  61. }
  62.  
  63. /*------------------------------------------------------.
  64. | Do uname, gethostname, or read file (/etc/systemid).  |
  65. `------------------------------------------------------*/
  66.  
  67. static const char *
  68. get_hostname (void)
  69. {
  70. #if HAVE_ETC_SYSTEMID
  71.  
  72.   FILE *fpsid = fopen ("/etc/systemid", "r");
  73.   static char buffer[STRING_BUFFER_SIZE];
  74.  
  75.   if (!fpsid)
  76.     return "???";
  77.   fgets (buffer, sizeof (buffer), fpsid);
  78.   fclose (fpsid);
  79.   buffer[strlen (buffer) - 1] = 0;
  80.   return buffer;
  81.  
  82. #else /* not HAVE_ETC_SYSTEMID */
  83.  
  84. #if HAVE_UNAME
  85.  
  86.   static struct utsname hostname_buffer;
  87.  
  88.   uname (&hostname_buffer);
  89.   return hostname_buffer.nodename;
  90.  
  91. #else /* not HAVE_UNAME */
  92.  
  93.   static char hostname_buffer[STRING_BUFFER_SIZE];
  94.  
  95.   gethostname (hostname_buffer, sizeof (hostname_buffer));
  96.   return hostname_buffer;
  97.  
  98. #endif /* not HAVE_UNAME */
  99. #endif /* not HAVE_ETC_SYSTEMID */
  100. }
  101.  
  102. /*------------------------------------------------------------------.
  103. | Fill BUFFER with a string representing the username and hostname  |
  104. | separated by an `@' sign, and return a pointer to the constructed |
  105. | string.  If BUFFER is NULL, use an internal buffer instead.        |
  106. `------------------------------------------------------------------*/
  107.  
  108. char *
  109. get_submitter (char *buffer)
  110. {
  111.   static char static_buffer[STRING_BUFFER_SIZE];
  112.  
  113.   if (!buffer)
  114.     buffer = static_buffer;
  115.   strcpy (buffer, get_username ());
  116.   strcat (buffer, "@");
  117.   return strcat (buffer, get_hostname ());
  118. }
  119.