calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / soltools / mkdepend / include.c
blob7f7955a6898493b4d91a313ba69916e58186e97f
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 #include <sal/types.h>
37 static void remove_dotdot( char * );
38 static int isdot( char const * );
39 static int isdotdot( char const * );
40 static int issymbolic(char * dir, char * component);
41 static int exists_path(struct IncludesCollection*, char*);
43 #ifdef S_IFLNK
44 static char *notdotdot[ MAXDIRS ];
45 #endif
47 struct inclist *inc_path(char *file, char *include, boolean dot, struct IncludesCollection *incCollection)
49 static char path[ BUFSIZ ];
50 char **pp, *p;
51 struct inclist *ip;
52 struct stat st;
53 boolean found = FALSE;
54 (void)dot;
57 * Check all previously found include files for a path that
58 * has already been expanded.
60 for (ip = inclist; ip->i_file; ip++)
61 if (strcmp(ip->i_incstring, include) == 0)
63 found = TRUE;
64 break;
68 * If the path was surrounded by "" or is an absolute path,
69 * then check the exact path provided.
71 // FIXME: creates duplicates in the dependency files if absolute paths are
72 // given, which certainly is not the intended behavior. Also it slows down
73 // makedepend performance considerably.
74 // if (!found && (dot || *include == '/')) {
76 // if ((exists_path(incCollection, include)) && stat(include, &st) == 0 && !( st.st_mode & S_IFDIR)) {
77 // ip = newinclude(include, include);
78 // found = TRUE;
79 // }
80 // else if (show_where_not)
81 // warning1("\tnot in %s\n", include);
82 // }
85 * See if this include file is in the directory of the
86 * file being compiled.
88 if (!found) {
89 for (p=file+strlen(file); p>file; p--)
90 if (*p == '/')
91 break;
92 if (p == file)
94 if(strlen(include) >= BUFSIZ )
96 fatalerr("include filename too long \"%s\"\n", include);
98 else
100 strcpy(path, include);
103 else
105 int partial = p - file;
106 size_t inc_len = strlen(include);
107 if(inc_len + partial >= BUFSIZ )
109 fatalerr("include filename too long \"%s\"\n", include);
111 else
113 memcpy(path, file, partial);
114 memcpy(path + partial, include, inc_len);
115 path[partial + inc_len] = 0;
118 remove_dotdot(path);
119 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !( st.st_mode & S_IFDIR)) {
120 ip = newinclude(path, include);
121 found = TRUE;
123 else if (show_where_not)
124 warning1("\tnot in %s\n", path);
128 * Check the include directories specified. (standard include dir
129 * should be at the end.)
131 if (!found)
132 for (pp = includedirs; *pp; pp++) {
133 SAL_WNODEPRECATED_DECLARATIONS_PUSH /* sprintf (macOS 13 SDK) */
134 sprintf(path, "%s/%s", *pp, include);
135 SAL_WNODEPRECATED_DECLARATIONS_POP
136 remove_dotdot(path);
137 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !(st.st_mode & S_IFDIR)) {
138 ip = newinclude(path, include);
139 found = TRUE;
140 break;
142 else if (show_where_not)
143 warning1("\tnot in %s\n", path);
146 if (!found)
147 ip = NULL;
148 return ip;
151 int exists_path(struct IncludesCollection *incCollection, char *path)
153 convert_slashes(path);
154 return call_IncludesCollection_exists(incCollection, path);
158 * Occasionally, pathnames are created that look like .../x/../y
159 * Any of the 'x/..' sequences within the name can be eliminated.
160 * (but only if 'x' is not a symbolic link!!)
162 void remove_dotdot(char *path)
164 char *end, *from, *to, **cp;
165 char *components[ MAXFILES ],
166 newpath[ BUFSIZ ];
167 boolean component_copied;
170 * slice path up into components.
172 to = newpath;
173 if (*path == '/')
174 *to++ = '/';
175 *to = '\0';
176 cp = components;
177 for (from=end=path; *end; end++)
178 if (*end == '/') {
179 while (*end == '/')
180 *end++ = '\0';
181 if (*from)
182 *cp++ = from;
183 from = end;
185 *cp++ = from;
186 *cp = NULL;
189 * Recursively remove all 'x/..' component pairs.
191 cp = components;
192 while(*cp) {
193 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
194 && !issymbolic(newpath, *cp))
196 char **fp = cp + 2;
197 char **tp = cp;
199 do {
200 *tp++ = *fp; /* move all the pointers down */
201 } while (*fp++);
202 if (cp != components)
203 cp--; /* go back and check for nested ".." */
204 } else {
205 cp++;
209 * Concatenate the remaining path elements.
211 cp = components;
212 component_copied = FALSE;
213 while(*cp) {
214 if (component_copied)
215 *to++ = '/';
216 component_copied = TRUE;
217 for (from = *cp; *from; )
218 *to++ = *from++;
219 *to = '\0';
220 cp++;
222 *to++ = '\0';
225 * copy the reconstituted path back to our pointer.
227 strcpy(path, newpath);
230 int isdot(char const *p)
232 if(p && p[0] == '.' && p[1] == '\0')
233 return TRUE;
234 return FALSE;
237 int isdotdot(char const *p)
239 if(p && p[0] == '.' && p[1] == '.' && p[2] == '\0')
240 return TRUE;
241 return FALSE;
244 int issymbolic(char *dir, char *component)
246 #ifdef S_IFLNK
247 struct stat st;
248 char buf[ BUFSIZ ], **pp;
250 #if defined __GNUC__ && !defined __clang__
251 #pragma GCC diagnostic push
252 #pragma GCC diagnostic ignored "-Wformat-truncation"
253 // silence "‘snprintf’ output may be truncated before the last format character", from gcc 8.3 to at least 9.2.1
254 #endif
255 int n = snprintf(buf, BUFSIZ, "%s%s%s", dir, *dir ? "/" : "", component);
256 #if defined __GNUC__ && !defined __clang__
257 #pragma GCC diagnostic pop
258 #endif
259 assert(n < BUFSIZ);
260 (void) n;
261 for (pp=notdotdot; *pp; pp++)
262 if (strcmp(*pp, buf) == 0)
263 return TRUE;
264 if (lstat(buf, &st) == 0
265 && (st.st_mode & S_IFMT) == S_IFLNK) {
266 *pp++ = copy(buf);
267 if (pp >= &notdotdot[ MAXDIRS ])
268 fatalerr("out of .. dirs, increase MAXDIRS\n");
269 return TRUE;
271 #else
272 (void)dir; (void)component;
273 #endif
274 return FALSE;
278 * Add an include file to the list of those included by 'file'.
280 struct inclist *newinclude(char const *newfile, char const *incstring)
282 struct inclist *ip;
285 * First, put this file on the global list of include files.
287 ip = inclistp++;
288 if (inclistp == inclist + MAXFILES - 1)
289 fatalerr("out of space: increase MAXFILES\n");
290 ip->i_file = copy(newfile);
291 if (incstring == NULL)
292 ip->i_incstring = ip->i_file;
293 else
294 ip->i_incstring = copy(incstring);
296 return ip;
299 void included_by(struct inclist *ip, struct inclist *newfile)
301 int i;
303 if (ip == NULL)
304 return;
306 * Put this include file (newfile) on the list of files included
307 * by 'file'. If 'file' is NULL, then it is not an include
308 * file itself (i.e. was probably mentioned on the command line).
309 * If it is already on the list, don't stick it on again.
311 if (ip->i_list == NULL)
312 ip->i_list = (struct inclist **)
313 malloc(sizeof(struct inclist *) * ++ip->i_listlen);
314 else {
315 for (i=0; i<ip->i_listlen; i++)
316 if (ip->i_list[ i ] == newfile) {
317 i = (int)strlen(newfile->i_file);
318 if (!(i > 2 &&
319 newfile->i_file[i-1] == 'c' &&
320 newfile->i_file[i-2] == '.'))
322 /* only complain if ip has */
323 /* no #include SYMBOL lines */
324 /* and is not a .c file */
325 if (warn_multiple)
327 warning("%s includes %s more than once!\n",
328 ip->i_file, newfile->i_file);
329 warning1("Already have\n");
330 for (i=0; i<ip->i_listlen; i++)
331 warning1("\t%s\n", ip->i_list[i]->i_file);
334 return;
336 ip->i_list = (struct inclist **) realloc(ip->i_list,
337 sizeof(struct inclist *) * ++ip->i_listlen);
339 ip->i_list[ ip->i_listlen-1 ] = newfile;
342 void inc_clean (void)
344 struct inclist *ip;
346 for (ip = inclist; ip < inclistp; ip++) {
347 ip->i_marked = FALSE;
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */