]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - compat/os/mswindows.h
Portability: provide xstatvfs() shim for system call statvfs()
[thirdparty/squid.git] / compat / os / mswindows.h
index 4caa8c62a8c9450e894de2153ff9081f6a1fd35a..dfe053b1c30217938ac743b59a62a53987dd4b11 100644 (file)
@@ -1,39 +1,27 @@
+/*
+ * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
 /*
  * AUTHOR: Andrey Shorin <tolsty@tushino.com>
  * AUTHOR: Guido Serassio <serassio@squid-cache.org>
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
- *
  */
+
 #ifndef SQUID_OS_MSWINDOWS_H
 #define SQUID_OS_MSWINDOWS_H
 
 #if _SQUID_WINDOWS_
 
+/****************************************************************************
+ *--------------------------------------------------------------------------*
+ * DO *NOT* MAKE ANY CHANGES below here unless you know what you're doing...*
+ *--------------------------------------------------------------------------*
+ ****************************************************************************/
+
 /* we target Windows XP and later - some API are missing otherwise */
 #if _SQUID_MINGW_
 #if WINVER < 0x0501
@@ -44,6 +32,9 @@
 #endif
 #endif /* _SQUID_MINGW_ */
 
+#if HAVE_DIRECT_H
+#include <direct.h>
+#endif
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif /* HAVE_FCNTL_H */
 #define NOMINMAX
 #endif
 
+/// some builds of MinGW do not define IPV6_V6ONLY socket option
+#if !defined(IPV6_V6ONLY)
+#define IPV6_V6ONLY 27
+#endif
+
 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
 # define __USE_FILE_OFFSET64   1
 #endif
@@ -104,38 +100,32 @@ typedef unsigned long ino_t;
 #define fstat _fstati64
 #define lseek _lseeki64
 #define memccpy _memccpy
-#define mkdir(p,F) _mkdir((p))
 #define mktemp _mktemp
 #define snprintf _snprintf
 #define stat _stati64
 #define strcasecmp _stricmp
-#define strdup _strdup
 #define strlwr _strlwr
 #define strncasecmp _strnicmp
 #define tempnam _tempnam
 #define vsnprintf _vsnprintf
 #endif
 
-/* CygWin and MinGW compilers need these. Microsoft C Compiler does not. */
-#if _SQUID_MINGW_ || _SQUID_CYGWIN_
-#define mkdir(p,F) mkdir((p))
-#endif
-
 /*  Microsoft C Compiler and CygWin need these. MinGW does not */
 #if defined(_MSC_VER) || _SQUID_CYGWIN_
 SQUIDCEXTERN int WIN32_ftruncate(int fd, off_t size);
 #define ftruncate WIN32_ftruncate
 SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
 #define truncate WIN32_truncate
+#define chdir _chdir
 #endif
 
 /* All three compiler systems need these: */
-#define chdir _chdir
 #define dup _dup
 #define dup2 _dup2
 #define fdopen _fdopen
 #define getcwd _getcwd
 #define getpid _getpid
+#define mkdir(p,F) mkdir((p))
 #define pclose _pclose
 #define popen _popen
 #define putenv _putenv
@@ -144,33 +134,76 @@ SQUIDCEXTERN int WIN32_truncate(const char *pathname, off_t length);
 #define umask _umask
 #define unlink _unlink
 
+#ifndef O_RDONLY
 #define O_RDONLY        _O_RDONLY
+#endif
+#ifndef O_WRONLY
 #define O_WRONLY        _O_WRONLY
+#endif
+#ifndef O_RDWR
 #define O_RDWR          _O_RDWR
+#endif
+#ifndef O_APPEND
 #define O_APPEND        _O_APPEND
-
+#endif
+#ifndef O_CREAT
 #define O_CREAT         _O_CREAT
+#endif
+#ifndef O_TRUNC
 #define O_TRUNC         _O_TRUNC
+#endif
+#ifndef O_EXCL
 #define O_EXCL          _O_EXCL
-
+#endif
+#ifndef O_TEXT
 #define O_TEXT          _O_TEXT
+#endif
+#ifndef O_BINARY
 #define O_BINARY        _O_BINARY
+#endif
+#ifndef O_RAW
 #define O_RAW           _O_BINARY
+#endif
+#ifndef O_TEMPORARY
 #define O_TEMPORARY     _O_TEMPORARY
+#endif
+#ifndef O_NOINHERIT
 #define O_NOINHERIT     _O_NOINHERIT
+#endif
+#ifndef O_SEQUENTIAL
 #define O_SEQUENTIAL    _O_SEQUENTIAL
+#endif
+#ifndef O_RANDOM
 #define O_RANDOM        _O_RANDOM
+#endif
+#ifndef O_NDELAY
 #define O_NDELAY       0
+#endif
 
+#ifndef S_IFMT
 #define S_IFMT   _S_IFMT
+#endif
+#ifndef S_IFDIR
 #define S_IFDIR  _S_IFDIR
+#endif
+#ifndef S_IFCHR
 #define S_IFCHR  _S_IFCHR
