filename.h 4.6 KB

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