xref: /csrg-svn/usr.bin/finger/net.c (revision 42731)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
540027Sbostic  * This code is derived from software contributed to Berkeley by
640027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
740027Sbostic  *
8*42731Sbostic  * %sccs.include.redist.c%
937660Sbostic  */
1037660Sbostic 
1137660Sbostic #ifndef lint
12*42731Sbostic static char sccsid[] = "@(#)net.c	5.5 (Berkeley) 06/01/90";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/socket.h>
1737660Sbostic #include <netinet/in.h>
1837660Sbostic #include <netdb.h>
1937660Sbostic #include <stdio.h>
2037660Sbostic #include <ctype.h>
2137660Sbostic 
2237660Sbostic netfinger(name)
2337660Sbostic 	char *name;
2437660Sbostic {
2537660Sbostic 	extern int lflag;
2637660Sbostic 	register FILE *fp;
2737660Sbostic 	register int c, lastc;
2837660Sbostic 	struct in_addr defaddr;
2937660Sbostic 	struct hostent *hp, def;
3037660Sbostic 	struct servent *sp;
3137660Sbostic 	struct sockaddr_in sin;
3237660Sbostic 	int s;
3337660Sbostic 	char *alist[1], *host, *rindex();
3437660Sbostic 	u_long inet_addr();
3537660Sbostic 
3637660Sbostic 	if (!(host = rindex(name, '@')))
3737660Sbostic 		return;
3837660Sbostic 	*host++ = NULL;
3937660Sbostic 	if (!(hp = gethostbyname(host))) {
4037660Sbostic 		defaddr.s_addr = inet_addr(host);
4137660Sbostic 		if (defaddr.s_addr == -1) {
4237660Sbostic 			(void)fprintf(stderr,
4337660Sbostic 			    "finger: unknown host: %s\n", host);
4437660Sbostic 			return;
4537660Sbostic 		}
4637660Sbostic 		def.h_name = host;
4737660Sbostic 		def.h_addr_list = alist;
4837660Sbostic 		def.h_addr = (char *)&defaddr;
4937660Sbostic 		def.h_length = sizeof(struct in_addr);
5037660Sbostic 		def.h_addrtype = AF_INET;
5137660Sbostic 		def.h_aliases = 0;
5237660Sbostic 		hp = &def;
5337660Sbostic 	}
5437660Sbostic 	if (!(sp = getservbyname("finger", "tcp"))) {
5537660Sbostic 		(void)fprintf(stderr, "finger: tcp/finger: unknown service\n");
5637660Sbostic 		return;
5737660Sbostic 	}
5837660Sbostic 	sin.sin_family = hp->h_addrtype;
5937660Sbostic 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
6037660Sbostic 	sin.sin_port = sp->s_port;
6137660Sbostic 	if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
6237660Sbostic 		perror("finger: socket");
6337660Sbostic 		return;
6437660Sbostic 	}
6537660Sbostic 
6637660Sbostic 	/* have network connection; identify the host connected with */
6737660Sbostic 	(void)printf("[%s]\n", hp->h_name);
6837660Sbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
6937660Sbostic 		perror("finger: connect");
7037660Sbostic 		(void)close(s);
7137660Sbostic 		return;
7237660Sbostic 	}
7337660Sbostic 
7437660Sbostic 	/* -l flag for remote fingerd  */
7537660Sbostic 	if (lflag)
7637660Sbostic 		write(s, "/W ", 3);
7737660Sbostic 	/* send the name followed by <CR><LF> */
7837660Sbostic 	(void)write(s, name, strlen(name));
7937660Sbostic 	(void)write(s, "\r\n", 2);
8037660Sbostic 
8137660Sbostic 	/*
8240190Sbostic 	 * Read from the remote system; once we're connected, we assume some
8340190Sbostic 	 * data.  If none arrives, we hang until the user interrupts.
8437660Sbostic 	 *
8540190Sbostic 	 * If we see a <CR> or a <CR> with the high bit set, treat it as
8640190Sbostic 	 * a newline; if followed by a newline character, only output one
8740190Sbostic 	 * newline.
8837660Sbostic 	 *
8937660Sbostic 	 * Otherwise, all high bits are stripped; if it isn't printable and
9037660Sbostic 	 * it isn't a space, we can simply set the 7th bit.  Every ASCII
9137660Sbostic 	 * character with bit 7 set is printable.
9237660Sbostic 	 */
9337660Sbostic 	if (fp = fdopen(s, "r"))
9437660Sbostic 		while ((c = getc(fp)) != EOF) {
9540190Sbostic 			c &= 0x7f;
9640190Sbostic 			if (c == 0x0d) {
9737660Sbostic 				c = '\n';
9840190Sbostic 				lastc = '\r';
9940190Sbostic 			} else {
10040190Sbostic 				if (!isprint(c) && !isspace(c))
10140190Sbostic 					c |= 0x40;
10240190Sbostic 				if (lastc != '\r' || c != '\n')
10340190Sbostic 					lastc = c;
10440190Sbostic 				else {
10540190Sbostic 					lastc = '\n';
10640190Sbostic 					continue;
10740190Sbostic 				}
10840190Sbostic 			}
10937660Sbostic 			putchar(c);
11037660Sbostic 		}
11137660Sbostic 	if (lastc != '\n')
11237660Sbostic 		putchar('\n');
11337660Sbostic 	(void)fclose(fp);
11437660Sbostic }
115