1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 ** Pathname subroutines.
9 #if defined(FREEBSD) || defined(BSDI) || defined(DARWIN)
10 #include <sys/types.h>
19 #include <sys/types.h>
22 #ifdef USE_REENTRANT_LIBC
24 #endif /* USE_REENTRANT_LIBC */
29 fail(char *format, ...)
34 #ifdef USE_REENTRANT_LIBC
39 fprintf(stderr, "%s: ", program);
41 vfprintf(stderr, format, ap);
45 #ifdef USE_REENTRANT_LIBC
47 fprintf(stderr, ": %s", r_strerror_r);
49 fprintf(stderr, ": %s", strerror(errno));
59 getcomponent(char *path, char *name)
68 } while (*path != '/' && *path != '\0');
77 /* The static buffer in Unixware's readdir is too small. */
78 struct dirent * readdir(DIR *d)
80 static struct dirent *buf = NULL;
81 #define MAX_PATH_LEN 1024
84 buf = (struct dirent *)xmalloc(sizeof(struct dirent) + MAX_PATH_LEN) ;
85 return readdir_r(d, buf);
89 /* APPARENT BUG - ignores argument "dir", uses ".." instead. */
91 ino2name(ino_t ino, char *dir)
97 dp = opendir(".."); /* XXX */
99 fail("cannot read parent directory");
101 if (!(ep = readdir(dp)))
102 fail("cannot find current directory");
103 if (ep->d_ino == ino)
106 name = xstrdup(ep->d_name);
117 fail("attempted to allocate %u bytes", size);
120 fail("cannot allocate %u bytes", size);
128 fail("Null pointer or empty string passed to xstrdup()");
129 return strcpy((char*)xmalloc(strlen(s) + 1), s);
133 xbasename(char *path)
137 if (!path || !path[0])
138 fail("Null pointer or empty string passed to xbasename()");
139 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
141 if (!cp) return path;
149 fail("Null pointer or empty string passed to xchdir()");
151 fail("cannot change directory to %s", dir);
155 relatepaths(char *from, char *to, char *outpath)
161 assert(*from == '/' && *to == '/');
162 if (!from || *from != '/')
163 fail("relatepaths: from path does not start with /");
164 if (!to || *to != '/')
165 fail("relatepaths: to path does not start with /");
167 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
170 while (cp[-1] != '/')
173 /* closest common ancestor is /, so use full pathname */
174 len = strlen(strcpy(outpath, to));
175 if (outpath[len] != '/') {
176 outpath[len++] = '/';
181 while ((cp2 = getcomponent(cp2, buf)) != 0) {
182 strcpy(outpath + len, "../");
185 while ((cp = getcomponent(cp, buf)) != 0) {
186 sprintf(outpath + len, "%s/", buf);
187 len += strlen(outpath + len);
194 reversepath(char *inpath, char *name, int len, char *outpath)
200 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
202 while ((cp2 = getcomponent(cp2, buf)) != 0) {
203 if (strcmp(buf, ".") == 0)
205 if (strcmp(buf, "..") == 0) {
206 if (stat(".", &sb) < 0)
207 fail("cannot stat current directory");
208 name = ino2name(sb.st_ino, "..");
217 strncpy(cp, "../", 3);
225 diagnosePath(const char * path)
233 if (!path || !path[0])
234 fail("Null pointer or empty string passed to mkdirs()");
235 myPath = strdup(path);
237 fail("strdup() failed!");
239 rv = lstat(myPath, &sb);
242 } else if (S_ISLNK(sb.st_mode)) {
243 rv = readlink(myPath, buf, sizeof buf);
250 fprintf(stderr, "%s is a link to %s\n", myPath, buf);
251 } else if (S_ISDIR(sb.st_mode)) {
252 fprintf(stderr, "%s is a directory\n", myPath);
253 rv = access(myPath, X_OK);
255 fprintf(stderr, "%s: no search permission\n", myPath);
258 fprintf(stderr, "%s is a file !?!\n", myPath);
259 rv = access(myPath, F_OK);
261 fprintf(stderr, "%s does not exist\n", myPath);
265 /* chop path off one level. */
266 slash = strrchr(myPath, '/');
268 slash = strrchr(myPath, '\\');