/[jscoverage]/trunk/js/config/pathsub.c
ViewVC logotype

Contents of /trunk/js/config/pathsub.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 399 - (show annotations)
Tue Dec 9 03:37:47 2008 UTC (11 years ago) by siliconforks
File MIME type: text/plain
File size: 5553 byte(s)
Use SpiderMonkey from Firefox 3.1b2.

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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37 /*
38 ** Pathname subroutines.
39 **
40 ** Brendan Eich, 8/29/95
41 */
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"
53
54 #ifdef USE_REENTRANT_LIBC
55 #include <libc_r.h>
56 #endif
57
58 #ifdef SUNOS4
59 #include "sunos4.h"
60 #endif
61
62 #ifndef D_INO
63 #define D_INO d_ino
64 #endif
65
66 char *program;
67
68 void
69 fail(char *format, ...)
70 {
71 int error;
72 va_list ap;
73
74 #ifdef USE_REENTRANT_LIBC
75 R_STRERROR_INIT_R();
76 #endif
77
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) {
84
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
91 }
92
93 putc('\n', stderr);
94 exit(1);
95 }
96
97 char *
98 getcomponent(char *path, char *name)
99 {
100 if (*path == '\0')
101 return 0;
102 if (*path == '/') {
103 *name++ = '/';
104 } else {
105 do {
106 *name++ = *path++;
107 } while (*path != '/' && *path != '\0');
108 }
109 *name = '\0';
110 while (*path == '/')
111 path++;
112 return path;
113 }
114
115 #ifdef LAME_READDIR
116 #include <sys/param.h>
117 /*
118 ** The static buffer in Unixware's readdir is too small.
119 */
120 struct dirent *readdir(DIR *d)
121 {
122 static struct dirent *buf = NULL;
123
124 if(buf == NULL)
125 buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
126 return(readdir_r(d, buf));
127 }
128 #endif
129
130 char *
131 ino2name(ino_t ino, char *dir)
132 {
133 DIR *dp;
134 struct dirent *ep;
135 char *name;
136
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;
145 }
146 name = xstrdup(ep->d_name);
147 closedir(dp);
148 return name;
149 }
150
151 void *
152 xmalloc(size_t size)
153 {
154 void *p = malloc(size);
155 if (!p)
156 fail("cannot allocate %u bytes", size);
157 return p;
158 }
159
160 char *
161 xstrdup(char *s)
162 {
163 return strcpy(xmalloc(strlen(s) + 1), s);
164 }
165
166 char *
167 xbasename(char *path)
168 {
169 char *cp;
170
171 while ((cp = strrchr(path, '/')) && cp[1] == '\0')
172 *cp = '\0';
173 if (!cp) return path;
174 return cp + 1;
175 }
176
177 void
178 xchdir(char *dir)
179 {
180 if (chdir(dir) < 0)
181 fail("cannot change directory to %s", dir);
182 }
183
184 int
185 relatepaths(char *from, char *to, char *outpath)
186 {
187 char *cp, *cp2;
188 int len;
189 char buf[NAME_MAX];
190
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';
203 }
204 } else {
205 len = 0;
206 while ((cp2 = getcomponent(cp2, buf)) != 0) {
207 strcpy(outpath + len, "../");
208 len += 3;
209 }
210 while ((cp = getcomponent(cp, buf)) != 0) {
211 sprintf(outpath + len, "%s/", buf);
212 len += strlen(outpath + len);
213 }
214 }
215 return len;
216 }
217
218 void
219 reversepath(char *inpath, char *name, int len, char *outpath)
220 {
221 char *cp, *cp2;
222 char buf[NAME_MAX];
223 struct stat sb;
224
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);
244 }
245 }
246 strcpy(outpath, cp);
247 }

  ViewVC Help
Powered by ViewVC 1.1.24