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 "/" |
H. Peter Anvin | 11a07a7 | 2017-04-23 23:01:00 -0700 | [diff] [blame^] | 45 | # define catsep '/' |
H. Peter Anvin | 0979957 | 2017-04-23 22:39:53 -0700 | [diff] [blame] | 46 | # define leaveonclean 1 |
| 47 | # define curdir "." |
| 48 | #elif defined(__MSDOS__) || defined(__WINDOWS__) || \ |
| 49 | defined(__OS2__) || defined(_WIN16) || defined(_WIN32) |
| 50 | # define separators "/\\:" |
| 51 | # define cleandirend "/\\" |
H. Peter Anvin | 11a07a7 | 2017-04-23 23:01:00 -0700 | [diff] [blame^] | 52 | # define catsep '\\' |
H. Peter Anvin | 0979957 | 2017-04-23 22:39:53 -0700 | [diff] [blame] | 53 | # define leaveonclean 2 /* Leave \\ at the start alone */ |
| 54 | # define curdir "." |
| 55 | #elif defined(Macintosh) /* MacOS classic? */ |
| 56 | # define separators ":" |
| 57 | # define curdir ":" |
H. Peter Anvin | 11a07a7 | 2017-04-23 23:01:00 -0700 | [diff] [blame^] | 58 | # define catsep ":" |
H. Peter Anvin | 0979957 | 2017-04-23 22:39:53 -0700 | [diff] [blame] | 59 | # define cleandirend ":" |
| 60 | # define leaveonclean 0 |
| 61 | # define leave_leading 1 |
| 62 | #elif defined(__VMS) |
| 63 | /* |
| 64 | * VMS filenames may have ;version at the end. Assume we should count that |
| 65 | * as part of the filename anyway. |
| 66 | */ |
| 67 | # define separators ":]" |
| 68 | # define curdir "[]" |
| 69 | #else |
| 70 | /* No idea what to do here, do nothing. Feel free to add new ones. */ |
| 71 | # define curdir "" |
| 72 | #endif |
| 73 | |
H. Peter Anvin | 11a07a7 | 2017-04-23 23:01:00 -0700 | [diff] [blame^] | 74 | /* |
| 75 | * This is an inline, because most compilers can greatly simplify this |
| 76 | * for a fixed string, like we have here. |
| 77 | */ |
H. Peter Anvin | 0979957 | 2017-04-23 22:39:53 -0700 | [diff] [blame] | 78 | static inline bool ismatch(const char *charset, char ch) |
| 79 | { |
| 80 | const char *p; |
| 81 | |
| 82 | for (p = charset; *p; p++) { |
| 83 | if (ch == *p) |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | static const char *first_filename_char(const char *path) |
| 91 | { |
| 92 | #ifdef separators |
| 93 | const char *p = path + strlen(path); |
| 94 | |
| 95 | while (p > path) { |
| 96 | if (!ismatch(separators, p[-1])) |
| 97 | return p; |
| 98 | p--; |
| 99 | } |
| 100 | |
| 101 | return p; |
| 102 | #else |
| 103 | return path; |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | /* Return the filename portion of a PATH as a new string */ |
| 108 | char *nasm_basename(const char *path) |
| 109 | { |
| 110 | return nasm_strdup(first_filename_char(path)); |
| 111 | } |
| 112 | |
| 113 | /* Return the directory name portion of a PATH as a new string */ |
| 114 | char *nasm_dirname(const char *path) |
| 115 | { |
| 116 | const char *p = first_filename_char(path); |
| 117 | const char *p0 = p; |
| 118 | (void)p0; /* Don't warn if unused */ |
| 119 | |
| 120 | if (p == path) |
| 121 | return nasm_strdup(curdir); |
| 122 | |
| 123 | #ifdef cleandirend |
| 124 | while (p > path+leaveonclean) { |
| 125 | if (ismatch(cleandirend, p[-1])) |
| 126 | break; |
| 127 | p--; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #ifdef leave_leading |
| 132 | /* If the directory contained ONLY separators, leave as-is */ |
| 133 | if (p == path+leaveonclean) |
| 134 | p = p0; |
| 135 | #endif |
| 136 | |
| 137 | return nasm_strndup(path, p-path); |
| 138 | } |
H. Peter Anvin | 11a07a7 | 2017-04-23 23:01:00 -0700 | [diff] [blame^] | 139 | |
| 140 | /* Concatenate a directory path and a filename */ |
| 141 | char *nasm_catfile(const char *dir, const char *file) |
| 142 | { |
| 143 | #ifndef catsep |
| 144 | return nasm_strcat(dir, file); |
| 145 | #else |
| 146 | size_t dl = strlen(dir); |
| 147 | size_t fl = strlen(file); |
| 148 | char *p; |
| 149 | bool dosep = true; |
| 150 | |
| 151 | if (!dl || ismatch(separators, dir[dl-1])) { |
| 152 | /* No separator necessary */ |
| 153 | dosep = false; |
| 154 | } |
| 155 | |
| 156 | p = nasm_malloc(dl + fl + dosep + 1); |
| 157 | |
| 158 | memcpy(p, dir, dl); |
| 159 | p += dl; |
| 160 | if (dosep) |
| 161 | *p++ = catsep; |
| 162 | |
| 163 | memcpy(p, file, fl+1); |
| 164 | |
| 165 | return p; |
| 166 | #endif |
| 167 | } |