File: dict.c

package info (click to toggle)
gdict 0.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 80 kB
  • ctags: 18
  • sloc: ansic: 287; makefile: 62
file content (80 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 
 * Michael Bacarella wrote the original incarnation of this program.
 * All I did was add a few loops to handle the data better.
 * Thanks goes out to #linuxos also.  :)
 */

#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

int main (int argc, char *argv[]) {

	struct sockaddr_in	Sockaddr;
	int	err;
	int 	sd;
	char	buffer[512];
	char    buffer_bin[65535];
	char    temp[12];
	char	eof = '\200';
	char	*location;
	int     nchars;

	if (argc < 2) {
		printf("Usage: %s <word to define>\n", argv[0]);
		exit(1);
	}

	sd = socket (AF_INET, SOCK_STREAM, 0);
	if (sd == -1) {
		fprintf(stderr, "Socket error!\n");
		exit(1);
	}

	sprintf (buffer, "DEFINE %s\n", argv[1]);
	
	Sockaddr.sin_family		= AF_INET;
	Sockaddr.sin_port		= htons(2627);
	Sockaddr.sin_addr.s_addr	= inet_addr("128.52.39.7");

	err = connect (sd, &Sockaddr, sizeof(Sockaddr) );
	
	if (err != 0) {
		printf("Could not connect to dictionary server!\n");
		exit(1);
	}

	err = send (sd, buffer, strlen(buffer), 0);
	
	if ((err = 0)) {
		printf("Send error!\n");
	}

	strcpy (buffer_bin, "");  /* Clear the string */
        
	memset (buffer, 0, sizeof (buffer));
	sprintf(temp, "SPELLING 0");  

        while (1) {
		if ((nchars = recv(sd, buffer, sizeof(buffer), 0) ) >= 1) {
			strncat (buffer_bin, buffer, nchars);
		}
                
		if ( strchr(buffer, eof) ) {
			location = strchr(buffer_bin, eof);
			*location = '\0';
			break;
		}
        
		if ( strstr(buffer, temp) ) {
			break;
		}
	}

	printf("%s\n", buffer_bin);
	
	exit(0);
}