don't bother reporting on mail -- it's never going to be right
[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
6f3f7e2c 19static char sccsid[] = "@(#)net.c 5.2 (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 /*
89 * read back what we get from the remote system.
90 *
91 * Note: once we connected to the remote site, we assume some data.
92 * If it can't/won't send any data, we hang here until Mr. User
93 * gets sufficiently bored to hit ^C.
94 *
95 * Some systems use the return key as a line terminator. These
96 * systems tend to also set the parity bit on every byte. If we
97 * see a <CR> with the 8th bit set, treat it as a newline character.
98 * 0x8d == <CR> with high bit set.
99 *
100 * Otherwise, all high bits are stripped; if it isn't printable and
101 * it isn't a space, we can simply set the 7th bit. Every ASCII
102 * character with bit 7 set is printable.
103 */
104 if (fp = fdopen(s, "r"))
105 while ((c = getc(fp)) != EOF) {
106 if (c == 0x8d)
107 c = '\n';
108 c &= 0x7f;
109 if (!isprint(c) && !isspace(c))
110 c |= 0x40;
111 lastc = c;
112 putchar(c);
113 }
114 if (lastc != '\n')
115 putchar('\n');
116 (void)fclose(fp);
117}