| 1 | /* |
| 2 | * Copyright (c) 1987, 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | osdosnui.c - DOS/Win-specific functions with no user-interface component |
| 10 | Function |
| 11 | Provides DOS/Windows implementations of a few miscellaneous osifc |
| 12 | functions that have no user-interface component. These are useful for |
| 13 | linking versions of programs with alternative UI's outside of the set |
| 14 | implemented in osdos.c and the like. |
| 15 | Notes |
| 16 | |
| 17 | Modified |
| 18 | 04/05/02 MJRoberts - Creation (from osdos.c) |
| 19 | */ |
| 20 | |
| 21 | #ifdef T_WIN32 |
| 22 | #include <windows.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <ctype.h> |
| 31 | #include <string.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | #include "std.h" |
| 35 | #include "os.h" |
| 36 | |
| 37 | #ifdef TURBO |
| 38 | #include <share.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <time.h> |
| 41 | #define _SH_DENYRW SH_DENYRW |
| 42 | #define _SH_DENYNO SH_DENYNO |
| 43 | #endif |
| 44 | |
| 45 | #ifdef DJGPP |
| 46 | #include <fcntl.h> |
| 47 | #include <dpmi.h> |
| 48 | #include <sys/stat.h> |
| 49 | #include <time.h> |
| 50 | #include "osstzprs.h" |
| 51 | #endif |
| 52 | |
| 53 | extern void canonicalize_path(char *); |
| 54 | extern void safe_strcpy(char *dst, size_t dstlen, const char *src); |
| 55 | extern void safe_strcpyl(char *dst, size_t dstl, const char *src, size_t srcl); |
| 56 | extern unsigned long oss_get_file_attrs(const char *fname); |
| 57 | |
| 58 | /* ------------------------------------------------------------------------ */ |
| 59 | /* |
| 60 | * For djgpp, we must define the _fsopen routine (fopen with share mode), |
| 61 | * since this is MS-specific and doesn't appear in the djgpp library. |
| 62 | */ |
| 63 | #ifdef DJGPP |
| 64 | static FILE *_fsopen(const char *fname, const char *mode, int sh_flags) |
| 65 | { |
| 66 | int old_flags; |
| 67 | FILE *fp; |
| 68 | |
| 69 | /* set the new share flags, remembering the previous flags */ |
| 70 | old_flags = __djgpp_share_flags; |
| 71 | __djgpp_share_flags = sh_flags; |
| 72 | |
| 73 | /* open the file */ |
| 74 | fp = fopen(fname, mode); |
| 75 | |
| 76 | /* restore the old share flags */ |
| 77 | __djgpp_share_flags = old_flags; |
| 78 | |
| 79 | /* return the result of the open */ |
| 80 | return fp; |
| 81 | } |
| 82 | #endif /* DJGPP */ |
| 83 | |
| 84 | |
| 85 | |
| 86 | /* ------------------------------------------------------------------------ */ |
| 87 | /* |
| 88 | * File I/O |
| 89 | */ |
| 90 | |
| 91 | #ifdef T_WIN32 |
| 92 | |
| 93 | /* |
| 94 | * Open a file. We'll open the low-level handle with the 'omode' mode, |
| 95 | * then open a FILE stream on the handle with the 'mode' string. |
| 96 | */ |
| 97 | osfildef *os_fsopen(const char *fname, const char *mode, int omode, int sh) |
| 98 | { |
| 99 | int fd; |
| 100 | |
| 101 | /* add the non-inheritable flag to the open mode */ |
| 102 | omode |= _O_NOINHERIT; |
| 103 | |
| 104 | /* try opening the file handle */ |
| 105 | fd = _sopen(fname, omode, sh, _S_IREAD | _S_IWRITE); |
| 106 | |
| 107 | /* if we didn't get a valid file, return null */ |
| 108 | if (fd == -1) |
| 109 | return 0; |
| 110 | |
| 111 | /* return a FILE stream on the handle */ |
| 112 | return _fdopen(fd, mode); |
| 113 | } |
| 114 | |
| 115 | #else |
| 116 | |
| 117 | /* |
| 118 | * Open text file for reading and writing, keeping existing data, creating |
| 119 | * a new file if the file doesn't already exist. |
| 120 | */ |
| 121 | osfildef *osfoprwt(const char *fname, os_filetype_t typ) |
| 122 | { |
| 123 | osfildef *fp; |
| 124 | |
| 125 | VARUSED(typ); |
| 126 | |
| 127 | /* |
| 128 | * try opening in "r+" mode - this will open an existing file in |
| 129 | * read/write mode, but will fail if the file doesn't already exist |
| 130 | */ |
| 131 | fp = _fsopen(fname, "r+", _SH_DENYRW); |
| 132 | |
| 133 | /* if that succeeded, return the file */ |
| 134 | if (fp != 0) |
| 135 | return fp; |
| 136 | |
| 137 | /* |
| 138 | * the file doesn't exist - create a new file in "w+" mode, which |
| 139 | * creates a new file and opens it in read/write mode |
| 140 | */ |
| 141 | return _fsopen(fname, "w+", _SH_DENYRW); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Open binary file for reading and writing, keeping existing data, |
| 146 | * creating a new file if the file doesn't already exist. |
| 147 | */ |
| 148 | osfildef *osfoprwb(const char *fname, os_filetype_t typ) |
| 149 | { |
| 150 | osfildef *fp; |
| 151 | |
| 152 | VARUSED(typ); |
| 153 | |
| 154 | /* |
| 155 | * try opening in "r+b" mode - this will open an existing file in |
| 156 | * read/write mode, but will fail if the file doesn't already exist |
| 157 | */ |
| 158 | fp = _fsopen(fname, "r+b", _SH_DENYRW); |
| 159 | |
| 160 | /* if that succeeded, return the file */ |
| 161 | if (fp != 0) |
| 162 | return fp; |
| 163 | |
| 164 | /* |
| 165 | * the file doesn't exist - create a new file in "w+b" mode, which |
| 166 | * creates a new file and opens it in read/write mode |
| 167 | */ |
| 168 | return _fsopen(fname, "w+b", _SH_DENYRW); |
| 169 | } |
| 170 | |
| 171 | #endif /* T_WIN32 */ |
| 172 | |
| 173 | |
| 174 | /* ------------------------------------------------------------------------ */ |
| 175 | /* |
| 176 | * Duplicate a file handle |
| 177 | */ |
| 178 | #if defined(TURBO) || defined(DJGPP) |
| 179 | # define ossfdup(fp, mode) fdopen(dup(fileno(fp)), mode) |
| 180 | #endif |
| 181 | #if defined(MICROSOFT) |
| 182 | # define ossfdup(fp, mode) _fdopen(_dup(_fileno(fp)), mode) |
| 183 | #endif |
| 184 | |
| 185 | osfildef *osfdup(osfildef *orig, const char *mode) |
| 186 | { |
| 187 | char realmode[5]; |
| 188 | char *p = realmode; |
| 189 | const char *m; |
| 190 | |
| 191 | /* verify that there aren't any unrecognized mode flags */ |
| 192 | for (m = mode ; *m != '\0' ; ++m) |
| 193 | { |
| 194 | if (strchr("rw+bst", *m) == 0) |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* figure the read/write mode - translate r+ and w+ to r+ */ |
| 199 | if ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '+') |
| 200 | *p++ = 'r', *p++ = '+'; |
| 201 | else if (mode[0] == 'r') |
| 202 | *p++ = 'r'; |
| 203 | else if (mode[0] == 'w') |
| 204 | *p++ = 'w'; |
| 205 | else |
| 206 | return 0; |
| 207 | |
| 208 | /* add the format mode - use binary for 'b' and 's', 't' otherwise */ |
| 209 | if (strchr(mode, 'b') != 0 || strchr(mode, 's') != 0) |
| 210 | *p++ = 'b'; |
| 211 | else |
| 212 | *p++ = 't'; |
| 213 | |
| 214 | /* end the mode string */ |
| 215 | *p = '\0'; |
| 216 | |
| 217 | /* duplicate the handle in the given mode */ |
| 218 | return ossfdup(orig, mode); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | #ifdef T_WIN32 |
| 223 | /* ------------------------------------------------------------------------ */ |
| 224 | /* |
| 225 | * Windows-specific implementation |
| 226 | */ |
| 227 | |
| 228 | /* Windows crypto context */ |
| 229 | static HCRYPTPROV oswin_hcp = 0; |
| 230 | |
| 231 | /* internal - get a windows crypto context if we don't have one already */ |
| 232 | static void init_crypto_ctx() |
| 233 | { |
| 234 | /* set up a crypto context */ |
| 235 | if (oswin_hcp == 0) |
| 236 | CryptAcquireContext(&oswin_hcp, 0, 0, PROV_RSA_FULL, |
| 237 | CRYPT_VERIFYCONTEXT); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * initialize |
| 242 | */ |
| 243 | int osnoui_init(int *argc, char *argv[], const char *prompt, |
| 244 | char *buf, int bufsiz) |
| 245 | { |
| 246 | /* initialize the windows crypto context */ |
| 247 | init_crypto_ctx(); |
| 248 | |
| 249 | /* success */ |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * uninitialize |
| 255 | */ |
| 256 | void osnoui_uninit() |
| 257 | { |
| 258 | /* done with our crypto context */ |
| 259 | if (oswin_hcp != 0) |
| 260 | { |
| 261 | CryptReleaseContext(oswin_hcp, 0); |
| 262 | oswin_hcp = 0; |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | |
| 267 | /* ------------------------------------------------------------------------ */ |
| 268 | /* |
| 269 | * Get the system clock in milliseconds. This is extremely easy for Win32 |
| 270 | * console apps, since there's an API that does exactly what we want. |
| 271 | */ |
| 272 | long os_get_sys_clock_ms(void) |
| 273 | { |
| 274 | return (long)GetTickCount(); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /* ------------------------------------------------------------------------ */ |
| 279 | /* |
| 280 | * Random numbers |
| 281 | */ |
| 282 | void os_gen_rand_bytes(unsigned char *buf, size_t len) |
| 283 | { |
| 284 | /* make sure we have a crypto context */ |
| 285 | init_crypto_ctx(); |
| 286 | |
| 287 | /* generate bytes via the system crypto API */ |
| 288 | CryptGenRandom(oswin_hcp, len, buf); |
| 289 | } |
| 290 | |
| 291 | /* ------------------------------------------------------------------------ */ |
| 292 | /* |
| 293 | * Get the absolute path for the given filename |
| 294 | */ |
| 295 | int os_get_abs_filename(char *buf, size_t buflen, const char *filename) |
| 296 | { |
| 297 | DWORD len; |
| 298 | |
| 299 | /* if the name is empty, use "." */ |
| 300 | if (filename[0] == '\0') |
| 301 | filename = "."; |
| 302 | |
| 303 | /* check for a relative path on a specific drive (e.g., "c:test") */ |
| 304 | if (isalpha(filename[0]) && filename[1] == ':' |
| 305 | && filename[2] != '\\' && filename[2] != '/') |
| 306 | { |
| 307 | /* |
| 308 | * GetFullPathName doesn't handle relative paths with drive |
| 309 | * letters, because Windows doesn't use the DOS scheme of |
| 310 | * maintaining a separate working directory per drive. First, |
| 311 | * check to see if we're talking about the working drive; if so, |
| 312 | * just remove the drive letter and get the full path as normal, |
| 313 | * since that's the drive we'd apply anyway. |
| 314 | */ |
| 315 | len = GetFullPathName(".", buflen, buf, 0); |
| 316 | if (len != 0 && len <= buflen |
| 317 | && toupper(buf[0]) == toupper(filename[0]) && buf[1] == ':') |
| 318 | { |
| 319 | /* |
| 320 | * it's on our working drive anyway, so we can just take off |
| 321 | * the drive letter and we'll get the right result - e.g., |
| 322 | * "c:test" -> "test" -> "c:\working_dir\test" |
| 323 | */ |
| 324 | filename += 2; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | /* |
| 329 | * It's on a different drive. Windows doesn't keep separate |
| 330 | * working directories per drive, so we'll simply pretend that |
| 331 | * the working directory on a drive that's not the current |
| 332 | * drive is the root folder on that drive. E.g., "k:test" -> |
| 333 | * "k:\test". |
| 334 | */ |
| 335 | |
| 336 | /* make sure we have room to add the "\" after the ":" */ |
| 337 | if (buflen < strlen(filename) + 2) |
| 338 | return FALSE; |
| 339 | |
| 340 | /* rebuild the path as "k:\path" */ |
| 341 | buf[0] = filename[0]; |
| 342 | buf[1] = filename[1]; |
| 343 | buf[2] = '\\'; |
| 344 | strcpy(buf+3, filename+2); |
| 345 | |
| 346 | /* handled */ |
| 347 | return TRUE; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /* get the full path name */ |
| 352 | len = GetFullPathName(filename, buflen, buf, 0); |
| 353 | if (len == 0 || len > buflen) |
| 354 | { |
| 355 | /* |
| 356 | * failed, or the buffer isn't big enough - copy the filename as-is |
| 357 | * and return failure |
| 358 | */ |
| 359 | safe_strcpy(buf, buflen, filename); |
| 360 | return FALSE; |
| 361 | } |
| 362 | |
| 363 | /* success */ |
| 364 | return TRUE; |
| 365 | } |
| 366 | |
| 367 | #else /* T_WIN32 */ |
| 368 | /* ------------------------------------------------------------------------ */ |
| 369 | /* |
| 370 | * DOS (non-Windows) implementation |
| 371 | */ |
| 372 | |
| 373 | int osnoui_init(int *argc, char *argv[], const char *prompt, |
| 374 | char *buf, int bufsiz) |
| 375 | { |
| 376 | /* no module-specific initialization is needed */ |
| 377 | } |
| 378 | |
| 379 | void osnoui_uninit() |
| 380 | { |
| 381 | /* no module-specific teardown is needed */ |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * Get the absolute path for a filename. This has slightly different |
| 387 | * implementations for Borland and DJGPP. |
| 388 | */ |
| 389 | #ifdef DJGPP |
| 390 | int os_get_abs_filename(char *buf, size_t buflen, const char *filename) |
| 391 | { |
| 392 | char tmp[FILENAME_MAX]; |
| 393 | char *p; |
| 394 | |
| 395 | /* if the filename is empty, use "." */ |
| 396 | if (filename[0] == '\0') |
| 397 | filename = "."; |
| 398 | |
| 399 | /* "fix" the path - this expands the filename to an absolute path */ |
| 400 | _fixpath(filename, tmp); |
| 401 | |
| 402 | /* ...it also changes \ to /, so undo that to keep things in DOS format */ |
| 403 | for (p = tmp ; *p != '\0' ; ++p) |
| 404 | { |
| 405 | if (*p == '/') |
| 406 | *p = '\\'; |
| 407 | } |
| 408 | |
| 409 | /* if the result fits the buffer, send it back, otherwise fail */ |
| 410 | if (strlen(tmp) < buflen) |
| 411 | { |
| 412 | strcpy(buf, tmp); |
| 413 | return TRUE; |
| 414 | } |
| 415 | else |
| 416 | return FALSE; |
| 417 | } |
| 418 | #endif /* DJGPP */ |
| 419 | #ifdef TURBO |
| 420 | int os_get_abs_filename(char *buf, size_t buflen, const char *filename) |
| 421 | { |
| 422 | /* if the filename is empty, use "." */ |
| 423 | if (filename[0] == '\0') |
| 424 | filename = "."; |
| 425 | |
| 426 | /* expand the path to absolute notation */ |
| 427 | return (_fullpath(buf, filename, buflen) != 0); |
| 428 | } |
| 429 | #endif /* TURBO */ |
| 430 | |
| 431 | /* |
| 432 | * Calculate the current system time in milliseconds. Returns a value |
| 433 | * relative to an arbitrary base time. |
| 434 | */ |
| 435 | long os_get_sys_clock_ms(void) |
| 436 | { |
| 437 | static unsigned long last_ticks; |
| 438 | long curtime; |
| 439 | |
| 440 | /* get the current system clock reading in clock ticks */ |
| 441 | #ifdef DJGPP |
| 442 | __dpmi_regs regs; |
| 443 | |
| 444 | regs.h.ah = 0; |
| 445 | __dpmi_int(0x1a, ®s); |
| 446 | curtime = (regs.x.cx << 16) | regs.x.dx; |
| 447 | #else /* DJGPP */ |
| 448 | asm { |
| 449 | push bp |
| 450 | mov ah, 00h |
| 451 | int 1ah |
| 452 | pop bp |
| 453 | |
| 454 | mov word ptr [curtime], dx |
| 455 | mov word ptr [curtime+2], cx |
| 456 | } |
| 457 | #endif /* DJGPP */ |
| 458 | |
| 459 | /* |
| 460 | * Check for clock rollover. The clock resets to 0 ticks at midnight. |
| 461 | * If the new tick count is lower than the last tick count we read, add |
| 462 | * 1573040 (the number of ticks in 24 hours) until it's greater. This |
| 463 | * ensures that we have continuity between readings. |
| 464 | */ |
| 465 | while (curtime < last_ticks) |
| 466 | curtime += 1573040; |
| 467 | |
| 468 | /* save the last tick count for checking rollover next time through */ |
| 469 | last_ticks = curtime; |
| 470 | |
| 471 | /* |
| 472 | * Convert the tick count to milliseconds - there are 18.2065 ticks |
| 473 | * per second, hence 18.2065 ticks per 1000 milliseconds, hence |
| 474 | * 1000/18.2065 milliseconds per tick. This is approximately 55 |
| 475 | * ticks per millisecond. Return the result of the conversion. |
| 476 | */ |
| 477 | return curtime * 55; |
| 478 | } |
| 479 | |
| 480 | #endif /* T_WIN32 */ |
| 481 | |
| 482 | |
| 483 | |
| 484 | /* ------------------------------------------------------------------------ */ |
| 485 | /* |
| 486 | * Compare path strings. For DOS/Windows, we treat upper and lower case as |
| 487 | * interchangeable, and we treat '/' and '\' as interchangeable. |
| 488 | */ |
| 489 | static int patheq(const char *a, const char *b, size_t len) |
| 490 | { |
| 491 | /* compare character by character */ |
| 492 | for ( ; len != 0 ; ++a, ++b, --len) |
| 493 | { |
| 494 | /* convert both characters to lower case */ |
| 495 | char ca = isupper(*a) ? tolower(*a) : *a; |
| 496 | char cb = isupper(*b) ? tolower(*b) : *b; |
| 497 | |
| 498 | /* convert forward slashes to backslashes */ |
| 499 | if (ca == '/') ca = '\\'; |
| 500 | if (cb == '/') cb = '\\'; |
| 501 | |
| 502 | /* if they differ after all of that, we don't have a match */ |
| 503 | if (ca != cb) |
| 504 | return FALSE; |
| 505 | } |
| 506 | |
| 507 | /* no mismatches */ |
| 508 | return TRUE; |
| 509 | } |
| 510 | |
| 511 | /* ------------------------------------------------------------------------ */ |
| 512 | /* |
| 513 | * Is the given path a root path? This returns true for a drive letter |
| 514 | * root (C: or C:\) or a UNC root (\\machine\root). |
| 515 | */ |
| 516 | static int is_root_path(const char *path) |
| 517 | { |
| 518 | /* note the length */ |
| 519 | size_t len = strlen(path); |
| 520 | |
| 521 | /* check for a drive letter root */ |
| 522 | if (isalpha(path[0]) |
| 523 | && path[1] == ':' |
| 524 | && (len == 2 |
| 525 | || (len == 3 && (path[2] == '/' || path[2] == '\\')))) |
| 526 | return TRUE; |
| 527 | |
| 528 | /* check for a UNC root */ |
| 529 | if ((path[0] == '\\' || path[0] == '/') |
| 530 | && (path[1] == '\\' || path[1] == '/')) |
| 531 | { |
| 532 | /* it starts as a UNC path; count path elements */ |
| 533 | int eles; |
| 534 | const char *p; |
| 535 | for (p = path + 2, eles = 1 ; *p != '\0' ; ++p) |
| 536 | { |
| 537 | /* if it's a non-trailing slash, count a new element */ |
| 538 | if ((*p == '\\' || *p == '/') |
| 539 | && p[1] != '\0' && p[1] != '\\' && p[1] != '/') |
| 540 | ++eles; |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * it's a root path if it has one or two elements (\\MACHINE or |
| 545 | * \\MACHINE\ROOT) |
| 546 | */ |
| 547 | return eles <= 2; |
| 548 | } |
| 549 | |
| 550 | /* not a root path */ |
| 551 | return FALSE; |
| 552 | } |
| 553 | |
| 554 | /* ------------------------------------------------------------------------ */ |
| 555 | /* |
| 556 | * Is the given file in the given directory? |
| 557 | */ |
| 558 | int os_is_file_in_dir(const char *filename, const char *path, |
| 559 | int allow_subdirs, int match_self) |
| 560 | { |
| 561 | char filename_buf[OSFNMAX], path_buf[OSFNMAX]; |
| 562 | size_t flen, plen; |
| 563 | int endsep = FALSE; |
| 564 | |
| 565 | /* absolute-ize the filename, if necessary */ |
| 566 | if (!os_is_file_absolute(filename)) |
| 567 | { |
| 568 | os_get_abs_filename(filename_buf, sizeof(filename_buf), filename); |
| 569 | filename = filename_buf; |
| 570 | } |
| 571 | |
| 572 | /* absolute-ize the path, if necessary */ |
| 573 | if (!os_is_file_absolute(path)) |
| 574 | { |
| 575 | os_get_abs_filename(path_buf, sizeof(path_buf), path); |
| 576 | path = path_buf; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * canonicalize the paths, to remove .. and . elements - this will make |
| 581 | * it possible to directly compare the path strings |
| 582 | */ |
| 583 | safe_strcpy(filename_buf, sizeof(filename_buf), filename); |
| 584 | canonicalize_path(filename_buf); |
| 585 | filename = filename_buf; |
| 586 | |
| 587 | safe_strcpy(path_buf, sizeof(path_buf), path); |
| 588 | canonicalize_path(path_buf); |
| 589 | path = path_buf; |
| 590 | |
| 591 | /* get the length of the filename and the length of the path */ |
| 592 | flen = strlen(filename); |
| 593 | plen = strlen(path); |
| 594 | |
| 595 | /* |
| 596 | * If the path ends in a separator character, ignore that. Exception: |
| 597 | * if the path is a root path, leave the separator intact. |
| 598 | */ |
| 599 | if (plen > 0 && (path[plen-1] == '\\' || path[plen-1] == '/') |
| 600 | && !is_root_path(path)) |
| 601 | --plen; |
| 602 | |
| 603 | /* if the path still ends in a path separator, so note */ |
| 604 | endsep = (plen > 0 && (path[plen-1] == '\\' || path[plen-1] == '/')); |
| 605 | |
| 606 | /* |
| 607 | * if the names match, return true if and only if the caller wants |
| 608 | * us to match the directory to itself |
| 609 | */ |
| 610 | if (flen == plen && memicmp(filename, path, flen) == 0) |
| 611 | return match_self; |
| 612 | |
| 613 | /* |
| 614 | * Check that the filename has 'path' as its path prefix. First, check |
| 615 | * that the leading substring of the filename matches 'path', ignoring |
| 616 | * case. Note that we need the filename to be at least two characters |
| 617 | * longer than the path: it must have a path separator after the path |
| 618 | * name, and at least one character for a filename past that. |
| 619 | * Exception: if the path already ends in a path separator, we don't |
| 620 | * need to add another one, so the filename just needs to be one |
| 621 | * character longer. |
| 622 | */ |
| 623 | if (flen < plen + (endsep ? 1 : 2) || !patheq(filename, path, plen)) |
| 624 | return FALSE; |
| 625 | |
| 626 | /* |
| 627 | * Okay, 'path' is the leading substring of 'filename'; next make sure |
| 628 | * that this prefix actually ends at a path separator character in the |
| 629 | * filename. (This is necessary so that we don't confuse "c:\a\b.txt" |
| 630 | * as matching "c:\abc\d.txt" - if we only matched the "c:\a" prefix, |
| 631 | * we'd miss the fact that the file is actually in directory "c:\abc", |
| 632 | * not "c:\a".) If the path itself ends in a path separator, we've |
| 633 | * made this check already simply by the substring match. |
| 634 | */ |
| 635 | if (!endsep && (filename[plen] != '\\' && filename[plen] != '/')) |
| 636 | return FALSE; |
| 637 | |
| 638 | /* |
| 639 | * We're good on the path prefix - we definitely have a file that's |
| 640 | * within the 'path' directory or one of its subdirectories. If we're |
| 641 | * allowed to match on subdirectories, we already have our answer |
| 642 | * (true). If we're not allowed to match subdirectories, we still have |
| 643 | * one more check, which is that the rest of the filename is free of |
| 644 | * path separator charactres. If it is, we have a file that's directly |
| 645 | * in the 'path' directory; otherwise it's in a subdirectory of 'path' |
| 646 | * and thus isn't a match. |
| 647 | */ |
| 648 | if (allow_subdirs) |
| 649 | { |
| 650 | /* |
| 651 | * filename is in the 'path' directory or one of its |
| 652 | * subdirectories, and we're allowed to match on subdirectories, so |
| 653 | * we have a match |
| 654 | */ |
| 655 | return TRUE; |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | const char *p; |
| 660 | |
| 661 | /* |
| 662 | * We're not allowed to match subdirectories, so scan the rest of |
| 663 | * the filename for path separators. If we find any, the file is |
| 664 | * in a subdirectory of 'path' rather than directly in 'path' |
| 665 | * itself, so it's not a match. If we don't find any separators, |
| 666 | * we have a file directly in 'path', so it's a match. |
| 667 | */ |
| 668 | for (p = filename + plen + (endsep ? 1 : 0) ; |
| 669 | *p != '\0' && *p != '/' && *p != '\\' ; ++p) ; |
| 670 | |
| 671 | /* |
| 672 | * if we reached the end of the string without finding a path |
| 673 | * separator character, it's a match |
| 674 | */ |
| 675 | return (*p == '\0'); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* ------------------------------------------------------------------------ */ |
| 680 | /* |
| 681 | * Match a string to a DOS device name. If the reference name contains a |
| 682 | * trailing '#', we'll match any digit from 1 to 9 to the '#'. |
| 683 | */ |
| 684 | static int devname_eq(const char *str, const char *ref) |
| 685 | { |
| 686 | /* compare each character */ |
| 687 | for ( ; *str != '\0' && *ref != '\0' ; ++str, ++ref) |
| 688 | { |
| 689 | /* get both characters in lower case */ |
| 690 | char strch = isupper(*str) ? tolower(*str) : *str; |
| 691 | char refch = isupper(*ref) ? tolower(*ref) : *ref; |
| 692 | |
| 693 | /* if they match exactly, proceed to the next character */ |
| 694 | if (refch == strch) |
| 695 | continue; |
| 696 | |
| 697 | /* |
| 698 | * if the reference character is a trailing '#', match to any digit |
| 699 | * 1-9 |
| 700 | */ |
| 701 | if (refch == '#' && ref[1] == '\0' |
| 702 | && (strch >= '1' && strch <= '9')) |
| 703 | continue; |
| 704 | |
| 705 | /* no match */ |
| 706 | return FALSE; |
| 707 | } |
| 708 | |
| 709 | /* if they didn't run out at the same time, we don't have a match */ |
| 710 | return *str == *ref; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | #ifdef DJGPP |
| 715 | #define _stat stat |
| 716 | #define _S_IFCHR S_IFCHR |
| 717 | #endif |
| 718 | #ifdef TURBO |
| 719 | #define _stat stat |
| 720 | #define _S_IFCHR S_IFCHR |
| 721 | #endif |
| 722 | |
| 723 | /* |
| 724 | * File mode |
| 725 | */ |
| 726 | int osfmode(const char *fname, int follow_links, |
| 727 | unsigned long *mode, unsigned long *attrs) |
| 728 | { |
| 729 | struct _stat s; |
| 730 | const char *root, *p; |
| 731 | static const char *devices[] = { |
| 732 | "con", "prn", "aux", "nul", "com#", "lpt#" |
| 733 | }; |
| 734 | int i; |
| 735 | char rbuf[32]; |
| 736 | unsigned long imode, iattrs; |
| 737 | |
| 738 | /* |
| 739 | * if the caller doesn't want the 'mode' or 'attrs' values, set the |
| 740 | * pointers to point to internal buffers so that we don't have to worry |
| 741 | * about null OUT variables beyond this point |
| 742 | */ |
| 743 | if (mode == 0) |
| 744 | mode = &imode; |
| 745 | if (attrs == 0) |
| 746 | attrs = &iattrs; |
| 747 | |
| 748 | /* clear the mode bits in case we don't find a match */ |
| 749 | *mode = 0; |
| 750 | |
| 751 | /* |
| 752 | * Check for the reserved DOS device filenames. Windows device name |
| 753 | * handling is a bit bizarre and, frankly, stupid: if the root filename |
| 754 | * (excluding the extension) is one of the special DOS device names, |
| 755 | * the entire rest of the name (drive, path, extension) is ignored, and |
| 756 | * the reserved name takes precedence. So, pull out the root name |
| 757 | * after the path, then strip off any extension, so that we have the |
| 758 | * part of the name that Windows treats as a device name. Run through |
| 759 | * the fixed list of reserved names. If we find a match, the name is |
| 760 | * definitely a device name; but then we have to check if the device |
| 761 | * actually exists on this system to determine if we should return |
| 762 | * "device" or "unknown". |
| 763 | */ |
| 764 | root = os_get_root_name((char *)fname); |
| 765 | for (p = root ; *p && *p != ':' && *p != '.' ; ++p) ; |
| 766 | |
| 767 | /* extract the root name into a buffer */ |
| 768 | safe_strcpyl(rbuf, sizeof(rbuf), root, p - root); |
| 769 | |
| 770 | /* look for a device name match */ |
| 771 | for (i = 0 ; i < sizeof(devices)/sizeof(devices[0]) ; ++i) |
| 772 | { |
| 773 | /* check the root name against the current device name */ |
| 774 | if (devname_eq(rbuf, devices[i])) |
| 775 | { |
| 776 | FILE *fp; |
| 777 | |
| 778 | /* |
| 779 | * We got a match, so this file refers to a device. Now we |
| 780 | * have to check to see if the device actually exists on this |
| 781 | * system. That's surprisingly difficult; there doesn't seem |
| 782 | * to exist a Windows API that returns consistent results. The |
| 783 | * only sure way seems to be to try opening the file. Some |
| 784 | * devices are read-only and some are write-only, so we need to |
| 785 | * try both modes. |
| 786 | * |
| 787 | * Opening the file as a device doesn't seem ideal for a number |
| 788 | * of reasons, such as the potential for false negatives due to |
| 789 | * sharing violations. But there doesn't seem to be any other |
| 790 | * reliable way to find out if the file is openable. |
| 791 | */ |
| 792 | *attrs = 0; |
| 793 | fp = _fsopen(rbuf, "r", _SH_DENYNO); |
| 794 | if (fp != 0) |
| 795 | { |
| 796 | *attrs |= OSFATTR_READ; |
| 797 | fclose(fp); |
| 798 | } |
| 799 | fp = _fsopen(rbuf, "w", _SH_DENYNO); |
| 800 | if (fp != 0) |
| 801 | { |
| 802 | *attrs |= OSFATTR_WRITE; |
| 803 | fclose(fp); |
| 804 | } |
| 805 | if (*attrs != 0) |
| 806 | { |
| 807 | /* it exists - return "char mode device" */ |
| 808 | *mode = OSFMODE_CHAR; |
| 809 | return TRUE; |
| 810 | } |
| 811 | |
| 812 | /* it's a device name, but it doesn't exist - return failure */ |
| 813 | return FALSE; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* get the file status information; return false on failure */ |
| 818 | if (_stat(fname, &s) < 0) |
| 819 | return FALSE; |
| 820 | |
| 821 | /* got it - return the mode information */ |
| 822 | *mode = (unsigned long)s.st_mode; |
| 823 | |
| 824 | /* get the file attributes */ |
| 825 | *attrs = oss_get_file_attrs(fname); |
| 826 | |
| 827 | /* indicate success */ |
| 828 | return TRUE; |
| 829 | } |
| 830 | |
| 831 | /* ------------------------------------------------------------------------ */ |
| 832 | #if (!defined(WINDOWS) && !defined(_Windows)) || defined(__DPMI16__) |
| 833 | # if defined(TURBO) || defined(DJGPP) |
| 834 | |
| 835 | /* get bitmap of valid drives */ |
| 836 | unsigned long ossgetdisks(void) |
| 837 | { |
| 838 | unsigned long mask; |
| 839 | unsigned long ret; |
| 840 | int drive; |
| 841 | |
| 842 | /* iterate over drives A: to Z: and test each one for validity */ |
| 843 | for (drive = 'A', mask = 1, ret = 0 ; drive <= 'Z' ; ++drive, mask <<= 1) |
| 844 | { |
| 845 | char dname[3]; |
| 846 | |
| 847 | /* build a filename string for the drive, like "X:" */ |
| 848 | dname[0] = drive; |
| 849 | dname[1] = ':'; |
| 850 | dname[2] = '\0'; |
| 851 | { |
| 852 | # ifdef DJGPP |
| 853 | int max_file_len; |
| 854 | int max_path_len; |
| 855 | char fstype[64]; |
| 856 | |
| 857 | /* |
| 858 | * check the file system type on the drive to see if the drive |
| 859 | * is valid |
| 860 | */ |
| 861 | if ((_get_volume_info(dname, &max_file_len, &max_path_len, |
| 862 | fstype) & _FILESYS_UNKNOWN) == 0) |
| 863 | { |
| 864 | /* |
| 865 | * it's not unknown, so it must be valid - include it in |
| 866 | * the result vector |
| 867 | */ |
| 868 | ret |= mask; |
| 869 | } |
| 870 | |
| 871 | # else /* DJGPP */ |
| 872 | char fcb[44]; |
| 873 | |
| 874 | /* |
| 875 | * Ask DOS to parse the drive letter filename (via int 0x21, |
| 876 | * function 0x290E). DOS will give us an error indication if |
| 877 | * the drive is invalid. |
| 878 | */ |
| 879 | asm { |
| 880 | push ds |
| 881 | push es |
| 882 | push bp |
| 883 | push si |
| 884 | push di |
| 885 | |
| 886 | mov si, ss |
| 887 | mov ds, si |
| 888 | push ds |
| 889 | pop es |
| 890 | lea si, dname |
| 891 | lea di, fcb |
| 892 | mov ax, 0x290E // parse filename |
| 893 | int 0x21 |
| 894 | cmp al, 0xFF |
| 895 | je bad_drive |
| 896 | } |
| 897 | |
| 898 | /* if we got this far, the drive is good */ |
| 899 | ret |= mask; |
| 900 | |
| 901 | bad_drive: |
| 902 | asm { |
| 903 | pop di |
| 904 | pop si |
| 905 | pop bp |
| 906 | pop es |
| 907 | pop ds |
| 908 | } |
| 909 | # endif |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | # endif /* TURBO || DJGPP */ |
| 917 | |
| 918 | # ifdef MSOS2 |
| 919 | /* get bitmap of valid drives */ |
| 920 | unsigned long ossgetdisks(void) |
| 921 | { |
| 922 | /* return zero to indicate this isn't implemented */ |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | # else /* MSOS2 */ |
| 927 | # ifdef MICROSOFT |
| 928 | |
| 929 | # ifdef T_WIN32 |
| 930 | |
| 931 | #include <io.h> |
| 932 | #include <direct.h> |
| 933 | unsigned long ossgetdisks(void) |
| 934 | { |
| 935 | return _getdrives(); |
| 936 | } |
| 937 | |
| 938 | # else /* T_WIN32 */ |
| 939 | |
| 940 | /* get disk: 0 = A:, 1 = B:, 2 = C:, etc */ |
| 941 | unsigned long ossgetdisks(void) |
| 942 | { |
| 943 | return _dos_getdrives(); |
| 944 | } |
| 945 | |
| 946 | # endif /* T_WIN32 */ |
| 947 | # endif /* MICROSOFT */ |
| 948 | # endif /* MSOS2 */ |
| 949 | #endif |
| 950 | |
| 951 | /* |
| 952 | * Get the root directory list. On DOS/Windows, this returns a list of |
| 953 | * drive root paths - C:\, D:\, etc. |
| 954 | */ |
| 955 | #ifdef T_WIN32 |
| 956 | size_t os_get_root_dirs(char *buf, size_t buflen) |
| 957 | { |
| 958 | return GetLogicalDriveStrings(buflen, buf); |
| 959 | } |
| 960 | #else |
| 961 | size_t os_get_root_dirs(char *buf, size_t buflen) |
| 962 | { |
| 963 | extern unsigned long ossgetdisks(void); |
| 964 | unsigned long d = ossgetdisks(); |
| 965 | int i, cnt; |
| 966 | unsigned long b; |
| 967 | size_t l; |
| 968 | char *p; |
| 969 | |
| 970 | /* count the drive bits set */ |
| 971 | for (i = 0, cnt = 0, b = 1 ; i < 32 ; ++i, b <<= 1) |
| 972 | { |
| 973 | if ((d & b) != 0) |
| 974 | ++cnt; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * figure the length needed - C:\ -> 4 characters (with null) for each |
| 979 | * drive, plus an extra null at the end of the list |
| 980 | */ |
| 981 | l = 4*cnt + 1; |
| 982 | |
| 983 | /* if the buffer isn't big enough, just return the length */ |
| 984 | if (l > buflen) |
| 985 | return l; |
| 986 | |
| 987 | /* fill in the buffer */ |
| 988 | for (p = buf, i = 0, b = 1 ; i < 32 ; ++i, b <<= 1) |
| 989 | { |
| 990 | if ((d & b) != 0) |
| 991 | { |
| 992 | *p++ = 'A' + i; |
| 993 | *p++ = ':'; |
| 994 | *p++ = '\\'; |
| 995 | *p++ = '\0'; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /* add the trailing null */ |
| 1000 | *p++ = '\0'; |
| 1001 | |
| 1002 | /* return the length */ |
| 1003 | return p - buf; |
| 1004 | } |
| 1005 | |
| 1006 | #endif |
| 1007 | |
| 1008 | #ifdef T_WIN32 |
| 1009 | /* |
| 1010 | * get the system time in nanoseconds |
| 1011 | */ |
| 1012 | void os_time_ns(os_time_t *seconds, long *nanoseconds) |
| 1013 | { |
| 1014 | FILETIME ft; |
| 1015 | ULARGE_INTEGER fti; |
| 1016 | |
| 1017 | /* get the system time in FILETIME format */ |
| 1018 | GetSystemTimeAsFileTime(&ft); |
| 1019 | |
| 1020 | /* |
| 1021 | * FILETIME is essentially a 64-bit value giving 100-nanosecond |
| 1022 | * intervals since the FILETIME Epoch, which is 1/1/1601 00:00:00 UTC. |
| 1023 | * We need to make two adjustments. First, subtract the number of |
| 1024 | * 100ns intervals between 1/1/1970 and 1/1/1601 to convert to the |
| 1025 | * interval since the Unix Eopch of 1/1/1970. (There are 134774 days |
| 1026 | * between these dates.) Second, mutliply by 100 to convert to |
| 1027 | * nanoseconds. |
| 1028 | */ |
| 1029 | fti.LowPart = ft.dwLowDateTime; |
| 1030 | fti.HighPart = ft.dwHighDateTime; |
| 1031 | fti.QuadPart -= 134774LL*24*60*60*10000000; |
| 1032 | fti.QuadPart *= 100; |
| 1033 | |
| 1034 | /* |
| 1035 | * Okay, we have nanoseconds since 1/1/1970 as a 64-bit value. Divide |
| 1036 | * by 1,000,000,000 to get seconds, and get the whole part and the |
| 1037 | * remainder as seconds and nanoseconds, respectively. |
| 1038 | */ |
| 1039 | *seconds = (os_time_t)(fti.QuadPart / 1000000000); |
| 1040 | *nanoseconds = (long)(fti.QuadPart % 1000000000); |
| 1041 | } |
| 1042 | #endif /* T_WIN32 */ |
| 1043 |