Mercurial > dwindows
comparison compat.c @ 580:d7c338ac926a
Sweeping changes to the compatibility module, moved all socket code out
of the DLL and into macros in compat.h, socksprintf() is now gone, replaced
by the sockprint() macro and the vargs() function. This allows the DLL
and application to be compiled with different compilers (and C libraries).
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Fri, 20 May 2005 03:32:25 +0000 |
parents | 1a210e2f214b |
children | f09977bc9b5c |
comparison
equal
deleted
inserted
replaced
579:cea01bd2e0a7 | 580:d7c338ac926a |
---|---|
37 usleep(period * 1000); | 37 usleep(period * 1000); |
38 #endif | 38 #endif |
39 } | 39 } |
40 #endif | 40 #endif |
41 | 41 |
42 int API sockread (int a, void *b, int c, int d) | |
43 { | |
44 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) | |
45 return recv(a,b,c,d); | |
46 #else | |
47 return read(a,b,c); | |
48 #endif | |
49 } | |
50 | |
51 int API sockwrite (int a, void *b, int c, int d) | |
52 { | |
53 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) | |
54 return send(a,b,c,d); | |
55 #else | |
56 return write(a,b,c); | |
57 #endif | |
58 } | |
59 | |
60 int API sockclose(int a) | |
61 { | |
62 #ifdef __IBMC__ | |
63 return soclose(a); | |
64 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
65 return closesocket(a); | |
66 #else | |
67 return close(a); | |
68 #endif | |
69 } | |
70 | |
71 int API makedir(char *path) | 42 int API makedir(char *path) |
72 { | 43 { |
73 #if defined(__IBMC__) || defined(__WATCOMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) | 44 #if defined(__IBMC__) || defined(__WATCOMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) |
74 return mkdir(path); | 45 return mkdir(path); |
75 #else | 46 #else |
76 return mkdir(path,S_IRWXU); | 47 return mkdir(path,S_IRWXU); |
77 #endif | 48 #endif |
78 } | 49 } |
79 | 50 |
80 void API nonblock(int fd) | 51 char * API vargs(char *buf, int len, char *format, ...) |
81 { | |
82 #if defined(__OS2__) && !defined(__EMX__) | |
83 static int _nonblock = 1; | |
84 | |
85 ioctl(fd, FIONBIO, (char *)&_nonblock, sizeof(_nonblock)); | |
86 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
87 static unsigned long _nonblock = 1; | |
88 | |
89 ioctlsocket(fd, FIONBIO, &_nonblock); | |
90 #else | |
91 fcntl(fd, F_SETFL, O_NONBLOCK); | |
92 #endif | |
93 } | |
94 | |
95 void API block(int fd) | |
96 { | |
97 #if defined(__OS2__) && !defined(__EMX__) | |
98 static int _nonblock = 0; | |
99 | |
100 ioctl(fd, FIONBIO, (char *)&_nonblock, sizeof(_nonblock)); | |
101 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
102 static unsigned long _nonblock = 0; | |
103 | |
104 ioctlsocket(fd, FIONBIO, &_nonblock); | |
105 #else | |
106 fcntl(fd, F_SETFL, 0); | |
107 #endif | |
108 } | |
109 | |
110 int API socksprintf(int fd, char *format, ...) | |
111 { | 52 { |
112 va_list args; | 53 va_list args; |
113 char outbuf[1024]; | |
114 int len; | |
115 | 54 |
116 va_start(args, format); | 55 va_start(args, format); |
117 vsprintf(outbuf, format, args); | 56 #ifdef HAVE_VSNPRINTF |
57 vsnprintf(buf, len, format, args); | |
58 #else | |
59 len = len; | |
60 vsprintf(buf, format, args); | |
61 #endif | |
118 va_end(args); | 62 va_end(args); |
119 | 63 |
120 len = strlen(outbuf); | 64 return buf; |
121 sockwrite(fd, outbuf, len, 0); | |
122 | |
123 return len; | |
124 } | |
125 | |
126 void API sockinit(void) | |
127 { | |
128 #ifdef __IBMC__ | |
129 sock_init(); | |
130 #elif defined(__WIN32__) || defined(WINNT) | |
131 WSADATA wsa; | |
132 | |
133 WSAStartup(MAKEWORD (1, 1), &wsa); | |
134 #endif /* !WIN32 */ | |
135 } | |
136 | |
137 void API sockshutdown(void) | |
138 { | |
139 #if defined(__WIN32__) || defined(WINNT) | |
140 WSACleanup(); | |
141 #endif /* !WIN32 */ | |
142 } | |
143 | |
144 int API sockpipe(int *pipes) | |
145 { | |
146 #ifndef NO_DOMAIN_SOCKETS | |
147 #ifndef HAVE_PIPE | |
148 struct sockaddr_un un; | |
149 #endif | |
150 #else | |
151 struct sockaddr_in server_addr; | |
152 struct sockaddr_in listen_addr = { 0 }; | |
153 int len = sizeof(struct sockaddr_in); | |
154 struct hostent *he; | |
155 #endif | |
156 #ifndef HAVE_PIPE | |
157 int tmpsock; | |
158 #endif | |
159 | |
160 #ifdef HAVE_PIPE | |
161 return pipe(pipes); | |
162 #elif !defined(NO_DOMAIN_SOCKETS) | |
163 static int instance = -1; | |
164 | |
165 instance++; | |
166 | |
167 /* Use UNIX domain sockets to pass messages */ | |
168 tmpsock = socket(AF_UNIX, SOCK_STREAM, 0); | |
169 pipes[1] = socket(AF_UNIX, SOCK_STREAM, 0); | |
170 memset(&un, 0, sizeof(un)); | |
171 un.sun_family=AF_UNIX; | |
172 sprintf(un.sun_path, PIPENAME, instance); | |
173 bind(tmpsock, (struct sockaddr *)&un, sizeof(un)); | |
174 listen(tmpsock, 0); | |
175 connect(pipes[1], (struct sockaddr *)&un, sizeof(un)); | |
176 pipes[0] = accept(tmpsock, 0, 0); | |
177 sockclose(tmpsock); | |
178 #else | |
179 /* Use localhost socket to pass messages if no domain sockets */ | |
180 he = gethostbyname("localhost"); | |
181 | |
182 if(he) | |
183 { | |
184 memset(&server_addr, 0, sizeof(server_addr)); | |
185 server_addr.sin_family = AF_INET; | |
186 server_addr.sin_port = 0; | |
187 server_addr.sin_addr.s_addr = INADDR_ANY; | |
188 if ((tmpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0 || bind(tmpsock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 || listen(tmpsock, 0) < 0) | |
189 return -1; | |
190 | |
191 memset(&listen_addr, 0, sizeof(listen_addr)); | |
192 getsockname(tmpsock, (struct sockaddr *)&listen_addr, &len); | |
193 | |
194 server_addr.sin_family = AF_INET; | |
195 server_addr.sin_port = listen_addr.sin_port; | |
196 server_addr.sin_addr.s_addr = *((unsigned long *)he->h_addr); | |
197 if((pipes[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0 || connect(pipes[1], (struct sockaddr *)&server_addr, sizeof(server_addr))) | |
198 return -1; | |
199 else | |
200 pipes[0] = accept(tmpsock, 0, 0); | |
201 sockclose(tmpsock); | |
202 } | |
203 else | |
204 return -1; | |
205 #endif | |
206 if(pipes[0] < 0 || pipes[1] < 0) | |
207 return -1; | |
208 return 0; | |
209 } | 65 } |
210 | 66 |
211 long double API drivefree(int drive) | 67 long double API drivefree(int drive) |
212 { | 68 { |
213 #if defined(__EMX__) || defined(__OS2__) | 69 #if defined(__EMX__) || defined(__OS2__) |