change to fold CR-NL into a single NL
[unix-history] / usr / src / usr.bin / finger / finger.c
index 873190b..a4d41de 100644 (file)
@@ -2,9 +2,6 @@
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1989 The Regents of the University of California.
  * All rights reserved.
  *
- * This code is derived from software contributed to Berkeley by
- * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
- *
  * Redistribution and use in source and binary forms are permitted
  * provided that the above copyright notice and this paragraph are
  * duplicated in all such forms and that any documentation,
  * Redistribution and use in source and binary forms are permitted
  * provided that the above copyright notice and this paragraph are
  * duplicated in all such forms and that any documentation,
@@ -25,7 +22,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)finger.c   5.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)finger.c   5.19 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -96,7 +93,7 @@ main(argc, argv)
                if (entries == 0)
                        (void)printf("No one logged on.\n");
        } else {
                if (entries == 0)
                        (void)printf("No one logged on.\n");
        } else {
-               userlist(argv);
+               userlist(argc, argv);
                /*
                 * Assign explicit "large" format if names given and -s not
                 * explicitly stated.  Force the -l AFTER we get names so any
                /*
                 * Assign explicit "large" format if names given and -s not
                 * explicitly stated.  Force the -l AFTER we get names so any
@@ -117,17 +114,16 @@ main(argc, argv)
 loginlist()
 {
        register PERSON *pn;
 loginlist()
 {
        register PERSON *pn;
-       register int fd;
        struct passwd *pw;
        struct utmp user;
        char name[UT_NAMESIZE + 1];
 
        struct passwd *pw;
        struct utmp user;
        char name[UT_NAMESIZE + 1];
 
-       if ((fd = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
+       if (!freopen(_PATH_UTMP, "r", stdin)) {
                (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
                exit(2);
        }
        name[UT_NAMESIZE] = NULL;
                (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
                exit(2);
        }
        name[UT_NAMESIZE] = NULL;
-       while (read(fd, (char *)&user, sizeof(user)) == sizeof(user)) {
+       while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                if (!user.ut_name[0])
                        continue;
                if ((pn = find_person(user.ut_name)) == NULL) {
                if (!user.ut_name[0])
                        continue;
                if ((pn = find_person(user.ut_name)) == NULL) {
@@ -138,33 +134,38 @@ loginlist()
                }
                enter_where(&user, pn);
        }
                }
                enter_where(&user, pn);
        }
-       (void)close(fd);
        for (pn = phead; lflag && pn != NULL; pn = pn->next)
                enter_lastlog(pn);
 }
 
        for (pn = phead; lflag && pn != NULL; pn = pn->next)
                enter_lastlog(pn);
 }
 
-#define        ARGIGNORE       (char *)0x01
-userlist(argv)
-       char **argv;
+userlist(argc, argv)
+       register argc;
+       register char **argv;
 {
 {
-       register char **ap;
+       register i;
        register PERSON *pn;
        PERSON *nethead;
        struct utmp user;
        struct passwd *pw;
        register PERSON *pn;
        PERSON *nethead;
        struct utmp user;
        struct passwd *pw;
-       int fd, dolocal, *index();
+       int dolocal, *used;
+       char *index();
+
+       if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
+               (void)fprintf(stderr, "finger: out of space.\n");
+               exit(1);
+       }
 
        /* pull out all network requests */
 
        /* pull out all network requests */
-       for (ap = argv, dolocal = 0, nethead = NULL; *ap != NULL; ap++) {
-               if (!index(*ap, '@')) {
+       for (i = 0, dolocal = 0, nethead = NULL; i < argc; i++) {
+               if (!index(argv[i], '@')) {
                        dolocal = 1;
                        continue;
                }
                pn = palloc();
                pn->next = nethead;
                nethead = pn;
                        dolocal = 1;
                        continue;
                }
                pn = palloc();
                pn->next = nethead;
                nethead = pn;
-               pn->name = *ap;
-               *ap = ARGIGNORE;
+               pn->name = argv[i];
+               used[i] = -1;
        }
 
        if (!dolocal)
        }
 
        if (!dolocal)
@@ -174,31 +175,26 @@ userlist(argv)
         * traverse the list of possible login names and check the login name
         * and real name against the name specified by the user.
         */
         * traverse the list of possible login names and check the login name
         * and real name against the name specified by the user.
         */
-       if (mflag)
-               for (ap = argv; *ap != NULL; ap++) {
-                       if (*ap == ARGIGNORE)
-                               continue;
-                       if ((pw = getpwnam(*ap)) == NULL)
-                               continue;
-                       enter_person(pw);
-                       *ap = ARGIGNORE;
-               }
-       else while (pw = getpwent())
-               for (ap = argv; *ap != NULL; ap++) {
-                       if (*ap == ARGIGNORE)
-                               continue;
-                       if (strcasecmp(pw->pw_name, *ap) && !match(pw, *ap))
-                               continue;
-                       enter_person(pw);
-                       *ap = ARGIGNORE;
-                       /* don't break, may be listed multiple times */
-               }
+       if (mflag) {
+               for (i = 0; i < argc; i++)
+                       if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
+                               enter_person(pw);
+                               used[i] = 1;
+                       }
+       } else while (pw = getpwent())
+               for (i = 0; i < argc; i++)
+                       if (used[i] >= 0 &&
+                           (!strcasecmp(pw->pw_name, argv[i]) ||
+                           match(pw, argv[i]))) {
+                               enter_person(pw);
+                               used[i] = 1;
+                       }
 
        /* list errors */
 
        /* list errors */
-       for (ap = argv; *ap != NULL; ap++)
-               if (*ap != ARGIGNORE)
+       for (i = 0; i < argc; i++)
+               if (!used[i])
                        (void)fprintf(stderr,
                        (void)fprintf(stderr,
-                           "finger: %s: no such user.\n", *ap);
+                           "finger: %s: no such user.\n", argv[i]);
 
        /* handle network requests */
 net:   for (pn = nethead; pn; pn = pn->next) {
 
        /* handle network requests */
 net:   for (pn = nethead; pn; pn = pn->next) {
@@ -214,18 +210,17 @@ net:      for (pn = nethead; pn; pn = pn->next) {
         * Scan thru the list of users currently logged in, saving
         * appropriate data whenever a match occurs.
         */
         * Scan thru the list of users currently logged in, saving
         * appropriate data whenever a match occurs.
         */
-       if ((fd = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
+       if (!freopen(_PATH_UTMP, "r", stdin)) {
                (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
                exit(1);
        }
                (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
                exit(1);
        }
-       while (read(fd, (char *)&user, sizeof(user)) == sizeof(user)) {
+       while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
                if (!user.ut_name[0])
                        continue;
                if ((pn = find_person(user.ut_name)) == NULL)
                        continue;
                enter_where(&user, pn);
        }
                if (!user.ut_name[0])
                        continue;
                if ((pn = find_person(user.ut_name)) == NULL)
                        continue;
                enter_where(&user, pn);
        }
-       (void)close(fd);
        for (pn = phead; pn != NULL; pn = pn->next)
                enter_lastlog(pn);
 }
        for (pn = phead; pn != NULL; pn = pn->next)
                enter_lastlog(pn);
 }