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
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.
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 "/\\"
52 # define leaveonclean 2 /* Leave \\ at the start alone */
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 "/"
66 # define leaveonclean 1
68 #elif defined(Macintosh) || defined(macintosh)
70 # define separators ":"
73 # define cleandirend ":"
74 # define leaveonclean 0
75 # define leave_leading 1
79 * VMS filenames may have ;version at the end. Assume we should count that
80 * as part of the filename anyway.
82 # define separators ":]"
85 /* No idea what to do here, do nothing. Feel free to add new ones. */
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
)
97 for (p
= charset
; *p
; p
++) {
105 static const char *first_filename_char(const char *path
)
108 const char *p
= path
+ strlen(path
);
111 if (ismatch(separators
, p
[-1]))
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
);
133 (void)p0
; /* Don't warn if unused */
136 return nasm_strdup(curdir
);
139 while (p
> path
+leaveonclean
) {
140 if (ismatch(cleandirend
, p
[-1]))
147 /* If the directory contained ONLY separators, leave as-is */
148 if (p
== path
+leaveonclean
)
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
)
163 return nasm_strcat(dir
, file
);
165 size_t dl
= strlen(dir
);
166 size_t fl
= strlen(file
);
170 if (!dl
|| ismatch(separators
, dir
[dl
-1])) {
171 /* No separator necessary */
175 p
= nasm_malloc(dl
+ fl
+ dosep
+ 1);
182 memcpy(p
, file
, fl
+1);