BSD 4_3_Reno release
[unix-history] / usr / src / libexec / fingerd / fingerd.c
index d23b433..e966e30 100644 (file)
@@ -3,16 +3,18 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * All rights reserved.
  *
  * 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,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement:  ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. Neither the name of the University nor the names of its
+ * contributors may be used to endorse or promote products derived
  * from this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * from this software without specific prior written permission.
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,86 +24,79 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)fingerd.c  5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)fingerd.c  5.6 (Berkeley) 6/1/90";
 #endif /* not lint */
 
 #endif /* not lint */
 
-/*
- * Finger server.
- */
-#include <sys/types.h>
-#include <netinet/in.h>
-
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
+#include "pathnames.h"
 
 
-main(argc, argv)
-       int argc;
-       char *argv[];
+main()
 {
 {
-       register char *sp;
-       char line[512];
+       register FILE *fp;
+       register int ch;
+       register char *lp;
+       int p[2];
+#define        ENTRIES 50
+       char **ap, *av[ENTRIES + 1], line[1024], *strtok();
+
+#ifdef LOGGING                                 /* unused for now */
+#include <netinet/in.h>
        struct sockaddr_in sin;
        struct sockaddr_in sin;
-       int i, p[2], pid, status;
-       FILE *fp;
-       char *av[4];
+       int sval;
+
+       sval = sizeof(sin);
+       if (getpeername(0, &sin, &sval) < 0)
+               fatal("getpeername");
+#endif
+
+       if (!fgets(line, sizeof(line), stdin))
+               exit(1);
 
 
-       i = sizeof (sin);
-       if (getpeername(0, &sin, &i) < 0)
-               fatal(argv[0], "getpeername");
-       line[0] = '\0';
-       gets(line);
-       sp = line;
        av[0] = "finger";
        av[0] = "finger";
-       i = 1;
-       while (1) {
-               while (isspace(*sp))
-                       sp++;
-               if (!*sp)
+       for (lp = line, ap = &av[1];;) {
+               *ap = strtok(lp, " \t\r\n");
+               if (!*ap)
                        break;
                        break;
-               if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
-                       sp += 2;
-                       av[i++] = "-l";
-               }
-               if (*sp && !isspace(*sp)) {
-                       av[i++] = sp;
-                       while (*sp && !isspace(*sp))
-                               sp++;
-                       *sp = '\0';
-               }
+               /* RFC742: "/[Ww]" == "-l" */
+               if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w'))
+                       *ap = "-l";
+               if (++ap == av + ENTRIES)
+                       break;
+               lp = NULL;
        }
        }
-       av[i] = 0;
+
        if (pipe(p) < 0)
        if (pipe(p) < 0)
-               fatal(argv[0], "pipe");
-       if ((pid = fork()) == 0) {
-               close(p[0]);
+               fatal("pipe");
+
+       switch(fork()) {
+       case 0:
+               (void)close(p[0]);
                if (p[1] != 1) {
                if (p[1] != 1) {
-                       dup2(p[1], 1);
-                       close(p[1]);
+                       (void)dup2(p[1], 1);
+                       (void)close(p[1]);
                }
                }
-               execv("/usr/ucb/finger", av);
+               execv(_PATH_FINGER, av);
                _exit(1);
                _exit(1);
+       case -1:
+               fatal("fork");
        }
        }
-       if (pid == -1)
-               fatal(argv[0], "fork");
-       close(p[1]);
-       if ((fp = fdopen(p[0], "r")) == NULL)
-               fatal(argv[0], "fdopen");
-       while ((i = getc(fp)) != EOF) {
-               if (i == '\n')
+       (void)close(p[1]);
+       if (!(fp = fdopen(p[0], "r")))
+               fatal("fdopen");
+       while ((ch = getc(fp)) != EOF) {
+               if (ch == '\n')
                        putchar('\r');
                        putchar('\r');
-               putchar(i);
+               putchar(ch);
        }
        }
-       fclose(fp);
-       while ((i = wait(&status)) != pid && i != -1)
-               ;
-       return(0);
+       exit(0);
 }
 
 }
 
-fatal(prog, s)
-       char *prog, *s;
+fatal(msg)
+       char *msg;
 {
 {
+       extern int errno;
+       char *strerror();
 
 
-       fprintf(stderr, "%s: ", prog);
-       perror(s);
+       fprintf(stderr, "fingerd: %s: %s\r\n", msg, strerror(errno));
        exit(1);
 }
        exit(1);
 }