Bug 508760 - Remove MSVC6 support from the tree; (Iv1) /toolkit/xre/*.cpp.
[mozilla-central.git] / config / pathsub.c
blob8384e5fb9828b32e55dd8041b24eb107616872f6
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
13 * License.
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.
22 * Contributor(s):
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
42 #include <assert.h>
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <sys/stat.h>
52 #include "pathsub.h"
54 #ifdef USE_REENTRANT_LIBC
55 #include <libc_r.h>
56 #endif
58 #ifdef SUNOS4
59 #include "sunos4.h"
60 #endif
62 #ifndef D_INO
63 #define D_INO d_ino
64 #endif
66 char *program;
68 void
69 fail(char *format, ...)
71 int error;
72 va_list ap;
74 #ifdef USE_REENTRANT_LIBC
75 R_STRERROR_INIT_R();
76 #endif
78 error = errno;
79 fprintf(stderr, "%s: ", program);
80 va_start(ap, format);
81 vfprintf(stderr, format, ap);
82 va_end(ap);
83 if (error) {
85 #ifdef USE_REENTRANT_LIBC
86 R_STRERROR_R(errno);
87 fprintf(stderr, ": %s", r_strerror_r);
88 #else
89 fprintf(stderr, ": %s", strerror(errno));
90 #endif
93 putc('\n', stderr);
94 exit(1);
97 char *
98 getcomponent(char *path, char *name)
100 if (*path == '\0')
101 return 0;
102 if (*path == '/') {
103 *name++ = '/';
104 } else {
105 do {
106 *name++ = *path++;
107 } while (*path != '/' && *path != '\0');
109 *name = '\0';
110 while (*path == '/')
111 path++;
112 return path;
115 #ifdef LAME_READDIR
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;
124 if(buf == NULL)
125 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
126 return(readdir_r(d, buf));
128 #endif
130 char *
131 ino2name(ino_t ino, char *dir)
133 DIR *dp;
134 struct dirent *ep;
135 char *name;
137 dp = opendir("..");
138 if (!dp)
139 fail("cannot read parent directory");
140 for (;;) {
141 if (!(ep = readdir(dp)))
142 fail("cannot find current directory");
143 if (ep->D_INO == ino)
144 break;
146 name = xstrdup(ep->d_name);
147 closedir(dp);
148 return name;
151 void *
152 xmalloc(size_t size)
154 void *p = malloc(size);
155 if (!p)
156 fail("cannot allocate %u bytes", size);
157 return p;
160 char *
161 xstrdup(char *s)
163 return strcpy(xmalloc(strlen(s) + 1), s);
166 char *
167 xbasename(char *path)
169 char *cp;
171 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
172 *cp = '\0';
173 if (!cp) return path;
174 return cp + 1;
177 void
178 xchdir(char *dir)
180 if (chdir(dir) < 0)
181 fail("cannot change directory to %s", dir);
185 relatepaths(char *from, char *to, char *outpath)
187 char *cp, *cp2;
188 int len;
189 char buf[NAME_MAX];
191 assert(*from == '/' && *to == '/');
192 for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
193 if (*cp == '\0')
194 break;
195 while (cp[-1] != '/')
196 cp--, cp2--;
197 if (cp - 1 == to) {
198 /* closest common ancestor is /, so use full pathname */
199 len = strlen(strcpy(outpath, to));
200 if (outpath[len] != '/') {
201 outpath[len++] = '/';
202 outpath[len] = '\0';
204 } else {
205 len = 0;
206 while ((cp2 = getcomponent(cp2, buf)) != 0) {
207 strcpy(outpath + len, "../");
208 len += 3;
210 while ((cp = getcomponent(cp, buf)) != 0) {
211 sprintf(outpath + len, "%s/", buf);
212 len += strlen(outpath + len);
215 return len;
218 void
219 reversepath(char *inpath, char *name, int len, char *outpath)
221 char *cp, *cp2;
222 char buf[NAME_MAX];
223 struct stat sb;
225 cp = strcpy(outpath + PATH_MAX - (len + 1), name);
226 cp2 = inpath;
227 while ((cp2 = getcomponent(cp2, buf)) != 0) {
228 if (strcmp(buf, ".") == 0)
229 continue;
230 if (strcmp(buf, "..") == 0) {
231 if (stat(".", &sb) < 0)
232 fail("cannot stat current directory");
233 name = ino2name(sb.st_ino, "..");
234 len = strlen(name);
235 cp -= len + 1;
236 strcpy(cp, name);
237 cp[len] = '/';
238 free(name);
239 xchdir("..");
240 } else {
241 cp -= 3;
242 strncpy(cp, "../", 3);
243 xchdir(buf);
246 strcpy(outpath, cp);