home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / pathalias10 / part03 / local.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  2KB  |  88 lines

  1. /* pathalias -- by steve bellovin, as told to peter honeyman */
  2. #ifndef lint
  3. static char    *sccsid = "@(#)local.c    9.2 88/06/10";
  4. #endif /* lint */
  5.  
  6. #include <stdio.h>
  7. #include "config.h"
  8.  
  9. #ifdef    UNAME
  10. #include <sys/utsname.h>
  11.  
  12. char    *
  13. local()
  14. {
  15.     static struct utsname utsname;
  16.     extern int uname();
  17.  
  18.     (void) uname(&utsname);
  19.     return(utsname.nodename);
  20. }
  21.  
  22. #else /* !UNAME */
  23.  
  24. char    *
  25. local()
  26. {
  27.     static char lname[64];
  28.     extern int gethostname();
  29.  
  30.     (void) gethostname(lname, (int) sizeof(lname));
  31.     lname[sizeof(lname)] = 0;
  32.     return(lname);
  33. }
  34.  
  35. #ifndef GETHOSTNAME
  36.  
  37. STATIC int
  38. gethostname(name, len)
  39.     char *name;
  40.     int len;
  41. {    FILE *whoami;
  42.     char *ptr;
  43.     extern int pclose();
  44.     extern FILE *fopen(), *popen();
  45.  
  46.     *name = '\0';
  47.  
  48.     /* try /etc/whoami */
  49.     if ((whoami = fopen("/etc/whoami", "r")) != 0) {
  50.         (void) fgets(name, len, whoami);
  51.         (void) fclose(whoami);
  52.         if ((ptr = index(name, '\n')) != 0)
  53.             *ptr = '\0';
  54.     }
  55.     if (*name)
  56.         return 0;
  57.  
  58.     /* try /usr/include/whoami.h */
  59.     if ((whoami = fopen("/usr/include/whoami.h", "r")) != 0) {
  60.         while (!feof(whoami)) {
  61.             char    buf[100];
  62.  
  63.             if (fgets(buf, 100, whoami) == 0)
  64.                 break;
  65.             if (sscanf(buf, "#define sysname \"%[^\"]\"", name))
  66.                 break;
  67.         }
  68.         (void) fclose(whoami);
  69.         if (*name)
  70.             return 0;
  71.     }
  72.  
  73.     /* ask uucp */
  74.     if ((whoami = popen("uuname -l", "r")) != 0) {
  75.         (void) fgets(name, len, whoami);
  76.         (void) pclose(whoami);
  77.         if ((ptr = index(name, '\n')) != 0)
  78.             *ptr = '\0';
  79.     }
  80.     if (*name)
  81.         return 0;
  82.     
  83.     /* aw hell, i give up!  is this really unix? */
  84.     return -1;
  85. }
  86. #endif /* GETHOSTNAME */
  87. #endif /* UNAME */
  88.