/xbmc/screensavers/rsxs-0.9/lib/dirname.c

http://github.com/xbmc/xbmc · C · 87 lines · 40 code · 10 blank · 37 comment · 14 complexity · f3c88cc2a2ee479a34e02d0ad846dcaa MD5 · raw file

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