H. Peter Anvin | 0979957 | 2017-04-23 22:39:53 -0700 | [diff] [blame^] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright 2017 The NASM Authors - All Rights Reserved |
| 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * path.c - host operating system specific pathname manipulation functions |
| 36 | */ |
| 37 | |
| 38 | #include "compiler.h" |
| 39 | #include "nasmlib.h" |
| 40 | #include "error.h" |
| 41 | |
| 42 | #if defined(unix) || defined(__unix) || defined(__unix__) |
| 43 | # define separators "/" |
| 44 | # define cleandirend "/" |
| 45 | # define leaveonclean 1 |
| 46 | # define curdir "." |
| 47 | #elif defined(__MSDOS__) || defined(__WINDOWS__) || \ |
| 48 | defined(__OS2__) || defined(_WIN16) || defined(_WIN32) |
| 49 | # define separators "/\\:" |
| 50 | # define cleandirend "/\\" |
| 51 | # define leaveonclean 2 /* Leave \\ at the start alone */ |
| 52 | # define curdir "." |
| 53 | #elif defined(Macintosh) /* MacOS classic? */ |
| 54 | # define separators ":" |
| 55 | # define curdir ":" |
| 56 | # define cleandirend ":" |
| 57 | # define leaveonclean 0 |
| 58 | # define leave_leading 1 |
| 59 | #elif defined(__VMS) |
| 60 | /* |
| 61 | * VMS filenames may have ;version at the end. Assume we should count that |
| 62 | * as part of the filename anyway. |
| 63 | */ |
| 64 | # define separators ":]" |
| 65 | # define curdir "[]" |
| 66 | #else |
| 67 | /* No idea what to do here, do nothing. Feel free to add new ones. */ |
| 68 | # define curdir "" |
| 69 | #endif |
| 70 | |
| 71 | static inline bool ismatch(const char *charset, char ch) |
| 72 | { |
| 73 | const char *p; |
| 74 | |
| 75 | for (p = charset; *p; p++) { |
| 76 | if (ch == *p) |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | static const char *first_filename_char(const char *path) |
| 84 | { |
| 85 | #ifdef separators |
| 86 | const char *p = path + strlen(path); |
| 87 | |
| 88 | while (p > path) { |
| 89 | if (!ismatch(separators, p[-1])) |
| 90 | return p; |
| 91 | p--; |
| 92 | } |
| 93 | |
| 94 | return p; |
| 95 | #else |
| 96 | return path; |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | /* Return the filename portion of a PATH as a new string */ |
| 101 | char *nasm_basename(const char *path) |
| 102 | { |
| 103 | return nasm_strdup(first_filename_char(path)); |
| 104 | } |
| 105 | |
| 106 | /* Return the directory name portion of a PATH as a new string */ |
| 107 | char *nasm_dirname(const char *path) |
| 108 | { |
| 109 | const char *p = first_filename_char(path); |
| 110 | const char *p0 = p; |
| 111 | (void)p0; /* Don't warn if unused */ |
| 112 | |
| 113 | if (p == path) |
| 114 | return nasm_strdup(curdir); |
| 115 | |
| 116 | #ifdef cleandirend |
| 117 | while (p > path+leaveonclean) { |
| 118 | if (ismatch(cleandirend, p[-1])) |
| 119 | break; |
| 120 | p--; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #ifdef leave_leading |
| 125 | /* If the directory contained ONLY separators, leave as-is */ |
| 126 | if (p == path+leaveonclean) |
| 127 | p = p0; |
| 128 | #endif |
| 129 | |
| 130 | return nasm_strndup(path, p-path); |
| 131 | } |