Annotation of 43BSDReno/usr.bin/finger/net.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.