home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / pathalias9 / part01 / local.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-08  |  1.4 KB  |  83 lines

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