OLD | NEW |
1 /* | 1 /* |
2 * File helper functions. | 2 * File helper functions. |
3 * | 3 * |
4 * Copyright (C) 2001-2007 Peter Johnson | 4 * Copyright (C) 2001-2007 Peter Johnson |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
11 * 2. Redistributions in binary form must reproduce the above copyright | 11 * 2. Redistributions in binary form must reproduce the above copyright |
12 * notice, this list of conditions and the following disclaimer in the | 12 * notice, this list of conditions and the following disclaimer in the |
13 * documentation and/or other materials provided with the distribution. | 13 * documentation and/or other materials provided with the distribution. |
14 * | 14 * |
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' | 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE | 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * POSSIBILITY OF SUCH DAMAGE. | 25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 #include <util.h> | 27 #include <util.h> |
28 /*@unused@*/ RCSID("$Id: file.c 2116 2008-07-15 05:49:29Z peter $"); | 28 /*@unused@*/ RCSID("$Id: file.c 2287 2010-02-13 08:42:27Z peter $"); |
29 | 29 |
30 /* Need either unistd.h or direct.h (on Windows) to prototype getcwd() */ | 30 /* Need either unistd.h or direct.h (on Windows) to prototype getcwd() */ |
31 #if defined(HAVE_UNISTD_H) | 31 #if defined(HAVE_UNISTD_H) |
32 #include <unistd.h> | 32 #include <unistd.h> |
33 #elif defined(HAVE_DIRECT_H) | 33 #elif defined(HAVE_DIRECT_H) |
34 #include <direct.h> | 34 #include <direct.h> |
35 #endif | 35 #endif |
36 | 36 |
| 37 #ifdef _WIN32 |
| 38 #include <io.h> |
| 39 #endif |
| 40 |
| 41 #ifdef HAVE_SYS_STAT_H |
| 42 #include <sys/stat.h> |
| 43 #endif |
| 44 |
37 #include <ctype.h> | 45 #include <ctype.h> |
38 #include <errno.h> | 46 #include <errno.h> |
39 | 47 |
40 #include "errwarn.h" | 48 #include "errwarn.h" |
41 #include "file.h" | 49 #include "file.h" |
42 | 50 |
43 #define BSIZE 8192 /* Fill block size */ | 51 #define BSIZE 8192 /* Fill block size */ |
44 | 52 |
45 | 53 |
46 void | 54 void |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 } | 236 } |
229 | 237 |
230 char * | 238 char * |
231 yasm__getcwd(void) | 239 yasm__getcwd(void) |
232 { | 240 { |
233 char *buf; | 241 char *buf; |
234 size_t size; | 242 size_t size; |
235 | 243 |
236 size = 1024; | 244 size = 1024; |
237 buf = yasm_xmalloc(size); | 245 buf = yasm_xmalloc(size); |
238 while (getcwd(buf, size) == NULL) { | 246 while (getcwd(buf, size-1) == NULL) { |
239 if (errno != ERANGE) { | 247 if (errno != ERANGE) { |
240 yasm__fatal(N_("could not determine current working directory")); | 248 yasm__fatal(N_("could not determine current working directory")); |
241 yasm_xfree(buf); | 249 yasm_xfree(buf); |
242 return NULL; | 250 return NULL; |
243 } | 251 } |
244 size *= 2; | 252 size *= 2; |
245 buf = yasm_xrealloc(buf, size); | 253 buf = yasm_xrealloc(buf, size); |
246 } | 254 } |
| 255 |
| 256 /* append a '/' if not already present */ |
| 257 size = strlen(buf); |
| 258 if (buf[size-1] != '\\' && buf[size-1] != '/') { |
| 259 buf[size] = '/'; |
| 260 buf[size+1] = '\0'; |
| 261 } |
247 return buf; | 262 return buf; |
248 } | 263 } |
249 | 264 |
250 char * | 265 char * |
251 yasm__abspath(const char *path) | 266 yasm__abspath(const char *path) |
252 { | 267 { |
253 char *curdir, *abspath; | 268 char *curdir, *abspath; |
254 | 269 |
255 curdir = yasm__getcwd(); | 270 curdir = yasm__getcwd(); |
256 abspath = yasm__combpath(curdir, path); | 271 abspath = yasm__combpath(curdir, path); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 if (*to == '/') | 451 if (*to == '/') |
437 out[j++] = '\\'; | 452 out[j++] = '\\'; |
438 else | 453 else |
439 out[j++] = *to; | 454 out[j++] = *to; |
440 } | 455 } |
441 out[j++] = '\0'; | 456 out[j++] = '\0'; |
442 | 457 |
443 return out; | 458 return out; |
444 } | 459 } |
445 | 460 |
| 461 size_t |
| 462 yasm__createpath_common(const char *path, int win) |
| 463 { |
| 464 const char *pp = path, *pe; |
| 465 char *ts, *tp; |
| 466 size_t len, lth; |
| 467 |
| 468 lth = len = strlen(path); |
| 469 ts = tp = (char *) malloc(len + 1); |
| 470 pe = pp + len; |
| 471 while (pe > pp) { |
| 472 if ((win && *pe == '\\') || *pe == '/') |
| 473 break; |
| 474 --pe; |
| 475 --lth; |
| 476 } |
| 477 |
| 478 while (pp <= pe) { |
| 479 if (pp == pe || (win && *pp == '\\') || *pp == '/') { |
| 480 #ifdef _WIN32 |
| 481 struct _finddata_t fi; |
| 482 intptr_t h; |
| 483 #elif defined(HAVE_SYS_STAT_H) |
| 484 struct stat fi; |
| 485 #endif |
| 486 *tp = '\0'; |
| 487 |
| 488 #ifdef _WIN32 |
| 489 h = _findfirst(ts, &fi); |
| 490 if (h != -1) { |
| 491 if (fi.attrib != _A_SUBDIR) { |
| 492 _findclose(h); |
| 493 break; |
| 494 } |
| 495 } else if (errno == ENOENT) { |
| 496 if (_mkdir(ts) == -1) { |
| 497 _findclose(h); |
| 498 lth = -1; |
| 499 break; |
| 500 } |
| 501 } |
| 502 _findclose(h); |
| 503 #elif defined(HAVE_SYS_STAT_H) |
| 504 if (stat(ts, &fi) != -1) { |
| 505 if (!S_ISDIR(fi.st_mode)) |
| 506 break; |
| 507 } else if (errno == ENOENT) { |
| 508 if (mkdir(ts, 0755) == -1) { |
| 509 lth = 0; |
| 510 break; |
| 511 } |
| 512 } |
| 513 #else |
| 514 break; |
| 515 #endif |
| 516 } |
| 517 *tp++ = *pp++; |
| 518 } |
| 519 free(ts); |
| 520 return lth; |
| 521 } |
| 522 |
446 typedef struct incpath { | 523 typedef struct incpath { |
447 STAILQ_ENTRY(incpath) link; | 524 STAILQ_ENTRY(incpath) link; |
448 /*@owned@*/ char *path; | 525 /*@owned@*/ char *path; |
449 } incpath; | 526 } incpath; |
450 | 527 |
451 STAILQ_HEAD(, incpath) incpaths = STAILQ_HEAD_INITIALIZER(incpaths); | 528 STAILQ_HEAD(incpath_head, incpath) incpaths = STAILQ_HEAD_INITIALIZER(incpaths); |
452 | 529 |
453 FILE * | 530 FILE * |
454 yasm_fopen_include(const char *iname, const char *from, const char *mode, | 531 yasm_fopen_include(const char *iname, const char *from, const char *mode, |
455 char **oname) | 532 char **oname) |
456 { | 533 { |
457 FILE *f; | 534 FILE *f; |
458 char *combine; | 535 char *combine; |
459 incpath *np; | 536 incpath *np; |
460 | 537 |
461 /* Try directly relative to from first, then each of the include paths */ | 538 /* Try directly relative to from first, then each of the include paths */ |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 if (fputc((int)((val >> 24) & 0xFF), f) == EOF) | 656 if (fputc((int)((val >> 24) & 0xFF), f) == EOF) |
580 return 0; | 657 return 0; |
581 if (fputc((int)((val >> 16) & 0xFF), f) == EOF) | 658 if (fputc((int)((val >> 16) & 0xFF), f) == EOF) |
582 return 0; | 659 return 0; |
583 if (fputc((int)((val >> 8) & 0xFF), f) == EOF) | 660 if (fputc((int)((val >> 8) & 0xFF), f) == EOF) |
584 return 0; | 661 return 0; |
585 if (fputc((int)(val & 0xFF), f) == EOF) | 662 if (fputc((int)(val & 0xFF), f) == EOF) |
586 return 0; | 663 return 0; |
587 return 1; | 664 return 1; |
588 } | 665 } |
OLD | NEW |