dirname.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* dirname - return directory part of PATH.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <libgen.h>
  17. #include <string.h>
  18. char *
  19. dirname (char *path)
  20. {
  21. static const char dot[] = ".";
  22. char *last_slash;
  23. /* Find last '/'. */
  24. last_slash = path != NULL ? strrchr (path, '/') : NULL;
  25. if (last_slash != NULL && last_slash != path && last_slash[1] == '\0')
  26. {
  27. /* Determine whether all remaining characters are slashes. */
  28. char *runp;
  29. for (runp = last_slash; runp != path; --runp)
  30. if (runp[-1] != '/')
  31. break;
  32. /* The '/' is the last character, we have to look further. */
  33. if (runp != path)
  34. last_slash = __memrchr (path, '/', runp - path);
  35. }
  36. if (last_slash != NULL)
  37. {
  38. /* Determine whether all remaining characters are slashes. */
  39. char *runp;
  40. for (runp = last_slash; runp != path; --runp)
  41. if (runp[-1] != '/')
  42. break;
  43. /* Terminate the path. */
  44. if (runp == path)
  45. {
  46. /* The last slash is the first character in the string. We have to
  47. return "/". As a special case we have to return "//" if there
  48. are exactly two slashes at the beginning of the string. See
  49. XBD 4.10 Path Name Resolution for more information. */
  50. if (last_slash == path + 1)
  51. ++last_slash;
  52. else
  53. last_slash = path + 1;
  54. }
  55. else
  56. last_slash = runp;
  57. last_slash[0] = '\0';
  58. }
  59. else
  60. /* This assignment is ill-designed but the XPG specs require to
  61. return a string containing "." in any case no directory part is
  62. found and so a static and constant string is required. */
  63. path = (char *) dot;
  64. return path;
  65. }