Research V7 development
authorDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 07:02:04 +0000 (02:02 -0500)
committerDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 07:02:04 +0000 (02:02 -0500)
Work on file usr/src/cmd/who.c

Co-Authored-By: Ken Thompson <ken@research.uucp>
Synthesized-from: v7

usr/src/cmd/who.c [new file with mode: 0644]

diff --git a/usr/src/cmd/who.c b/usr/src/cmd/who.c
new file mode 100644 (file)
index 0000000..194fe96
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * who
+ */
+
+#include <stdio.h>
+#include <utmp.h>
+#include <pwd.h>
+struct utmp utmp;
+struct passwd *pw;
+struct passwd *getpwuid();
+
+char *ttyname(), *rindex(), *ctime(), *strcpy(), *index();
+main(argc, argv)
+char **argv;
+{
+       register char *tp, *s;
+       register FILE *fi;
+
+       s = "/etc/utmp";
+       if(argc == 2)
+               s = argv[1];
+       if (argc==3) {
+               tp = ttyname(0);
+               if (tp)
+                       tp = index(tp+1, '/') + 1;
+               else {  /* no tty - use best guess from passwd file */
+                       pw = getpwuid(getuid());
+                       strcpy(utmp.ut_name, pw?pw->pw_name: "?");
+                       strcpy(utmp.ut_line, "tty??");
+                       time(&utmp.ut_time);
+                       putline();
+                       exit(0);
+               }
+       }
+       if ((fi = fopen(s, "r")) == NULL) {
+               puts("who: cannot open utmp");
+               exit(1);
+       }
+       while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
+               if(argc==3) {
+                       if (strcmp(utmp.ut_line, tp))
+                               continue;
+#ifdef interdata
+                       printf("(Interdata) ");
+#endif
+                       putline();
+                       exit(0);
+               }
+               if(utmp.ut_name[0] == '\0' && argc==1)
+                       continue;
+               putline();
+       }
+}
+
+putline()
+{
+       register char *cbuf;
+
+       printf("%-8.8s %-8.8s", utmp.ut_name, utmp.ut_line);
+       cbuf = ctime(&utmp.ut_time);
+       printf("%.12s\n", cbuf+4);
+}