Debian Codegraph @

texk/kpathsea/xdirname.c

raw
1 /* xdirname.c: return the directory part of a path.
2
3 Copyright 1999, 2008, 2011, 2016, 2017 Karl Berry.
4 Copyright 2005 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 /* Return directory for NAME. This is "." if NAME contains no directory
20 separators (should never happen for selfdir), else whatever precedes
21 the final directory separator, but with multiple separators stripped.
22 For example, `xdirname ("/foo//bar.baz")' returns "/foo". Always
23 return a new string. */
24
25 #include <kpathsea/config.h>
26 #include <kpathsea/c-pathch.h>
27
28 string
29 xdirname (const_string name)
30 {
31 string ret;
32 unsigned limit = 0, loc;
33 #if defined(WIN32)
34 string p;
35 #endif
36
37 /* Ignore a NULL name. */
38 if (!name)
39 return NULL;
40
41 if (NAME_BEGINS_WITH_DEVICE(name)) {
42 limit = 2;
43 } else if (IS_UNC_NAME(name)) {
44 for (limit = 2; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
45 ;
46 if (name[limit++] && name[limit] && !IS_DIR_SEP (name[limit])) {
47 for (; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
48 ;
49 limit--;
50 } else
51 /* malformed UNC name, backup */
52 limit = 0;
53 }
54
55 #if defined(WIN32) && defined (KPSE_COMPAT_API)
56 for (loc = strlen (name); loc > limit &&
57 (!IS_DIR_SEP (name[loc-1]) ||
58 (loc > 1 && IS_KANJI(name+loc-2))); loc--)
59 ;
60 #else
61 for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
62 ;
63 #endif
64
65 if (loc == limit) {
66 if (limit == 0)
67 ret = xstrdup (".");
68 else if (limit == 2) {
69 ret = (string)xmalloc(4);
70 ret[0] = name[0];
71 ret[1] = name[1];
72 ret[2] = '.';
73 ret[3] = '\0';
74 } else {
75 /* UNC name is "//server/share". */
76 ret = xstrdup (name);
77 }
78 } else {
79 /* If have ///a, must return /, so don't strip off everything. */
80 #if defined(WIN32) && defined (KPSE_COMPAT_API)
81 while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
82 if (loc > 1 && IS_KANJI(name+loc-2))
83 break;
84 loc--;
85 }
86 #else
87 while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
88 loc--;
89 }
90 #endif
91 ret = (string)xmalloc(loc+1);
92 strncpy(ret, name, loc);
93 ret[loc] = '\0';
94 }
95
96 #if defined(WIN32)
97 for (p = ret; *p; p++) {
98 if (*p == '\\')
99 *p = '/';
100 #if defined (KPSE_COMPAT_API)
101 else if (IS_KANJI(p))
102 p++;
103 #endif
104 }
105 #endif
106
107 return ret;
108 }