home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / uumail / gethostnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.2 KB  |  68 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)gethostnam.c    6.1 (down!honey) 85/01/21";
  3. static char    rcsid[] = "$Header: gethostnam.c,v 6.2 85/08/03 00:25:16 UUCP Exp $";
  4. #endif lint
  5.  
  6. #ifndef GETHOSTNAME
  7. #include "uuconf.h"
  8.  
  9. void
  10. gethostname(name, len)
  11. char    *name;
  12. {
  13.     FILE    *whoami, *fopen(), *popen();
  14.     char    *ptr, *index();
  15. #ifdef SYSIII
  16.     struct utsname utsn;
  17. #endif
  18.  
  19.     *name = '\0';
  20. #ifdef SYSIII
  21.     if (uname(&utsn) != -1)
  22.     {
  23.         strcpy(name,utsn.nodename);
  24.         len = strlen(name);
  25.           return;
  26.     }
  27. #endif
  28.  
  29.     /* try /etc/whoami */
  30.     if ((whoami = fopen("/etc/whoami", "r")) != 0) {
  31.         (void) fgets(name, len, whoami);
  32.         (void) fclose(whoami);
  33.         if ((ptr = index(name, '\n')) != 0)
  34.             *ptr = '\0';
  35.     }
  36.     if (*name)
  37.         return;
  38.  
  39.     /* try /usr/include/whoami.h */
  40.     if ((whoami = fopen("/usr/include/whoami.h", "r")) != 0) {
  41.         while (!feof(whoami)) {
  42.             char    buf[100];
  43.  
  44.             if (fgets(buf, 100, whoami) == 0)
  45.                 break;
  46.             if (sscanf(buf, "#define sysname \"%[^\"]\"", name))
  47.                 break;
  48.         }
  49.         (void) fclose(whoami);
  50.         if (*name)
  51.             return;
  52.     }
  53.  
  54.     /* ask uucp */
  55.     if ((whoami = popen("uuname -l", "r")) != 0) {
  56.         (void) fgets(name, len, whoami);
  57.         (void) pclose(whoami);
  58.         if ((ptr = index(name, '\n')) != 0)
  59.             *ptr = '\0';
  60.     }
  61.     if (*name)
  62.         return;
  63.     
  64.     /* failure */
  65.     return;
  66. }
  67. #endif GETHOSTNAME
  68.