dirname.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* $OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
  2. /*
  3. * Copyright (c) 1997 Todd C. Miller <[email protected]>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  19. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  20. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef lint
  29. static char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
  30. const char* libtar_compat_dirname_getrcsid() { return rcsid; }
  31. #endif /* not lint */
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <libtar/compat.h>
  35. #include <libtarint/internal.h>
  36. char *
  37. openbsd_dirname(path)
  38. const char *path;
  39. {
  40. static char bname[TAR_MAXPATHLEN];
  41. register const char *endp;
  42. /* Empty or NULL string gets treated as "." */
  43. if (path == NULL || *path == '\0') {
  44. (void)strcpy(bname, ".");
  45. return(bname);
  46. }
  47. /* Strip trailing slashes */
  48. endp = path + strlen(path) - 1;
  49. while (endp > path && *endp == '/')
  50. endp--;
  51. /* Find the start of the dir */
  52. while (endp > path && *endp != '/')
  53. endp--;
  54. /* Either the dir is "/" or there are no slashes */
  55. if (endp == path) {
  56. (void)strcpy(bname, *endp == '/' ? "/" : ".");
  57. return(bname);
  58. } else {
  59. do {
  60. endp--;
  61. } while (endp > path && *endp == '/');
  62. }
  63. if (endp - path + 1 > sizeof(bname)) {
  64. errno = ENAMETOOLONG;
  65. return(NULL);
  66. }
  67. (void)strncpy(bname, path, endp - path + 1);
  68. bname[endp - path + 1] = '\0';
  69. return(bname);
  70. }