basename.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  18. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  19. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*!
  28. * \file fs/basename.c
  29. * \brief Returns last component of a pathname.
  30. *
  31. * \verbatim
  32. *
  33. * $Log$
  34. * Revision 1.2 2009/02/13 14:52:05 haraldkipp
  35. * Include memdebug.h for heap management debugging support.
  36. *
  37. * Revision 1.1 2006/08/01 07:42:56 haraldkipp
  38. * New functions extract last component and parent directory from pathnames.
  39. *
  40. *
  41. * \endverbatim
  42. */
  43. #include <errno.h>
  44. #include <libgen.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <memdebug.h>
  48. #if !defined(MAXPATHLEN)
  49. #define MAXPATHLEN 256
  50. #endif
  51. /*!
  52. * \brief Return the last component from a given pathname.
  53. *
  54. * Trailing slashes are removed.
  55. *
  56. * \note This function returns a pointer to internal static storage space
  57. * that will be overwritten by subsequent calls.
  58. *
  59. * \param path Pointer to the pathname.
  60. *
  61. * \return Pointer to the last component. If the specified pathname contains
  62. * slashes only, a single slash is returned. If the pathname is empty
  63. * or a NULL pointer, then a single dot is returned. In case of any
  64. * error, a NULL pointer is returned and the global variable errno is
  65. * set to indicate the error.
  66. */
  67. char *basename(const char *path)
  68. {
  69. static char *bname = NULL;
  70. const char *endp, *startp;
  71. if (bname == NULL) {
  72. bname = (char *) malloc(MAXPATHLEN);
  73. if (bname == NULL)
  74. return (NULL);
  75. }
  76. /* Empty or NULL string gets treated as "." */
  77. if (path == NULL || *path == '\0') {
  78. strcpy(bname, ".");
  79. return (bname);
  80. }
  81. /* Strip trailing slashes */
  82. endp = path + strlen(path) - 1;
  83. while (endp > path && *endp == '/')
  84. endp--;
  85. /* All slashes becomes "/" */
  86. if (endp == path && *endp == '/') {
  87. strcpy(bname, "/");
  88. return (bname);
  89. }
  90. /* Find the start of the base */
  91. startp = endp;
  92. while (startp > path && *(startp - 1) != '/')
  93. startp--;
  94. if ((endp - startp) + 2 >= MAXPATHLEN) {
  95. errno = ENAMETOOLONG;
  96. return (NULL);
  97. }
  98. strcpy(bname, startp);
  99. bname[(endp - startp) + 1] = 0;
  100. return (bname);
  101. }