+#endif
+#ifndef S_IFREG
 #define S_IFREG  _S_IFREG
+#endif
+#ifndef S_IREAD
 #define S_IREAD  _S_IREAD
+#endif
+#ifndef S_IWRITE
 #define S_IWRITE _S_IWRITE
+#endif
+#ifndef S_IEXEC
 #define S_IEXEC  _S_IEXEC
-
+#endif
+#ifndef S_IRWXO
 #define S_IRWXO 007
+#endif
 
 #if defined(_MSC_VER)
 #define        S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
@@ -212,19 +245,6 @@ struct group {
     char    **gr_mem;      /* group members */
 };
 
-struct statfs {
-    long    f_type;     /* type of filesystem (see below) */
-    long    f_bsize;    /* optimal transfer block size */
-    long    f_blocks;   /* total data blocks in file system */
-    long    f_bfree;    /* free blocks in fs */
-    long    f_bavail;   /* free blocks avail to non-superuser */
-    long    f_files;    /* total file nodes in file system */
-    long    f_ffree;    /* free file nodes in fs */
-    long    f_fsid;     /* file system id */
-    long    f_namelen;  /* maximum length of filenames */
-    long    f_spare[6]; /* spare for later */
-};
-
 #if !HAVE_GETTIMEOFDAY
 struct timezone {
     int        tz_minuteswest; /* minutes west of Greenwich */
@@ -388,7 +408,7 @@ SQUIDCEXTERN int _free_osfhnd(int);
 
 SQUIDCEXTERN THREADLOCAL int ws32_result;
 
-#ifdef __cplusplus
+#if defined(__cplusplus)
 
 inline int
 close(int fd)
@@ -459,11 +479,26 @@ index(const char *s, int c)
     return (char *)strchr(s,c);
 }
 
+// stdlib <functional> definitions are required before std API redefinitions.
+#include <functional>
+
 /** \cond AUTODOCS-IGNORE */
 namespace Squid
 {
 /** \endcond */
 
+/*
+ * Each of these functions is defined in the Squid namespace so as not to
+ * clash with the winsock.h and winsock2.h definitions.
+ * It is then paired with a #define to cause these wrappers to be used by
+ * the main code instead of those system definitions.
+ *
+ * We do this wrapper in order to:
+ * - cast the parameter types in only one place, and
+ * - record errors in POSIX errno variable, and
+ * - map the FD value used by Squid to the socket handes used by Windows.
+ */
+
 inline int
 accept(int s, struct sockaddr * a, socklen_t * l)
 {
@@ -475,6 +510,7 @@ accept(int s, struct sockaddr * a, socklen_t * l)
     } else
         return _open_osfhandle(result, 0);
 }
+#define accept(s,a,l) Squid::accept(s,a,reinterpret_cast<socklen_t*>(l))
 
 inline int
 bind(int s, const struct sockaddr * n, socklen_t l)
@@ -485,6 +521,7 @@ bind(int s, const struct sockaddr * n, socklen_t l)
     } else
         return 0;
 }
+#define bind(s,n,l) Squid::bind(s,n,l)
 
 inline int
 connect(int s, const struct sockaddr * n, socklen_t l)
@@ -496,10 +533,10 @@ connect(int s, const struct sockaddr * n, socklen_t l)
     } else
         return 0;
 }
+#define connect(s,n,l) Squid::connect(s,n,l)
 
 inline struct hostent *
-gethostbyname(const char *n)
-{
+gethostbyname(const char *n) {
     HOSTENT FAR * result;
     if ((result = ::gethostbyname(n)) == NULL)
         errno = WSAGetLastError();
@@ -525,11 +562,10 @@ gethostbyaddr(const void * a, size_t l, int t)
         errno = WSAGetLastError();
     return result;
 }
-using Squid::gethostbyaddr;
-//#define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
+#define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
 
 inline int
-getsockname(int s, struct sockaddr * n, size_t * l)
+getsockname(int s, struct sockaddr * n, socklen_t * l)
 {
     int i=*l;
     if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
@@ -538,8 +574,7 @@ getsockname(int s, struct sockaddr * n, size_t * l)
     } else
         return 0;
 }
-using Squid::getsockname;
-//#define getsockname(s,a,l) Squid::getsockname(s,a,l)
+#define getsockname(s,a,l) Squid::getsockname(s,a,reinterpret_cast<socklen_t*>(l))
 
 inline int
 gethostname(char * n, size_t l)
@@ -550,8 +585,7 @@ gethostname(char * n, size_t l)
     } else
         return 0;
 }
-using Squid::gethostname;
-//#define gethostname(n,l) Squid::gethostname(n,l)
+#define gethostname(n,l) Squid::gethostname(n,l)
 
 inline int
 getsockopt(int s, int l, int o, void * v, socklen_t * n)
@@ -563,6 +597,7 @@ getsockopt(int s, int l, int o, void * v, socklen_t * n)
     } else
         return 0;
 }
+#define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
 
 /* Simple ioctl() emulation */
 inline int
