1 /*
2  * Random win32 compat.
3  *
4  * Copyright (c) 2007-2009  Marko Kreen, Skype Technologies OÜ
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _USUAL_BASE_WIN32_H_
20 #define _USUAL_BASE_WIN32_H_
21 
22 #include <winsock2.h>
23 #include <windows.h>
24 
25 #include <io.h>
26 #include <process.h>
27 
28 #ifndef ECONNABORTED
29 #define ECONNABORTED WSAECONNABORTED
30 #endif
31 #ifndef EMSGSIZE
32 #define EMSGSIZE WSAEMSGSIZE
33 #endif
34 #ifndef EINPROGRESS
35 #define EINPROGRESS WSAEWOULDBLOCK /* WSAEINPROGRESS */
36 #endif
37 #ifndef SHUT_RDWR
38 #define SHUT_RDWR SD_BOTH
39 #endif
40 #ifndef ENOTCONN
41 #define ENOTCONN WSAENOTCONN
42 #endif
43 #ifndef ECONNRESET
44 #define ECONNRESET WSAECONNRESET
45 #endif
46 
47 #undef EAGAIN
48 #define EAGAIN WSAEWOULDBLOCK /* WSAEAGAIN */
49 
50 #ifndef EAFNOSUPPORT
51 #define EAFNOSUPPORT ENOSYS
52 #endif
53 
54 #ifndef AI_ADDRCONFIG
55 #define AI_ADDRCONFIG 0
56 #endif
57 
58 /* dummy types / functions */
59 #define hstrerror strerror
60 #define getuid() (6667)
61 #define setsid() getpid()
62 #define setgid(x) (-1)
63 #define setuid(x) (-1)
64 #define fork() (errno = ENOSYS, -1)
65 #define geteuid() getuid()
setgroups(int ngroups,const gid_t * gidsets)66 static inline int setgroups(int ngroups, const gid_t *gidsets) { errno = EINVAL; return -1; }
67 #define chown(f, u, g) (-1)
68 
69 #define srandom(s) srand(s)
70 #define random() rand()
71 
72 #ifdef _MSC_VER
73 
74 #define snprintf(fmt, ...) _snprintf(fmt, __VA_ARGS__)
75 
strcasecmp(const char * a,const char * b)76 static inline int strcasecmp(const char *a, const char *b)
77 {
78 	return _stricmp(a, b);
79 }
80 
strncasecmp(const char * a,const char * b,size_t cnt)81 static inline int strncasecmp(const char *a, const char *b, size_t cnt)
82 {
83 	return _strnicmp(a, b, cnt);
84 }
85 
86 typedef int ssize_t;
87 
88 #endif
89 
90 /* getrlimit() */
91 #define RLIMIT_NOFILE -1
92 struct rlimit {
93 	int rlim_cur;
94 	int rlim_max;
95 };
getrlimit(int res,struct rlimit * dst)96 static inline int getrlimit(int res, struct rlimit *dst)
97 {
98 	dst->rlim_cur = dst->rlim_max = -1;
99 	return 0;
100 }
101 
102 /* dummy getpwnam() */
103 struct passwd {
104 	char *pw_name;
105 	char *pw_passwd;
106 	uid_t pw_uid;
107 	pid_t pw_gid;
108 	char *pw_gecos;
109 	char *pw_dir;
110 	char *pw_shell;
111 };
getpwnam(const char * u)112 static inline struct passwd *getpwnam(const char *u) { return NULL; }
getpwuid(uid_t uid)113 static inline struct passwd *getpwuid(uid_t uid) { return NULL; }
114 
115 /* dummy getgrnam() */
116 struct group {
117 	char *gr_name;
118 	char *gr_passwd;
119 	gid_t gr_gid;
120 	char **gr_mem;
121 };
getgrnam(const char * g)122 static inline struct group *getgrnam(const char *g) { return NULL; }
getgrgid(gid_t gid)123 static inline struct group *getgrgid(gid_t gid) { return NULL; }
124 
125 /* format specifiers that should be in <inttypes.h> */
126 #ifndef HAVE_INTTYPES_H
127 
128 #define PRId8	"d"
129 #define PRId16	"d"
130 #define PRId32	"d"
131 #define PRId64	"I64d"
132 
133 #define PRIi8	"d"
134 #define PRIi16	"d"
135 #define PRIi32	"d"
136 #define PRIi64	"I64d"
137 
138 #define PRIo8	"o"
139 #define PRIo16	"o"
140 #define PRIo32	"o"
141 #define PRIo64	"I64o"
142 
143 #define PRIu8	"u"
144 #define PRIu16	"u"
145 #define PRIu32	"u"
146 #define PRIu64	"I64u"
147 
148 #define PRIx8	"x"
149 #define PRIx16	"x"
150 #define PRIx32	"x"
151 #define PRIx64	"I64x"
152 
153 #define PRIX8	"X"
154 #define PRIX16	"X"
155 #define PRIX32	"X"
156 #define PRIX64	"I64X"
157 
158 #endif
159 
160 #endif
161