Back to index

lightning-sunbird  0.9+nobinonly
pathsub.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038 ** Pathname subroutines.
00039 */
00040 #include <assert.h>
00041 #if defined(FREEBSD) || defined(BSDI) || defined(DARWIN)
00042 #include <sys/types.h>
00043 #endif /* FREEBSD */
00044 #include <dirent.h>
00045 #include <errno.h>
00046 #include <stdarg.h>
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <unistd.h>
00051 #include <sys/types.h>
00052 #include <sys/stat.h>
00053 #include "pathsub.h"
00054 #ifdef USE_REENTRANT_LIBC
00055 #include "libc_r.h"
00056 #endif /* USE_REENTRANT_LIBC */
00057 
00058 char *program;
00059 
00060 void
00061 fail(char *format, ...)
00062 {
00063     int error;
00064     va_list ap;
00065 
00066 #ifdef USE_REENTRANT_LIBC
00067     R_STRERROR_INIT_R();
00068 #endif
00069 
00070     error = errno;
00071     fprintf(stderr, "%s: ", program);
00072     va_start(ap, format);
00073     vfprintf(stderr, format, ap);
00074     va_end(ap);
00075     if (error) {
00076 
00077 #ifdef USE_REENTRANT_LIBC
00078     R_STRERROR_R(errno);
00079        fprintf(stderr, ": %s", r_strerror_r);
00080 #else
00081        fprintf(stderr, ": %s", strerror(errno));
00082 #endif
00083     }
00084  
00085     putc('\n', stderr);
00086     abort();
00087     exit(1);
00088 }
00089 
00090 char *
00091 getcomponent(char *path, char *name)
00092 {
00093     if (*path == '\0')
00094        return 0;
00095     if (*path == '/') {
00096        *name++ = '/';
00097     } else {
00098        do {
00099            *name++ = *path++;
00100        } while (*path != '/' && *path != '\0');
00101     }
00102     *name = '\0';
00103     while (*path == '/')
00104        path++;
00105     return path;
00106 }
00107 
00108 #ifdef UNIXWARE
00109 /* The static buffer in Unixware's readdir is too small. */
00110 struct dirent * readdir(DIR *d)
00111 {
00112     static struct dirent *buf = NULL;
00113 #define MAX_PATH_LEN 1024
00114 
00115     if (buf == NULL)
00116        buf = (struct dirent *)xmalloc(sizeof(struct dirent) + MAX_PATH_LEN) ;
00117     return readdir_r(d, buf);
00118 }
00119 #endif
00120 
00121 /* APPARENT BUG - ignores argument "dir", uses ".." instead. */
00122 char *
00123 ino2name(ino_t ino, char *dir)
00124 {
00125     DIR *dp;
00126     struct dirent *ep;
00127     char *name;
00128 
00129     dp = opendir("..");            /* XXX */
00130     if (!dp)
00131        fail("cannot read parent directory");
00132     for (;;) {
00133        if (!(ep = readdir(dp)))
00134            fail("cannot find current directory");
00135        if (ep->d_ino == ino)
00136            break;
00137     }
00138     name = xstrdup(ep->d_name);
00139     closedir(dp);
00140     return name;
00141 }
00142 
00143 void *
00144 xmalloc(size_t size)
00145 {
00146     void *p;
00147 
00148     if (size <= 0)
00149        fail("attempted to allocate %u bytes", size);
00150     p = malloc(size);
00151     if (!p)
00152        fail("cannot allocate %u bytes", size);
00153     return p;
00154 }
00155 
00156 char *
00157 xstrdup(char *s)
00158 {
00159     if (!s || !s[0]) 
00160        fail("Null pointer or empty string passed to xstrdup()");
00161     return strcpy((char*)xmalloc(strlen(s) + 1), s);
00162 }
00163 
00164 char *
00165 xbasename(char *path)
00166 {
00167     char *cp;
00168 
00169     if (!path || !path[0]) 
00170        fail("Null pointer or empty string passed to xbasename()");
00171     while ((cp = strrchr(path, '/')) && cp[1] == '\0')
00172        *cp = '\0';
00173     if (!cp) return path;
00174     return cp + 1;
00175 }
00176 
00177 void
00178 xchdir(char *dir)
00179 {
00180     if (!dir || !dir[0]) 
00181        fail("Null pointer or empty string passed to xchdir()");
00182     if (chdir(dir) < 0)
00183        fail("cannot change directory to %s", dir);
00184 }
00185 
00186 int
00187 relatepaths(char *from, char *to, char *outpath)
00188 {
00189     char *cp, *cp2;
00190     int len;
00191     char buf[NAME_MAX];
00192 
00193     assert(*from == '/' && *to == '/');
00194     if (!from || *from != '/')
00195        fail("relatepaths: from path does not start with /");
00196     if (!to || *to != '/')
00197        fail("relatepaths: to   path does not start with /");
00198 
00199     for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
00200        if (*cp == '\0')
00201            break;
00202     while (cp[-1] != '/')
00203        cp--, cp2--;
00204     if (cp - 1 == to) {
00205        /* closest common ancestor is /, so use full pathname */
00206        len = strlen(strcpy(outpath, to));
00207        if (outpath[len] != '/') {
00208            outpath[len++] = '/';
00209            outpath[len] = '\0';
00210        }
00211     } else {
00212        len = 0;
00213        while ((cp2 = getcomponent(cp2, buf)) != 0) {
00214            strcpy(outpath + len, "../");
00215            len += 3;
00216        }
00217        while ((cp = getcomponent(cp, buf)) != 0) {
00218            sprintf(outpath + len, "%s/", buf);
00219            len += strlen(outpath + len);
00220        }
00221     }
00222     return len;
00223 }
00224 
00225 void
00226 reversepath(char *inpath, char *name, int len, char *outpath)
00227 {
00228     char *cp, *cp2;
00229     char buf[NAME_MAX];
00230     struct stat sb;
00231 
00232     cp = strcpy(outpath + PATH_MAX - (len + 1), name);
00233     cp2 = inpath;
00234     while ((cp2 = getcomponent(cp2, buf)) != 0) {
00235        if (strcmp(buf, ".") == 0)
00236            continue;
00237        if (strcmp(buf, "..") == 0) {
00238            if (stat(".", &sb) < 0)
00239               fail("cannot stat current directory");
00240            name = ino2name(sb.st_ino, "..");
00241            len = strlen(name);
00242            cp -= len + 1;
00243            strcpy(cp, name);
00244            cp[len] = '/';
00245            free(name);
00246            xchdir("..");
00247        } else {
00248            cp -= 3;
00249            strncpy(cp, "../", 3);
00250            xchdir(buf);
00251        }
00252     }
00253     strcpy(outpath, cp);
00254 }
00255 
00256 void
00257 diagnosePath(const char * path)
00258 {
00259     char *    myPath;
00260     char *      slash;
00261     int              rv;
00262     struct stat sb;
00263     char      buf[BUFSIZ];
00264 
00265     if (!path || !path[0]) 
00266        fail("Null pointer or empty string passed to mkdirs()");
00267     myPath = strdup(path);
00268     if (!myPath)
00269        fail("strdup() failed!");
00270     do {
00271        rv = lstat(myPath, &sb);
00272        if (rv < 0) {
00273            perror(myPath);
00274        } else if (S_ISLNK(sb.st_mode)) {
00275            rv = readlink(myPath, buf, sizeof buf);
00276            if (rv < 0) {
00277               perror("readlink");
00278               buf[0] = 0;
00279            } else {
00280               buf[rv] = 0;
00281            }
00282            fprintf(stderr, "%s is a link to %s\n", myPath, buf);
00283        } else if (S_ISDIR(sb.st_mode)) {
00284            fprintf(stderr, "%s is a directory\n", myPath);
00285            rv = access(myPath, X_OK);
00286            if (rv < 0) {
00287               fprintf(stderr, "%s: no search permission\n", myPath);
00288            }
00289        } else {
00290            fprintf(stderr, "%s is a file !?!\n", myPath);
00291            rv = access(myPath, F_OK);
00292            if (rv < 0) {
00293               fprintf(stderr, "%s does not exist\n", myPath);
00294            }
00295        }
00296 
00297        /* chop path off one level. */
00298        slash = strrchr(myPath, '/');
00299        if (!slash)
00300            slash = strrchr(myPath, '\\');
00301        if (!slash)
00302            slash = myPath;
00303        *slash = 0;
00304     } while (myPath[0]);
00305     free(myPath);
00306 }