dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / in.fingerd.c
blob403dc4b22f1e0a90e4616aa117f2a052598558a9
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * Finger server.
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <stdio.h>
44 #include <ctype.h>
46 #define MAXARGS 10
48 void fatal(char *prog, char *s);
50 int
51 main(argc, argv)
52 int argc;
53 char *argv[];
55 register char *sp;
56 char line[512];
57 struct sockaddr_storage sin;
58 pid_t pid, w;
59 int i, p[2], status;
60 FILE *fp;
61 char *av[MAXARGS + 1];
62 socklen_t len = sizeof (sin);
64 if (getpeername(0, (struct sockaddr *)&sin, &len) < 0)
65 fatal(argv[0], "getpeername");
66 line[0] = '\0';
67 if (fgets(line, sizeof (line), stdin) == NULL)
68 exit(1);
69 sp = line;
70 av[0] = "finger";
71 i = 1;
73 /* skip past leading white space */
74 while (isspace(*sp))
75 sp++;
78 * The finger protocol says a "/W" switch means verbose output.
79 * We explicitly set either the "long" or "short" output flags
80 * to the finger program so that we don't have to know what what
81 * the "finger" program's default is.
83 if (*sp == '/' && (sp[1] == 'W' || sp[1] == 'w')) {
84 sp += 2;
85 av[i++] = "-l";
86 } else {
87 av[i++] = "-s";
90 /* look for username arguments */
91 while (i < MAXARGS) {
93 /* skip over leading white space */
94 while (isspace(*sp))
95 sp++;
97 /* check for end of "command line" */
98 if (*sp == '\0')
99 break;
101 /* pick up another name argument */
102 av[i++] = sp;
103 while ((*sp != '\0') && !isspace(*sp))
104 sp++;
106 /* check again for end of "command line" */
107 if (*sp == '\0')
108 break;
109 else
110 *sp++ = '\0';
113 av[i] = NULL;
114 if (pipe(p) < 0)
115 fatal(argv[0], "pipe");
117 if ((pid = fork()) == 0) {
118 close(p[0]);
119 if (p[1] != 1) {
120 dup2(p[1], 1);
121 close(p[1]);
123 execv("/usr/bin/finger", av);
124 printf("No local finger program found\n");
125 fflush(stdout);
126 _exit(1);
128 if (pid == (pid_t)-1)
129 fatal(argv[0], "fork");
130 close(p[1]);
131 if ((fp = fdopen(p[0], "r")) == NULL)
132 fatal(argv[0], "fdopen");
133 while ((i = getc(fp)) != EOF) {
134 if (i == '\n')
135 putchar('\r');
136 putchar(i);
138 fclose(fp);
139 while ((w = wait(&status)) != pid && w != (pid_t)-1)
141 return (0);
144 void
145 fatal(prog, s)
146 char *prog, *s;
149 fprintf(stderr, "%s: ", prog);
150 perror(s);
151 exit(1);