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