1 /* Basic filename support macros.
2    Copyright (C) 2001-2004, 2007-2020 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* From Paul Eggert and Jim Meyering.  */
18 
19 #ifndef _FILENAME_H
20 #define _FILENAME_H
21 
22 #include <string.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
29 /* Filename support.
30    ISSLASH(C)                  tests whether C is a directory separator
31                                character.
32    HAS_DEVICE(Filename)        tests whether Filename contains a device
33                                specification.
34    FILE_SYSTEM_PREFIX_LEN(Filename)  length of the device specification
35                                      at the beginning of Filename,
36                                      index of the part consisting of
37                                      alternating components and slashes.
38    FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
39                                1 when a non-empty device specification
40                                can be followed by an empty or relative
41                                part,
42                                0 when a non-empty device specification
43                                must be followed by a slash,
44                                0 when device specification don't exist.
45    IS_ABSOLUTE_FILE_NAME(Filename)
46                                tests whether Filename is independent of
47                                any notion of "current directory".
48    IS_RELATIVE_FILE_NAME(Filename)
49                                tests whether Filename may be concatenated
50                                to a directory filename.
51    Note: On native Windows, OS/2, DOS, "c:" is neither an absolute nor a
52    relative file name!
53    IS_FILE_NAME_WITH_DIR(Filename)  tests whether Filename contains a device
54                                     or directory specification.
55  */
56 #if defined _WIN32 || defined __CYGWIN__ \
57     || defined __EMX__ || defined __MSDOS__ || defined __DJGPP__
58   /* Native Windows, Cygwin, OS/2, DOS */
59 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
60   /* Internal macro: Tests whether a character is a drive letter.  */
61 # define _IS_DRIVE_LETTER(C) \
62     (((C) >= 'A' && (C) <= 'Z') || ((C) >= 'a' && (C) <= 'z'))
63   /* Help the compiler optimizing it.  This assumes ASCII.  */
64 # undef _IS_DRIVE_LETTER
65 # define _IS_DRIVE_LETTER(C) \
66     (((unsigned int) (C) | ('a' - 'A')) - 'a' <= 'z' - 'a')
67 # define HAS_DEVICE(Filename) \
68     (_IS_DRIVE_LETTER ((Filename)[0]) && (Filename)[1] == ':')
69 # define FILE_SYSTEM_PREFIX_LEN(Filename) (HAS_DEVICE (Filename) ? 2 : 0)
70 # ifdef __CYGWIN__
71 #  define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
72 # else
73    /* On native Windows, OS/2, DOS, the system has the notion of a
74       "current directory" on each drive.  */
75 #  define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 1
76 # endif
77 # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
78 #  define IS_ABSOLUTE_FILE_NAME(Filename) \
79      ISSLASH ((Filename)[FILE_SYSTEM_PREFIX_LEN (Filename)])
80 # else
81 #  define IS_ABSOLUTE_FILE_NAME(Filename) \
82      (ISSLASH ((Filename)[0]) || HAS_DEVICE (Filename))
83 # endif
84 # define IS_RELATIVE_FILE_NAME(Filename) \
85     (! (ISSLASH ((Filename)[0]) || HAS_DEVICE (Filename)))
86 # define IS_FILE_NAME_WITH_DIR(Filename) \
87     (strchr ((Filename), '/') != NULL || strchr ((Filename), '\\') != NULL \
88      || HAS_DEVICE (Filename))
89 #else
90   /* Unix */
91 # define ISSLASH(C) ((C) == '/')
92 # define HAS_DEVICE(Filename) ((void) (Filename), 0)
93 # define FILE_SYSTEM_PREFIX_LEN(Filename) ((void) (Filename), 0)
94 # define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
95 # define IS_ABSOLUTE_FILE_NAME(Filename) ISSLASH ((Filename)[0])
96 # define IS_RELATIVE_FILE_NAME(Filename) (! ISSLASH ((Filename)[0]))
97 # define IS_FILE_NAME_WITH_DIR(Filename) (strchr ((Filename), '/') != NULL)
98 #endif
99 
100 /* Deprecated macros.  For backward compatibility with old users of the
101    'filename' module.  */
102 #define IS_ABSOLUTE_PATH IS_ABSOLUTE_FILE_NAME
103 #define IS_PATH_WITH_DIR IS_FILE_NAME_WITH_DIR
104 
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* _FILENAME_H */
111