1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)net.c 5.5 (Berkeley) 06/01/90"; 13 #endif /* not lint */ 14 15 #include <sys/types.h> 16 #include <sys/socket.h> 17 #include <netinet/in.h> 18 #include <netdb.h> 19 #include <stdio.h> 20 #include <ctype.h> 21 22 netfinger(name) 23 char *name; 24 { 25 extern int lflag; 26 register FILE *fp; 27 register int c, lastc; 28 struct in_addr defaddr; 29 struct hostent *hp, def; 30 struct servent *sp; 31 struct sockaddr_in sin; 32 int s; 33 char *alist[1], *host, *rindex(); 34 u_long inet_addr(); 35 36 if (!(host = rindex(name, '@'))) 37 return; 38 *host++ = NULL; 39 if (!(hp = gethostbyname(host))) { 40 defaddr.s_addr = inet_addr(host); 41 if (defaddr.s_addr == -1) { 42 (void)fprintf(stderr, 43 "finger: unknown host: %s\n", host); 44 return; 45 } 46 def.h_name = host; 47 def.h_addr_list = alist; 48 def.h_addr = (char *)&defaddr; 49 def.h_length = sizeof(struct in_addr); 50 def.h_addrtype = AF_INET; 51 def.h_aliases = 0; 52 hp = &def; 53 } 54 if (!(sp = getservbyname("finger", "tcp"))) { 55 (void)fprintf(stderr, "finger: tcp/finger: unknown service\n"); 56 return; 57 } 58 sin.sin_family = hp->h_addrtype; 59 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 60 sin.sin_port = sp->s_port; 61 if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { 62 perror("finger: socket"); 63 return; 64 } 65 66 /* have network connection; identify the host connected with */ 67 (void)printf("[%s]\n", hp->h_name); 68 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 69 perror("finger: connect"); 70 (void)close(s); 71 return; 72 } 73 74 /* -l flag for remote fingerd */ 75 if (lflag) 76 write(s, "/W ", 3); 77 /* send the name followed by <CR><LF> */ 78 (void)write(s, name, strlen(name)); 79 (void)write(s, "\r\n", 2); 80 81 /* 82 * Read from the remote system; once we're connected, we assume some 83 * data. If none arrives, we hang until the user interrupts. 84 * 85 * If we see a <CR> or a <CR> with the high bit set, treat it as 86 * a newline; if followed by a newline character, only output one 87 * newline. 88 * 89 * Otherwise, all high bits are stripped; if it isn't printable and 90 * it isn't a space, we can simply set the 7th bit. Every ASCII 91 * character with bit 7 set is printable. 92 */ 93 if (fp = fdopen(s, "r")) 94 while ((c = getc(fp)) != EOF) { 95 c &= 0x7f; 96 if (c == 0x0d) { 97 c = '\n'; 98 lastc = '\r'; 99 } else { 100 if (!isprint(c) && !isspace(c)) 101 c |= 0x40; 102 if (lastc != '\r' || c != '\n') 103 lastc = c; 104 else { 105 lastc = '\n'; 106 continue; 107 } 108 } 109 putchar(c); 110 } 111 if (lastc != '\n') 112 putchar('\n'); 113 (void)fclose(fp); 114 } 115