Initial import (again)
[srvx.git] / src / compat.c
1 #undef gettimeofday
2 #undef memcpy
3 #undef memset
4 #undef strerror
5
6 #include "common.h"
7
8 #ifdef HAVE_SYS_TIMEB_H
9 # include <sys/timeb.h>
10 #endif
11 #ifdef HAVE_MEMORY_H
12 # include <memory.h>
13 #endif
14
15 #if !defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
16 extern gettimeofday(struct timeval * tv, struct timezone * tz);
17 {
18     if (!tv)
19     {
20         errno = EFAULT;
21         return -1;
22     }
23
24     struct timeb tb;
25
26     ftime(&tb); /* FIXME: some versions are void return others int */
27
28     tv->tv_sec  = tb.time;
29     tv->tv_usec = ((long)tb.millitm)*1000;
30     if (tz)
31     {
32         tz->tz_minuteswest = 0;
33         tz->tz_dsttime     = 0;
34     }
35
36     return 0;
37 }
38 #endif
39
40 #ifndef HAVE_MEMCPY
41 extern void * memcpy(void * dest, void const * src, unsigned long n)
42 {
43 #ifdef HAVE_BCOPY
44     bcopy(src,dest,n);
45     return dest;
46 #else
47 /* very slow, your fault for not having memcpy()*/
48     unsigned char * td=dest;
49     unsigned char * ts=src;
50     unsigned long   i;
51
52     if (!td || !ts)
53         return NULL;
54
55     for (i=0; i<n; i++)
56         td[i] = ts[i];
57     return dest;
58 #endif
59 }
60 #endif
61
62 #ifndef HAVE_MEMSET
63 /* very slow, deal with it */
64 extern void * memset(void * dest, int c, unsigned long n)
65 {
66     unsigned char * temp=dest;
67     unsigned long   i;
68
69     if (!temp)
70         return NULL;
71
72     for (i=0; i<n; i++)
73         temp[i] = (unsigned char)c;
74     return dest;
75 }
76 #endif
77
78 #ifndef HAVE_STRDUP
79 extern char * strdup(char const * str)
80 {
81     char * out;
82
83     if (!str)
84         return NULL;
85     if (!(out = malloc(strlen(str)+1)))
86         return NULL;
87     strcpy(out,str);
88     return out;
89 }
90 #endif
91
92 #ifndef HAVE_STRERROR
93 extern char const * strerror(int errornum)
94 {
95     if (errornum==0)
96         return "No error";
97 #ifdef EPERM
98     if (errornum==EPERM)
99         return "Operation not permitted";
100 #endif
101 #ifdef ENOENT
102     if (errornum==ENOENT)
103         return "No such file or directory";
104 #endif
105 #ifdef ESRCH
106     if (errornum==ESRCH)
107         return "No such process";
108 #endif
109 #ifdef EINTR
110     if (errornum==EINTR)
111         return "Interrupted system call";
112 #endif
113 #ifdef EIO
114     if (errornum==EIO)
115         return "I/O error";
116 #endif
117 #ifdef ENXIO
118     if (errornum==EIO)
119         return "No such device or address";
120 #endif
121 #ifdef EBADF
122     if (errornum==EBADF)
123         return "Bad file number";
124 #endif
125 #ifdef EAGAIN
126     if (errornum==EAGAIN)
127         return "Try again";
128 #endif
129 #ifdef ENOMEM
130     if (errornum==ENOMEM)
131         return "Out of memory";
132 #endif
133 #ifdef EACCES
134     if (errornum==EACCES)
135         return "Permission denied";
136 #endif
137 #ifdef EFAULT
138     if (errornum==EFAULT)
139         return "Bad address";
140 #endif
141 #ifdef EBUSY
142     if (errornum==EBUSY)
143         return "Device or resource busy";
144 #endif
145 #ifdef EEXIST
146     if (errornum==EEXIST)
147         return "File exists";
148 #endif
149 #ifdef EXDEV
150     if (errornum==EXDEV)
151         return "Cross-device link";
152 #endif
153 #ifdef EDEADLK
154     if (errornum==EXDEV)
155         return "Resource deadlock would occur";
156 #endif
157 #ifdef EDEADLOCK
158     if (errornum==EDEADLOCK)
159         return "Resource deadlock would occur";
160 #endif
161 #ifdef ENODEV
162     if (errornum==ENODEV)
163         return "No such device";
164 #endif
165 #ifdef ENOTDIR
166     if (errornum==ENOTDIR)
167         return "Not a directory";
168 #endif
169 #ifdef EISDIR
170     if (errornum==EISDIR)
171         return "Is a directory";
172 #endif
173 #ifdef EINVAL
174     if (errornum==EINVAL)
175         return "Invalid argument";
176 #endif
177 #ifdef ENFILE
178     if (errornum==ENFILE)
179         return "Too many open files in system";
180 #endif
181 #ifdef EMFILE
182     if (errornum==EMFILE)
183         return "Too many open files";
184 #endif
185 #ifdef ENOTTY
186     if (errornum==ENOTTY)
187         return "Not a typewriter";
188 #endif
189 #ifdef ETXTBSY
190     if (errornum==ETXTBSY)
191         return "Text file busy";
192 #endif
193 #ifdef EFBIG
194     if (errornum==EFBIG)
195         return "File too large";
196 #endif
197 #ifdef ENOSPC
198     if (errornum==ENOSPC)
199         return "No space left on device";
200 #endif
201 #ifdef ESPIPE
202     if (errornum==ESPIPE)
203         return "Illegal seek";
204 #endif
205 #ifdef EROFS
206     if (errornum==EROFS)
207         return "Read-only file system";
208 #endif
209 #ifdef EMLINK
210     if (errornum==EMLINK)
211         return "Too many links";
212 #endif
213 #ifdef EPIPE
214     if (errornum==EPIPE)
215         return "Broken pipe";
216 #endif
217 #ifdef EDOM
218     if (errornum==EDOM)
219         return "Math argument out of domain of func";
220 #endif
221 #ifdef ERANGE
222     if (errornum==ERANGE)
223         return "Math result not representable";
224 #endif
225 #ifdef ENAMETOOLONG
226     if (errornum==ENAMETOOLONG)
227         return "File name too long";
228 #endif
229 #ifdef ENOLCK
230     if (errornum==ENOLCK)
231         return "No record locks avaliable";
232 #endif
233 #ifdef ENOSYS
234     if (errornum==ENOSYS)
235         return "Function not implemented";
236 #endif
237 #ifdef ENOTEMPTY
238     if (errornum==ENOTEMPTY)
239         return "Directory not empty";
240 #endif
241 #ifdef ELOOP
242     if (errornum==ELOOP)
243         return "Too many symbolic links encountered";
244 #endif
245 #ifdef EHOSTDOWN
246     if (errornum==EHOSTDOWN)
247         return "Host is down";
248 #endif
249 #ifdef EHOSTUNREACH
250     if (errornum==EHOSTUNREACH)
251         return "No route to host";
252 #endif
253 #ifdef EALREADY
254     if (errornum==EALREADY)
255         return "Operation already in progress";
256 #endif
257 #ifdef EINPROGRESS
258     if (errornum==EINPROGRESS)
259         return "Operation now in progress";
260 #endif
261 #ifdef ESTALE
262     if (errornum==ESTALE)
263         return "Stale NFS filehandle";
264 #endif
265 #ifdef EDQUOT
266     if (errornum==EDQUOT)
267         return "Quota exceeded";
268 #endif
269 #ifdef EWOULDBLOCK
270     if (errornum==EWOULDBLOCK)
271         return "Operation would block";
272 #endif
273 #ifdef ECOMM
274     if (errornum==ECOMM)
275         return "Communication error on send";
276 #endif
277 #ifdef EPROTO
278     if (errornum==EPROTO)
279         return "Protocol error";
280 #endif
281 #ifdef EPROTONOSUPPORT
282     if (errornum==EPROTONOSUPPORT)
283         return "Protocol not supported";
284 #endif
285 #ifdef ESOCKTNOSUPPORT
286     if (errornum==ESOCKTNOSUPPORT)
287         return "Socket type not supported";
288 #endif
289 #ifdef ESOCKTNOSUPPORT
290     if (errornum==EOPNOTSUPP)
291         return "Operation not supported";
292 #endif
293 #ifdef EPFNOSUPPORT
294     if (errornum==EPFNOSUPPORT)
295         return "Protocol family not supported";
296 #endif
297 #ifdef EAFNOSUPPORT
298     if (errornum==EAFNOSUPPORT)
299         return "Address family not supported by protocol family";
300 #endif
301 #ifdef EADDRINUSE
302     if (errornum==EADDRINUSE)
303         return "Address already in use";
304 #endif
305 #ifdef EADDRNOTAVAIL
306     if (errornum==EADDRNOTAVAIL)
307         return "Cannot assign requested address";
308 #endif
309 #ifdef ENETDOWN
310     if (errornum==ENETDOWN)
311         return "Network is down";
312 #endif
313 #ifdef ENETUNREACH
314     if (errornum==ENETUNREACH)
315         return "Network is unreachable";
316 #endif
317 #ifdef ENETRESET
318     if (errornum==ENETRESET)
319         return "Network dropped connection on reset";
320 #endif
321 #ifdef ECONNABORTED
322     if (errornum==ECONNABORTED)
323         return "Software caused connection abort";
324 #endif
325 #ifdef ECONNRESET
326     if (errornum==ECONNRESET)
327         return " Connection reset by peer";
328 #endif
329 #ifdef ENOBUFS
330     if (errornum==ENOBUFS)
331         return "No buffer space available";
332 #endif
333 #ifdef EISCONN
334     if (errornum==EISCONN)
335         return "Socket is already connected";
336 #endif
337 #ifdef ENOTCONN
338     if (errornum==ENOTCONN)
339         return "Socket is not connected";
340 #endif
341 #ifdef ESHUTDOWN
342     if (errornum==ESHUTDOWN)
343         return " Cannot send after socket shutdown";
344 #endif
345 #ifdef ETIMEDOUT
346     if (errornum==ETIMEDOUT)
347         return "Connection timed out";
348 #endif
349 #ifdef ECONNREFUSED
350     if (errornum==ECONNREFUSED)
351         return "Connection refused";
352 #endif
353     return "Unknown error";
354 }
355 #endif