xref: /csrg-svn/libexec/fingerd/fingerd.c (revision 16770)
116471Ssam #ifndef lint
2*16770Sralph static char sccsid[] = "@(#)fingerd.c	1.3 (Berkeley) 07/26/84";
316471Ssam #endif
416471Ssam 
516471Ssam /*
616471Ssam  * Finger server.
716471Ssam  */
816471Ssam #include <sys/types.h>
916471Ssam #include <netinet/in.h>
1016471Ssam 
1116471Ssam #include <stdio.h>
1216471Ssam #include <ctype.h>
1316471Ssam 
1416471Ssam main(argc, argv)
1516471Ssam 	char *argv[];
1616471Ssam {
17*16770Sralph 	register char *sp;
18*16770Sralph 	char line[512];
1916471Ssam 	struct sockaddr_in sin;
20*16770Sralph 	int i, p[2], pid, status;
2116595Sralph 	FILE *fp;
22*16770Sralph 	char *av[4];
2316471Ssam 
2416471Ssam 	i = sizeof (sin);
2516471Ssam 	if (getpeername(0, &sin, &i) < 0)
2616471Ssam 		fatal(argv[0], "getpeername");
2716471Ssam 	line[0] = '\0';
2816471Ssam 	gets(line);
2916595Sralph 	sp = line;
30*16770Sralph 	av[0] = "finger";
31*16770Sralph 	i = 1;
3216471Ssam 	while (1) {
3316595Sralph 		while (isspace(*sp))
3416595Sralph 			sp++;
3516595Sralph 		if (!*sp)
3616471Ssam 			break;
3716595Sralph 		if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
3816595Sralph 			sp += 2;
39*16770Sralph 			av[i++] = "-l";
4016471Ssam 		}
41*16770Sralph 		if (*sp && !isspace(*sp)) {
42*16770Sralph 			av[i++] = sp;
43*16770Sralph 			while (*sp && !isspace(*sp))
44*16770Sralph 				sp++;
45*16770Sralph 			*sp = '\0';
46*16770Sralph 		}
4716471Ssam 	}
48*16770Sralph 	av[i] = 0;
49*16770Sralph 	if (pipe(p) < 0)
50*16770Sralph 		fatal(argv[0], "pipe");
51*16770Sralph 	if ((pid = fork()) == 0) {
52*16770Sralph 		close(p[0]);
53*16770Sralph 		if (p[1] != 1) {
54*16770Sralph 			dup2(p[1], 1);
55*16770Sralph 			close(p[1]);
56*16770Sralph 		}
57*16770Sralph 		execv("/usr/ucb/finger", av);
58*16770Sralph 		_exit(1);
59*16770Sralph 	}
60*16770Sralph 	if (pid == -1)
61*16770Sralph 		fatal(argv[0], "fork");
62*16770Sralph 	close(p[1]);
63*16770Sralph 	if ((fp = fdopen(p[0], "r")) == NULL)
64*16770Sralph 		fatal(argv[0], "fdopen");
6516595Sralph 	while ((i = getc(fp)) != EOF) {
6616595Sralph 		if (i == '\n')
6716595Sralph 			putchar('\r');
6816595Sralph 		putchar(i);
6916595Sralph 	}
70*16770Sralph 	fclose(fp);
71*16770Sralph 	while ((i = wait(&status)) != pid && i != -1)
72*16770Sralph 		;
7316595Sralph 	return(0);
7416471Ssam }
7516471Ssam 
7616471Ssam fatal(prog, s)
7716471Ssam 	char *prog, *s;
7816471Ssam {
7916471Ssam 
8016471Ssam 	fprintf(stderr, "%s: ", prog);
8116471Ssam 	perror(s);
8216471Ssam 	exit(1);
8316471Ssam }
84