Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | ** Pathname subroutines. |
michael@0 | 7 | */ |
michael@0 | 8 | #include <assert.h> |
michael@0 | 9 | #if defined(FREEBSD) || defined(BSDI) || defined(DARWIN) |
michael@0 | 10 | #include <sys/types.h> |
michael@0 | 11 | #endif /* FREEBSD */ |
michael@0 | 12 | #include <dirent.h> |
michael@0 | 13 | #include <errno.h> |
michael@0 | 14 | #include <stdarg.h> |
michael@0 | 15 | #include <stdio.h> |
michael@0 | 16 | #include <stdlib.h> |
michael@0 | 17 | #include <string.h> |
michael@0 | 18 | #include <unistd.h> |
michael@0 | 19 | #include <sys/types.h> |
michael@0 | 20 | #include <sys/stat.h> |
michael@0 | 21 | #include "pathsub.h" |
michael@0 | 22 | #ifdef USE_REENTRANT_LIBC |
michael@0 | 23 | #include "libc_r.h" |
michael@0 | 24 | #endif /* USE_REENTRANT_LIBC */ |
michael@0 | 25 | |
michael@0 | 26 | char *program; |
michael@0 | 27 | |
michael@0 | 28 | void |
michael@0 | 29 | fail(char *format, ...) |
michael@0 | 30 | { |
michael@0 | 31 | int error; |
michael@0 | 32 | va_list ap; |
michael@0 | 33 | |
michael@0 | 34 | #ifdef USE_REENTRANT_LIBC |
michael@0 | 35 | R_STRERROR_INIT_R(); |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | error = errno; |
michael@0 | 39 | fprintf(stderr, "%s: ", program); |
michael@0 | 40 | va_start(ap, format); |
michael@0 | 41 | vfprintf(stderr, format, ap); |
michael@0 | 42 | va_end(ap); |
michael@0 | 43 | if (error) { |
michael@0 | 44 | |
michael@0 | 45 | #ifdef USE_REENTRANT_LIBC |
michael@0 | 46 | R_STRERROR_R(errno); |
michael@0 | 47 | fprintf(stderr, ": %s", r_strerror_r); |
michael@0 | 48 | #else |
michael@0 | 49 | fprintf(stderr, ": %s", strerror(errno)); |
michael@0 | 50 | #endif |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | putc('\n', stderr); |
michael@0 | 54 | abort(); |
michael@0 | 55 | exit(1); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | char * |
michael@0 | 59 | getcomponent(char *path, char *name) |
michael@0 | 60 | { |
michael@0 | 61 | if (*path == '\0') |
michael@0 | 62 | return 0; |
michael@0 | 63 | if (*path == '/') { |
michael@0 | 64 | *name++ = '/'; |
michael@0 | 65 | } else { |
michael@0 | 66 | do { |
michael@0 | 67 | *name++ = *path++; |
michael@0 | 68 | } while (*path != '/' && *path != '\0'); |
michael@0 | 69 | } |
michael@0 | 70 | *name = '\0'; |
michael@0 | 71 | while (*path == '/') |
michael@0 | 72 | path++; |
michael@0 | 73 | return path; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | #ifdef UNIXWARE |
michael@0 | 77 | /* The static buffer in Unixware's readdir is too small. */ |
michael@0 | 78 | struct dirent * readdir(DIR *d) |
michael@0 | 79 | { |
michael@0 | 80 | static struct dirent *buf = NULL; |
michael@0 | 81 | #define MAX_PATH_LEN 1024 |
michael@0 | 82 | |
michael@0 | 83 | if (buf == NULL) |
michael@0 | 84 | buf = (struct dirent *)xmalloc(sizeof(struct dirent) + MAX_PATH_LEN) ; |
michael@0 | 85 | return readdir_r(d, buf); |
michael@0 | 86 | } |
michael@0 | 87 | #endif |
michael@0 | 88 | |
michael@0 | 89 | /* APPARENT BUG - ignores argument "dir", uses ".." instead. */ |
michael@0 | 90 | char * |
michael@0 | 91 | ino2name(ino_t ino, char *dir) |
michael@0 | 92 | { |
michael@0 | 93 | DIR *dp; |
michael@0 | 94 | struct dirent *ep; |
michael@0 | 95 | char *name; |
michael@0 | 96 | |
michael@0 | 97 | dp = opendir(".."); /* XXX */ |
michael@0 | 98 | if (!dp) |
michael@0 | 99 | fail("cannot read parent directory"); |
michael@0 | 100 | for (;;) { |
michael@0 | 101 | if (!(ep = readdir(dp))) |
michael@0 | 102 | fail("cannot find current directory"); |
michael@0 | 103 | if (ep->d_ino == ino) |
michael@0 | 104 | break; |
michael@0 | 105 | } |
michael@0 | 106 | name = xstrdup(ep->d_name); |
michael@0 | 107 | closedir(dp); |
michael@0 | 108 | return name; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | void * |
michael@0 | 112 | xmalloc(size_t size) |
michael@0 | 113 | { |
michael@0 | 114 | void *p; |
michael@0 | 115 | |
michael@0 | 116 | if (size <= 0) |
michael@0 | 117 | fail("attempted to allocate %u bytes", size); |
michael@0 | 118 | p = malloc(size); |
michael@0 | 119 | if (!p) |
michael@0 | 120 | fail("cannot allocate %u bytes", size); |
michael@0 | 121 | return p; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | char * |
michael@0 | 125 | xstrdup(char *s) |
michael@0 | 126 | { |
michael@0 | 127 | if (!s || !s[0]) |
michael@0 | 128 | fail("Null pointer or empty string passed to xstrdup()"); |
michael@0 | 129 | return strcpy((char*)xmalloc(strlen(s) + 1), s); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | char * |
michael@0 | 133 | xbasename(char *path) |
michael@0 | 134 | { |
michael@0 | 135 | char *cp; |
michael@0 | 136 | |
michael@0 | 137 | if (!path || !path[0]) |
michael@0 | 138 | fail("Null pointer or empty string passed to xbasename()"); |
michael@0 | 139 | while ((cp = strrchr(path, '/')) && cp[1] == '\0') |
michael@0 | 140 | *cp = '\0'; |
michael@0 | 141 | if (!cp) return path; |
michael@0 | 142 | return cp + 1; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | void |
michael@0 | 146 | xchdir(char *dir) |
michael@0 | 147 | { |
michael@0 | 148 | if (!dir || !dir[0]) |
michael@0 | 149 | fail("Null pointer or empty string passed to xchdir()"); |
michael@0 | 150 | if (chdir(dir) < 0) |
michael@0 | 151 | fail("cannot change directory to %s", dir); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | int |
michael@0 | 155 | relatepaths(char *from, char *to, char *outpath) |
michael@0 | 156 | { |
michael@0 | 157 | char *cp, *cp2; |
michael@0 | 158 | int len; |
michael@0 | 159 | char buf[NAME_MAX]; |
michael@0 | 160 | |
michael@0 | 161 | assert(*from == '/' && *to == '/'); |
michael@0 | 162 | if (!from || *from != '/') |
michael@0 | 163 | fail("relatepaths: from path does not start with /"); |
michael@0 | 164 | if (!to || *to != '/') |
michael@0 | 165 | fail("relatepaths: to path does not start with /"); |
michael@0 | 166 | |
michael@0 | 167 | for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++) |
michael@0 | 168 | if (*cp == '\0') |
michael@0 | 169 | break; |
michael@0 | 170 | while (cp[-1] != '/') |
michael@0 | 171 | cp--, cp2--; |
michael@0 | 172 | if (cp - 1 == to) { |
michael@0 | 173 | /* closest common ancestor is /, so use full pathname */ |
michael@0 | 174 | len = strlen(strcpy(outpath, to)); |
michael@0 | 175 | if (outpath[len] != '/') { |
michael@0 | 176 | outpath[len++] = '/'; |
michael@0 | 177 | outpath[len] = '\0'; |
michael@0 | 178 | } |
michael@0 | 179 | } else { |
michael@0 | 180 | len = 0; |
michael@0 | 181 | while ((cp2 = getcomponent(cp2, buf)) != 0) { |
michael@0 | 182 | strcpy(outpath + len, "../"); |
michael@0 | 183 | len += 3; |
michael@0 | 184 | } |
michael@0 | 185 | while ((cp = getcomponent(cp, buf)) != 0) { |
michael@0 | 186 | sprintf(outpath + len, "%s/", buf); |
michael@0 | 187 | len += strlen(outpath + len); |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | return len; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | void |
michael@0 | 194 | reversepath(char *inpath, char *name, int len, char *outpath) |
michael@0 | 195 | { |
michael@0 | 196 | char *cp, *cp2; |
michael@0 | 197 | char buf[NAME_MAX]; |
michael@0 | 198 | struct stat sb; |
michael@0 | 199 | |
michael@0 | 200 | cp = strcpy(outpath + PATH_MAX - (len + 1), name); |
michael@0 | 201 | cp2 = inpath; |
michael@0 | 202 | while ((cp2 = getcomponent(cp2, buf)) != 0) { |
michael@0 | 203 | if (strcmp(buf, ".") == 0) |
michael@0 | 204 | continue; |
michael@0 | 205 | if (strcmp(buf, "..") == 0) { |
michael@0 | 206 | if (stat(".", &sb) < 0) |
michael@0 | 207 | fail("cannot stat current directory"); |
michael@0 | 208 | name = ino2name(sb.st_ino, ".."); |
michael@0 | 209 | len = strlen(name); |
michael@0 | 210 | cp -= len + 1; |
michael@0 | 211 | strcpy(cp, name); |
michael@0 | 212 | cp[len] = '/'; |
michael@0 | 213 | free(name); |
michael@0 | 214 | xchdir(".."); |
michael@0 | 215 | } else { |
michael@0 | 216 | cp -= 3; |
michael@0 | 217 | strncpy(cp, "../", 3); |
michael@0 | 218 | xchdir(buf); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | strcpy(outpath, cp); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | void |
michael@0 | 225 | diagnosePath(const char * path) |
michael@0 | 226 | { |
michael@0 | 227 | char * myPath; |
michael@0 | 228 | char * slash; |
michael@0 | 229 | int rv; |
michael@0 | 230 | struct stat sb; |
michael@0 | 231 | char buf[BUFSIZ]; |
michael@0 | 232 | |
michael@0 | 233 | if (!path || !path[0]) |
michael@0 | 234 | fail("Null pointer or empty string passed to mkdirs()"); |
michael@0 | 235 | myPath = strdup(path); |
michael@0 | 236 | if (!myPath) |
michael@0 | 237 | fail("strdup() failed!"); |
michael@0 | 238 | do { |
michael@0 | 239 | rv = lstat(myPath, &sb); |
michael@0 | 240 | if (rv < 0) { |
michael@0 | 241 | perror(myPath); |
michael@0 | 242 | } else if (S_ISLNK(sb.st_mode)) { |
michael@0 | 243 | rv = readlink(myPath, buf, sizeof(buf) - 1); |
michael@0 | 244 | if (rv < 0) { |
michael@0 | 245 | perror("readlink"); |
michael@0 | 246 | buf[0] = 0; |
michael@0 | 247 | } else { |
michael@0 | 248 | buf[rv] = 0; |
michael@0 | 249 | } |
michael@0 | 250 | fprintf(stderr, "%s is a link to %s\n", myPath, buf); |
michael@0 | 251 | } else if (S_ISDIR(sb.st_mode)) { |
michael@0 | 252 | fprintf(stderr, "%s is a directory\n", myPath); |
michael@0 | 253 | rv = access(myPath, X_OK); |
michael@0 | 254 | if (rv < 0) { |
michael@0 | 255 | fprintf(stderr, "%s: no search permission\n", myPath); |
michael@0 | 256 | } |
michael@0 | 257 | } else { |
michael@0 | 258 | fprintf(stderr, "%s is a file !?!\n", myPath); |
michael@0 | 259 | rv = access(myPath, F_OK); |
michael@0 | 260 | if (rv < 0) { |
michael@0 | 261 | fprintf(stderr, "%s does not exist\n", myPath); |
michael@0 | 262 | } |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | /* chop path off one level. */ |
michael@0 | 266 | slash = strrchr(myPath, '/'); |
michael@0 | 267 | if (!slash) |
michael@0 | 268 | slash = strrchr(myPath, '\\'); |
michael@0 | 269 | if (!slash) |
michael@0 | 270 | slash = myPath; |
michael@0 | 271 | *slash = 0; |
michael@0 | 272 | } while (myPath[0]); |
michael@0 | 273 | free(myPath); |
michael@0 | 274 | } |