dirname-lgpl.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* dirname.c -- return all but the last element in a file name
  2. Copyright (C) 1990, 1998, 2000-2001, 2003-2006, 2009-2013 Free Software
  3. 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 <stdlib.h>
  17. #include <string.h>
  18. /* Return the length of the prefix of FILE that will be used by
  19. dir_name. If FILE is in the working directory, this returns zero
  20. even though 'dir_name (FILE)' will return ".". Works properly even
  21. if there are trailing slashes (by effectively ignoring them). */
  22. size_t
  23. dir_len (char const *file)
  24. {
  25. size_t prefix_length = FILE_SYSTEM_PREFIX_LEN (file);
  26. size_t length;
  27. /* Advance prefix_length beyond important leading slashes. */
  28. prefix_length += (prefix_length != 0
  29. ? (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
  30. && ISSLASH (file[prefix_length]))
  31. : (ISSLASH (file[0])
  32. ? ((DOUBLE_SLASH_IS_DISTINCT_ROOT
  33. && ISSLASH (file[1]) && ! ISSLASH (file[2])
  34. ? 2 : 1))
  35. : 0));
  36. /* Strip the basename and any redundant slashes before it. */
  37. for (length = last_component (file) - file;
  38. prefix_length < length; length--)
  39. if (! ISSLASH (file[length - 1]))
  40. break;
  41. return length;
  42. }
  43. /* In general, we can't use the builtin 'dirname' function if available,
  44. since it has different meanings in different environments.
  45. In some environments the builtin 'dirname' modifies its argument.
  46. Return the leading directories part of FILE, allocated with malloc.
  47. Works properly even if there are trailing slashes (by effectively
  48. ignoring them). Return NULL on failure.
  49. If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
  50. lstat (base_name (FILE)); } will access the same file. Likewise,
  51. if the sequence { chdir (dir_name (FILE));
  52. rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
  53. to "foo" in the same directory FILE was in. */
  54. char *
  55. mdir_name (char const *file)
  56. {
  57. size_t length = dir_len (file);
  58. bool append_dot = (length == 0
  59. || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
  60. && length == FILE_SYSTEM_PREFIX_LEN (file)
  61. && file[2] != '\0' && ! ISSLASH (file[2])));
  62. char *dir = malloc (length + append_dot + 1);
  63. if (!dir)
  64. return NULL;
  65. memcpy (dir, file, length);
  66. if (append_dot)
  67. dir[length++] = '.';
  68. dir[length] = '\0';
  69. return dir;
  70. }