basename.c 3.8 KB

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