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 $ */
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.
34 void remove_dotdot( char * );
36 int isdotdot( char * );
37 int issymbolic(char * dir
, char * component
);
38 int exists_path(struct IncludesCollection
*, char*);
41 extern struct inclist inclist
[ MAXFILES
],
43 extern char *includedirs
[ ];
44 extern char *notdotdot
[ ];
45 extern boolean show_where_not
;
46 extern boolean warn_multiple
;
48 struct inclist
*inc_path(char *file
, char *include
, boolean dot
, struct IncludesCollection
*incCollection
)
50 static char path
[ BUFSIZ
];
54 boolean found
= FALSE
;
58 * Check all previously found include files for a path that
59 * has already been expanded.
61 for (ip
= inclist
; ip
->i_file
; ip
++)
62 if ((strcmp(ip
->i_incstring
, include
) == 0) && !ip
->i_included_sym
)
69 * If the path was surrounded by "" or is an absolute path,
70 * then check the exact path provided.
72 // FIXME: creates duplicates in the dependency files if absolute paths are
73 // given, which certainly is not the intended behavior. Also it slows down
74 // makedepend performance considerably.
75 // if (!found && (dot || *include == '/')) {
77 // if ((exists_path(incCollection, include)) && stat(include, &st) == 0 && !( st.st_mode & S_IFDIR)) {
78 // ip = newinclude(include, include);
81 // else if (show_where_not)
82 // warning1("\tnot in %s\n", include);
86 * See if this include file is in the directory of the
87 * file being compiled.
90 for (p
=file
+strlen(file
); p
>file
; p
--)
95 if(strlen(include
) >= BUFSIZ
)
97 fatalerr("include filename too long \"%s\"\n", include
);
101 strcpy(path
, include
);
106 int partial
= (p
- file
);
107 size_t inc_len
= strlen(include
);
108 if(inc_len
+ partial
>= BUFSIZ
)
110 fatalerr("include filename too long \"%s\"\n", include
);
114 memcpy(path
, file
, partial
);
115 memcpy(path
+ partial
, include
, inc_len
);
116 path
[partial
+ inc_len
] = 0;
120 if ((exists_path(incCollection
, path
)) && stat(path
, &st
) == 0 && !( st
.st_mode
& S_IFDIR
)) {
121 ip
= newinclude(path
, include
);
124 else if (show_where_not
)
125 warning1("\tnot in %s\n", path
);
129 * Check the include directories specified. (standard include dir
130 * should be at the end.)
133 for (pp
= includedirs
; *pp
; pp
++) {
134 sprintf(path
, "%s/%s", *pp
, include
);
136 if ((exists_path(incCollection
, path
)) && stat(path
, &st
) == 0 && !(st
.st_mode
& S_IFDIR
)) {
137 ip
= newinclude(path
, include
);
141 else if (show_where_not
)
142 warning1("\tnot in %s\n", path
);
150 int exists_path(struct IncludesCollection
*incCollection
, char *path
)
152 convert_slashes(path
);
153 return call_IncludesCollection_exists(incCollection
, path
);
157 * Occasionally, pathnames are created that look like .../x/../y
158 * Any of the 'x/..' sequences within the name can be eliminated.
159 * (but only if 'x' is not a symbolic link!!)
161 void remove_dotdot(char *path
)
163 char *end
, *from
, *to
, **cp
;
164 char *components
[ MAXFILES
],
166 boolean component_copied
;
169 * slice path up into components.
176 for (from
=end
=path
; *end
; end
++)
188 * Recursively remove all 'x/..' component pairs.
192 if (!isdot(*cp
) && !isdotdot(*cp
) && isdotdot(*(cp
+1))
193 && !issymbolic(newpath
, *cp
))
199 *tp
++ = *fp
; /* move all the pointers down */
201 if (cp
!= components
)
202 cp
--; /* go back and check for nested ".." */
208 * Concatenate the remaining path elements.
211 component_copied
= FALSE
;
213 if (component_copied
)
215 component_copied
= TRUE
;
216 for (from
= *cp
; *from
; )
224 * copy the reconstituted path back to our pointer.
226 strcpy(path
, newpath
);
231 if(p
&& p
[0] == '.' && p
[1] == '\0')
236 int isdotdot(char *p
)
238 if(p
&& p
[0] == '.' && p
[1] == '.' && p
[2] == '\0')
243 int issymbolic(char *dir
, char *component
)
247 char buf
[ BUFSIZ
], **pp
;
249 sprintf(buf
, "%s%s%s", dir
, *dir
? "/" : "", component
);
250 for (pp
=notdotdot
; *pp
; pp
++)
251 if (strcmp(*pp
, buf
) == 0)
253 if (lstat(buf
, &st
) == 0
254 && (st
.st_mode
& S_IFMT
) == S_IFLNK
) {
256 if (pp
>= ¬dotdot
[ MAXDIRS
])
257 fatalerr("out of .. dirs, increase MAXDIRS\n");
265 * Add an include file to the list of those included by 'file'.
267 struct inclist
*newinclude(char *newfile
, char *incstring
)
272 * First, put this file on the global list of include files.
275 if (inclistp
== inclist
+ MAXFILES
- 1)
276 fatalerr("out of space: increase MAXFILES\n");
277 ip
->i_file
= copy(newfile
);
278 ip
->i_included_sym
= FALSE
;
279 if (incstring
== NULL
)
280 ip
->i_incstring
= ip
->i_file
;
282 ip
->i_incstring
= copy(incstring
);
287 void included_by(struct inclist
*ip
, struct inclist
*newfile
)
294 * Put this include file (newfile) on the list of files included
295 * by 'file'. If 'file' is NULL, then it is not an include
296 * file itself (i.e. was probably mentioned on the command line).
297 * If it is already on the list, don't stick it on again.
299 if (ip
->i_list
== NULL
)
300 ip
->i_list
= (struct inclist
**)
301 malloc(sizeof(struct inclist
*) * ++ip
->i_listlen
);
303 for (i
=0; i
<ip
->i_listlen
; i
++)
304 if (ip
->i_list
[ i
] == newfile
) {
305 i
= (int)strlen(newfile
->i_file
);
306 if (!ip
->i_included_sym
&&
308 newfile
->i_file
[i
-1] == 'c' &&
309 newfile
->i_file
[i
-2] == '.'))
311 /* only complain if ip has */
312 /* no #include SYMBOL lines */
313 /* and is not a .c file */
316 warning("%s includes %s more than once!\n",
317 ip
->i_file
, newfile
->i_file
);
318 warning1("Already have\n");
319 for (i
=0; i
<ip
->i_listlen
; i
++)
320 warning1("\t%s\n", ip
->i_list
[i
]->i_file
);
325 ip
->i_list
= (struct inclist
**) realloc(ip
->i_list
,
326 sizeof(struct inclist
*) * ++ip
->i_listlen
);
328 ip
->i_list
[ ip
->i_listlen
-1 ] = newfile
;
331 void inc_clean (void)
335 for (ip
= inclist
; ip
< inclistp
; ip
++) {
336 ip
->i_marked
= FALSE
;
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */