MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
win32.c
1 /* Copyright (c) 1983, 1990, 1993
2  * The Regents of the University of California. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  * This product includes software developed by the University of
15  * California, Berkeley and its contributors.
16  * 4. Neither the name of the University nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33  *
34  * Permission to use, copy, modify, and distribute this software for any
35  * purpose with or without fee is hereby granted, provided that the above
36  * copyright notice and this permission notice appear in all copies, and that
37  * the name of Digital Equipment Corporation not be used in advertising or
38  * publicity pertaining to distribution of the document or software without
39  * specific, written prior permission.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
42  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
44  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
45  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
46  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
47  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
48  * SOFTWARE.
49  *
50  */
51 
52 #include "win32.h"
53 #include <limits.h>
54 #include <ctype.h>
55 #include <time.h>
56 #include <signal.h>
57 
58 /*
59  * Check whether "cp" is a valid ascii representation
60  * of an Internet address and convert to a binary address.
61  * Returns 1 if the address is valid, 0 if not.
62  * This replaces inet_addr, the return value from which
63  * cannot distinguish between failure and a local broadcast address.
64  */
65 int inet_aton(register const char *cp, struct in_addr *addr)
66 {
67  register uint32_t val;
68  register int base, n;
69  register char c;
70  u_int parts[4];
71  register u_int *pp = parts;
72 
73  c = *cp;
74  for (;;) {
75  /*
76  * Collect number up to ``.''.
77  * Values are specified as for C:
78  * 0x=hex, 0=octal, isdigit=decimal.
79  */
80  if (!isdigit(c))
81  return (0);
82  val = 0; base = 10;
83  if (c == '0') {
84  c = *++cp;
85  if (c == 'x' || c == 'X')
86  base = 16, c = *++cp;
87  else
88  base = 8;
89  }
90  for (;;) {
91  if (isascii(c) && isdigit(c)) {
92  val = (val * base) + (c - '0');
93  c = *++cp;
94  } else if (base == 16 && isascii(c) && isxdigit(c)) {
95  val = (val << 4) |
96  (c + 10 - (islower(c) ? 'a' : 'A'));
97  c = *++cp;
98  } else
99  break;
100  }
101  if (c == '.') {
102  /*
103  * Internet format:
104  * a.b.c.d
105  * a.b.c (with c treated as 16 bits)
106  * a.b (with b treated as 24 bits)
107  */
108  if (pp >= parts + 3)
109  return (0);
110  *pp++ = val;
111  c = *++cp;
112  } else
113  break;
114  }
115  /*
116  * Check for trailing characters.
117  */
118  if (c != '\0' && (!isascii(c) || !isspace(c)))
119  return (0);
120  /*
121  * Concoct the address according to
122  * the number of parts specified.
123  */
124  n = pp - parts + 1;
125  switch (n) {
126 
127  case 0:
128  return (0); /* initial nondigit */
129 
130  case 1: /* a -- 32 bits */
131  break;
132 
133  case 2: /* a.b -- 8.24 bits */
134  if (val > 0xffffff)
135  return (0);
136  val |= parts[0] << 24;
137  break;
138 
139  case 3: /* a.b.c -- 8.8.16 bits */
140  if (val > 0xffff)
141  return (0);
142  val |= (parts[0] << 24) | (parts[1] << 16);
143  break;
144 
145  case 4: /* a.b.c.d -- 8.8.8.8 bits */
146  if (val > 0xff)
147  return (0);
148  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
149  break;
150  }
151  if (addr)
152  addr->s_addr = htonl(val);
153  return (1);
154 }
155 
156 unsigned __int64 strtoull(const char *p,char **pend,int base) {
157  unsigned __int64 number = 0;
158  int c;
159  int error;
160 
161  while (('\b' == *p) || ('\t' == *p)) // skip leading white space
162  p++;
163  if (*p == '+')
164  p++;
165  switch (base) {
166  case 0:
167  base = 10; // assume decimal base
168  if (*p == '0') {
169  base = 8; // could be octal
170  p++;
171  switch (*p) {
172  case 'x':
173  case 'X':
174  base = 16; // hex
175  p++;
176  break;
177 #if BINARY
178  case 'b':
179  case 'B':
180  base = 2; // binary
181  p++;
182  break;
183 #endif
184  }
185  }
186  break;
187  case 16: // skip over '0x' and '0X'
188  if (*p == '0' && (p[1] == 'x' || p[1] == 'X'))
189  p += 2;
190  break;
191 #if BINARY
192  case 2: // skip over '0b' and '0B'
193  if (*p == '0' && (p[1] == 'b' || p[1] == 'B'))
194  p += 2;
195  break;
196 #endif
197  }
198  error = 0;
199  while (1) {
200  c = *p;
201  if ('0' <= c && c <= '9')
202  c -= '0';
203  else if ('a' <= c && c <= 'z')
204  c -= 'a' - 10;
205  else if ('A' <= c && c <= 'Z')
206  c -= 'A' - 10;
207  else // unrecognized character
208  break;
209  if (c >= base) // not in number base
210  break;
211  if ((ULLONG_MAX - c) / base < number)
212  error = 1;
213  number = number * base + c;
214  p++;
215  }
216  if (pend)
217  *pend = (char *)p;
218  if (error) {
219  number = ULLONG_MAX;
220  errno = ERANGE;
221  }
222  return number;
223 }
224 
225 int getrusage(int who, struct rusage * rusage) {
226  FILETIME starttime;
227  FILETIME exittime;
228  FILETIME kerneltime;
229  FILETIME usertime;
230  ULARGE_INTEGER li;
231 
232  if (rusage == (struct rusage *)NULL) {
233  errno = EFAULT;
234  return -1;
235  }
236  memset(rusage, 0, sizeof(struct rusage));
237  if (GetProcessTimes(GetCurrentProcess(),
238  &starttime, &exittime, &kerneltime,
239  &usertime) == 0) {
240  /* Where is dosmaperr declared. Will address later. */
241  /* _dosmaperr(GetLastError()); */
242  return -1;
243  }
244  /* Convert FILETIMEs (0.1 us) to struct timeval */
245  memcpy(&li, &kerneltime, sizeof(FILETIME));
246  li.QuadPart /= 10L; /* Convert to microseconds */
247  rusage->ru_stime.tv_sec = (long)(li.QuadPart / 1000000L);
248  rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
249  memcpy(&li, &usertime, sizeof(FILETIME));
250  li.QuadPart /= 10L; /* Convert to microseconds */
251  rusage->ru_utime.tv_sec = (long)(li.QuadPart / 1000000L);
252  rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
253  return(0);
254 }
255 
256 int sleep(int seconds) {
257  Sleep(seconds*1000);
258  return 0;
259 }
260 
261 int kill(int pid, int sig) {
262  if (TerminateProcess((HANDLE)pid, 0))
263  return 0;
264  return -1;
265 }
266 
267 int spawn_memcached(int argc, char **argv) {
268  char buffer[4096];
269  int offset=0;
270 
271  for (int ii = 0; ii < argc; ++ii) {
272  if (strcmp("-d", argv[ii]) != 0) {
273  offset += snprintf(buffer + offset, sizeof(buffer) - offset,
274  "%s ", argv[ii]);
275  }
276  }
277 
278  STARTUPINFO sinfo = { .cb = sizeof(sinfo) };
279  PROCESS_INFORMATION pinfo;
280 
281  if (CreateProcess(argv[0], buffer, NULL, NULL, FALSE,
282  CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW,
283  NULL, NULL, &sinfo, &pinfo)) {
284  exit(0);
285  }
286 
287  return -1;
288 }
289 
290 extern int sigaction(int sig, struct sigaction *act, struct sigaction *oact)
291 {
292  if (sig == SIGHUP) {
293  return 0;
294  }
295 
296  void (*ret)(int) = signal(sig, act->sa_handler);
297  if (oact != NULL) {
298  oact->sa_handler = ret;
299  }
300  if (ret == SIG_ERR) {
301  return -1;
302  }
303 
304  return 0;
305 }
306 
307 void initialize_sockets(void)
308 {
309  WSADATA wsaData;
310  if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
311  fprintf(stderr, "Socket Initialization Error. Program aborted\n");
312  exit(EXIT_FAILURE);
313  }
314 }