]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/os/mswindows.h
Windows: simplify abstraction layer header files
[thirdparty/squid.git] / compat / os / mswindows.h
1 /*
2 * AUTHOR: Andrey Shorin <tolsty@tushino.com>
3 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32 #ifndef SQUID_OS_MSWINDOWS_H
33 #define SQUID_OS_MSWINDOWS_H
34
35 #if _SQUID_WINDOWS_
36
37 #define ACL WindowsACL
38 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
39 #if _MSC_VER == 1400
40 #define _CRT_SECURE_NO_DEPRECATE
41 #pragma warning( disable : 4290 )
42 #pragma warning( disable : 4996 )
43 #endif
44 #endif
45
46 /* Some MinGW version defines min() and max() as macros
47 causing the fail of the build process. The following
48 #define will disable that definition
49 */
50 #if defined(__GNUC__)
51 #define NOMINMAX
52 #endif
53
54 #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
55 # define __USE_FILE_OFFSET64 1
56 #endif
57
58 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
59
60 #if defined __USE_FILE_OFFSET64
61 typedef uint64_t ino_t;
62 #else
63 typedef unsigned long ino_t;
64 #endif
65
66 #define INT64_MAX _I64_MAX
67 #define INT64_MIN _I64_MIN
68
69 #include "default_config_file.h"
70 /* Some tricks for MS Compilers */
71 #define __STDC__ 1
72 #define THREADLOCAL __declspec(thread)
73
74 #elif defined(__GNUC__) /* gcc environment */
75
76 #define THREADLOCAL __attribute__((section(".tls")))
77
78 #endif
79
80 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
81 #define alloca _alloca
82 #endif
83 #define chdir _chdir
84 #define dup _dup
85 #define dup2 _dup2
86 #define fdopen _fdopen
87 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
88 #define fileno _fileno
89 #define fstat _fstati64
90 #endif
91 #if !defined(_SQUID_MINGW_) // MinGW defines these properly
92 SQUIDCEXTERN int WIN32_ftruncate(int fd, off_t size);
93 #define ftruncate WIN32_ftruncate
94 SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
95 #define truncate WIN32_truncate
96 #endif
97 #define getcwd _getcwd
98 #define getpid _getpid
99 #define getrusage WIN32_getrusage
100 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
101 #define lseek _lseeki64
102 #define memccpy _memccpy
103 #define mkdir(p,F) _mkdir((p))
104 #define mktemp _mktemp
105 #endif
106 #if _SQUID_MINGW_
107 #define mkdir(p,F) mkdir((p))
108 #endif
109 #define pclose _pclose
110 #define popen _popen
111 #define putenv _putenv
112 #define setmode _setmode
113 #define sleep(t) Sleep((t)*1000)
114 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
115 #define snprintf _snprintf
116 #define stat _stati64
117 #define strcasecmp _stricmp
118 #define strdup _strdup
119 #define strlwr _strlwr
120 #define strncasecmp _strnicmp
121 #define tempnam _tempnam
122 #endif
123 #define umask _umask
124 #define unlink _unlink
125 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
126 #define vsnprintf _vsnprintf
127 #endif
128
129 #define O_RDONLY _O_RDONLY
130 #define O_WRONLY _O_WRONLY
131 #define O_RDWR _O_RDWR
132 #define O_APPEND _O_APPEND
133
134 #define O_CREAT _O_CREAT
135 #define O_TRUNC _O_TRUNC
136 #define O_EXCL _O_EXCL
137
138 #define O_TEXT _O_TEXT
139 #define O_BINARY _O_BINARY
140 #define O_RAW _O_BINARY
141 #define O_TEMPORARY _O_TEMPORARY
142 #define O_NOINHERIT _O_NOINHERIT
143 #define O_SEQUENTIAL _O_SEQUENTIAL
144 #define O_RANDOM _O_RANDOM
145 #define O_NDELAY 0
146
147 #define S_IFMT _S_IFMT
148 #define S_IFDIR _S_IFDIR
149 #define S_IFCHR _S_IFCHR
150 #define S_IFREG _S_IFREG
151 #define S_IREAD _S_IREAD
152 #define S_IWRITE _S_IWRITE
153 #define S_IEXEC _S_IEXEC
154
155 #define S_IRWXO 007
156 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
157 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
158 #endif
159
160 #define SIGHUP 1 /* hangup */
161 #define SIGKILL 9 /* kill (cannot be caught or ignored) */
162 #define SIGBUS 10 /* bus error */
163 #define SIGPIPE 13 /* write on a pipe with no one to read it */
164 #define SIGCHLD 20 /* to parent on child stop or exit */
165 #define SIGUSR1 30 /* user defined signal 1 */
166 #define SIGUSR2 31 /* user defined signal 2 */
167
168 #if !_SQUID_CYGWIN_
169 typedef int uid_t;
170 typedef int gid_t;
171 #endif
172
173 struct passwd {
174 char *pw_name; /* user name */
175 char *pw_passwd; /* user password */
176 uid_t pw_uid; /* user id */
177 gid_t pw_gid; /* group id */
178 char *pw_gecos; /* real name */
179 char *pw_dir; /* home directory */
180 char *pw_shell; /* shell program */
181 };
182
183 struct group {
184 char *gr_name; /* group name */
185 char *gr_passwd; /* group password */
186 gid_t gr_gid; /* group id */
187 char **gr_mem; /* group members */
188 };
189
190 struct statfs {
191 long f_type; /* type of filesystem (see below) */
192 long f_bsize; /* optimal transfer block size */
193 long f_blocks; /* total data blocks in file system */
194 long f_bfree; /* free blocks in fs */
195 long f_bavail; /* free blocks avail to non-superuser */
196 long f_files; /* total file nodes in file system */
197 long f_ffree; /* free file nodes in fs */
198 long f_fsid; /* file system id */
199 long f_namelen; /* maximum length of filenames */
200 long f_spare[6]; /* spare for later */
201 };
202
203 #if !HAVE_GETTIMEOFDAY
204 struct timezone {
205 int tz_minuteswest; /* minutes west of Greenwich */
206 int tz_dsttime; /* type of dst correction */
207 };
208 #endif
209
210 #define CHANGE_FD_SETSIZE 1
211 #if CHANGE_FD_SETSIZE && SQUID_MAXFD > DEFAULT_FD_SETSIZE
212 #define FD_SETSIZE SQUID_MAXFD
213 #endif
214
215 #include <process.h>
216 #include <errno.h>
217 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
218 #include <winsock2.h>
219 #endif
220 #include <ws2tcpip.h>
221 #if (EAI_NODATA == EAI_NONAME)
222 #undef EAI_NODATA
223 #define EAI_NODATA WSANO_DATA
224 #endif
225 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
226 /* Hack to suppress compiler warnings on FD_SET() & FD_CLR() */
227 #pragma warning (push)
228 #pragma warning (disable:4142)
229 #endif
230 /* prevent inclusion of wingdi.h */
231 #define NOGDI
232 #include <ws2spi.h>
233 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
234 #pragma warning (pop)
235 #endif
236 #include <io.h>
237
238 typedef char * caddr_t;
239
240 #ifndef _PATH_DEVNULL
241 #define _PATH_DEVNULL "NUL"
242 #endif
243
244 #undef FD_CLOSE
245 #undef FD_OPEN
246 #undef FD_READ
247 #undef FD_WRITE
248
249 #ifndef EISCONN
250 #define EISCONN WSAEISCONN
251 #endif
252 #ifndef EINPROGRESS
253 #define EINPROGRESS WSAEINPROGRESS
254 #endif
255 #ifndef EWOULDBLOCK
256 #define EWOULDBLOCK WSAEWOULDBLOCK
257 #endif
258 #ifndef EALREADY
259 #define EALREADY WSAEALREADY
260 #endif
261 #ifndef ETIMEDOUT
262 #define ETIMEDOUT WSAETIMEDOUT
263 #endif
264 #ifndef ECONNREFUSED
265 #define ECONNREFUSED WSAECONNREFUSED
266 #endif
267 #ifndef ECONNRESET
268 #define ECONNRESET WSAECONNRESET
269 #endif
270 #ifndef ENOTCONN
271 #define ENOTCONN WSAENOTCONN
272 #endif
273 #ifndef ERESTART
274 #define ERESTART WSATRY_AGAIN
275 #endif
276 #ifndef EAFNOSUPPORT
277 #define EAFNOSUPPORT WSAEAFNOSUPPORT
278 #endif
279 #ifndef ENETUNREACH
280 #define ENETUNREACH WSAENETUNREACH
281 #endif
282
283 #undef h_errno
284 #define h_errno errno /* we'll set it ourselves */
285
286 #undef FD_CLR
287 #define FD_CLR(fd, set) do { \
288 u_int __i; \
289 SOCKET __sock = _get_osfhandle(fd); \
290 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count ; __i++) { \
291 if (((fd_set FAR *)(set))->fd_array[__i] == __sock) { \
292 while (__i < ((fd_set FAR *)(set))->fd_count-1) { \
293 ((fd_set FAR *)(set))->fd_array[__i] = \
294 ((fd_set FAR *)(set))->fd_array[__i+1]; \
295 __i++; \
296 } \
297 ((fd_set FAR *)(set))->fd_count--; \
298 break; \
299 } \
300 } \
301 } while(0)
302
303 #undef FD_SET
304 #define FD_SET(fd, set) do { \
305 u_int __i; \
306 SOCKET __sock = _get_osfhandle(fd); \
307 for (__i = 0; __i < ((fd_set FAR *)(set))->fd_count; __i++) { \
308 if (((fd_set FAR *)(set))->fd_array[__i] == (__sock)) { \
309 break; \
310 } \
311 } \
312 if (__i == ((fd_set FAR *)(set))->fd_count) { \
313 if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) { \
314 ((fd_set FAR *)(set))->fd_array[__i] = (__sock); \
315 ((fd_set FAR *)(set))->fd_count++; \
316 } \
317 } \
318 } while(0)
319
320 #undef FD_ISSET
321 #define FD_ISSET(fd, set) Win32__WSAFDIsSet(fd, (fd_set FAR *)(set))
322
323 /* internal to Microsoft CRTLIB */
324 typedef struct {
325 long osfhnd; /* underlying OS file HANDLE */
326 char osfile; /* attributes of file (e.g., open in text mode?) */
327 char pipech; /* one char buffer for handles opened on pipes */
328 #ifdef _MT
329 int lockinitflag;
330 CRITICAL_SECTION lock;
331 #endif /* _MT */
332 } ioinfo;
333 #define IOINFO_L2E 5
334 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
335 #define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)) )
336 #define _osfile(i) ( _pioinfo(i)->osfile )
337 #define _osfhnd(i) ( _pioinfo(i)->osfhnd )
338 #define FOPEN 0x01 /* file handle open */
339
340 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
341
342 SQUIDCEXTERN _CRTIMP ioinfo * __pioinfo[];
343 SQUIDCEXTERN int __cdecl _free_osfhnd(int);
344
345 #elif _SQUID_MINGW_ /* MinGW environment */
346
347 __MINGW_IMPORT ioinfo * __pioinfo[];
348 SQUIDCEXTERN int _free_osfhnd(int);
349
350 #endif
351
352 SQUIDCEXTERN THREADLOCAL int ws32_result;
353
354 #define strerror(e) WIN32_strerror(e)
355 #define HAVE_STRERROR 1
356
357 #ifdef __cplusplus
358
359 inline
360 int close(int fd)
361 {
362 char l_so_type[sizeof(int)];
363 int l_so_type_siz = sizeof(l_so_type);
364 SOCKET sock = _get_osfhandle(fd);
365
366 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
367 int result = 0;
368 if (closesocket(sock) == SOCKET_ERROR) {
369 errno = WSAGetLastError();
370 result = 1;
371 }
372 _free_osfhnd(fd);
373 _osfile(fd) = 0;
374 return result;
375 } else
376 return _close(fd);
377 }
378
379 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
380
381 #ifndef _S_IREAD
382 #define _S_IREAD 0x0100
383 #endif
384
385 #ifndef _S_IWRITE
386 #define _S_IWRITE 0x0080
387 #endif
388
389 inline
390 int open(const char *filename, int oflag, int pmode = 0)
391 {
392 return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
393 }
394 #endif
395
396 inline
397 int read(int fd, void * buf, size_t siz)
398 {
399 char l_so_type[sizeof(int)];
400 int l_so_type_siz = sizeof(l_so_type);
401 SOCKET sock = _get_osfhandle(fd);
402
403 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
404 return ::recv(sock, (char FAR *) buf, (int)siz, 0);
405 else
406 return _read(fd, buf, (unsigned int)siz);
407 }
408
409 inline
410 int write(int fd, const void * buf, size_t siz)
411 {
412 char l_so_type[sizeof(int)];
413 int l_so_type_siz = sizeof(l_so_type);
414 SOCKET sock = _get_osfhandle(fd);
415
416 if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
417 return ::send(sock, (char FAR *) buf, siz, 0);
418 else
419 return _write(fd, buf, siz);
420 }
421
422 inline
423 char *index(const char *s, int c)
424 {
425 return (char *)strchr(s,c);
426 }
427
428 /** \cond AUTODOCS-IGNORE */
429 namespace Squid
430 {
431 /** \endcond */
432
433 inline
434 int accept(int s, struct sockaddr * a, size_t * l)
435 {
436 SOCKET result;
437 if ((result = ::accept(_get_osfhandle(s), a, (int *)l)) == INVALID_SOCKET) {
438 if (WSAEMFILE == (errno = WSAGetLastError()))
439 errno = EMFILE;
440 return -1;
441 } else
442 return _open_osfhandle(result, 0);
443 }
444
445 inline
446 int bind(int s, struct sockaddr * n, int l)
447 {
448 if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
449 errno = WSAGetLastError();
450 return -1;
451 } else
452 return 0;
453 }
454
455 inline
456 int connect(int s, const struct sockaddr * n, int l)
457 {
458 if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
459 if (WSAEMFILE == (errno = WSAGetLastError()))
460 errno = EMFILE;
461 return -1;
462 } else
463 return 0;
464 }
465
466 inline
467 struct hostent * gethostbyname (const char *n) {
468 HOSTENT FAR * result;
469 if ((result = ::gethostbyname(n)) == NULL)
470 errno = WSAGetLastError();
471 return result;
472 }
473 #define gethostbyname(n) Squid::gethostbyname(n)
474
475 inline
476 SERVENT FAR* getservbyname (const char * n, const char * p)
477 {
478 SERVENT FAR * result;
479 if ((result = ::getservbyname(n, p)) == NULL)
480 errno = WSAGetLastError();
481 return result;
482 }
483 #define getservbyname(n,p) Squid::getservbyname(n,p)
484
485 inline
486 HOSTENT FAR * gethostbyaddr(const char * a, int l, int t)
487 {
488 HOSTENT FAR * result;
489 if ((result = ::gethostbyaddr(a, l, t)) == NULL)
490 errno = WSAGetLastError();
491 return result;
492 }
493 #define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
494
495 inline
496 int getsockname(int s, struct sockaddr * n, size_t * l)
497 {
498 if ((::getsockname(_get_osfhandle(s), n, (int *)l)) == SOCKET_ERROR) {
499 errno = WSAGetLastError();
500 return -1;
501 } else
502 return 0;
503 }
504
505 inline
506 int gethostname(char * n, size_t l)
507 {
508 if ((::gethostname(n, l)) == SOCKET_ERROR) {
509 errno = WSAGetLastError();
510 return -1;
511 } else
512 return 0;
513 }
514 #define gethostname(n,l) Squid::gethostname(n,l)
515
516 inline
517 int getsockopt(int s, int l, int o, void * v, int * n)
518 {
519 Sleep(1);
520 if ((::getsockopt(_get_osfhandle(s), l, o,(char *) v, n)) == SOCKET_ERROR) {
521 errno = WSAGetLastError();
522 return -1;
523 } else
524 return 0;
525 }
526
527 /* Simple ioctl() emulation */
528 inline
529 int ioctl(int s, int c, void * a)
530 {
531 if ((::ioctlsocket(_get_osfhandle(s), c, (u_long FAR *)a)) == SOCKET_ERROR) {
532 errno = WSAGetLastError();
533 return -1;
534 } else
535 return 0;
536 }
537
538 inline
539 int ioctlsocket(int s, long c, u_long FAR * a)
540 {
541 if ((::ioctlsocket(_get_osfhandle(s), c, a)) == SOCKET_ERROR) {
542 errno = WSAGetLastError();
543 return -1;
544 } else
545 return 0;
546 }
547
548 inline
549 int listen(int s, int b)
550 {
551 if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
552 if (WSAEMFILE == (errno = WSAGetLastError()))
553 errno = EMFILE;
554 return -1;
555 } else
556 return 0;
557 }
558 #define listen(s,b) Squid::listen(s,b)
559
560 inline
561 int recv(int s, void * b, size_t l, int f)
562 {
563 int result;
564 if ((result = ::recv(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
565 errno = WSAGetLastError();
566 return -1;
567 } else
568 return result;
569 }
570
571 inline
572 int recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, size_t * fl)
573 {
574 int result;
575 if ((result = ::recvfrom(_get_osfhandle(s), (char *)b, l, f, fr, (int *)fl)) == SOCKET_ERROR) {
576 errno = WSAGetLastError();
577 return -1;
578 } else
579 return result;
580 }
581
582 inline
583 int select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
584 {
585 int result;
586 if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
587 errno = WSAGetLastError();
588 return -1;
589 } else
590 return result;
591 }
592 #define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
593
594 inline
595 int send(int s, const void * b, size_t l, int f)
596 {
597 int result;
598 if ((result = ::send(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
599 errno = WSAGetLastError();
600 return -1;
601 } else
602 return result;
603 }
604
605 inline
606 int sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, int tl)
607 {
608 int result;
609 if ((result = ::sendto(_get_osfhandle(s), (char *)b, l, f, t, tl)) == SOCKET_ERROR) {
610 errno = WSAGetLastError();
611 return -1;
612 } else
613 return result;
614 }
615
616 inline
617 int setsockopt(SOCKET s, int l, int o, const char * v, int n)
618 {
619 SOCKET socket;
620
621 socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((int)s));
622
623 if (::setsockopt(socket, l, o, v, n) == SOCKET_ERROR) {
624 errno = WSAGetLastError();
625 return -1;
626 } else
627 return 0;
628 }
629 #define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
630
631 inline
632 int shutdown(int s, int h)
633 {
634 if (::shutdown(_get_osfhandle(s),h) == SOCKET_ERROR) {
635 errno = WSAGetLastError();
636 return -1;
637 } else
638 return 0;
639 }
640
641 inline
642 int socket(int f, int t, int p)
643 {
644 SOCKET result;
645 if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
646 if (WSAEMFILE == (errno = WSAGetLastError()))
647 errno = EMFILE;
648 return -1;
649 } else
650 return _open_osfhandle(result, 0);
651 }
652 #define socket(f,t,p) Squid::socket(f,t,p)
653
654 inline int
655 pipe(int pipefd[2])
656 {
657 return _pipe(pipefd,4096,_O_BINARY);
658 }
659 using Squid::pipe;
660
661 inline
662 int WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
663 {
664 if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
665 errno = WSAGetLastError();
666 return -1;
667 } else
668 return 0;
669 }
670
671 #undef WSADuplicateSocket
672 inline
673 int WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
674 {
675 #ifdef UNICODE
676 if (::WSADuplicateSocketW(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
677 #else
678 if (::WSADuplicateSocketA(_get_osfhandle(s), n, l) == SOCKET_ERROR) {
679 #endif
680 errno = WSAGetLastError();
681 return -1;
682 } else
683 return 0;
684 }
685
686 #undef WSASocket
687 inline
688 int WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
689 {
690 SOCKET result;
691 #ifdef UNICODE
692 if ((result = ::WSASocketW(a, t, p, i, g, f)) == INVALID_SOCKET) {
693 #else
694 if ((result = ::WSASocketA(a, t, p, i, g, f)) == INVALID_SOCKET) {
695 #endif
696 if (WSAEMFILE == (errno = WSAGetLastError()))
697 errno = EMFILE;
698 return -1;
699 } else
700 return _open_osfhandle(result, 0);
701 }
702
703 } /* namespace Squid */
704
705 #else /* #ifdef __cplusplus */
706 #define connect(s,n,l) \
707 (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
708 (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
709 #define gethostbyname(n) \
710 (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
711 (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
712 #define gethostname(n,l) \
713 (SOCKET_ERROR == gethostname(n,l) ? \
714 (errno = WSAGetLastError()), -1 : 0)
715 #define recv(s,b,l,f) \
716 (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
717 (errno = WSAGetLastError()), -1 : ws32_result)
718 #define sendto(s,b,l,f,t,tl) \
719 (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
720 (errno = WSAGetLastError()), -1 : ws32_result)
721 #define select(n,r,w,e,t) \
722 (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
723 (errno = WSAGetLastError()), -1 : ws32_result)
724 #define socket(f,t,p) \
725 (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
726 ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
727 (SOCKET)_open_osfhandle(ws32_result,0))
728 #define write _write /* Needed in util.c */
729 #define open _open /* Needed in win32lib.c */
730 #endif /* #ifdef __cplusplus */
731
732 #if HAVE_SYS_RESOURCE_H
733 #include <sys/resource.h>
734 #else
735 #define RUSAGE_SELF 0 /* calling process */
736 #define RUSAGE_CHILDREN -1 /* terminated child processes */
737
738 struct rusage {
739 struct timeval ru_utime; /* user time used */
740 struct timeval ru_stime; /* system time used */
741 long ru_maxrss; /* integral max resident set size */
742 long ru_ixrss; /* integral shared text memory size */
743 long ru_idrss; /* integral unshared data size */
744 long ru_isrss; /* integral unshared stack size */
745 long ru_minflt; /* page reclaims */
746 long ru_majflt; /* page faults */
747 long ru_nswap; /* swaps */
748 long ru_inblock; /* block input operations */
749 long ru_oublock; /* block output operations */
750 long ru_msgsnd; /* messages sent */
751 long ru_msgrcv; /* messages received */
752 long ru_nsignals; /* signals received */
753 long ru_nvcsw; /* voluntary context switches */
754 long ru_nivcsw; /* involuntary context switches */
755 };
756 #endif /* HAVE_SYS_RESOURCE_H */
757
758 #undef ACL
759
760 #if !defined(getpagesize)
761 /* Windows may lack getpagesize() prototype */
762 SQUIDCEXTERN size_t getpagesize(void);
763 #endif
764
765 /* gcc doesn't recognize the Windows native 64 bit formatting tags causing
766 * the compile fail, so we must disable the check on native Windows.
767 */
768 #if __GNUC__
769 #define PRINTF_FORMAT_ARG1
770 #define PRINTF_FORMAT_ARG2
771 #define PRINTF_FORMAT_ARG3
772 #endif
773
774 /* XXX: the logic around this is a bit warped:
775 * we #define ACL unconditionally at the top of this file,
776 * then #undef ACL unconditionally hafway down,
777 * then here re-define ACL to the same value as at the top,
778 * then include windows.h and #undef ACL again.
779 */
780 #ifndef ACL
781 #define ACL WindowsACL
782 #define _MSWIN_ACL_WAS_NOT_DEFINED 1
783 #endif
784 #include <windows.h>
785 #if _MSWIN_ACL_WAS_NOT_DEFINED
786 #undef ACL
787 #undef _MSWIN_ACL_WAS_NOT_DEFINED
788 #endif
789
790 #endif /* _SQUID_WINDOWS_ */
791 #endif /* SQUID_OS_MSWINDOWS_H */