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

Defined functions

netfinger defined in line 32; used 1 times

Defined variables

sccsid defined in line 22; never used
Last modified: 1990-03-18
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3257
Valid CSS Valid XHTML 1.0 Strict