Wed May 15 10:14:14 CDT 1996 Rob Tulloh <tulloh@tivoli.com>
[make.git] / w32 / pathstuff.c
blobe5011f850d7d122c952fe725dcd2ffaf6ce91032
1 #include <string.h>
2 #include <stdlib.h>
3 #include "make.h"
5 /*
6 * Convert delimiter separated path to Canonical format.
7 */
8 char *
9 convert_Path_to_win32(char *Path, char to_delim)
11 char *etok; /* token separator for old Path */
12 char *p; /* points to element of old Path */
14 /* is this a multi-element Path ? */
15 for (p = Path, etok = strpbrk(p, ":;");
16 etok;
17 etok = strpbrk(p, ":;"))
18 if ((etok - p) == 1) {
19 if (*(etok - 1) == ';' ||
20 *(etok - 1) == ':') {
21 etok[-1] = to_delim;
22 etok[0] = to_delim;
23 p = ++etok;
24 continue; /* ignore empty bucket */
25 } else if (etok = strpbrk(etok+1, ":;")) {
26 /* found one to count, handle drive letter */
27 *etok = to_delim;
28 p = ++etok;
29 } else
30 /* all finished, force abort */
31 p += strlen(p);
32 } else {
33 /* found another one, no drive letter */
34 *etok = to_delim;
35 p = ++etok;
38 #if 0
39 /* convert to backward slashes */
40 for (p = Path, p = strchr(p, '/'); p; p = strchr(p, '/'))
41 *p = '\\';
42 #endif
43 return Path;
47 * Convert to forward slashes. Resolve to full pathname optionally
49 char *
50 w32ify(char *filename, int resolve)
52 static char w32_path[FILENAME_MAX];
53 char *p;
55 if (resolve)
56 _fullpath(w32_path, filename, sizeof (w32_path));
57 else
58 strncpy(w32_path, filename, sizeof (w32_path));
60 for (p = w32_path; p && *p; p++)
61 if (*p == '\\')
62 *p = '/';
64 return w32_path;
67 char *
68 getcwd_fs(char* buf, int len)
70 char *p;
72 if (p = getcwd(buf, len)) {
73 char *q = w32ify(buf, 0);
74 strncpy(buf, q, len);
77 return p;
80 #ifdef unused
82 * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
83 * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
84 * _NutPathToNutc() fails to convert, just return the path we were handed
85 * and assume the caller will know what to do with it (It was probably
86 * a mistake to try and convert it anyway due to some of the bizarre things
87 * that might look like pathnames in makefiles).
89 char *
90 convert_path_to_nutc(char *path)
92 int count; /* count of path elements */
93 char *nutc_path; /* new NutC path */
94 int nutc_path_len; /* length of buffer to allocate for new path */
95 char *pathp; /* pointer to nutc_path used to build it */
96 char *etok; /* token separator for old path */
97 char *p; /* points to element of old path */
98 char sep; /* what flavor of separator used in old path */
99 char *rval;
101 /* is this a multi-element path ? */
102 for (p = path, etok = strpbrk(p, ":;"), count = 0;
103 etok;
104 etok = strpbrk(p, ":;"))
105 if ((etok - p) == 1) {
106 if (*(etok - 1) == ';' ||
107 *(etok - 1) == ':') {
108 p = ++etok;
109 continue; /* ignore empty bucket */
110 } else if (etok = strpbrk(etok+1, ":;"))
111 /* found one to count, handle drive letter */
112 p = ++etok, count++;
113 else
114 /* all finished, force abort */
115 p += strlen(p);
116 } else
117 /* found another one, no drive letter */
118 p = ++etok, count++;
120 if (count) {
121 count++; /* x1;x2;x3 <- need to count x3 */
124 * Hazard a guess on how big the buffer needs to be.
125 * We have to convert things like c:/foo to /c=/foo.
127 nutc_path_len = strlen(path) + (count*2) + 1;
128 nutc_path = xmalloc(nutc_path_len);
129 pathp = nutc_path;
130 *pathp = '\0';
133 * Loop through PATH and convert one elemnt of the path at at
134 * a time. Single file pathnames will fail this and fall
135 * to the logic below loop.
137 for (p = path, etok = strpbrk(p, ":;");
138 etok;
139 etok = strpbrk(p, ":;")) {
141 /* don't trip up on device specifiers or empty path slots */
142 if ((etok - p) == 1)
143 if (*(etok - 1) == ';' ||
144 *(etok - 1) == ':') {
145 p = ++etok;
146 continue;
147 } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
148 break; /* thing found was a WIN32 pathname */
150 /* save separator */
151 sep = *etok;
153 /* terminate the current path element -- temporarily */
154 *etok = '\0';
156 #ifdef __NUTC__
157 /* convert to NutC format */
158 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
159 free(nutc_path);
160 rval = savestring(path, strlen(path));
161 return rval;
163 #else
164 *pathp++ = '/';
165 *pathp++ = p[0];
166 *pathp++ = '=';
167 *pathp++ = '/';
168 strcpy(pathp, &p[2]);
169 #endif
171 pathp += strlen(pathp);
172 *pathp++ = ':'; /* use Unix style path separtor for new path */
173 *pathp = '\0'; /* make sure we are null terminaed */
175 /* restore path separator */
176 *etok = sep;
178 /* point p to first char of next path element */
179 p = ++etok;
182 } else {
183 nutc_path_len = strlen(path) + 3;
184 nutc_path = xmalloc(nutc_path_len);
185 pathp = nutc_path;
186 *pathp = '\0';
187 p = path;
191 * OK, here we handle the last element in PATH (e.g. c of a;b;c)
192 * or the path was a single filename and will be converted
193 * here. Note, testing p here assures that we don't trip up
194 * on paths like a;b; which have trailing delimiter followed by
195 * nothing.
197 if (*p != '\0') {
198 #ifdef __NUTC__
199 if (_NutPathToNutc(p, pathp, 0) == FALSE) {
200 free(nutc_path);
201 rval = savestring(path, strlen(path));
202 return rval;
204 #else
205 *pathp++ = '/';
206 *pathp++ = p[0];
207 *pathp++ = '=';
208 *pathp++ = '/';
209 strcpy(pathp, &p[2]);
210 #endif
211 } else
212 *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
214 rval = savestring(nutc_path, strlen(nutc_path));
215 free(nutc_path);
216 return rval;
219 #endif