home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / shar349.zip / src / who@wher.c < prev   
C/C++ Source or Header  |  1990-09-24  |  3KB  |  110 lines

  1. /* $Header: /u/rhg/src/shar/who@where.c,v 3.49 90/09/12 15:15:33 rhg Exp $ */
  2.  
  3. /*+-------------------------------------------------------------------------
  4.     who@where.c - find out who i am & where i am
  5.     ...!gatech!kd4nc!n4hgf!wht (wht%n4hgf@gatech.edu)
  6. --------------------------------------------------------------------------*/
  7. /*+:EDITS:*/
  8. /*:09-12-1990-01:04-rhg@cps.com-added declarations of strcpy and strcat */
  9. /*:09-09-1990-19:49-rhg@cps.com-added explicit return statement to who_where */
  10. /*:04-03-1990-19:55-wht@n4hgf-get rid of complicated who_am_i */
  11. /*:04-01-1990-13:30-pat@rwing-use utsname.nodename instead of sysname */
  12. /*:04-02-1990-12:12-wht@n4hgf-sigh... some pwd.h dont declare functions */
  13. /*:03-29-1990-18:23-wht@n4hgf-add automatic sequent support */
  14. /*:03-28-1990-15:24-wht@n4hgf-creation */
  15.  
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <pwd.h>
  19.  
  20. /* assume system v unless otherwise fixed */
  21. #if (defined(pyr) || defined(vax) || defined(sequent)) && !defined(BSD42) && !defined(SYS5)
  22. #define BSD42
  23. #endif
  24. #if defined(sun)    /* this miscreant doesn't exactly fit BSD or SYSV */
  25. #undef BSD42
  26. #undef SYS5
  27. #endif
  28. #if !defined(BSD42) && !defined(sun) && !defined(SYS5)
  29. #define SYS5
  30. #endif
  31.  
  32. #if defined(sun) || defined(BSD42)
  33. #define strchr    index
  34. #define strrchr    rindex
  35. #endif
  36.  
  37. #if !defined(SYS5) || defined(sun)
  38. #include <sys/time.h>
  39. extern int errno;
  40. #else
  41. #include <sys/utsname.h>
  42. #include <time.h>
  43. #endif    /* system dependencies */
  44.  
  45. char *strcpy();
  46. char *strcat();
  47. struct passwd *getpwuid();
  48.  
  49. /*+-------------------------------------------------------------------------
  50.     who_am_i() - get user name
  51. --------------------------------------------------------------------------*/
  52. char *
  53. who_am_i()
  54. {
  55.     struct passwd *passwd;
  56.     passwd = getpwuid(getuid());
  57.     (void)endpwent();
  58.     if(passwd == (struct passwd *)0)
  59.         return("???");
  60.     return(passwd->pw_name);
  61.  
  62. }    /* end of who_am_i */
  63.  
  64. /*+-------------------------------------------------------------------------
  65.     where_am_i() - do uname, gethostname, or read file (/etc/systemid)
  66. --------------------------------------------------------------------------*/
  67. char *
  68. where_am_i()
  69. {
  70. #if defined(M_SYS5)    /* SCO UNIX or XENIX */
  71. FILE *fpsid = fopen("/etc/systemid","r");
  72. static char s20[20];
  73.     if(!fpsid)
  74.         return("???");
  75.     fgets(s20,sizeof(s20),fpsid);
  76.     fclose(fpsid);
  77.     s20[strlen(s20) - 1] = 0;
  78.     return(s20);
  79. #else /* M_SYS5 */
  80. #if defined(SYS5)
  81. static struct utsname where_i_am;
  82.     uname(&where_i_am);
  83.     return(where_i_am.nodename);
  84. #else /* SYS5 */
  85. static char where_i_am[64];
  86.     gethostname(where_i_am,sizeof(where_i_am));
  87.     return(where_i_am);
  88. #endif /* SYS5 */
  89. #endif /* M_SYS5 */
  90. }    /* end of where_am_i */
  91.  
  92. /*+-------------------------------------------------------------------------
  93.     who_where(buf)
  94. --------------------------------------------------------------------------*/
  95. char *
  96. who_where(buf)
  97. char *buf;
  98. {
  99. static char ww[64];
  100.  
  101.     if(!buf)
  102.         buf = ww;
  103.     strcpy(buf,who_am_i());
  104.     strcat(buf,"@");
  105.     return(strcat(buf,where_am_i()));
  106. }    /* end of who_where */
  107.  
  108. /* vi: set tabstop=4 shiftwidth=4: */
  109. /* end of who@where.c */
  110.