1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 ** Pathname subroutines.
40 ** Brendan Eich, 8/29/95
43 #include <sys/types.h>
54 #ifdef USE_REENTRANT_LIBC
69 fail(char *format, ...)
74 #ifdef USE_REENTRANT_LIBC
79 fprintf(stderr, "%s: ", program);
81 vfprintf(stderr, format, ap);
85 #ifdef USE_REENTRANT_LIBC
87 fprintf(stderr, ": %s", r_strerror_r);
89 fprintf(stderr, ": %s", strerror(errno));
98 getcomponent(char *path, char *name)
107 } while (*path != '/' && *path != '\0');
116 #include <sys/param.h>
118 ** The static buffer in Unixware's readdir is too small.
120 struct dirent *readdir(DIR *d)
122 static struct dirent *buf = NULL;
125 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
126 return(readdir_r(d, buf));
131 ino2name(ino_t ino, char *dir)
139 fail("cannot read parent directory");
141 if (!(ep = readdir(dp)))
142 fail("cannot find current directory");
143 if (ep->D_INO == ino)
146 name = xstrdup(ep->d_name);
154 void *p = malloc(size);
156 fail("cannot allocate %u bytes", size);
163 return strcpy(xmalloc(strlen(s) + 1), s);
167 xbasename(char *path)
171 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
173 if (!cp) return path;
181 fail("cannot change directory to %s", dir);
185 relatepaths(char *from, char *to, char *outpath)
191 assert(*from == '/' && *to == '/');
192 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
195 while (cp[-1] != '/')
198 /* closest common ancestor is /, so use full pathname */
199 len = strlen(strcpy(outpath, to));
200 if (outpath[len] != '/') {
201 outpath[len++] = '/';
206 while ((cp2 = getcomponent(cp2, buf)) != 0) {
207 strcpy(outpath + len, "../");
210 while ((cp = getcomponent(cp, buf)) != 0) {
211 sprintf(outpath + len, "%s/", buf);
212 len += strlen(outpath + len);
219 reversepath(char *inpath, char *name, int len, char *outpath)
225 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
227 while ((cp2 = getcomponent(cp2, buf)) != 0) {
228 if (strcmp(buf, ".") == 0)
230 if (strcmp(buf, "..") == 0) {
231 if (stat(".", &sb) < 0)
232 fail("cannot stat current directory");
233 name = ino2name(sb.st_ino, "..");
242 strncpy(cp, "../", 3);