ldirname.c revision 1.1 1 /* Libiberty dirname. Like dirname, but is not overridden by the
2 system C library.
3 Copyright (C) 2025 Free Software Foundation, Inc.
4
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 /*
22
23 @deftypefn Replacement {char*} ldirname (const char *@var{name})
24
25 Given a pointer to a string containing a typical pathname
26 (@samp{/usr/src/cmd/ls/ls.c} for example), returns a string containing the
27 passed string up to, but not including, the final directory separator.
28
29 If the given pathname doesn't contain a directory separator then this funtion
30 returns the empty string; this includes an empty given pathname. @code{NULL}
31 is returned on memory allocation error.
32
33 @end deftypefn
34
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 #include "ansidecl.h"
41 #include "libiberty.h"
42 #include "safe-ctype.h"
43 #include "filenames.h"
44
45 /* For malloc. */
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49
50 /* For memcpy. */
51 # if HAVE_STRING_H
52 # include <string.h>
53 # else
54 # if HAVE_STRINGS_H
55 # include <strings.h>
56 # endif
57 # endif
58
59 #define LDIRNAME(FPREFIX,DIRSEP) \
60 char *FPREFIX##_ldirname (const char *name) \
61 { \
62 /* Note that lbasename guarantees that the returned */ \
63 /* pointer lies within the passed string. */ \
64 const char *basename = FPREFIX##_lbasename (name); \
65 size_t size = basename - name; \
66 char *res = NULL; \
67 \
68 res = (char*) malloc (size + 1); \
69 if (res != NULL) \
70 { \
71 if (size > 0) \
72 { \
73 if (IS_DIR_SEPARATOR_1 ((DIRSEP),name[size - 1])) \
74 size -= 1; \
75 memcpy (res, name, size); \
76 } \
77 res[size] = '\0'; \
78 } \
79 \
80 return res; \
81 }
82
83 LDIRNAME(dos,1)
84 LDIRNAME(unix,0)
85
86 char *
87 ldirname (const char *name)
88 {
89 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
90 return dos_ldirname (name);
91 #else
92 return unix_ldirname (name);
93 #endif
94 }
95