|
|
1.1 ! root 1: /* ! 2: * who ! 3: */ ! 4: ! 5: #include <stdio.h> ! 6: #include <utmp.h> ! 7: #include <pwd.h> ! 8: #include <sys/types.h> ! 9: #include <sys/stat.h> ! 10: struct utmp utmp; ! 11: struct passwd *pw; ! 12: struct passwd *getpwuid(); ! 13: int idleflg; ! 14: int whoami; ! 15: ! 16: char *mytty(), *ttyname(), *ctime(), *strcpy(), *strchr(), *getlogin(); ! 17: long time(); ! 18: ! 19: main(argc, argv) ! 20: char **argv; ! 21: { ! 22: register char *tp, *s; ! 23: register FILE *fi; ! 24: char *myname = getlogin(); ! 25: ! 26: s = "/etc/utmp"; ! 27: if (argc>1 && strcmp(argv[1], "-i")==0) { ! 28: idleflg++; ! 29: argc--; ! 30: argv++; ! 31: } ! 32: if(argc == 2) ! 33: s = argv[1]; ! 34: if(argc == 3) { ! 35: whoami++; ! 36: tp = mytty(); ! 37: } ! 38: if ((fi = fopen(s, "r")) == NULL) { ! 39: fprintf(stderr, "who: cannot open %s\n", s); ! 40: exit(1); ! 41: } ! 42: while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) { ! 43: if(whoami) { ! 44: if (strncmp(utmp.ut_line, tp, sizeof(utmp.ut_line))) ! 45: continue; ! 46: strcpy(utmp.ut_name, myname); ! 47: putline(); ! 48: exit(0); ! 49: } ! 50: if(utmp.ut_name[0] == '\0' && argc==1) ! 51: continue; ! 52: putline(); ! 53: } ! 54: if (whoami) { ! 55: strcpy(utmp.ut_name, myname); ! 56: strcpy(utmp.ut_line, tp); ! 57: time(&utmp.ut_time); ! 58: putline(); ! 59: } ! 60: exit(0); ! 61: } ! 62: ! 63: char * ! 64: mytty() ! 65: { ! 66: char *tp; ! 67: ! 68: if ((tp = ttyname(0)) == NULL ! 69: || (tp = strchr(tp+1, '/')) == NULL) ! 70: return ("tty??"); ! 71: return (tp+1); ! 72: } ! 73: ! 74: putline() ! 75: { ! 76: struct stat statb; ! 77: char tname[64]; ! 78: ! 79: printf("%-8.8s %-8.8s %.12s", ! 80: utmp.ut_name, utmp.ut_line, 4+ctime(&utmp.ut_time)); ! 81: if (idleflg) { ! 82: long t = time((long *)0); ! 83: strcpy(tname, "/dev/"); ! 84: strncat(tname, utmp.ut_line, 8); ! 85: if (stat(tname, &statb)>=0 ! 86: && t > statb.st_atime+600) { ! 87: t -= statb.st_atime; ! 88: t /= 60; ! 89: printf("%5.0d:%.2d", (int)t/60, (int)t%60); ! 90: } ! 91: } ! 92: printf("\n"); ! 93: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.