| 1 |
/* gawkmisc.c --- miscellaneous gawk routines that are OS specific. -*-C-*- */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (C) 1986, 1988, 1989, 1991 - 2003, 2012, 2016-2019, 2021-2024, |
| 5 |
* the Free Software Foundation, Inc. |
| 6 |
* |
| 7 |
* This file is part of GAWK, the GNU implementation of the |
| 8 |
* AWK Progamming Language. |
| 9 |
* |
| 10 |
* GAWK is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation; either version 3 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* GAWK is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 23 |
*/ |
| 24 |
|
| 25 |
const char quote = '\''; |
| 26 |
const char envsep = ';'; |
| 27 |
|
| 28 |
# ifdef DEFPATH |
| 29 |
const char *defpath = DEFPATH; |
| 30 |
# else |
| 31 |
const char *defpath = ".;c:\\lib\\awk;c:\\gnu\\lib\\awk"; |
| 32 |
# endif |
| 33 |
/* the Makefile should define DEFLIBPATH */ |
| 34 |
const char *deflibpath = DEFLIBPATH; |
| 35 |
|
| 36 |
#ifdef __MINGW32__ |
| 37 |
|
| 38 |
#ifdef HAVE_SOCKETS |
| 39 |
#include <socket.h> |
| 40 |
|
| 41 |
#undef socket |
| 42 |
#undef setsockopt |
| 43 |
#undef bind |
| 44 |
#undef connect |
| 45 |
#undef listen |
| 46 |
#undef accept |
| 47 |
#undef recvfrom |
| 48 |
#undef shutdown |
| 49 |
#endif |
| 50 |
|
| 51 |
#define WIN32_LEAN_AND_MEAN |
| 52 |
#include <windows.h> |
| 53 |
|
| 54 |
/* gawk_name --- pull out the "gawk" part from how the OS called us */ |
| 55 |
|
| 56 |
const char * |
| 57 |
gawk_name(filespec) |
| 58 |
const char *filespec; |
| 59 |
{ |
| 60 |
char *p, *q; |
| 61 |
|
| 62 |
p = (char *) filespec; /* Sloppy... */ |
| 63 |
|
| 64 |
/* OS/2 allows / for directory separator too */ |
| 65 |
if ((q = strrchr(p, '\\')) != NULL) |
| 66 |
p = q + 1; |
| 67 |
if ((q = strrchr(p, '/')) != NULL |
| 68 |
&& (p == NULL || q > p)) /* support mixed d:\foo/bar\gawk.exe */ |
| 69 |
p = q + 1; |
| 70 |
if ((q = strchr(p, '.')) != NULL) |
| 71 |
*q = '\0'; |
| 72 |
return strlwr(p); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/* os_arg_fixup --- fixup the command line */ |
| 77 |
|
| 78 |
void |
| 79 |
os_arg_fixup(argcp, argvp) |
| 80 |
int *argcp; |
| 81 |
char ***argvp; |
| 82 |
{ |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
/* os_devopen --- open special per-OS devices */ |
| 87 |
|
| 88 |
int |
| 89 |
os_devopen(name, flag) |
| 90 |
const char *name; |
| 91 |
int flag; |
| 92 |
{ |
| 93 |
if (strcmp(name, "/dev/null") == 0) |
| 94 |
return open("NUL", flag); |
| 95 |
/* FIXME: */ |
| 96 |
/* else if (strcmp(name, "/dev/tty") == 0) |
| 97 |
* return open("???", flag); |
| 98 |
*/ |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
/* optimal_bufsize --- determine optimal buffer size */ |
| 103 |
|
| 104 |
size_t |
| 105 |
optimal_bufsize(fd, stb) |
| 106 |
int fd; |
| 107 |
struct stat *stb; |
| 108 |
{ |
| 109 |
/* |
| 110 |
* DOS doesn't have the file system block size in the |
| 111 |
* stat structure. So we have to make some sort of reasonable |
| 112 |
* guess. We use stdio's BUFSIZ, since that is what it was |
| 113 |
* meant for in the first place. |
| 114 |
*/ |
| 115 |
#define DEFBLKSIZE BUFSIZ |
| 116 |
|
| 117 |
if (S_ISDIR(stb->st_mode)) |
| 118 |
return DEFBLKSIZE; |
| 119 |
|
| 120 |
/* force all members to zero in case OS doesn't use all of them. */ |
| 121 |
memset(stb, '\0', sizeof(struct stat)); |
| 122 |
|
| 123 |
if (fstat(fd, stb) == -1) |
| 124 |
fatal("can't stat fd %d (%s)", fd, strerror(errno)); |
| 125 |
if (S_ISREG(stb->st_mode) |
| 126 |
&& 0 < stb->st_size && stb->st_size < DEFBLKSIZE) /* small file */ |
| 127 |
return stb->st_size; |
| 128 |
return DEFBLKSIZE; |
| 129 |
} |
| 130 |
|
| 131 |
/* ispath --- return true if path has directory components */ |
| 132 |
|
| 133 |
int |
| 134 |
ispath(file) |
| 135 |
const char *file; |
| 136 |
{ |
| 137 |
for (; *file; file++) { |
| 138 |
switch (*file) { |
| 139 |
case '/': |
| 140 |
case '\\': |
| 141 |
case ':': |
| 142 |
return 1; |
| 143 |
} |
| 144 |
} |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
/* isdirpunct --- return true if char is a directory separator */ |
| 149 |
|
| 150 |
int |
| 151 |
isdirpunct(c) |
| 152 |
int c; |
| 153 |
{ |
| 154 |
return (strchr(":\\/", c) != NULL); |
| 155 |
} |
| 156 |
|
| 157 |
/* os_close_on_exec --- set close on exec flag, print warning if fails */ |
| 158 |
|
| 159 |
void |
| 160 |
os_close_on_exec(fd, name, what, dir) |
| 161 |
int fd; |
| 162 |
const char *name, *what, *dir; |
| 163 |
{ |
| 164 |
HANDLE fh = (HANDLE)_get_osfhandle(fd); |
| 165 |
|
| 166 |
if (fh && fh != INVALID_HANDLE_VALUE) |
| 167 |
SetHandleInformation(fh, HANDLE_FLAG_INHERIT, 0); |
| 168 |
} |
| 169 |
|
| 170 |
/* os_isdir --- is this an fd on a directory? */ |
| 171 |
|
| 172 |
#if ! defined(S_ISDIR) && defined(S_IFDIR) |
| 173 |
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 174 |
#endif |
| 175 |
|
| 176 |
int |
| 177 |
os_isdir(fd) |
| 178 |
int fd; |
| 179 |
{ |
| 180 |
struct stat sbuf; |
| 181 |
|
| 182 |
return (fstat(fd, &sbuf) == 0 && S_ISDIR(sbuf.st_mode)); |
| 183 |
} |
| 184 |
|
| 185 |
/* os_isreadable --- fd can be read from */ |
| 186 |
|
| 187 |
int |
| 188 |
os_isreadable(const awk_input_buf_t *iobuf, bool *isdir) |
| 189 |
{ |
| 190 |
*isdir = false; |
| 191 |
|
| 192 |
switch (iobuf->sbuf.st_mode & S_IFMT) { |
| 193 |
case S_IFREG: |
| 194 |
case S_IFCHR: /* ttys, /dev/null, .. */ |
| 195 |
#ifdef S_IFSOCK |
| 196 |
case S_IFSOCK: |
| 197 |
#endif |
| 198 |
#ifdef S_IFIFO |
| 199 |
case S_IFIFO: |
| 200 |
#endif |
| 201 |
return true; |
| 202 |
case S_IFDIR: |
| 203 |
*isdir = true; |
| 204 |
/* fall through */ |
| 205 |
default: |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* os_is_setuid --- true if running setuid root */ |
| 211 |
|
| 212 |
int |
| 213 |
os_is_setuid() |
| 214 |
{ |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
/* os_setbinmode --- set binary mode on file */ |
| 219 |
|
| 220 |
static int orig_tty_mode = -1; |
| 221 |
|
| 222 |
int |
| 223 |
os_setbinmode(fd, mode) |
| 224 |
int fd, mode; |
| 225 |
{ |
| 226 |
int prev_mode = setmode(fd, mode); |
| 227 |
|
| 228 |
/* Save the original tty mode as we found it. */ |
| 229 |
if (orig_tty_mode == -1 && fd >= 0 && fd <= 2) |
| 230 |
orig_tty_mode = prev_mode; |
| 231 |
return prev_mode; |
| 232 |
} |
| 233 |
|
| 234 |
/* os_restore_mode --- restore the original mode of the console device */ |
| 235 |
|
| 236 |
void |
| 237 |
os_restore_mode (fd) |
| 238 |
int fd; |
| 239 |
{ |
| 240 |
if (orig_tty_mode != -1) { |
| 241 |
setmode(fd, orig_tty_mode); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* os_isatty --- return true if fd is a tty */ |
| 246 |
|
| 247 |
int |
| 248 |
os_isatty(int fd) |
| 249 |
{ |
| 250 |
return (isatty(fd) && lseek(fd, SEEK_CUR, 0) == -1); |
| 251 |
} |
| 252 |
|
| 253 |
/* files_are_same --- return true if files are identical */ |
| 254 |
|
| 255 |
int |
| 256 |
files_are_same(char *path, SRCFILE *src) |
| 257 |
{ |
| 258 |
struct stat st; |
| 259 |
size_t pathlen; |
| 260 |
char *p, *s; |
| 261 |
|
| 262 |
if (stat (path, & st) == 0) { |
| 263 |
/* If they have a working `stat', honor that. */ |
| 264 |
if (!(st.st_dev == src->sbuf.st_dev |
| 265 |
&& st.st_ino == src->sbuf.st_ino)) |
| 266 |
return 0; |
| 267 |
|
| 268 |
/* Compare modification times. */ |
| 269 |
if (st.st_mtime != src->mtime) |
| 270 |
return 0; |
| 271 |
|
| 272 |
/* Compare absolute file names case-insensitively, and |
| 273 |
treat forward- and back-slashes as equal. */ |
| 274 |
pathlen = strlen(path); |
| 275 |
for (p = path, s = src->fullpath; |
| 276 |
p <= path + pathlen; |
| 277 |
p++, s++) { |
| 278 |
if (tolower(*p) != tolower(*s) |
| 279 |
&& !((*p == '/' || *p == '\\') |
| 280 |
&& (*s == '/' || *s == '\\'))) |
| 281 |
return 0; |
| 282 |
} |
| 283 |
return 1; |
| 284 |
} |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
extern void *xmalloc (size_t); |
| 289 |
|
| 290 |
/* Emulate setenv and unsetenv. |
| 291 |
|
| 292 |
Note: this is only actually used in MinGW64 builds, since |
| 293 |
mingw.org's MinGW has these functions as inlines in stdlib.h since |
| 294 |
mingwrt-5.0. */ |
| 295 |
int |
| 296 |
setenv (const char *name, const char *value, int rewrite) |
| 297 |
{ |
| 298 |
char *entry; |
| 299 |
|
| 300 |
if (*value == '=') |
| 301 |
++value; |
| 302 |
|
| 303 |
if (getenv (name) && !rewrite) |
| 304 |
return 0; |
| 305 |
|
| 306 |
entry = xmalloc (strlen (name) + 1 + strlen (value) + 1); |
| 307 |
strcat (strcat (strcpy (entry, name), "="), value); |
| 308 |
if (putenv (entry) != 0) |
| 309 |
{ |
| 310 |
free (entry); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
int |
| 317 |
unsetenv (const char *name) |
| 318 |
{ |
| 319 |
if (!name || !*name || strchr (name, '=') != NULL) |
| 320 |
return -1; |
| 321 |
|
| 322 |
return setenv (name, "", 1); |
| 323 |
} |
| 324 |
|
| 325 |
/* MinGW 3.21 and later defines usleep as an inline function in |
| 326 |
unistd.h, which conflicts with the version below. */ |
| 327 |
#if __MINGW32_MAJOR_VERSION + (__MINGW32_MINOR_VERSION > 20) < 4 |
| 328 |
int |
| 329 |
usleep(unsigned int usec) |
| 330 |
{ |
| 331 |
double msecf = usec / 1000.0; |
| 332 |
|
| 333 |
Sleep ((DWORD)msecf); |
| 334 |
|
| 335 |
return usec - msecf * 1000 < 0 ? 0 : (int)(usec - msecf * 1000); |
| 336 |
} |
| 337 |
#endif |
| 338 |
|
| 339 |
/* The implementation of wctob in the MS runtime is problematic |
| 340 |
because it doesn't allow to distinguish between WEOF and 0xff, due |
| 341 |
to integer sign extension. It also causes failures in dfa.c when |
| 342 |
characters with the 8th bit set are involved. This replacement |
| 343 |
version fixes that. */ |
| 344 |
|
| 345 |
#include <wchar.h> |
| 346 |
|
| 347 |
int |
| 348 |
wctob (wint_t wc) |
| 349 |
{ |
| 350 |
char buf[64]; |
| 351 |
|
| 352 |
if (!(MB_CUR_MAX <= sizeof (buf))) |
| 353 |
abort (); |
| 354 |
/* Handle the case where WEOF is a value that does not fit in a wchar_t. */ |
| 355 |
if (wc == (wchar_t)wc) |
| 356 |
if (wctomb (buf, (wchar_t)wc) == 1) |
| 357 |
return (unsigned char) buf[0]; |
| 358 |
return EOF; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
#undef setlocale |
| 363 |
#include <locale.h> |
| 364 |
|
| 365 |
/* On Posix systems, 'setlocale' looks at LC_* variables in the |
| 366 |
environment, and Gawk users might expect that on Windows as well. |
| 367 |
The replacement implementation below does that, and also fixes a |
| 368 |
few additional quirks with locales on Windows. */ |
| 369 |
static const char * |
| 370 |
lc_var (int category) |
| 371 |
{ |
| 372 |
static const char *loc_name[LC_MAX - LC_MIN + 1] = { |
| 373 |
"LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC", "LC_TIME" |
| 374 |
}; |
| 375 |
|
| 376 |
/* This function assumes LC_* categories are small numbers between 0 |
| 377 |
and 5, as shown above, so if that changes at some point, complain |
| 378 |
vociferously. */ |
| 379 |
if (LC_ALL != 0 || LC_CTYPE != 2 || LC_TIME != 5) |
| 380 |
abort (); |
| 381 |
/* Ensured by the caller, so should never happen. */ |
| 382 |
if (category < LC_MIN || category > LC_MAX) |
| 383 |
return "????"; |
| 384 |
return loc_name[category]; |
| 385 |
} |
| 386 |
|
| 387 |
char * |
| 388 |
w32_setlocale (int category, const char *value) |
| 389 |
{ |
| 390 |
const char *new_locale = value; |
| 391 |
|
| 392 |
if (LC_MIN <= category && category <= LC_MAX |
| 393 |
&& value && *value == '\0') |
| 394 |
{ |
| 395 |
const char *lc_val = getenv ("LC_ALL"); |
| 396 |
|
| 397 |
if (!lc_val) |
| 398 |
lc_val = getenv (lc_var (category)); |
| 399 |
if (!lc_val) |
| 400 |
lc_val = getenv ("LANG"); |
| 401 |
if (lc_val) |
| 402 |
new_locale = lc_val; |
| 403 |
} |
| 404 |
|
| 405 |
/* If VALUE includes a codeset, i.e. a Windows codepage number, we |
| 406 |
must also set the LC_CTYPE locale to the same value, because |
| 407 |
LC_CTYPE is the only category which is documented to be able to |
| 408 |
change the codepage. */ |
| 409 |
if (category != LC_ALL && category != LC_CTYPE) |
| 410 |
{ |
| 411 |
const char *p = strchr (new_locale, '.'); |
| 412 |
|
| 413 |
if (p && isdigit (p[1])) |
| 414 |
setlocale (LC_CTYPE, new_locale); |
| 415 |
} |
| 416 |
return setlocale (category, new_locale); |
| 417 |
} |
| 418 |
|
| 419 |
/* Replacement for the missing nl_langinfo. Only CODESET is currently |
| 420 |
supported. */ |
| 421 |
#include <langinfo.h> |
| 422 |
|
| 423 |
char * |
| 424 |
nl_langinfo (int item) |
| 425 |
{ |
| 426 |
switch (item) |
| 427 |
{ |
| 428 |
case CODESET: |
| 429 |
{ |
| 430 |
/* Shamelessly stolen from Gnulib's nl_langinfo.c. */ |
| 431 |
static char buf[2 + 10 + 1]; |
| 432 |
char const *locale = setlocale (LC_CTYPE, NULL); |
| 433 |
char *codeset = buf; |
| 434 |
size_t codesetlen; |
| 435 |
codeset[0] = '\0'; |
| 436 |
|
| 437 |
if (locale && locale[0]) |
| 438 |
{ |
| 439 |
/* If the locale name contains an encoding after the |
| 440 |
dot, return it. */ |
| 441 |
char *dot = strchr (locale, '.'); |
| 442 |
|
| 443 |
if (dot) |
| 444 |
{ |
| 445 |
/* Look for the possible @... trailer and remove it, |
| 446 |
if any. */ |
| 447 |
char *codeset_start = dot + 1; |
| 448 |
char const *modifier = strchr (codeset_start, '@'); |
| 449 |
|
| 450 |
if (! modifier) |
| 451 |
codeset = codeset_start; |
| 452 |
else |
| 453 |
{ |
| 454 |
codesetlen = modifier - codeset_start; |
| 455 |
if (codesetlen < sizeof buf) |
| 456 |
{ |
| 457 |
codeset = memcpy (buf, codeset_start, codesetlen); |
| 458 |
codeset[codesetlen] = '\0'; |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
/* If setlocale is successful, it returns the number of the |
| 464 |
codepage, as a string. Otherwise, fall back on Windows |
| 465 |
API GetACP, which returns the locale's codepage as a |
| 466 |
number (although this doesn't change according to what |
| 467 |
the 'setlocale' call specified). Either way, prepend |
| 468 |
"CP" to make it a valid codeset name. */ |
| 469 |
codesetlen = strlen (codeset); |
| 470 |
if (0 < codesetlen && codesetlen < sizeof buf - 2) |
| 471 |
memmove (buf + 2, codeset, codesetlen + 1); |
| 472 |
else |
| 473 |
sprintf (buf + 2, "%u", GetACP ()); |
| 474 |
codeset = memcpy (buf, "CP", 2); |
| 475 |
|
| 476 |
return codeset; |
| 477 |
} |
| 478 |
default: |
| 479 |
return (char *) ""; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
/* Convert a Unicode codepoint UC to the corresponding multibyte string |
| 484 |
STR in the current system codepage. Return the number of bytes in |
| 485 |
STR, or -1 if the codepage does not support UC. */ |
| 486 |
size_t w32_wc_to_lc (int uc, char *str) |
| 487 |
{ |
| 488 |
BOOL used_default = FALSE; |
| 489 |
wchar_t wc[3]; |
| 490 |
int retval; |
| 491 |
|
| 492 |
if (uc > 0x10FFFF) |
| 493 |
return (size_t) -1; |
| 494 |
|
| 495 |
/* Convert UC to UTF-16. */ |
| 496 |
wc[1] = wc[2] = 0; |
| 497 |
if (uc < 0x10000) |
| 498 |
wc[0] = uc; |
| 499 |
else |
| 500 |
{ |
| 501 |
wc[0] = ((uc - 0x10000) >> 10) + 0xD800; /* high surrogate */ |
| 502 |
wc[1] = (uc & 0x3FF) + 0xDC00; /* low surrogate */ |
| 503 |
} |
| 504 |
/* WideCharToMultiByte returns the number of bytes, including the |
| 505 |
terminating null, written to STR on success, zero on falure. */ |
| 506 |
retval = WideCharToMultiByte (CP_ACP, 0, wc, -1, |
| 507 |
str, MB_LEN_MAX, NULL, &used_default); |
| 508 |
if (!retval || used_default) |
| 509 |
return (size_t) -1; |
| 510 |
return retval - 1; /* exclude the terminating null */ |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
* On MS-Windows with MinGW, execvp causes the shell and the re-exec'ed |
| 515 |
* dgawk to compete for the keyboard input. |
| 516 |
* |
| 517 |
* This will need work if we ever need a real version of execvp. |
| 518 |
*/ |
| 519 |
int w32_execvp(const char *file, char **argv) |
| 520 |
{ |
| 521 |
if (_spawnvp(_P_WAIT, file, (const char * const *)argv) != -1) |
| 522 |
exit(EXIT_SUCCESS); |
| 523 |
|
| 524 |
return -1; |
| 525 |
} |
| 526 |
|
| 527 |
#ifdef DYNAMIC |
| 528 |
|
| 529 |
#include <winerror.h> |
| 530 |
#include <dlfcn.h> |
| 531 |
|
| 532 |
static DWORD last_err; |
| 533 |
|
| 534 |
void * |
| 535 |
dlopen (const char *file, int mode) |
| 536 |
{ |
| 537 |
char dllfn[MAX_PATH], *p; |
| 538 |
HANDLE dllhandle; |
| 539 |
|
| 540 |
if (mode != RTLD_LAZY) |
| 541 |
{ |
| 542 |
errno = EINVAL; |
| 543 |
last_err = ERROR_INVALID_PARAMETER; |
| 544 |
return NULL; |
| 545 |
} |
| 546 |
|
| 547 |
/* MSDN says to be sure to use backslashes in the DLL file name. */ |
| 548 |
strcpy (dllfn, file); |
| 549 |
for (p = dllfn; *p; p++) |
| 550 |
if (*p == '/') |
| 551 |
*p = '\\'; |
| 552 |
|
| 553 |
dllhandle = LoadLibrary (dllfn); |
| 554 |
if (!dllhandle) |
| 555 |
last_err = GetLastError (); |
| 556 |
|
| 557 |
return dllhandle; |
| 558 |
} |
| 559 |
|
| 560 |
char * |
| 561 |
dlerror (void) |
| 562 |
{ |
| 563 |
static char errbuf[1024]; |
| 564 |
DWORD ret; |
| 565 |
|
| 566 |
if (!last_err) |
| 567 |
return NULL; |
| 568 |
|
| 569 |
ret = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
| 570 |
| FORMAT_MESSAGE_IGNORE_INSERTS, |
| 571 |
NULL, last_err, 0, errbuf, sizeof (errbuf), NULL); |
| 572 |
while (ret > 0 && (errbuf[ret - 1] == '\n' || errbuf[ret - 1] == '\r')) |
| 573 |
--ret; |
| 574 |
|
| 575 |
errbuf[ret] = '\0'; |
| 576 |
if (!ret) |
| 577 |
sprintf (errbuf, "Error code %lu", last_err); |
| 578 |
|
| 579 |
last_err = 0; |
| 580 |
return errbuf; |
| 581 |
} |
| 582 |
|
| 583 |
int |
| 584 |
dlclose (void *handle) |
| 585 |
{ |
| 586 |
if (!handle || handle == INVALID_HANDLE_VALUE) |
| 587 |
return -1; |
| 588 |
if (!FreeLibrary (handle)) |
| 589 |
return -1; |
| 590 |
|
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
void * |
| 595 |
dlsym (void *handle, const char *name) |
| 596 |
{ |
| 597 |
FARPROC addr = NULL; |
| 598 |
|
| 599 |
if (!handle || handle == INVALID_HANDLE_VALUE) |
| 600 |
{ |
| 601 |
last_err = ERROR_INVALID_PARAMETER; |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
|
| 605 |
addr = GetProcAddress (handle, name); |
| 606 |
if (!addr) |
| 607 |
last_err = GetLastError (); |
| 608 |
|
| 609 |
return (void *)addr; |
| 610 |
} |
| 611 |
#endif /* DYNAMIC */ |
| 612 |
|
| 613 |
#ifdef HAVE_SOCKETS |
| 614 |
int |
| 615 |
socket_to_fd(SOCKET s) |
| 616 |
{ |
| 617 |
return (s == INVALID_SOCKET |
| 618 |
? INVALID_HANDLE |
| 619 |
: _open_osfhandle (s, O_BINARY | O_NOINHERIT)); |
| 620 |
} |
| 621 |
|
| 622 |
int |
| 623 |
w32_socket(int family, int type, int protocol) |
| 624 |
{ |
| 625 |
/* We need to use WSASocket rather than socket, since the latter |
| 626 |
creates overlapped sockets that cannot be used in file I/O |
| 627 |
APIs. */ |
| 628 |
SOCKET s = WSASocket (family, type, protocol, NULL, 0, 0); |
| 629 |
|
| 630 |
if (s == INVALID_SOCKET) |
| 631 |
{ |
| 632 |
switch (WSAGetLastError ()) |
| 633 |
{ |
| 634 |
case WSAEMFILE: |
| 635 |
errno = EMFILE; |
| 636 |
break; |
| 637 |
case WSANOTINITIALISED: |
| 638 |
case WSAENETDOWN: |
| 639 |
errno = EACCES; |
| 640 |
break; |
| 641 |
case WSAENOBUFS: |
| 642 |
errno = ENOMEM; |
| 643 |
break; |
| 644 |
case WSAEFAULT: |
| 645 |
errno = EFAULT; |
| 646 |
break; |
| 647 |
default: |
| 648 |
errno = EINVAL; |
| 649 |
break; |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
return socket_to_fd (s); |
| 654 |
} |
| 655 |
|
| 656 |
int |
| 657 |
w32_setsockopt (int fd, int level, int optname, const char *optval, int optlen) |
| 658 |
{ |
| 659 |
SOCKET s = FD_TO_SOCKET (fd); |
| 660 |
|
| 661 |
return setsockopt (s, level, optname, optval, optlen); |
| 662 |
} |
| 663 |
|
| 664 |
int |
| 665 |
w32_bind (int fd, const struct sockaddr *name, int namelen) |
| 666 |
{ |
| 667 |
SOCKET s = FD_TO_SOCKET (fd); |
| 668 |
|
| 669 |
return bind (s, name, namelen); |
| 670 |
} |
| 671 |
|
| 672 |
int |
| 673 |
w32_connect (int fd, const struct sockaddr *name, int namelen) |
| 674 |
{ |
| 675 |
SOCKET s = FD_TO_SOCKET (fd); |
| 676 |
|
| 677 |
return connect (s, name, namelen); |
| 678 |
} |
| 679 |
|
| 680 |
int |
| 681 |
w32_listen (int fd, int backlog) |
| 682 |
{ |
| 683 |
SOCKET s = FD_TO_SOCKET (fd); |
| 684 |
|
| 685 |
return listen (s, backlog); |
| 686 |
} |
| 687 |
|
| 688 |
int |
| 689 |
w32_accept (int fd, struct sockaddr *addr, int *addrlen) |
| 690 |
{ |
| 691 |
SOCKET s = FD_TO_SOCKET (fd); |
| 692 |
|
| 693 |
return socket_to_fd (accept (s, addr, addrlen)); |
| 694 |
} |
| 695 |
|
| 696 |
SOCKET |
| 697 |
valid_socket (int fd) |
| 698 |
{ |
| 699 |
SOCKET s = FD_TO_SOCKET (fd); |
| 700 |
int ov, ol = 4; |
| 701 |
|
| 702 |
if (s == INVALID_SOCKET |
| 703 |
|| (getsockopt (s, SOL_SOCKET, SO_TYPE, (char *)&ov, &ol) == SOCKET_ERROR |
| 704 |
&& WSAGetLastError() == WSAENOTSOCK)) |
| 705 |
return (SOCKET)0; |
| 706 |
return s; |
| 707 |
} |
| 708 |
|
| 709 |
int |
| 710 |
w32_closesocket (int fd) |
| 711 |
{ |
| 712 |
SOCKET s = valid_socket (fd); |
| 713 |
int res1, res2 = 0; |
| 714 |
|
| 715 |
if (!s && fd == FAKE_FD_VALUE) |
| 716 |
return 0; |
| 717 |
|
| 718 |
res1 = close (fd); |
| 719 |
if (s) |
| 720 |
res2 = closesocket (s); |
| 721 |
|
| 722 |
if (res1 == -1 || res2 == SOCKET_ERROR) |
| 723 |
return -1; |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
int |
| 728 |
w32_recvfrom (int fd, char *buf, int len, int flags, |
| 729 |
struct sockaddr *from, int *fromlen) |
| 730 |
{ |
| 731 |
SOCKET s = FD_TO_SOCKET (fd); |
| 732 |
|
| 733 |
return recvfrom (s, buf, len, flags, from, fromlen); |
| 734 |
} |
| 735 |
|
| 736 |
int |
| 737 |
w32_shutdown (int fd, int how) |
| 738 |
{ |
| 739 |
SOCKET s = FD_TO_SOCKET (fd); |
| 740 |
|
| 741 |
return shutdown (s, how); |
| 742 |
} |
| 743 |
|
| 744 |
#endif /* HAVE_SOCKETS */ |
| 745 |
|
| 746 |
/* Translate abnormal exit status of Windows programs into the signal |
| 747 |
that terminated the program. This is required to support scm_kill |
| 748 |
and WTERMSIG. */ |
| 749 |
|
| 750 |
#include <signal.h> |
| 751 |
|
| 752 |
struct signal_and_status { |
| 753 |
int sig; |
| 754 |
unsigned status; |
| 755 |
}; |
| 756 |
|
| 757 |
static const struct signal_and_status sigtbl[] = { |
| 758 |
{SIGSEGV, 0xC0000005}, /* access to invalid address */ |
| 759 |
{SIGSEGV, 0xC0000008}, /* invalid handle */ |
| 760 |
{SIGILL, 0xC000001D}, /* illegal instruction */ |
| 761 |
{SIGILL, 0xC0000025}, /* non-continuable instruction */ |
| 762 |
{SIGSEGV, 0xC000008C}, /* array bounds exceeded */ |
| 763 |
{SIGFPE, 0xC000008D}, /* float denormal */ |
| 764 |
{SIGFPE, 0xC000008E}, /* float divide by zero */ |
| 765 |
{SIGFPE, 0xC000008F}, /* float inexact */ |
| 766 |
{SIGFPE, 0xC0000090}, /* float invalid operation */ |
| 767 |
{SIGFPE, 0xC0000091}, /* float overflow */ |
| 768 |
{SIGFPE, 0xC0000092}, /* float stack check */ |
| 769 |
{SIGFPE, 0xC0000093}, /* float underflow */ |
| 770 |
{SIGFPE, 0xC0000094}, /* integer divide by zero */ |
| 771 |
{SIGFPE, 0xC0000095}, /* integer overflow */ |
| 772 |
{SIGILL, 0xC0000096}, /* privileged instruction */ |
| 773 |
{SIGSEGV, 0xC00000FD}, /* stack overflow */ |
| 774 |
{SIGTERM, 0xC000013A}, /* Ctrl-C exit */ |
| 775 |
{SIGINT, 0xC000013A} |
| 776 |
}; |
| 777 |
|
| 778 |
int |
| 779 |
w32_status_to_termsig (unsigned status) |
| 780 |
{ |
| 781 |
int i; |
| 782 |
|
| 783 |
for (i = 0; i < sizeof (sigtbl) / sizeof (sigtbl[0]); i++) |
| 784 |
if (status == sigtbl[i].status) |
| 785 |
return sigtbl[i].sig; |
| 786 |
|
| 787 |
return SIGTERM; |
| 788 |
} |
| 789 |
|
| 790 |
/* Emulate the missing strsignal. */ |
| 791 |
char * |
| 792 |
strsignal (int signo) |
| 793 |
{ |
| 794 |
switch (signo) |
| 795 |
{ |
| 796 |
case SIGINT: |
| 797 |
return "SIGINT"; |
| 798 |
case SIGILL: |
| 799 |
return "SIGILL"; |
| 800 |
case SIGFPE: |
| 801 |
return "SIGFPE"; |
| 802 |
case SIGSEGV: |
| 803 |
return "SIGSEGV"; |
| 804 |
case SIGTERM: |
| 805 |
return "SIGTERM"; |
| 806 |
case SIGBREAK: |
| 807 |
return "SIGBREAK"; |
| 808 |
case SIGABRT: |
| 809 |
return "SIGABRT"; |
| 810 |
default: |
| 811 |
return "Unknown signal"; |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
void |
| 816 |
os_maybe_set_errno (void) |
| 817 |
{ |
| 818 |
if (errno == 0 || errno == EINVAL) { |
| 819 |
DWORD w32err = GetLastError (); |
| 820 |
|
| 821 |
switch (w32err) |
| 822 |
{ |
| 823 |
/* When stdout is redirected to a pipe, and the program that |
| 824 |
reads the pipe (e.g., a pager) exits, Windows doesn't set |
| 825 |
errno to a useful value. Help it DTRT. */ |
| 826 |
case ERROR_BAD_PIPE: |
| 827 |
case ERROR_PIPE_BUSY: |
| 828 |
case ERROR_NO_DATA: |
| 829 |
case ERROR_PIPE_NOT_CONNECTED: |
| 830 |
errno = EPIPE; |
| 831 |
break; |
| 832 |
default: |
| 833 |
errno = EINVAL; |
| 834 |
break; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
void |
| 840 |
init_sockets(void) |
| 841 |
{ |
| 842 |
#if defined(HAVE_SOCKETS) |
| 843 |
WSADATA winsockData; |
| 844 |
int errcode; |
| 845 |
|
| 846 |
if ((errcode = WSAStartup (0x101, &winsockData)) != 0 |
| 847 |
|| winsockData.wVersion != 0x101) |
| 848 |
fatal(_("cannot start Winsock (%d)"), errcode); |
| 849 |
#endif |
| 850 |
} |
| 851 |
|
| 852 |
#endif /* __MINGW32__ */ |