basename.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* basename.c -- return the last element in a file name
  2. Copyright (C) 1990, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006 Free
  3. Software Foundation, Inc.
  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. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "dirname.h"
  16. #include <string.h>
  17. #include "xalloc.h"
  18. #include "xstrndup.h"
  19. /* Return the address of the last file name component of NAME. If
  20. NAME has no relative file name components because it is a file
  21. system root, return the empty string. */
  22. char *
  23. last_component (char const *name)
  24. {
  25. char const *base = name + FILE_SYSTEM_PREFIX_LEN (name);
  26. char const *p;
  27. bool saw_slash = false;
  28. while (ISSLASH (*base))
  29. base++;
  30. for (p = base; *p; p++)
  31. {
  32. if (ISSLASH (*p))
  33. saw_slash = true;
  34. else if (saw_slash)
  35. {
  36. base = p;
  37. saw_slash = false;
  38. }
  39. }
  40. return (char *) base;
  41. }
  42. /* In general, we can't use the builtin `basename' function if available,
  43. since it has different meanings in different environments.
  44. In some environments the builtin `basename' modifies its argument.
  45. Return the last file name component of NAME, allocated with
  46. xmalloc. On systems with drive letters, a leading "./"
  47. distinguishes relative names that would otherwise look like a drive
  48. letter. Unlike POSIX basename(), NAME cannot be NULL,
  49. base_name("") returns "", and the first trailing slash is not
  50. stripped.
  51. If lstat (NAME) would succeed, then { chdir (dir_name (NAME));
  52. lstat (base_name (NAME)); } will access the same file. Likewise,
  53. if the sequence { chdir (dir_name (NAME));
  54. rename (base_name (NAME), "foo"); } succeeds, you have renamed NAME
  55. to "foo" in the same directory NAME was in. */
  56. char *
  57. base_name (char const *name)
  58. {
  59. char const *base = last_component (name);
  60. size_t length;
  61. /* If there is no last component, then name is a file system root or the
  62. empty string. */
  63. if (! *base)
  64. return xstrndup (name, base_len (name));
  65. /* Collapse a sequence of trailing slashes into one. */
  66. length = base_len (base);
  67. if (ISSLASH (base[length]))
  68. length++;
  69. /* On systems with drive letters, `a/b:c' must return `./b:c' rather
  70. than `b:c' to avoid confusion with a drive letter. On systems
  71. with pure POSIX semantics, this is not an issue. */
  72. if (FILE_SYSTEM_PREFIX_LEN (base))
  73. {
  74. char *p = xmalloc (length + 3);
  75. p[0] = '.';
  76. p[1] = '/';
  77. memcpy (p + 2, base, length);
  78. p[length + 2] = '\0';
  79. return p;
  80. }
  81. /* Finally, copy the basename. */
  82. return xstrndup (base, length);
  83. }
  84. /* Return the length of the basename NAME. Typically NAME is the
  85. value returned by base_name or last_component. Act like strlen
  86. (NAME), except omit all trailing slashes. */
  87. size_t
  88. base_len (char const *name)
  89. {
  90. size_t len;
  91. size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
  92. for (len = strlen (name); 1 < len && ISSLASH (name[len - 1]); len--)
  93. continue;
  94. if (DOUBLE_SLASH_IS_DISTINCT_ROOT && len == 1
  95. && ISSLASH (name[0]) && ISSLASH (name[1]) && ! name[2])
  96. return 2;
  97. if (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && prefix_len
  98. && len == prefix_len && ISSLASH (name[prefix_len]))
  99. return prefix_len + 1;
  100. return len;
  101. }