Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc-extra / emu / rrgb / csock.c
blob3e65e52fc2399485bff47a384f658892f7977223
1 /*
3 $Id$
4 This file is part of socket(1).
5 Copyright (C) 1992 by Juergen Nickelsen <nickel@cs.tu-berlin.de>
6 Please read the file COPYRIGHT for further details.
8 */
10 #if defined(__linux__) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__sun)
11 #include <unistd.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <signal.h>
22 #include "csock.h"
24 void init_signals(void);
26 int socket_poll( int asocket )
28 fd_set read_fd;
29 int ret;
30 struct timeval wait;
32 FD_ZERO(&read_fd);
33 FD_SET(asocket, &read_fd);
35 wait.tv_sec=0;
36 wait.tv_usec=0;
38 /* Wait for input */
39 ret = select( asocket+1, &read_fd, NULL, NULL, &wait );
40 if (ret<0 && errno != EINTR) {
41 return -1;
43 return ret;
46 int socket_get( int asocket )
48 char buffer[2];
49 if (socket_read( asocket, buffer, 1)!=-1) {
50 return buffer[0];
52 return -1;
55 int socket_read( int asocket, char *buffer, int len )
57 int got;
59 got = read( asocket, buffer, len );
60 if (got==-1) {
61 return -1;
63 return got;
66 int socket_write( int sink, char *buffer, int len )
68 int wrote;
70 while (len>0) {
71 wrote = write( sink, buffer, len );
72 if (wrote == -1) {
73 return -1;
75 len-=wrote;
77 return 0;
80 int socket_init(int port)
82 struct sockaddr_in sock;
83 int ret;
85 ret = socket(AF_INET, SOCK_STREAM, 0);
86 if (ret<0)
87 return -1;
88 /* Clear the sockaddr_in */
89 memset((void *)&sock, 0, sizeof(struct sockaddr_in));
91 sock.sin_family = AF_INET;
92 sock.sin_addr.s_addr = htonl(INADDR_ANY);
93 sock.sin_port = htons(port);
95 if (bind(ret, (struct sockaddr *)&sock, sizeof(struct sockaddr_in))<0) {
96 return -2; /* Bind error */
98 if (listen(ret, 1)<0) {
99 return -3; /* Listen error */
101 return ret;
104 int socket_main(int hsocket)
106 struct sockaddr_in sock;
107 int asocket; /* Active socket */
108 socklen_t sock_len;
109 char buffer[200];
110 int got;
112 sock_len = sizeof(struct sockaddr_in);
113 asocket = accept( hsocket, (struct sockaddr *)&sock, &sock_len );
114 if (asocket == -1) {
115 return -1;
117 socket_write( asocket, "Hi there\n", 9 );
118 while (1) {
119 while (socket_poll(asocket)==0) {}
120 if ((got=socket_read(asocket, buffer, 200))>0) {
121 socket_write( asocket, buffer, got );
123 else
124 break;
126 close(asocket);
127 return 0;
130 /* Signal handler, print message and exit */
131 void exitsig(sig)
132 int sig ;
134 if (sig != SIGUSR1) {
135 fprintf(stderr, "\nSignal %i occured, exiting\n", sig) ;
137 exit(-sig) ;
140 /* set up signal handling. All except TSTP, CONT, CLD, and QUIT
141 * are caught with exitsig(). */
142 void init_signals()
144 int i ;
145 #ifdef BSD_SIG_SETMASK /* only with BSD signals */
146 static struct sigvec svec = { exitsig, ~0, 0 } ;
147 #endif
149 for (i = 0; i < NSIG; i++) {
150 switch (i) {
151 #ifdef SIGTSTP
152 case SIGTSTP:
153 case SIGTTOU:
154 case SIGTTIN:
155 case SIGSTOP:
156 case SIGCONT:
157 continue ;
158 #endif
159 #if !defined (SIGCHLD) && defined (SIGCLD)
160 #define SIGCHLD SIGCLD
161 #endif
162 #ifdef SIGCHLD
163 case SIGCHLD:
164 continue ;
165 #endif
166 #ifdef SIGWINCH
167 case SIGWINCH: /* it is ridiculous to exit on WINCH */
168 continue ;
169 #endif
170 case SIGQUIT: /* if the user wants a core dump, */
171 continue ; /* they can have it. */
172 default:
173 #ifdef BSD_SIG_SETMASK
174 sigvec(i, &svec, NULL) ;
175 #else
176 signal(i, exitsig) ;
177 #endif