1: static char *sccsid = "@(#)who.c	4.4 (Berkeley) 3/15/82";
   2: /*
   3:  * who
   4:  */
   5: 
   6: #include <whoami.h>
   7: #include <stdio.h>
   8: #include <utmp.h>
   9: #include <pwd.h>
  10: #include <ctype.h>
  11: 
  12: #define NMAX sizeof(utmp.ut_name)
  13: #define LMAX sizeof(utmp.ut_line)
  14: 
  15: struct  utmp utmp;
  16: struct  passwd *pw;
  17: struct  passwd *getpwuid();
  18: 
  19: char    *ttyname(), *rindex(), *ctime(), *strcpy();
  20: 
  21: main(argc, argv)
  22:     int argc;
  23:     char **argv;
  24: {
  25:     register char *tp, *s;
  26:     register FILE *fi;
  27:     extern char _sobuf[];
  28: 
  29:     setbuf(stdout, _sobuf);
  30:     s = "/etc/utmp";
  31:     if(argc == 2)
  32:         s = argv[1];
  33:     if (argc==3) {
  34:         tp = ttyname(0);
  35:         if (tp)
  36:             tp = rindex(tp, '/') + 1;
  37:         else {  /* no tty - use best guess from passwd file */
  38:             pw = getpwuid(getuid());
  39:             strcpy(utmp.ut_name, pw?pw->pw_name: "?");
  40:             strcpy(utmp.ut_line, "tty??");
  41:             time(&utmp.ut_time);
  42:             putline();
  43:             exit(0);
  44:         }
  45:     }
  46:     if ((fi = fopen(s, "r")) == NULL) {
  47:         puts("who: cannot open utmp");
  48:         exit(1);
  49:     }
  50:     while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
  51:         if(argc==3) {
  52:             if (strcmp(utmp.ut_line, tp))
  53:                 continue;
  54:             putline();
  55:             exit(0);
  56:         }
  57:         if(utmp.ut_name[0] == '\0' && argc==1)
  58:             continue;
  59:         putline();
  60:     }
  61: }
  62: 
  63: putline()
  64: {
  65:     register char *cbuf;
  66: 
  67:     printf("%-*.*s %-*.*s", NMAX, NMAX, utmp.ut_name, LMAX, LMAX, utmp.ut_line);
  68:     cbuf = ctime(&utmp.ut_time);
  69:     printf("%.12s\n", cbuf+4);
  70: }

Defined functions

main defined in line 21; never used
putline defined in line 63; used 3 times

Defined variables

pw defined in line 16; used 3 times
sccsid defined in line 1; never used
utmp defined in line 15; used 12 times

Defined macros

LMAX defined in line 13; used 2 times
  • in line 67(2)
NMAX defined in line 12; used 2 times
  • in line 67(2)
Last modified: 1983-07-27
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 759
Valid CSS Valid XHTML 1.0 Strict