1 /* xdirname.c: return the directory part of a path.
3 Copyright 2005 Olaf Weber.
4 Copyright 1999 Karl Berry.
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
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Return directory for NAME. This is "." if NAME contains no directory
22 separators (should never happen for selfdir), else whatever precedes
23 the final directory separator, but with multiple separators stripped.
24 For example, `xdirname ("/foo//bar.baz")' returns "/foo". Always
25 return a new string. */
27 #include <kpathsea/config.h>
28 #include <kpathsea/c-pathch.h>
31 xdirname P1C(const_string, name)
34 unsigned limit = 0, loc;
36 /* Ignore a NULL name. */
40 if (NAME_BEGINS_WITH_DEVICE(name)) {
42 #if defined(WIN32) || defined(__CYGWIN__)
43 } else if (IS_UNC_NAME(name)) {
44 for (limit = 2; name[limit] && !IS_DIR_SEP(name[limit]); limit++)
47 for (limit++ ; name[limit] && !IS_DIR_SEP(name[limit]); limit++)
51 /* malformed UNC name, backup */
57 for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
60 if (loc == limit && limit > 0) {
62 ret = (string)xmalloc(limit + 2);
68 ret = (string)xmalloc(limit + 2);
72 /* If have ///a, must return /, so don't strip off everything. */
73 while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
76 ret = (string)xmalloc(loc+1);
77 strncpy(ret, name, loc);
87 "\\\\neuromancer\\fptex\\bin\\win32\\kpsewhich.exe",
88 "\\\\neuromancer\\fptex\\win32\\kpsewhich.exe",
89 "\\\\neuromancer\\fptex\\kpsewhich.exe",
90 "\\\\neuromancer\\kpsewhich.exe",
91 "p:\\bin\\win32\\kpsewhich.exe",
92 "p:\\win32\\kpsewhich.exe",
94 "p:bin\\win32\\kpsewhich.exe",
95 "p:win32\\kpsewhich.exe",
98 "/usr/bin/win32/kpsewhich.exe",
99 "/usr/bin/kpsewhich.exe",
100 "/usr/kpsewhich.exe",
101 "///usr/kpsewhich.exe",
109 for (p = tab; *p; p++)
110 printf("name %s, dirname %s\n", *p, xdirname(*p));
118 standalone-compile-command: "gcc -g -I. -I.. -DTEST variable.c kpathsea.a"