gcc: newer gcc trip on -Wstringop-truncation for valid uses of strncpy()
[nasm.git] / nasmlib / path.c
blob9dadaba87bcfdf1ee1fc9604fb01c09a2d36a57b
1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
35 * path.c - host operating system specific pathname manipulation functions
37 * This file is inherently nonportable ... please help adjusting it to
38 * any new platforms that may be necessary.
41 #include "compiler.h"
42 #include "nasmlib.h"
43 #include "error.h"
45 #if defined(__MSDOS__) || defined(__DOS__) || \
46 defined(__WINDOWS__) || defined(_Windows) || \
47 defined(__OS2__) || defined(_WIN16) || defined(WIN32) || defined(_WIN32)
48 /* MS-DOS/Windows and like operating systems */
49 # define separators "/\\:"
50 # define cleandirend "/\\"
51 # define catsep '\\'
52 # define leaveonclean 2 /* Leave \\ at the start alone */
53 # define curdir "."
54 #elif defined(unix) || defined(__unix) || defined(__unix__) || \
55 defined(__UNIX__) || defined(__Unix__) || \
56 defined(__MACH__) || defined(__BEOS__)
57 /* Unix and Unix-like operating systems and others using
58 * the equivalent syntax (slashes as only separators, no concept of volume)
60 * This must come after the __MSDOS__ section, since it seems that at
61 * least DJGPP defines __unix__ despite not being a Unix environment at all.
63 # define separators "/"
64 # define cleandirend "/"
65 # define catsep '/'
66 # define leaveonclean 1
67 # define curdir "."
68 #elif defined(Macintosh) || defined(macintosh)
69 /* MacOS classic */
70 # define separators ":"
71 # define curdir ":"
72 # define catsep ':'
73 # define cleandirend ":"
74 # define leaveonclean 0
75 # define leave_leading 1
76 #elif defined(__VMS)
77 /* VMS *
79 * VMS filenames may have ;version at the end. Assume we should count that
80 * as part of the filename anyway.
82 # define separators ":]"
83 # define curdir "[]"
84 #else
85 /* No idea what to do here, do nothing. Feel free to add new ones. */
86 # define curdir ""
87 #endif
90 * This is an inline, because most compilers can greatly simplify this
91 * for a fixed string, like we have here.
93 static inline bool ismatch(const char *charset, char ch)
95 const char *p;
97 for (p = charset; *p; p++) {
98 if (ch == *p)
99 return true;
102 return false;
105 static const char *first_filename_char(const char *path)
107 #ifdef separators
108 const char *p = path + strlen(path);
110 while (p > path) {
111 if (ismatch(separators, p[-1]))
112 return p;
113 p--;
116 return p;
117 #else
118 return path;
119 #endif
122 /* Return the filename portion of a PATH as a new string */
123 char *nasm_basename(const char *path)
125 return nasm_strdup(first_filename_char(path));
128 /* Return the directory name portion of a PATH as a new string */
129 char *nasm_dirname(const char *path)
131 const char *p = first_filename_char(path);
132 const char *p0 = p;
133 (void)p0; /* Don't warn if unused */
135 if (p == path)
136 return nasm_strdup(curdir);
138 #ifdef cleandirend
139 while (p > path+leaveonclean) {
140 if (ismatch(cleandirend, p[-1]))
141 break;
142 p--;
144 #endif
146 #ifdef leave_leading
147 /* If the directory contained ONLY separators, leave as-is */
148 if (p == path+leaveonclean)
149 p = p0;
150 #endif
152 return nasm_strndup(path, p-path);
156 * Concatenate a directory path and a filename. Note that this function
157 * currently does NOT handle the case where file itself contains
158 * directory components (except on Unix platforms, because it is trivial.)
160 char *nasm_catfile(const char *dir, const char *file)
162 #ifndef catsep
163 return nasm_strcat(dir, file);
164 #else
165 size_t dl = strlen(dir);
166 size_t fl = strlen(file);
167 char *p;
168 bool dosep = true;
170 if (!dl || ismatch(separators, dir[dl-1])) {
171 /* No separator necessary */
172 dosep = false;
175 p = nasm_malloc(dl + fl + dosep + 1);
177 memcpy(p, dir, dl);
178 p += dl;
179 if (dosep)
180 *p++ = catsep;
182 memcpy(p, file, fl+1);
184 return p;
185 #endif