dtudcfonted: Resolve CID 86280
[oweals/cde.git] / cde / config / makedepend / include.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: include.c /main/21 1998/02/06 11:10:06 kaleb $ */
24 /*
25
26 Copyright (c) 1993, 1994, 1998 The Open Group
27
28 All Rights Reserved.
29
30 The above copyright notice and this permission notice shall be included in
31 all copies or substantial portions of the Software.
32
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.
39
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.
43
44 */
45
46
47 #include "def.h"
48
49 extern struct   inclist inclist[ MAXFILES ],
50                         *inclistp;
51 extern char     *includedirs[ ];
52 extern char     *notdotdot[ ];
53 extern boolean show_where_not;
54 extern boolean warn_multiple;
55
56 boolean
57 isdot(p)
58         register char   *p;
59 {
60         if(p && *p++ == '.' && *p++ == '\0')
61                 return(TRUE);
62         return(FALSE);
63 }
64
65 boolean
66 isdotdot(p)
67         register char   *p;
68 {
69         if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
70                 return(TRUE);
71         return(FALSE);
72 }
73
74 boolean
75 issymbolic(dir, component)
76         register char   *dir, *component;
77 {
78 #ifdef S_IFLNK
79         struct stat     st;
80         char    buf[ BUFSIZ ], **pp;
81
82         snprintf(buf, BUFSIZ, "%s%s%s", dir, *dir ? "/" : "", component);
83         for (pp=notdotdot; *pp; pp++)
84                 if (strcmp(*pp, buf) == 0)
85                         return (TRUE);
86         if (lstat(buf, &st) == 0
87         && (st.st_mode & S_IFMT) == S_IFLNK) {
88                 *pp++ = copy(buf);
89                 if (pp >= &notdotdot[ MAXDIRS ])
90                         fatalerr("out of .. dirs, increase MAXDIRS\n");
91                 return(TRUE);
92         }
93 #endif
94         return(FALSE);
95 }
96
97 /*
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!!)
101  */
102 void
103 remove_dotdot(path)
104         char    *path;
105 {
106         register char   *end, *from, *to, **cp;
107         char            *components[ MAXFILES ],
108                         newpath[ BUFSIZ ];
109         boolean         component_copied;
110
111         /*
112          * slice path up into components.
113          */
114         to = newpath;
115         if (*path == '/')
116                 *to++ = '/';
117         *to = '\0';
118         cp = components;
119         for (from=end=path; *end; end++)
120                 if (*end == '/') {
121                         while (*end == '/')
122                                 *end++ = '\0';
123                         if (*from)
124                                 *cp++ = from;
125                         from = end;
126                 }
127         *cp++ = from;
128         *cp = NULL;
129
130         /*
131          * Recursively remove all 'x/..' component pairs.
132          */
133         cp = components;
134         while(*cp) {
135                 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
136                     && !issymbolic(newpath, *cp))
137                 {
138                     char **fp = cp + 2;
139                     char **tp = cp;
140
141                     do 
142                         *tp++ = *fp; /* move all the pointers down */
143                     while (*fp++);
144                     if (cp != components)
145                         cp--;   /* go back and check for nested ".." */
146                 } else {
147                     cp++;
148                 }
149         }
150         /*
151          * Concatenate the remaining path elements.
152          */
153         cp = components;
154         component_copied = FALSE;
155         while(*cp) {
156                 if (component_copied)
157                         *to++ = '/';
158                 component_copied = TRUE;
159                 for (from = *cp; *from; )
160                         *to++ = *from++;
161                 *to = '\0';
162                 cp++;
163         }
164         *to++ = '\0';
165
166         /*
167          * copy the reconstituted path back to our pointer.
168          */
169         strncpy(path, newpath, BUFSIZ);
170 }
171
172 /*
173  * Add an include file to the list of those included by 'file'.
174  */
175 struct inclist *newinclude(newfile, incstring)
176         register char   *newfile, *incstring;
177 {
178         register struct inclist *ip;
179
180         /*
181          * First, put this file on the global list of include files.
182          */
183         ip = inclistp++;
184         if (inclistp == inclist + MAXFILES - 1)
185                 fatalerr("out of space: increase MAXFILES\n");
186         ip->i_file = copy(newfile);
187
188         if (incstring == NULL)
189                 ip->i_incstring = ip->i_file;
190         else
191                 ip->i_incstring = copy(incstring);
192
193         return(ip);
194 }
195
196 void
197 included_by(ip, newfile)
198         register struct inclist *ip, *newfile;
199 {
200         register int i;
201
202         if (ip == NULL)
203                 return;
204         /*
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.
209          */
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);
215         } else {
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) &&
220                                 !(i > 2 &&
221                                   newfile->i_file[i-1] == 'c' &&
222                                   newfile->i_file[i-2] == '.'))
223                             {
224                                 /* only bitch if ip has */
225                                 /* no #include SYMBOL lines  */
226                                 /* and is not a .c file */
227                                 if (warn_multiple)
228                                 {
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);
234                                 }
235                             }
236                             return;
237                         }
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);
242         }
243         ip->i_list[ ip->i_listlen-1 ] = newfile;
244         ip->i_merged[ ip->i_listlen-1 ] = FALSE;
245 }
246
247 void
248 inc_clean ()
249 {
250         register struct inclist *ip;
251
252         for (ip = inclist; ip < inclistp; ip++) {
253                 ip->i_flags &= ~MARKED;
254         }
255 }
256
257 struct inclist *inc_path(file, include, dot)
258         register char   *file,
259                         *include;
260         boolean dot;
261 {
262         static char     path[ BUFSIZ ];
263         register char           **pp, *p;
264         register struct inclist *ip;
265         struct stat     st;
266         boolean found = FALSE;
267
268         /*
269          * Check all previously found include files for a path that
270          * has already been expanded.
271          */
272         for (ip = inclist; ip->i_file; ip++)
273             if ((strcmp(ip->i_incstring, include) == 0) &&
274                 !(ip->i_flags & INCLUDED_SYM))
275             {
276                 found = TRUE;
277                 break;
278             }
279
280         /*
281          * If the path was surrounded by "" or is an absolute path,
282          * then check the exact path provided.
283          */
284         if (!found && (dot || *include == '/')) {
285                 if (stat(include, &st) == 0) {
286                         ip = newinclude(include, include);
287                         found = TRUE;
288                 }
289                 else if (show_where_not)
290                         warning1("\tnot in %s\n", include);
291         }
292
293         /*
294          * If the path was surrounded by "" see if this include file is in the
295          * directory of the file being parsed.
296          */
297         if (!found && dot) {
298                 for (p=file+strlen(file); p>file; p--)
299                         if (*p == '/')
300                                 break;
301                 if (p == file)
302                         strncpy(path, include, BUFSIZ);
303                 else {
304                         strncpy(path, file, (p-file) + 1);
305                         path[ (p-file) + 1 ] = '\0';
306                         strncpy(path + (p-file) + 1, include,
307                                                 BUFSIZ - (p-file) - 1);
308                 }
309                 remove_dotdot(path);
310                 if (stat(path, &st) == 0) {
311                         ip = newinclude(path, include);
312                         found = TRUE;
313                 }
314                 else if (show_where_not)
315                         warning1("\tnot in %s\n", path);
316         }
317
318         /*
319          * Check the include directories specified. (standard include dir
320          * should be at the end.)
321          */
322         if (!found)
323                 for (pp = includedirs; *pp; pp++) {
324                         snprintf(path, BUFSIZ, "%s/%s", *pp, include);
325                         remove_dotdot(path);
326                         if (stat(path, &st) == 0) {
327                                 ip = newinclude(path, include);
328                                 found = TRUE;
329                                 break;
330                         }
331                         else if (show_where_not)
332                                 warning1("\tnot in %s\n", path);
333                 }
334
335         if (!found)
336                 ip = NULL;
337         return(ip);
338 }