@@ -574,6 +609,7 @@ ioctl(int s, int c, void * a)
     } else
         return 0;
 }
+#define ioctl(s,c,a) Squid::ioctl(s,c,a)
 
 inline int
 ioctlsocket(int s, long c, u_long FAR * a)
@@ -584,6 +620,7 @@ ioctlsocket(int s, long c, u_long FAR * a)
     } else
         return 0;
 }
+#define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
 
 inline int
 listen(int s, int b)
@@ -595,8 +632,7 @@ listen(int s, int b)
     } else
         return 0;
 }
-using Squid::listen;
-//#define listen(s,b) Squid::listen(s,b)
+#define listen(s,b) Squid::listen(s,b)
 
 inline ssize_t
 recv(int s, void * b, size_t l, int f)
@@ -611,7 +647,7 @@ recv(int s, void * b, size_t l, int f)
 #define recv(s,b,l,f) Squid::recv(s,b,l,f)
 
 inline ssize_t
-recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, size_t * fl)
+recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, socklen_t * fl)
 {
     ssize_t result;
     int ifl=*fl;
@@ -621,7 +657,7 @@ recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, size_t * fl)
     } else
         return result;
 }
-using Squid::recvfrom;
+#define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,reinterpret_cast<socklen_t*>(n))
 
 inline int
 select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
@@ -645,8 +681,7 @@ send(int s, const char * b, size_t l, int f)
     } else
         return result;
 }
-using Squid::send;
-//#define send(s,b,l,f) Squid::send(s,b,l,f)
+#define send(s,b,l,f) Squid::send(s,reinterpret_cast<const char*>(b),l,f)
 
 inline ssize_t
 sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, socklen_t tl)
@@ -658,6 +693,7 @@ sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, sockle
     } else
         return result;
 }
+#define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
 
 inline int
 setsockopt(SOCKET s, int l, int o, const void * v, socklen_t n)
@@ -683,6 +719,7 @@ shutdown(int s, int h)
     } else
         return 0;
 }
+#define shutdown(s,h) Squid::shutdown(s,h)
 
 inline int
 socket(int f, int t, int p)
@@ -702,7 +739,7 @@ pipe(int pipefd[2])
 {
     return _pipe(pipefd,4096,_O_BINARY);
 }
-using Squid::pipe;
+#define pipe(a) Squid::pipe(a)
 
 inline int
 WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
@@ -713,6 +750,7 @@ WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
     } else
         return 0;
 }
+#define WSAAsyncSelect(s,h,w,e) Squid::WSAAsyncSelect(s,h,w,e)
 
 #undef WSADuplicateSocket
 inline int
@@ -728,6 +766,7 @@ WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
     } else
         return 0;
 }
+#define WSADuplicateSocket(s,n,l) Squid::WSADuplicateSocket(s,n,l)
 
 #undef WSASocket
 inline int
@@ -745,6 +784,7 @@ WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
     } else
         return _open_osfhandle(result, 0);
 }
+#define WSASocket(a,t,p,i,g,f) Squid::WSASocket(a,t,p,i,g,f)
 
 } /* namespace Squid */
 
@@ -775,6 +815,11 @@ WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
 #define open       _open /* Needed in win32lib.c */
 #endif /* #ifdef __cplusplus */
 
+/* provide missing definitions from resoruce.h */
+/* NP: sys/resource.h and sys/time.h are apparently order-dependant. */
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
 #if HAVE_SYS_RESOURCE_H
 #include <sys/resource.h>
 #else
@@ -805,49 +850,48 @@ struct rusage {
 
 SQUIDCEXTERN int chroot(const char *dirname);
 SQUIDCEXTERN int kill(pid_t, int);
-SQUIDCEXTERN int statfs(const char *, struct statfs *);
 SQUIDCEXTERN struct passwd * getpwnam(char *unused);
 SQUIDCEXTERN struct group * getgrnam(char *unused);
 
 static inline uid_t
 geteuid(void)
 {
-       return 100;
+    return 100;
 }
 static inline int
 seteuid (uid_t euid)
 {
-       return 0;
+    return 0;
 }
 static inline uid_t
 getuid(void)
 {
-       return 100;
+    return 100;
 }
 static inline int
 setuid (uid_t uid)
 {
-       return 0;
+    return 0;
 }
 static inline gid_t
 getegid(void)
 {
-       return 100;
+    return 100;
 }
 static inline int
 setegid (gid_t egid)
 {
-       return 0;
+    return 0;
 }
 static inline int
 getgid(void)
 {
-       return 100;
+    return 100;
 }
 static inline int
 setgid (gid_t gid)
 {
-       return 0;
+    return 0;
 }
 
 /* for some reason autoconf misdetects getpagesize.. */
@@ -928,7 +972,5 @@ void syslog(int priority, const char *fmt, ...);
 /* prototypes */
 void WIN32_maperror(unsigned long WIN32_oserrno);
 
-#include "compat/strsep.h"
-
 #endif /* _SQUID_WINDOWS_ */
 #endif /* SQUID_OS_MSWINDOWS_H */