Imported Upstream version 3.82
[platform/upstream/make.git] / w32 / pathstuff.c
1 /* Path conversion for Windows pathnames.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 This file is part of GNU Make.
5
6 GNU Make is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "make.h"
19 #include <string.h>
20 #include <stdlib.h>
21 #include "pathstuff.h"
22
23 /*
24  * Convert delimiter separated vpath to Canonical format.
25  */
26 char *
27 convert_vpath_to_windows32(char *Path, char to_delim)
28 {
29     char *etok;            /* token separator for old Path */
30
31         /*
32          * Convert all spaces to delimiters. Note that pathnames which
33          * contain blanks get trounced here. Use 8.3 format as a workaround.
34          */
35         for (etok = Path; etok && *etok; etok++)
36                 if (isblank ((unsigned char) *etok))
37                         *etok = to_delim;
38
39         return (convert_Path_to_windows32(Path, to_delim));
40 }
41
42 /*
43  * Convert delimiter separated path to Canonical format.
44  */
45 char *
46 convert_Path_to_windows32(char *Path, char to_delim)
47 {
48     char *etok;            /* token separator for old Path */
49     char *p;            /* points to element of old Path */
50
51     /* is this a multi-element Path ? */
52     /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
53        delimiters as well, as a way to handle quoted directories in
54        PATH?  */
55     for (p = Path, etok = strpbrk(p, ":;");
56          etok;
57          etok = strpbrk(p, ":;"))
58         if ((etok - p) == 1) {
59             if (*(etok - 1) == ';' ||
60                 *(etok - 1) == ':') {
61                 etok[-1] = to_delim;
62                 etok[0] = to_delim;
63                 p = ++etok;
64                 continue;    /* ignore empty bucket */
65             } else if (!isalpha ((unsigned char) *p)) {
66                 /* found one to count, handle things like '.' */
67                 *etok = to_delim;
68                 p = ++etok;
69             } else if ((*etok == ':') && (etok = strpbrk(etok+1, ":;"))) {
70                 /* found one to count, handle drive letter */
71                 *etok = to_delim;
72                 p = ++etok;
73             } else
74                 /* all finished, force abort */
75                 p += strlen(p);
76         } else if (*p == '"') { /* a quoted directory */
77             for (p++; *p && *p != '"'; p++) /* skip quoted part */
78                 ;
79             etok = strpbrk(p, ":;");        /* find next delimiter */
80             if (etok) {
81                 *etok = to_delim;
82                 p = ++etok;
83             } else
84                 p += strlen(p);
85         } else {
86             /* found another one, no drive letter */
87             *etok = to_delim;
88             p = ++etok;
89         }
90
91     return Path;
92 }
93
94 /*
95  * Convert to forward slashes. Resolve to full pathname optionally
96  */
97 char *
98 w32ify(const char *filename, int resolve)
99 {
100     static char w32_path[FILENAME_MAX];
101     char *p;
102
103     if (resolve)
104         _fullpath(w32_path, filename, sizeof (w32_path));
105     else
106         strncpy(w32_path, filename, sizeof (w32_path));
107
108     for (p = w32_path; p && *p; p++)
109         if (*p == '\\')
110             *p = '/';
111
112     return w32_path;
113 }
114
115 char *
116 getcwd_fs(char* buf, int len)
117 {
118         char *p = getcwd(buf, len);
119
120         if (p) {
121                 char *q = w32ify(buf, 0);
122                 strncpy(buf, q, len);
123         }
124
125         return p;
126 }
127
128 #ifdef unused
129 /*
130  * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
131  * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
132  * _NutPathToNutc() fails to convert, just return the path we were handed
133  * and assume the caller will know what to do with it (It was probably
134  * a mistake to try and convert it anyway due to some of the bizarre things
135  * that might look like pathnames in makefiles).
136  */
137 char *
138 convert_path_to_nutc(char *path)
139 {
140     int  count;            /* count of path elements */
141     char *nutc_path;     /* new NutC path */
142     int  nutc_path_len;    /* length of buffer to allocate for new path */
143     char *pathp;        /* pointer to nutc_path used to build it */
144     char *etok;            /* token separator for old path */
145     char *p;            /* points to element of old path */
146     char sep;            /* what flavor of separator used in old path */
147     char *rval;
148
149     /* is this a multi-element path ? */
150     for (p = path, etok = strpbrk(p, ":;"), count = 0;
151          etok;
152          etok = strpbrk(p, ":;"))
153         if ((etok - p) == 1) {
154             if (*(etok - 1) == ';' ||
155                 *(etok - 1) == ':') {
156                 p = ++etok;
157                 continue;    /* ignore empty bucket */
158             } else if (etok = strpbrk(etok+1, ":;"))
159                 /* found one to count, handle drive letter */
160                 p = ++etok, count++;
161             else
162                 /* all finished, force abort */
163                 p += strlen(p);
164         } else
165             /* found another one, no drive letter */
166             p = ++etok, count++;
167
168     if (count) {
169         count++;    /* x1;x2;x3 <- need to count x3 */
170
171         /*
172          * Hazard a guess on how big the buffer needs to be.
173          * We have to convert things like c:/foo to /c=/foo.
174          */
175         nutc_path_len = strlen(path) + (count*2) + 1;
176         nutc_path = xmalloc(nutc_path_len);
177         pathp = nutc_path;
178         *pathp = '\0';
179
180         /*
181          * Loop through PATH and convert one elemnt of the path at at
182          * a time. Single file pathnames will fail this and fall
183          * to the logic below loop.
184          */
185         for (p = path, etok = strpbrk(p, ":;");
186              etok;
187              etok = strpbrk(p, ":;")) {
188
189             /* don't trip up on device specifiers or empty path slots */
190             if ((etok - p) == 1)
191                 if (*(etok - 1) == ';' ||
192                     *(etok - 1) == ':') {
193                     p = ++etok;
194                     continue;
195                 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
196                     break;    /* thing found was a WINDOWS32 pathname */
197
198             /* save separator */
199             sep = *etok;
200
201             /* terminate the current path element -- temporarily */
202             *etok = '\0';
203
204 #ifdef __NUTC__
205             /* convert to NutC format */
206             if (_NutPathToNutc(p, pathp, 0) == FALSE) {
207                 free(nutc_path);
208                 rval = savestring(path, strlen(path));
209                 return rval;
210             }
211 #else
212             *pathp++ = '/';
213             *pathp++ = p[0];
214             *pathp++ = '=';
215             *pathp++ = '/';
216             strcpy(pathp, &p[2]);
217 #endif
218
219             pathp += strlen(pathp);
220             *pathp++ = ':';     /* use Unix style path separtor for new path */
221             *pathp   = '\0'; /* make sure we are null terminaed */
222
223             /* restore path separator */
224             *etok = sep;
225
226             /* point p to first char of next path element */
227             p = ++etok;
228
229         }
230     } else {
231         nutc_path_len = strlen(path) + 3;
232         nutc_path = xmalloc(nutc_path_len);
233         pathp = nutc_path;
234         *pathp = '\0';
235         p = path;
236     }
237
238     /*
239       * OK, here we handle the last element in PATH (e.g. c of a;b;c)
240      * or the path was a single filename and will be converted
241      * here. Note, testing p here assures that we don't trip up
242      * on paths like a;b; which have trailing delimiter followed by
243      * nothing.
244      */
245     if (*p != '\0') {
246 #ifdef __NUTC__
247         if (_NutPathToNutc(p, pathp, 0) == FALSE) {
248             free(nutc_path);
249             rval = savestring(path, strlen(path));
250             return rval;
251         }
252 #else
253         *pathp++ = '/';
254         *pathp++ = p[0];
255         *pathp++ = '=';
256         *pathp++ = '/';
257         strcpy(pathp, &p[2]);
258 #endif
259     } else
260         *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
261
262     rval = savestring(nutc_path, strlen(nutc_path));
263     free(nutc_path);
264     return rval;
265 }
266
267 #endif