xref: /original-bsd/libexec/fingerd/fingerd.c (revision 753853ba)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)fingerd.c	5.8 (Berkeley) 02/06/92";
16 #endif /* not lint */
17 
18 #include <errno.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <strings.h>
23 #include "pathnames.h"
24 
25 void err __P((const char *, ...));
26 
27 int
28 main()
29 {
30 	register FILE *fp;
31 	register int ch;
32 	register char *lp;
33 	int p[2];
34 #define	ENTRIES	50
35 	char **ap, *av[ENTRIES + 1], **comp, line[1024];
36 
37 #ifdef LOGGING					/* Unused for now. */
38 #include <netinet/in.h>
39 	struct sockaddr_in sin;
40 	int sval;
41 
42 	sval = sizeof(sin);
43 	if (getpeername(0, &sin, &sval) < 0)
44 		err("getpeername: %s", strerror(errno));
45 #endif
46 
47 	if (!fgets(line, sizeof(line), stdin))
48 		exit(1);
49 
50 	comp = &av[1];
51 	for (lp = line, ap = &av[2];;) {
52 		*ap = strtok(lp, " \t\r\n");
53 		if (*ap == NULL)
54 			break;
55 		/* RFC742: "/[Ww]" == "-l" */
56 		if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) {
57 			av[1] = "-l";
58 			comp = &av[0];
59 		}
60 		else if (++ap == av + ENTRIES)
61 			break;
62 		lp = NULL;
63 	}
64 	*comp = "finger";
65 
66 	if (pipe(p) < 0)
67 		err("pipe: %s", strerror(errno));
68 
69 	switch(vfork()) {
70 	case 0:
71 		(void)close(p[0]);
72 		if (p[1] != 1) {
73 			(void)dup2(p[1], 1);
74 			(void)close(p[1]);
75 		}
76 		execv(_PATH_FINGER, comp);
77 		err("execv: %s: %s", _PATH_FINGER, strerror(errno));
78 		_exit(1);
79 	case -1:
80 		err("fork: %s", strerror(errno));
81 	}
82 	(void)close(p[1]);
83 	if (!(fp = fdopen(p[0], "r")))
84 		err("fdopen: %s", strerror(errno));
85 	while ((ch = getc(fp)) != EOF) {
86 		if (ch == '\n')
87 			putchar('\r');
88 		putchar(ch);
89 	}
90 	exit(0);
91 }
92 
93 #if __STDC__
94 #include <stdarg.h>
95 #else
96 #include <varargs.h>
97 #endif
98 
99 void
100 #if __STDC__
101 err(const char *fmt, ...)
102 #else
103 err(fmt, va_alist)
104 	char *fmt;
105         va_dcl
106 #endif
107 {
108 	va_list ap;
109 #if __STDC__
110 	va_start(ap, fmt);
111 #else
112 	va_start(ap);
113 #endif
114 	(void)fprintf(stderr, "fingerd: ");
115 	(void)vfprintf(stderr, fmt, ap);
116 	va_end(ap);
117 	(void)fprintf(stderr, "\r\n");
118 	exit(1);
119 	/* NOTREACHED */
120 }
121