new strsep semantics
[unix-history] / usr / src / usr.bin / finger / net.c
CommitLineData
6b8910b8
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
6b8910b8
KB
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
04df419f 19static char sccsid[] = "@(#)net.c 5.4 (Berkeley) %G%";
6b8910b8
KB
20#endif /* not lint */
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <netdb.h>
26#include <stdio.h>
27#include <ctype.h>
28
29netfinger(name)
30 char *name;
31{
32 extern int lflag;
33 register FILE *fp;
34 register int c, lastc;
35 struct in_addr defaddr;
36 struct hostent *hp, def;
37 struct servent *sp;
38 struct sockaddr_in sin;
39 int s;
40 char *alist[1], *host, *rindex();
41 u_long inet_addr();
42
43 if (!(host = rindex(name, '@')))
44 return;
45 *host++ = NULL;
46 if (!(hp = gethostbyname(host))) {
47 defaddr.s_addr = inet_addr(host);
48 if (defaddr.s_addr == -1) {
49 (void)fprintf(stderr,
50 "finger: unknown host: %s\n", host);
51 return;
52 }
53 def.h_name = host;
54 def.h_addr_list = alist;
55 def.h_addr = (char *)&defaddr;
56 def.h_length = sizeof(struct in_addr);
57 def.h_addrtype = AF_INET;
58 def.h_aliases = 0;
59 hp = &def;
60 }
61 if (!(sp = getservbyname("finger", "tcp"))) {
62 (void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
63 return;
64 }
65 sin.sin_family = hp->h_addrtype;
66 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
67 sin.sin_port = sp->s_port;
68 if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
69 perror("finger: socket");
70 return;
71 }
72
73 /* have network connection; identify the host connected with */
74 (void)printf("[%s]\n", hp->h_name);
75 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
76 perror("finger: connect");
77 (void)close(s);
78 return;
79 }
80
81 /* -l flag for remote fingerd */
82 if (lflag)
83 write(s, "/W ", 3);
84 /* send the name followed by <CR><LF> */
85 (void)write(s, name, strlen(name));
86 (void)write(s, "\r\n", 2);
87
88 /*
04df419f
KB
89 * Read from the remote system; once we're connected, we assume some
90 * data. If none arrives, we hang until the user interrupts.
6b8910b8 91 *
04df419f
KB
92 * If we see a <CR> or a <CR> with the high bit set, treat it as
93 * a newline; if followed by a newline character, only output one
94 * newline.
6b8910b8
KB
95 *
96 * Otherwise, all high bits are stripped; if it isn't printable and
97 * it isn't a space, we can simply set the 7th bit. Every ASCII
98 * character with bit 7 set is printable.
99 */
100 if (fp = fdopen(s, "r"))
101 while ((c = getc(fp)) != EOF) {
6b8910b8 102 c &= 0x7f;
04df419f
KB
103 if (c == 0x0d) {
104 c = '\n';
105 lastc = '\r';
106 } else {
107 if (!isprint(c) && !isspace(c))
108 c |= 0x40;
109 if (lastc != '\r' || c != '\n')
110 lastc = c;
111 else {
112 lastc = '\n';
113 continue;
114 }
115 }
6b8910b8
KB
116 putchar(c);
117 }
118 if (lastc != '\n')
119 putchar('\n');
120 (void)fclose(fp);
121}