nss: upgrade to release 3.73
[LibreOffice.git] / soltools / mkdepend / include.c
blobfd005c3284c7534490b0b58da38d726eaabbe9e0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* $XConsortium: include.c,v 1.17 94/12/05 19:33:08 gildea Exp $ */
3 /*
5 Copyright (c) 1993, 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
31 #include "def.h"
32 #include <string.h>
33 #include <assert.h>
35 static void remove_dotdot( char * );
36 static int isdot( char const * );
37 static int isdotdot( char const * );
38 static int issymbolic(char * dir, char * component);
39 static int exists_path(struct IncludesCollection*, char*);
41 #ifdef S_IFLNK
42 static char *notdotdot[ MAXDIRS ];
43 #endif
45 struct inclist *inc_path(char *file, char *include, boolean dot, struct IncludesCollection *incCollection)
47 static char path[ BUFSIZ ];
48 char **pp, *p;
49 struct inclist *ip;
50 struct stat st;
51 boolean found = FALSE;
52 (void)dot;
55 * Check all previously found include files for a path that
56 * has already been expanded.
58 for (ip = inclist; ip->i_file; ip++)
59 if (strcmp(ip->i_incstring, include) == 0)
61 found = TRUE;
62 break;
66 * If the path was surrounded by "" or is an absolute path,
67 * then check the exact path provided.
69 // FIXME: creates duplicates in the dependency files if absolute paths are
70 // given, which certainly is not the intended behavior. Also it slows down
71 // makedepend performance considerably.
72 // if (!found && (dot || *include == '/')) {
74 // if ((exists_path(incCollection, include)) && stat(include, &st) == 0 && !( st.st_mode & S_IFDIR)) {
75 // ip = newinclude(include, include);
76 // found = TRUE;
77 // }
78 // else if (show_where_not)
79 // warning1("\tnot in %s\n", include);
80 // }
83 * See if this include file is in the directory of the
84 * file being compiled.
86 if (!found) {
87 for (p=file+strlen(file); p>file; p--)
88 if (*p == '/')
89 break;
90 if (p == file)
92 if(strlen(include) >= BUFSIZ )
94 fatalerr("include filename too long \"%s\"\n", include);
96 else
98 strcpy(path, include);
101 else
103 int partial = p - file;
104 size_t inc_len = strlen(include);
105 if(inc_len + partial >= BUFSIZ )
107 fatalerr("include filename too long \"%s\"\n", include);
109 else
111 memcpy(path, file, partial);
112 memcpy(path + partial, include, inc_len);
113 path[partial + inc_len] = 0;
116 remove_dotdot(path);
117 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !( st.st_mode & S_IFDIR)) {
118 ip = newinclude(path, include);
119 found = TRUE;
121 else if (show_where_not)
122 warning1("\tnot in %s\n", path);
126 * Check the include directories specified. (standard include dir
127 * should be at the end.)
129 if (!found)
130 for (pp = includedirs; *pp; pp++) {
131 sprintf(path, "%s/%s", *pp, include);
132 remove_dotdot(path);
133 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !(st.st_mode & S_IFDIR)) {
134 ip = newinclude(path, include);
135 found = TRUE;
136 break;
138 else if (show_where_not)
139 warning1("\tnot in %s\n", path);
142 if (!found)
143 ip = NULL;
144 return ip;
147 int exists_path(struct IncludesCollection *incCollection, char *path)
149 convert_slashes(path);
150 return call_IncludesCollection_exists(incCollection, path);
154 * Occasionally, pathnames are created that look like .../x/../y
155 * Any of the 'x/..' sequences within the name can be eliminated.
156 * (but only if 'x' is not a symbolic link!!)
158 void remove_dotdot(char *path)
160 char *end, *from, *to, **cp;
161 char *components[ MAXFILES ],
162 newpath[ BUFSIZ ];
163 boolean component_copied;
166 * slice path up into components.
168 to = newpath;
169 if (*path == '/')
170 *to++ = '/';
171 *to = '\0';
172 cp = components;
173 for (from=end=path; *end; end++)
174 if (*end == '/') {
175 while (*end == '/')
176 *end++ = '\0';
177 if (*from)
178 *cp++ = from;
179 from = end;
181 *cp++ = from;
182 *cp = NULL;
185 * Recursively remove all 'x/..' component pairs.
187 cp = components;
188 while(*cp) {
189 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
190 && !issymbolic(newpath, *cp))
192 char **fp = cp + 2;
193 char **tp = cp;
195 do {
196 *tp++ = *fp; /* move all the pointers down */
197 } while (*fp++);
198 if (cp != components)
199 cp--; /* go back and check for nested ".." */
200 } else {
201 cp++;
205 * Concatenate the remaining path elements.
207 cp = components;
208 component_copied = FALSE;
209 while(*cp) {
210 if (component_copied)
211 *to++ = '/';
212 component_copied = TRUE;
213 for (from = *cp; *from; )
214 *to++ = *from++;
215 *to = '\0';
216 cp++;
218 *to++ = '\0';
221 * copy the reconstituted path back to our pointer.
223 strcpy(path, newpath);
226 int isdot(char const *p)
228 if(p && p[0] == '.' && p[1] == '\0')
229 return TRUE;
230 return FALSE;
233 int isdotdot(char const *p)
235 if(p && p[0] == '.' && p[1] == '.' && p[2] == '\0')
236 return TRUE;
237 return FALSE;
240 int issymbolic(char *dir, char *component)
242 #ifdef S_IFLNK
243 struct stat st;
244 char buf[ BUFSIZ ], **pp;
246 #if defined __GNUC__ && !defined __clang__
247 #pragma GCC diagnostic push
248 #pragma GCC diagnostic ignored "-Wformat-truncation"
249 // silence "‘snprintf’ output may be truncated before the last format character", from gcc 8.3 to at least 9.2.1
250 #endif
251 int n = snprintf(buf, BUFSIZ, "%s%s%s", dir, *dir ? "/" : "", component);
252 #if defined __GNUC__ && !defined __clang__
253 #pragma GCC diagnostic pop
254 #endif
255 assert(n < BUFSIZ);
256 (void) n;
257 for (pp=notdotdot; *pp; pp++)
258 if (strcmp(*pp, buf) == 0)
259 return TRUE;
260 if (lstat(buf, &st) == 0
261 && (st.st_mode & S_IFMT) == S_IFLNK) {
262 *pp++ = copy(buf);
263 if (pp >= &notdotdot[ MAXDIRS ])
264 fatalerr("out of .. dirs, increase MAXDIRS\n");
265 return TRUE;
267 #else
268 (void)dir; (void)component;
269 #endif
270 return FALSE;
274 * Add an include file to the list of those included by 'file'.
276 struct inclist *newinclude(char const *newfile, char const *incstring)
278 struct inclist *ip;
281 * First, put this file on the global list of include files.
283 ip = inclistp++;
284 if (inclistp == inclist + MAXFILES - 1)
285 fatalerr("out of space: increase MAXFILES\n");
286 ip->i_file = copy(newfile);
287 if (incstring == NULL)
288 ip->i_incstring = ip->i_file;
289 else
290 ip->i_incstring = copy(incstring);
292 return ip;
295 void included_by(struct inclist *ip, struct inclist *newfile)
297 int i;
299 if (ip == NULL)
300 return;
302 * Put this include file (newfile) on the list of files included
303 * by 'file'. If 'file' is NULL, then it is not an include
304 * file itself (i.e. was probably mentioned on the command line).
305 * If it is already on the list, don't stick it on again.
307 if (ip->i_list == NULL)
308 ip->i_list = (struct inclist **)
309 malloc(sizeof(struct inclist *) * ++ip->i_listlen);
310 else {
311 for (i=0; i<ip->i_listlen; i++)
312 if (ip->i_list[ i ] == newfile) {
313 i = (int)strlen(newfile->i_file);
314 if (!(i > 2 &&
315 newfile->i_file[i-1] == 'c' &&
316 newfile->i_file[i-2] == '.'))
318 /* only complain if ip has */
319 /* no #include SYMBOL lines */
320 /* and is not a .c file */
321 if (warn_multiple)
323 warning("%s includes %s more than once!\n",
324 ip->i_file, newfile->i_file);
325 warning1("Already have\n");
326 for (i=0; i<ip->i_listlen; i++)
327 warning1("\t%s\n", ip->i_list[i]->i_file);
330 return;
332 ip->i_list = (struct inclist **) realloc(ip->i_list,
333 sizeof(struct inclist *) * ++ip->i_listlen);
335 ip->i_list[ ip->i_listlen-1 ] = newfile;
338 void inc_clean (void)
340 struct inclist *ip;
342 for (ip = inclist; ip < inclistp; ip++) {
343 ip->i_marked = FALSE;
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */