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