beta-0.89.2
[luatex.git] / source / texk / kpathsea / xdirname.c
blobc5b38bae8910833b120c3ddcc6316c08238abaa3
1 /* xdirname.c: return the directory part of a path.
3 Copyright 1999, 2008, 2011 Karl Berry.
4 Copyright 2005 Olaf Weber.
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.
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.
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/>. */
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. */
25 #include <kpathsea/config.h>
26 #include <kpathsea/c-pathch.h>
28 string
29 xdirname (const_string name)
31 string ret;
32 unsigned limit = 0, loc;
33 #if defined(WIN32)
34 string p;
35 unsigned i, j;
36 #endif
38 /* Ignore a NULL name. */
39 if (!name)
40 return NULL;
42 if (NAME_BEGINS_WITH_DEVICE(name)) {
43 limit = 2;
44 } else if (IS_UNC_NAME(name)) {
45 for (limit = 2; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
46 #if defined(WIN32)
47 if (IS_KANJI(name+limit)) limit++
48 #endif
50 if (name[limit++] && name[limit] && !IS_DIR_SEP (name[limit])) {
51 for (; name[limit] && !IS_DIR_SEP (name[limit]); limit++)
52 #if defined(WIN32)
53 if (IS_KANJI(name+limit)) limit++
54 #endif
56 limit--;
57 } else
58 /* malformed UNC name, backup */
59 limit = 0;
62 #if defined(WIN32)
63 j = loc = limit;
64 if (j > 2) j++;
65 for (i = j; name[i]; i++) {
66 if (IS_DIR_SEP (name[i])) {
67 j = i;
68 for (i++; IS_DIR_SEP (name[i]); i++)
70 loc = i + 1;
71 } else if (IS_KANJI(name+i)) i++;
73 #else
74 for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
76 #endif
78 if (loc == limit) {
79 if (limit == 0)
80 ret = xstrdup (".");
81 else if (limit == 2) {
82 ret = (string)xmalloc(4);
83 ret[0] = name[0];
84 ret[1] = name[1];
85 ret[2] = '.';
86 ret[3] = '\0';
87 } else {
88 /* UNC name is "//server/share". */
89 ret = xstrdup (name);
91 } else {
92 /* If have ///a, must return /, so don't strip off everything. */
93 #if defined(WIN32)
94 loc = j;
95 if (loc == limit && IS_DIR_SEP (name[loc])) loc++;
96 #else
97 while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
98 loc--;
100 #endif
101 ret = (string)xmalloc(loc+1);
102 strncpy(ret, name, loc);
103 ret[loc] = '\0';
106 #if defined(WIN32)
107 for (p = ret; *p; p++) {
108 if (*p == '\\')
109 *p = '/';
110 else if (IS_KANJI(p))
111 p++;
113 #endif
115 return ret;