1 /* Macros for taking apart, interpreting and processing file names.
2 
3    These are here because some non-Posix (a.k.a. DOSish) systems have
4    drive letter brain-damage at the beginning of an absolute file name,
5    use forward- and back-slash in path names interchangeably, and
6    some of them have case-insensitive file names.
7 
8    Copyright (C) 2000-2020 Free Software Foundation, Inc.
9 
10 This file is part of BFD, the Binary File Descriptor library.
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
25 
26 #ifndef FILENAMES_H
27 #define FILENAMES_H
28 
29 #include "hashtab.h" /* for hashval_t */
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #if defined(__MSDOS__) || (defined(_WIN32) && ! defined(__CYGWIN__)) || \
36     defined(__OS2__)
37 #  ifndef HAVE_DOS_BASED_FILE_SYSTEM
38 #    define HAVE_DOS_BASED_FILE_SYSTEM 1
39 #  endif
40 #  ifndef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
41 #    define HAVE_CASE_INSENSITIVE_FILE_SYSTEM 1
42 #  endif
43 #  define HAS_DRIVE_SPEC(f) HAS_DOS_DRIVE_SPEC (f)
44 #  define IS_DIR_SEPARATOR(c) IS_DOS_DIR_SEPARATOR (c)
45 #  define IS_ABSOLUTE_PATH(f) IS_DOS_ABSOLUTE_PATH (f)
46 #else /* not DOSish */
47 #  if defined(__APPLE__)
48 #    ifndef HAVE_CASE_INSENSITIVE_FILE_SYSTEM
49 #      define HAVE_CASE_INSENSITIVE_FILE_SYSTEM 1
50 #    endif
51 #  endif /* __APPLE__ */
52 #  define HAS_DRIVE_SPEC(f) (0)
53 #  define IS_DIR_SEPARATOR(c) IS_UNIX_DIR_SEPARATOR (c)
54 #  define IS_ABSOLUTE_PATH(f) IS_UNIX_ABSOLUTE_PATH (f)
55 #endif
56 
57 #define IS_DIR_SEPARATOR_1(dos_based, c)				\
58   (((c) == '/')								\
59    || (((c) == '\\') && (dos_based)))
60 
61 #define HAS_DRIVE_SPEC_1(dos_based, f)			\
62   ((f)[0] && ((f)[1] == ':') && (dos_based))
63 
64 /* Remove the drive spec from F, assuming HAS_DRIVE_SPEC (f).
65    The result is a pointer to the remainder of F.  */
66 #define STRIP_DRIVE_SPEC(f)	((f) + 2)
67 
68 #define IS_DOS_DIR_SEPARATOR(c) IS_DIR_SEPARATOR_1 (1, c)
69 #define IS_DOS_ABSOLUTE_PATH(f) IS_ABSOLUTE_PATH_1 (1, f)
70 #define HAS_DOS_DRIVE_SPEC(f) HAS_DRIVE_SPEC_1 (1, f)
71 
72 #define IS_UNIX_DIR_SEPARATOR(c) IS_DIR_SEPARATOR_1 (0, c)
73 #define IS_UNIX_ABSOLUTE_PATH(f) IS_ABSOLUTE_PATH_1 (0, f)
74 
75 /* Note that when DOS_BASED is true, IS_ABSOLUTE_PATH accepts d:foo as
76    well, although it is only semi-absolute.  This is because the users
77    of IS_ABSOLUTE_PATH want to know whether to prepend the current
78    working directory to a file name, which should not be done with a
79    name like d:foo.  */
80 #define IS_ABSOLUTE_PATH_1(dos_based, f)		 \
81   (IS_DIR_SEPARATOR_1 (dos_based, (f)[0])		 \
82    || HAS_DRIVE_SPEC_1 (dos_based, f))
83 
84 extern int filename_cmp (const char *s1, const char *s2);
85 #define FILENAME_CMP(s1, s2)	filename_cmp(s1, s2)
86 
87 extern int filename_ncmp (const char *s1, const char *s2,
88 			  size_t n);
89 
90 extern hashval_t filename_hash (const void *s);
91 
92 extern int filename_eq (const void *s1, const void *s2);
93 
94 extern int canonical_filename_eq (const char *a, const char *b);
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* FILENAMES_H */
101