tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / xdirname.c
1 /* xdirname.c: return the directory part of a path.
2
3     Copyright 2005 Olaf Weber.
4     Copyright 1999 Karl Berry.
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
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
19 */
20
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.  */
26
27 #include <kpathsea/config.h>
28 #include <kpathsea/c-pathch.h>
29
30 string
31 xdirname P1C(const_string, name)
32 {
33     string ret;
34     unsigned limit = 0, loc;
35     
36     /* Ignore a NULL name. */
37     if (!name)
38         return NULL;
39     
40     if (NAME_BEGINS_WITH_DEVICE(name)) {
41         limit = 2;
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++)
45             ;
46         if (name[limit]) {
47             for (limit++ ; name[limit] && !IS_DIR_SEP(name[limit]); limit++)
48                 ;
49             limit--;
50         } else {
51             /* malformed UNC name, backup */
52             limit = 2;
53         }
54 #endif
55     }
56     
57     for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
58         ;
59     
60     if (loc == limit && limit > 0) {
61         if (limit == 2) {
62             ret = (string)xmalloc(limit + 2);
63             ret[0] = name[0];
64             ret[1] = name[1];
65             ret[2] = '.';
66             ret[3] = '\0';
67         } else {
68             ret = (string)xmalloc(limit + 2);
69             strcpy(ret, name);
70         }
71     } else {
72         /* If have ///a, must return /, so don't strip off everything.  */
73         while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
74             loc--;
75         }
76         ret = (string)xmalloc(loc+1);
77         strncpy(ret, name, loc);
78         ret[loc] = '\0';
79     }
80     
81     return ret;
82 }
83 \f
84 #ifdef TEST
85
86 char *tab[] = {
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",
93     "p:\\kpsewhich.exe",
94     "p:bin\\win32\\kpsewhich.exe",
95     "p:win32\\kpsewhich.exe",
96     "p:kpsewhich.exe",
97     "p:///kpsewhich.exe",
98     "/usr/bin/win32/kpsewhich.exe",
99     "/usr/bin/kpsewhich.exe",
100     "/usr/kpsewhich.exe",
101     "///usr/kpsewhich.exe",
102     "///kpsewhich.exe",
103     NULL 
104 };
105
106 int main()
107 {
108     char **p;
109     for (p = tab; *p; p++)
110         printf("name %s, dirname %s\n", *p, xdirname(*p));
111     return 0;
112 }
113 #endif /* TEST */
114
115
116 /*
117 Local variables:
118 standalone-compile-command: "gcc -g -I. -I.. -DTEST variable.c kpathsea.a"
119 End:
120 */
121