File: finger.c

package info (click to toggle)
cgiemail 1.6-31
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 320 kB
  • ctags: 114
  • sloc: ansic: 1,520; makefile: 135; sh: 34
file content (66 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**********************************************************************
 *  finger.c -- get a finger query
 *
 * Copyright 1994 by the Massachusetts Institute of Technology
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include "mit-copyright.h"

static char rcsid[]="$Header: /mit/wwwdev/src/cgi/RCS/finger.c,v 1.2 95/01/23 18:09:52 brlewis Exp $";

#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>

int
finger(query, host, buf, bufsize)
     char *query, *host, *buf;
     int bufsize;
{
  struct hostent *hp;
  struct sockaddr_in sname;
  struct servent *sp;
  int sock, red=1, total=0;

  /* resolve hostname */
  hp = gethostbyname(host);
  if (!hp) return(-1);

  /* resolve portname */
  sp = getservbyname("finger", "tcp");

  /* build socket name for connect() */
  memset(&sname, 0, sizeof(sname));
  sname.sin_family = AF_INET;
  memcpy(&(sname.sin_addr), hp->h_addr, hp->h_length);
  sname.sin_port = sp->s_port;

  /* create socket */
  sock = socket(PF_INET, SOCK_STREAM, 0);

  /* connect */
  if (connect(sock, &sname, sizeof(sname)) < 0) return(-1);

  /* send query */
  write(sock, query, strlen(query));
  write(sock, "\n", 1);

  /* receive result */
  while(bufsize > 1 && red > 0)
    {
      /* read more bytes into remaining buffer */
      red = read(sock, buf, bufsize);
      if (red > 0)
	{
	  buf += red;
	  bufsize -= red;
	  total += red;
	}
    }
  buf[0] = '\0';
  close(sock);
  return(total);
}