2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: include.c /main/21 1998/02/06 11:10:06 kaleb $ */
26 Copyright (c) 1993, 1994, 1998 The Open Group
30 The above copyright notice and this permission notice shall be included in
31 all copies or substantial portions of the Software.
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
37 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
38 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 Except as contained in this notice, the name of The Open Group shall not be
41 used in advertising or otherwise to promote the sale, use or other dealings
42 in this Software without prior written authorization from The Open Group.
49 extern struct inclist inclist[ MAXFILES ],
51 extern char *includedirs[ ];
52 extern char *notdotdot[ ];
53 extern boolean show_where_not;
54 extern boolean warn_multiple;
60 if(p && *p++ == '.' && *p++ == '\0')
69 if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
75 issymbolic(dir, component)
76 register char *dir, *component;
80 char buf[ BUFSIZ ], **pp;
82 sprintf(buf, "%s%s%s", dir, *dir ? "/" : "", component);
83 for (pp=notdotdot; *pp; pp++)
84 if (strcmp(*pp, buf) == 0)
86 if (lstat(buf, &st) == 0
87 && (st.st_mode & S_IFMT) == S_IFLNK) {
89 if (pp >= ¬dotdot[ MAXDIRS ])
90 fatalerr("out of .. dirs, increase MAXDIRS\n");
98 * Occasionally, pathnames are created that look like .../x/../y
99 * Any of the 'x/..' sequences within the name can be eliminated.
100 * (but only if 'x' is not a symbolic link!!)
106 register char *end, *from, *to, **cp;
107 char *components[ MAXFILES ],
109 boolean component_copied;
112 * slice path up into components.
119 for (from=end=path; *end; end++)
131 * Recursively remove all 'x/..' component pairs.
135 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
136 && !issymbolic(newpath, *cp))
142 *tp++ = *fp; /* move all the pointers down */
144 if (cp != components)
145 cp--; /* go back and check for nested ".." */
151 * Concatenate the remaining path elements.
154 component_copied = FALSE;
156 if (component_copied)
158 component_copied = TRUE;
159 for (from = *cp; *from; )
167 * copy the reconstituted path back to our pointer.
169 strcpy(path, newpath);
173 * Add an include file to the list of those included by 'file'.
175 struct inclist *newinclude(newfile, incstring)
176 register char *newfile, *incstring;
178 register struct inclist *ip;
181 * First, put this file on the global list of include files.
184 if (inclistp == inclist + MAXFILES - 1)
185 fatalerr("out of space: increase MAXFILES\n");
186 ip->i_file = copy(newfile);
188 if (incstring == NULL)
189 ip->i_incstring = ip->i_file;
191 ip->i_incstring = copy(incstring);
197 included_by(ip, newfile)
198 register struct inclist *ip, *newfile;
205 * Put this include file (newfile) on the list of files included
206 * by 'file'. If 'file' is NULL, then it is not an include
207 * file itself (i.e. was probably mentioned on the command line).
208 * If it is already on the list, don't stick it on again.
210 if (ip->i_list == NULL) {
211 ip->i_list = (struct inclist **)
212 malloc(sizeof(struct inclist *) * ++ip->i_listlen);
213 ip->i_merged = (boolean *)
214 malloc(sizeof(boolean) * ip->i_listlen);
216 for (i=0; i<ip->i_listlen; i++)
217 if (ip->i_list[ i ] == newfile) {
218 i = strlen(newfile->i_file);
219 if (!(ip->i_flags & INCLUDED_SYM) &&
221 newfile->i_file[i-1] == 'c' &&
222 newfile->i_file[i-2] == '.'))
224 /* only bitch if ip has */
225 /* no #include SYMBOL lines */
226 /* and is not a .c file */
229 warning("%s includes %s more than once!\n",
230 ip->i_file, newfile->i_file);
231 warning1("Already have\n");
232 for (i=0; i<ip->i_listlen; i++)
233 warning1("\t%s\n", ip->i_list[i]->i_file);
238 ip->i_list = (struct inclist **) realloc(ip->i_list,
239 sizeof(struct inclist *) * ++ip->i_listlen);
240 ip->i_merged = (boolean *)
241 realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen);
243 ip->i_list[ ip->i_listlen-1 ] = newfile;
244 ip->i_merged[ ip->i_listlen-1 ] = FALSE;
250 register struct inclist *ip;
252 for (ip = inclist; ip < inclistp; ip++) {
253 ip->i_flags &= ~MARKED;
257 struct inclist *inc_path(file, include, dot)
262 static char path[ BUFSIZ ];
263 register char **pp, *p;
264 register struct inclist *ip;
266 boolean found = FALSE;
269 * Check all previously found include files for a path that
270 * has already been expanded.
272 for (ip = inclist; ip->i_file; ip++)
273 if ((strcmp(ip->i_incstring, include) == 0) &&
274 !(ip->i_flags & INCLUDED_SYM))
281 * If the path was surrounded by "" or is an absolute path,
282 * then check the exact path provided.
284 if (!found && (dot || *include == '/')) {
285 if (stat(include, &st) == 0) {
286 ip = newinclude(include, include);
289 else if (show_where_not)
290 warning1("\tnot in %s\n", include);
294 * If the path was surrounded by "" see if this include file is in the
295 * directory of the file being parsed.
298 for (p=file+strlen(file); p>file; p--)
302 strcpy(path, include);
304 strncpy(path, file, (p-file) + 1);
305 path[ (p-file) + 1 ] = '\0';
306 strcpy(path + (p-file) + 1, include);
309 if (stat(path, &st) == 0) {
310 ip = newinclude(path, include);
313 else if (show_where_not)
314 warning1("\tnot in %s\n", path);
318 * Check the include directories specified. (standard include dir
319 * should be at the end.)
322 for (pp = includedirs; *pp; pp++) {
323 sprintf(path, "%s/%s", *pp, include);
325 if (stat(path, &st) == 0) {
326 ip = newinclude(path, include);
330 else if (show_where_not)
331 warning1("\tnot in %s\n", path);