1 /* $XConsortium: include.c,v 1.17 94/12/05 19:33:08 gildea Exp $ */
4 Copyright (c) 1993, 1994 X Consortium
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of the X Consortium shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from the X Consortium.
33 void remove_dotdot( char * );
35 int isdotdot( char * );
36 int issymbolic(char * dir
, char * component
);
37 int exists_path(struct IncludesCollection
*, char*);
40 extern struct inclist inclist
[ MAXFILES
],
42 extern char *includedirs
[ ];
43 extern char *notdotdot
[ ];
44 extern boolean show_where_not
;
45 extern boolean warn_multiple
;
47 struct inclist
*inc_path(file
, include
, dot
, incCollection
)
51 struct IncludesCollection
* incCollection
;
53 static char path
[ BUFSIZ
];
54 register char **pp
, *p
;
55 register struct inclist
*ip
;
57 boolean found
= FALSE
;
60 * Check all previously found include files for a path that
61 * has already been expanded.
63 for (ip
= inclist
; ip
->i_file
; ip
++)
64 if ((strcmp(ip
->i_incstring
, include
) == 0) && !ip
->i_included_sym
)
71 * If the path was surrounded by "" or is an absolute path,
72 * then check the exact path provided.
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
);
80 else if (show_where_not
)
81 warning1("\tnot in %s\n", include
);
85 * See if this include file is in the directory of the
86 * file being compiled.
89 for (p
=file
+strlen(file
); p
>file
; p
--)
93 strcpy(path
, include
);
95 strncpy(path
, file
, (p
-file
) + 1);
96 path
[ (p
-file
) + 1 ] = '\0';
97 strcpy(path
+ (p
-file
) + 1, include
);
100 if ((exists_path(incCollection
, path
)) && stat(path
, &st
) == 0 && !( st
.st_mode
& S_IFDIR
)) {
101 ip
= newinclude(path
, include
);
104 else if (show_where_not
)
105 warning1("\tnot in %s\n", path
);
109 * Check the include directories specified. (standard include dir
110 * should be at the end.)
113 for (pp
= includedirs
; *pp
; pp
++) {
114 sprintf(path
, "%s/%s", *pp
, include
);
116 if ((exists_path(incCollection
, path
)) && stat(path
, &st
) == 0 && !(st
.st_mode
& S_IFDIR
)) {
117 ip
= newinclude(path
, include
);
121 else if (show_where_not
)
122 warning1("\tnot in %s\n", path
);
130 int exists_path(incCollection
, path
)
131 struct IncludesCollection
* incCollection
;
134 convert_slashes(path
);
135 return call_IncludesCollection_exists(incCollection
, path
);
139 * Occasionally, pathnames are created that look like .../x/../y
140 * Any of the 'x/..' sequences within the name can be eliminated.
141 * (but only if 'x' is not a symbolic link!!)
143 void remove_dotdot(path
)
146 register char *end
, *from
, *to
, **cp
;
147 char *components
[ MAXFILES
],
149 boolean component_copied
;
152 * slice path up into components.
159 for (from
=end
=path
; *end
; end
++)
171 * Recursively remove all 'x/..' component pairs.
175 if (!isdot(*cp
) && !isdotdot(*cp
) && isdotdot(*(cp
+1))
176 && !issymbolic(newpath
, *cp
))
182 *tp
++ = *fp
; /* move all the pointers down */
184 if (cp
!= components
)
185 cp
--; /* go back and check for nested ".." */
191 * Concatenate the remaining path elements.
194 component_copied
= FALSE
;
196 if (component_copied
)
198 component_copied
= TRUE
;
199 for (from
= *cp
; *from
; )
207 * copy the reconstituted path back to our pointer.
209 strcpy(path
, newpath
);
215 if(p
&& *p
++ == '.' && *p
++ == '\0')
223 if(p
&& *p
++ == '.' && *p
++ == '.' && *p
++ == '\0')
228 int issymbolic(dir
, component
)
229 register char *dir
, *component
;
233 char buf
[ BUFSIZ
], **pp
;
235 sprintf(buf
, "%s%s%s", dir
, *dir
? "/" : "", component
);
236 for (pp
=notdotdot
; *pp
; pp
++)
237 if (strcmp(*pp
, buf
) == 0)
239 if (lstat(buf
, &st
) == 0
240 && (st
.st_mode
& S_IFMT
) == S_IFLNK
) {
242 if (pp
>= ¬dotdot
[ MAXDIRS
])
243 fatalerr("out of .. dirs, increase MAXDIRS\n");
251 * Add an include file to the list of those included by 'file'.
253 struct inclist
*newinclude(newfile
, incstring
)
254 register char *newfile
, *incstring
;
256 register struct inclist
*ip
;
259 * First, put this file on the global list of include files.
262 if (inclistp
== inclist
+ MAXFILES
- 1)
263 fatalerr("out of space: increase MAXFILES\n");
264 ip
->i_file
= copy(newfile
);
265 ip
->i_included_sym
= FALSE
;
266 if (incstring
== NULL
)
267 ip
->i_incstring
= ip
->i_file
;
269 ip
->i_incstring
= copy(incstring
);
274 void included_by(ip
, newfile
)
275 register struct inclist
*ip
, *newfile
;
282 * Put this include file (newfile) on the list of files included
283 * by 'file'. If 'file' is NULL, then it is not an include
284 * file itself (i.e. was probably mentioned on the command line).
285 * If it is already on the list, don't stick it on again.
287 if (ip
->i_list
== NULL
)
288 ip
->i_list
= (struct inclist
**)
289 malloc(sizeof(struct inclist
*) * ++ip
->i_listlen
);
291 for (i
=0; i
<ip
->i_listlen
; i
++)
292 if (ip
->i_list
[ i
] == newfile
) {
293 i
= strlen(newfile
->i_file
);
294 if (!ip
->i_included_sym
&&
296 newfile
->i_file
[i
-1] == 'c' &&
297 newfile
->i_file
[i
-2] == '.'))
299 /* only complain if ip has */
300 /* no #include SYMBOL lines */
301 /* and is not a .c file */
304 warning("%s includes %s more than once!\n",
305 ip
->i_file
, newfile
->i_file
);
306 warning1("Already have\n");
307 for (i
=0; i
<ip
->i_listlen
; i
++)
308 warning1("\t%s\n", ip
->i_list
[i
]->i_file
);
313 ip
->i_list
= (struct inclist
**) realloc(ip
->i_list
,
314 sizeof(struct inclist
*) * ++ip
->i_listlen
);
316 ip
->i_list
[ ip
->i_listlen
-1 ] = newfile
;
321 register struct inclist
*ip
;
323 for (ip
= inclist
; ip
< inclistp
; ip
++) {
324 ip
->i_marked
= FALSE
;