]>
jfr.im git - irc/evilnet/x3.git/blob - src/compat.c
8 #ifdef HAVE_SYS_TIMEB_H
9 # include <sys/timeb.h>
14 #ifdef HAVE_ARPA_INET_H
15 # include <arpa/inet.h>
18 #if !defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
19 extern gettimeofday(struct timeval
* tv
, struct timezone
* tz
);
29 ftime(&tb
); /* FIXME: some versions are void return others int */
32 tv
->tv_usec
= ((long)tb
.millitm
)*1000;
35 tz
->tz_minuteswest
= 0;
44 extern void * memcpy(void * dest
, void const * src
, unsigned long n
)
46 /* very slow, your fault for not having memcpy()*/
47 unsigned char * td
=dest
;
48 unsigned char * ts
=src
;
61 /* very slow, deal with it */
62 extern void * memset(void * dest
, int c
, unsigned long n
)
64 unsigned char * temp
=dest
;
71 temp
[i
] = (unsigned char)c
;
77 extern char * strdup(char const * str
)
83 if (!(out
= malloc(strlen(str
)+1)))
91 extern char const * strerror(int errornum
)
97 return "Operation not permitted";
100 if (errornum
==ENOENT
)
101 return "No such file or directory";
105 return "No such process";
109 return "Interrupted system call";
117 return "No such device or address";
121 return "Bad file number";
124 if (errornum
==EAGAIN
)
128 if (errornum
==ENOMEM
)
129 return "Out of memory";
132 if (errornum
==EACCES
)
133 return "Permission denied";
136 if (errornum
==EFAULT
)
137 return "Bad address";
141 return "Device or resource busy";
144 if (errornum
==EEXIST
)
145 return "File exists";
149 return "Cross-device link";
153 return "Resource deadlock would occur";
156 if (errornum
==EDEADLOCK
)
157 return "Resource deadlock would occur";
160 if (errornum
==ENODEV
)
161 return "No such device";
164 if (errornum
==ENOTDIR
)
165 return "Not a directory";
168 if (errornum
==EISDIR
)
169 return "Is a directory";
172 if (errornum
==EINVAL
)
173 return "Invalid argument";
176 if (errornum
==ENFILE
)
177 return "Too many open files in system";
180 if (errornum
==EMFILE
)
181 return "Too many open files";
184 if (errornum
==ENOTTY
)
185 return "Not a typewriter";
188 if (errornum
==ETXTBSY
)
189 return "Text file busy";
193 return "File too large";
196 if (errornum
==ENOSPC
)
197 return "No space left on device";
200 if (errornum
==ESPIPE
)
201 return "Illegal seek";
205 return "Read-only file system";
208 if (errornum
==EMLINK
)
209 return "Too many links";
213 return "Broken pipe";
217 return "Math argument out of domain of func";
220 if (errornum
==ERANGE
)
221 return "Math result not representable";
224 if (errornum
==ENAMETOOLONG
)
225 return "File name too long";
228 if (errornum
==ENOLCK
)
229 return "No record locks avaliable";
232 if (errornum
==ENOSYS
)
233 return "Function not implemented";
236 if (errornum
==ENOTEMPTY
)
237 return "Directory not empty";
241 return "Too many symbolic links encountered";
244 if (errornum
==EHOSTDOWN
)
245 return "Host is down";
248 if (errornum
==EHOSTUNREACH
)
249 return "No route to host";
252 if (errornum
==EALREADY
)
253 return "Operation already in progress";
256 if (errornum
==EINPROGRESS
)
257 return "Operation now in progress";
260 if (errornum
==ESTALE
)
261 return "Stale NFS filehandle";
264 if (errornum
==EDQUOT
)
265 return "Quota exceeded";
268 if (errornum
==EWOULDBLOCK
)
269 return "Operation would block";
273 return "Communication error on send";
276 if (errornum
==EPROTO
)
277 return "Protocol error";
279 #ifdef EPROTONOSUPPORT
280 if (errornum
==EPROTONOSUPPORT
)
281 return "Protocol not supported";
283 #ifdef ESOCKTNOSUPPORT
284 if (errornum
==ESOCKTNOSUPPORT
)
285 return "Socket type not supported";
287 #ifdef ESOCKTNOSUPPORT
288 if (errornum
==EOPNOTSUPP
)
289 return "Operation not supported";
292 if (errornum
==EPFNOSUPPORT
)
293 return "Protocol family not supported";
296 if (errornum
==EAFNOSUPPORT
)
297 return "Address family not supported by protocol family";
300 if (errornum
==EADDRINUSE
)
301 return "Address already in use";
304 if (errornum
==EADDRNOTAVAIL
)
305 return "Cannot assign requested address";
308 if (errornum
==ENETDOWN
)
309 return "Network is down";
312 if (errornum
==ENETUNREACH
)
313 return "Network is unreachable";
316 if (errornum
==ENETRESET
)
317 return "Network dropped connection on reset";
320 if (errornum
==ECONNABORTED
)
321 return "Software caused connection abort";
324 if (errornum
==ECONNRESET
)
325 return " Connection reset by peer";
328 if (errornum
==ENOBUFS
)
329 return "No buffer space available";
332 if (errornum
==EISCONN
)
333 return "Socket is already connected";
336 if (errornum
==ENOTCONN
)
337 return "Socket is not connected";
340 if (errornum
==ESHUTDOWN
)
341 return " Cannot send after socket shutdown";
344 if (errornum
==ETIMEDOUT
)
345 return "Connection timed out";
348 if (errornum
==ECONNREFUSED
)
349 return "Connection refused";
351 return "Unknown error";
355 #ifndef HAVE_GETADDRINFO
357 int getaddrinfo(const char *node
, const char *service
, const struct addrinfo
*hints
, struct addrinfo
**res
)
359 /* Only support IPv4 if OS doesn't provide this function. */
360 struct sockaddr_in sin
;
362 if (hints
&& hints
->ai_family
!= AF_INET
)
364 memset(&sin
, 0, sizeof(sin
));
365 sin
.sin_family
= AF_INET
;
368 if (hints
&& hints
->ai_flags
& AI_NUMERICHOST
) {
370 if (!inet_aton(node
, &sin
.sin_addr
))
373 sin
.sin_addr
.s_addr
= inet_addr(node
);
374 if (sin
.sin_addr
.s_addr
== INADDR_NONE
)
379 he
= gethostbyname(node
);
382 memcpy(&sin
.sin_addr
, he
->h_addr
, he
->h_length
);
384 } else if (hints
&& hints
->ai_flags
& AI_PASSIVE
) {
385 /* leave it unspecifed */
387 inet_aton("127.0.0.1", &sin
.sin_addr
);
391 sin
.sin_port
= ntohs(0);
392 else if (!(sin
.sin_port
= ntohs(atoi(service
))))
395 *res
= calloc(1, sizeof(**res
) + sizeof(sin
));
396 (*res
)->ai_family
= sin
.sin_family
;
397 (*res
)->ai_socktype
= hints
&& hints
->ai_socktype
? hints
->ai_socktype
: SOCK_STREAM
;
398 (*res
)->ai_protocol
= hints
&& hints
->ai_socktype
? hints
->ai_socktype
: 0;
399 (*res
)->ai_addrlen
= sizeof(sin
);
400 (*res
)->ai_addr
= (struct sockaddr
*)(*res
+ 1);
401 memcpy((*res
)->ai_addr
, &sin
, (*res
)->ai_addrlen
);
402 (*res
)->ai_canonname
= 0;
407 /* TODO: implement fallback getnameinfo() */
409 void freeaddrinfo(struct addrinfo
*res
)
411 struct addrinfo
*next
;
412 for (; res
; res
= next
) {