home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part03 / who.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  2.3 KB  |  114 lines

  1. /* who.c: clone of who program
  2. Daniel J. Bernstein, brnstnd@nyu.edu.
  3. Depends on getopt.h, fmt.h.
  4. Requires BSD-style utmp and gethostname.
  5. 7/22/91: Baseline. who 2.0, public domain.
  6. No known patent problems.
  7.  
  8. Documentation in who.1.
  9.  
  10. This is based on the who.c that came with pty 3.0 but is much cleaner.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <utmp.h>
  16. #include <time.h>
  17. extern char *asctime();
  18. extern char *ttyname();
  19. #include "config/utmpfile.h"
  20. #include "getopt.h"
  21. #include "fmt.h"
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.  int opt;
  28.  int flagloggedin;
  29.  int flagwhoami;
  30.  char *ttyn;
  31.  static struct utmp ut;
  32.  static char hostname[120];
  33.  char *fn;
  34.  FILE *fi;
  35.  static char output[500];
  36.  char *t;
  37.  unsigned int len;
  38.  
  39.  flagloggedin = 2;
  40.  flagwhoami = 0;
  41.  while ((opt = getopt(argc,argv,"lL")) != opteof)
  42.    switch(opt)
  43.     {
  44.      case 'L':
  45.        flagloggedin = 1;
  46.        break;
  47.      case 'l':
  48.        flagloggedin = 0;
  49.        break;
  50.      case '?':
  51.      default:
  52.        exit(1);
  53.     }
  54.  argc -= optind;
  55.  argv += optind;
  56.  
  57.  if (argc > 1)
  58.   {
  59.    flagwhoami = 1;
  60.    if (!isatty(0))
  61.     {
  62.      fprintf(stderr,"who: fatal: stdin not a tty\n");
  63.      exit(1);
  64.     }
  65.    ttyn = ttyname(0) + 5; /* XXX: /dev/ */
  66.    gethostname(hostname,sizeof(hostname));
  67.   }
  68.  
  69.  if (flagloggedin == 2)
  70.    flagloggedin = !argc;
  71.  fn = ((argc == 1) ? argv[0] : UTMP_FILE);
  72.  
  73.  if (!(fi = fopen(fn,"r")))
  74.   {
  75.    t = output;
  76.    t += fmt_strncpy(t,"who: cannot open ",0);
  77.    t += fmt_strncpy(t,fn,(output + sizeof(output)) - t - 3);
  78.    *t = 0;
  79.    perror(output);
  80.    exit(1);
  81.   }
  82.  
  83.  while (fread((char *) &ut,sizeof(ut),1,fi))
  84.   {
  85.    if (ut.ut_name[0] || !flagloggedin)
  86.      if (!flagwhoami || !strncmp(ut.ut_line,ttyn,8))
  87.       {
  88.        t = output;
  89.        if (flagwhoami)
  90.     {
  91.      t += fmt_strncpy(t,hostname,0);
  92.      *t++ = '!';
  93.     }
  94.        len = fmt_strncpy(t,"        ",0);
  95.        t[fmt_strncpy(t,ut.ut_name,len)] = ' '; t += len; *t++ = ' ';
  96.        len = fmt_strncpy(t,"        ",0);
  97.        t[fmt_strncpy(t,ut.ut_line,len)] = ' '; t += len;
  98.        len = fmt_strncpy(t,"            ",0);
  99.        t[fmt_strncpy(t,asctime(localtime(&ut.ut_time)) + 4,len)] = ' ';
  100.        t += len;
  101.        if (ut.ut_host[0])
  102.     {
  103.      *t++ = '\t';
  104.      *t++ = '(';
  105.      t += fmt_strncpy(t,ut.ut_host,16);
  106.      *t++ = ')';
  107.     }
  108.        *t = 0;
  109.        puts(output);
  110.       }
  111.   }
  112.  exit(0);
  113. }
  114.