home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / libamiga / gethostname.c < prev    next >
C/C++ Source or Header  |  1990-12-25  |  2KB  |  114 lines

  1. /*    :ts=4
  2.  *    v7 gethostname simulation
  3.  *    taken from pathalias and cleaned up.  Thanks, Peter.
  4.  *
  5.  *    $Id: gethostname.c,v 1.1 90/05/21 23:50:53 crash Exp Locker: crash $
  6.  *
  7.  *    $Log:    gethostname.c,v $
  8.  * Revision 1.1  90/05/21  23:50:53  crash
  9.  * Initial revision
  10.  * 
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. #ifndef index
  16. # define index    strchr
  17. # define rindex    strrchr
  18. #endif
  19.  
  20. /* imports from libc */
  21. extern char *index(), *strncpy();
  22. extern FILE *fopen(), *popen();
  23.  
  24. /* forwards */
  25. void readhostfile();
  26.  
  27. #define MAXHOST 256
  28.  
  29. #ifndef min
  30. # define min(a,b) ((a) < (b)? (a): (b))
  31. #endif
  32.  
  33. static char defhost[] = "anonymous";
  34.  
  35. #ifdef AMIGA
  36. static char buff[BUFSIZ];
  37. #else
  38. static char *namefiles[] = {
  39.     "/etc/whoami",
  40.     "/etc/systemid",
  41.     NULL,
  42. };
  43. #endif /* AMIGA */
  44.  
  45. int gethostname( register char *hostname, int size)
  46. {
  47. #ifdef AMIGA
  48.     *hostname = '\0';
  49.  
  50.     /* try the NEWSCTL directory for a file called "whoami" */
  51.     strcpy(buff, ctlfile("whoami"));
  52.     readhostfile(hostname, size, buff);
  53.     if (*hostname != '\0')
  54.         return( 0 );
  55. #else
  56.     register FILE *whoami;
  57.     register char **namep;
  58.  
  59.     *hostname = '\0';
  60.  
  61.     /* try files in namefiles */
  62.     for (namep = namefiles; *namep != NULL; namep++) {
  63.         readhostfile(hostname, size, *namep);
  64.         if (*hostname != '\0')
  65.             return 0;
  66.     }
  67.     /* try /usr/include/whoami.h */
  68.     if ((whoami = fopen("/usr/include/whoami.h", "r")) != NULL) {
  69.         while (!feof(whoami)) {
  70.             char sysname[MAXHOST];
  71.  
  72.             if (fgets(sysname, MAXHOST, whoami) == NULL)
  73.                 break;
  74.             if (sscanf(sysname, "#define sysname \"%[^\"]\"",
  75.                 hostname) > 0)
  76.                 break;
  77.         }
  78.         (void) fclose(whoami);
  79.         if (*hostname != '\0')
  80.             return 0;
  81.     }
  82.     /* ask uucp */
  83.     if ((whoami = popen("PATH=/bin:/usr/bin:/usr/ucb uuname -l", "r")) != NULL) {
  84.         register char *ptr;
  85.  
  86.         (void) fgets(hostname, size, whoami);
  87.         (void) pclose(whoami);
  88.         if ((ptr = index(hostname, '\n')) != NULL)
  89.             *ptr = '\0';
  90.     }
  91.     if (*hostname != '\0')
  92.         return 0;
  93. #endif /* AMIGA */
  94.  
  95.     /* aw hell, i give up!  is this a real unix? */
  96.     (void) strncpy(hostname, defhost, min(sizeof defhost, size));
  97.     return 0;
  98. }
  99.  
  100. /* read host name from file */
  101. static void readhostfile( char *hostname, int size, char *file)
  102. {
  103.     register FILE *whoami;
  104.  
  105.     if ((whoami = fopen(file, "r")) != NULL) {
  106.         register char *ptr;
  107.  
  108.         (void) fgets(hostname, size, whoami);
  109.         (void) fclose(whoami);
  110.         if ((ptr = index(hostname, '\n')) != NULL)
  111.             *ptr = '\0';
  112.     }
  113. }
  114.