|
|
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: ! 15: char *cttyname(), *rindex(), *ctime(), *strcpy(), *index(), *getlogin(); ! 16: long time(); ! 17: ! 18: main(argc, argv) ! 19: char **argv; ! 20: { ! 21: register char *tp, *s; ! 22: register FILE *fi; ! 23: char *myname = getlogin(); ! 24: ! 25: s = "/etc/utmp"; ! 26: if (argc>1 && strcmp(argv[1], "-i")==0) { ! 27: idleflg++; ! 28: argc--; ! 29: argv++; ! 30: } ! 31: if(argc == 2) ! 32: s = argv[1]; ! 33: tp = cttyname(0); ! 34: if (tp) ! 35: tp = index(tp+1, '/') + 1; ! 36: else ! 37: tp = "tty??"; ! 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(argc==3) { ! 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 (argc==3) { ! 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: putline() ! 64: { ! 65: struct stat statb; ! 66: char tname[64]; ! 67: ! 68: printf("%-8.8s %-8.8s %.12s", ! 69: utmp.ut_name, utmp.ut_line, 4+ctime(&utmp.ut_time)); ! 70: if (idleflg) { ! 71: long t = time(0); ! 72: strcpy(tname, "/dev/"); ! 73: strncat(tname, utmp.ut_line, 8); ! 74: if (stat(tname, &statb)>=0 ! 75: && t > statb.st_atime+600) { ! 76: t -= statb.st_atime; ! 77: t /= 60; ! 78: printf("%5.0d:%.2d", (int)t/60, (int)t%60); ! 79: } ! 80: } ! 81: printf("\n"); ! 82: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.