backward compatible processing for "+/pattern"
[unix-history] / usr / src / usr.bin / who / who.c
index 22bc4eb..93d7b7f 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)who.c      5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)who.c      5.5 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -30,7 +30,6 @@ static char sccsid[] = "@(#)who.c     5.1 (Berkeley) %G%";
 struct utmp utmp;
 struct passwd *pw;
 struct passwd *getpwuid();
 struct utmp utmp;
 struct passwd *pw;
 struct passwd *getpwuid();
-char   hostname[32];
 
 char   *ttyname(), *rindex(), *ctime(), *strcpy();
 
 
 char   *ttyname(), *rindex(), *ctime(), *strcpy();
 
@@ -40,9 +39,7 @@ main(argc, argv)
 {
        register char *tp, *s;
        register FILE *fi;
 {
        register char *tp, *s;
        register FILE *fi;
-       extern char _sobuf[];
 
 
-       setbuf(stdout, _sobuf);
        s = "/etc/utmp";
        if(argc == 2)
                s = argv[1];
        s = "/etc/utmp";
        if(argc == 2)
                s = argv[1];
@@ -51,31 +48,34 @@ main(argc, argv)
                if (tp)
                        tp = rindex(tp, '/') + 1;
                else {  /* no tty - use best guess from passwd file */
                if (tp)
                        tp = rindex(tp, '/') + 1;
                else {  /* no tty - use best guess from passwd file */
-                       pw = getpwuid(getuid());
-                       strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
-                       strcpy(utmp.ut_line, "tty??");
-                       time(&utmp.ut_time);
-                       putline();
+                       (void)strcpy(utmp.ut_line, "tty??");
+                       guess();
                        exit(0);
                }
        }
                        exit(0);
                }
        }
-       if ((fi = fopen(s, "r")) == NULL) {
-               puts("who: cannot open utmp");
+       if (!(fi = fopen(s, "r"))) {
+               fprintf(stderr, "who: cannot read %s.\n", s);
                exit(1);
        }
        while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
                if (argc == 3) {
                exit(1);
        }
        while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
                if (argc == 3) {
-                       gethostname(hostname, sizeof (hostname));
                        if (strcmp(utmp.ut_line, tp))
                                continue;
                        if (strcmp(utmp.ut_line, tp))
                                continue;
-                       printf("%s!", hostname);
-                       putline();
+                       if (!utmp.ut_name[0])
+                               guess();
+                       else
+                               putline();
                        exit(0);
                }
                if (utmp.ut_name[0] == '\0' && argc == 1)
                        continue;
                putline();
        }
                        exit(0);
                }
                if (utmp.ut_name[0] == '\0' && argc == 1)
                        continue;
                putline();
        }
+       if (argc == 3) {
+               strncpy(utmp.ut_line, tp, sizeof(utmp.ut_line));
+               guess();
+       }
+       exit(0);
 }
 
 putline()
 }
 
 putline()
@@ -91,3 +91,11 @@ putline()
                printf("\t(%.*s)", HMAX, utmp.ut_host);
        putchar('\n');
 }
                printf("\t(%.*s)", HMAX, utmp.ut_host);
        putchar('\n');
 }
+
+guess()
+{
+       pw = getpwuid(getuid());
+       strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
+       time(&utmp.ut_time);
+       putline();
+}