include.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright (c) 1993, 1994, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. */
  20. #include "def.h"
  21. static boolean
  22. isdot(const char *p)
  23. {
  24. if(p && *p++ == '.' && *p++ == '\0')
  25. return(TRUE);
  26. return(FALSE);
  27. }
  28. static boolean
  29. isdotdot(const char *p)
  30. {
  31. if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
  32. return(TRUE);
  33. return(FALSE);
  34. }
  35. static boolean
  36. issymbolic(const char *dir, const char *component)
  37. {
  38. #ifdef S_IFLNK
  39. struct stat st;
  40. char buf[ BUFSIZ ], **pp;
  41. snprintf(buf, sizeof(buf), "%s%s%s", dir, *dir ? "/" : "", component);
  42. for (pp=notdotdot; *pp; pp++)
  43. if (strcmp(*pp, buf) == 0)
  44. return (TRUE);
  45. if (lstat(buf, &st) == 0
  46. && (st.st_mode & S_IFMT) == S_IFLNK) {
  47. *pp++ = strdup(buf);
  48. if (pp >= &notdotdot[ MAXDIRS ])
  49. fatalerr("out of .. dirs, increase MAXDIRS\n");
  50. return(TRUE);
  51. }
  52. #endif
  53. return(FALSE);
  54. }
  55. /*
  56. * Occasionally, pathnames are created that look like .../x/../y
  57. * Any of the 'x/..' sequences within the name can be eliminated.
  58. * (but only if 'x' is not a symbolic link!!)
  59. */
  60. static void
  61. remove_dotdot(char *path)
  62. {
  63. register char *end, *from, *to, **cp;
  64. char *components[ MAXFILES ],
  65. newpath[ BUFSIZ ];
  66. boolean component_copied;
  67. /*
  68. * slice path up into components.
  69. */
  70. to = newpath;
  71. if (*path == '/')
  72. *to++ = '/';
  73. *to = '\0';
  74. cp = components;
  75. for (from=end=path; *end; end++)
  76. if (*end == '/') {
  77. while (*end == '/')
  78. *end++ = '\0';
  79. if (*from)
  80. *cp++ = from;
  81. from = end;
  82. }
  83. *cp++ = from;
  84. *cp = NULL;
  85. /*
  86. * Recursively remove all 'x/..' component pairs.
  87. */
  88. cp = components;
  89. while(*cp) {
  90. if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
  91. && !issymbolic(newpath, *cp))
  92. {
  93. char **fp = cp + 2;
  94. char **tp = cp;
  95. do
  96. *tp++ = *fp; /* move all the pointers down */
  97. while (*fp++);
  98. if (cp != components)
  99. cp--; /* go back and check for nested ".." */
  100. } else {
  101. cp++;
  102. }
  103. }
  104. /*
  105. * Concatenate the remaining path elements.
  106. */
  107. cp = components;
  108. component_copied = FALSE;
  109. while(*cp) {
  110. if (component_copied)
  111. *to++ = '/';
  112. component_copied = TRUE;
  113. for (from = *cp; *from; )
  114. *to++ = *from++;
  115. *to = '\0';
  116. cp++;
  117. }
  118. *to++ = '\0';
  119. /*
  120. * copy the reconstituted path back to our pointer.
  121. */
  122. strcpy(path, newpath);
  123. }
  124. /*
  125. * Add an include file to the list of those included by 'file'.
  126. */
  127. struct inclist *
  128. newinclude(const char *newfile, const char *incstring)
  129. {
  130. register struct inclist *ip;
  131. /*
  132. * First, put this file on the global list of include files.
  133. */
  134. ip = inclistp++;
  135. if (inclistp == inclist + MAXFILES - 1)
  136. fatalerr("out of space: increase MAXFILES\n");
  137. ip->i_file = strdup(newfile);
  138. if (incstring == NULL)
  139. ip->i_incstring = ip->i_file;
  140. else
  141. ip->i_incstring = strdup(incstring);
  142. inclistnext = inclistp;
  143. return(ip);
  144. }
  145. void
  146. included_by(struct inclist *ip, struct inclist *newfile)
  147. {
  148. register int i;
  149. if (ip == NULL)
  150. return;
  151. /*
  152. * Put this include file (newfile) on the list of files included
  153. * by 'file'. If 'file' is NULL, then it is not an include
  154. * file itself (i.e. was probably mentioned on the command line).
  155. * If it is already on the list, don't stick it on again.
  156. */
  157. if (ip->i_list == NULL) {
  158. ip->i_list = malloc(sizeof(struct inclist *) * ++ip->i_listlen);
  159. ip->i_merged = malloc(sizeof(boolean) * ip->i_listlen);
  160. } else {
  161. for (i=0; i<ip->i_listlen; i++)
  162. if (ip->i_list[ i ] == newfile) {
  163. i = strlen(newfile->i_file);
  164. if (!(ip->i_flags & INCLUDED_SYM) &&
  165. !(i > 2 &&
  166. newfile->i_file[i-1] == 'c' &&
  167. newfile->i_file[i-2] == '.'))
  168. {
  169. /* only bitch if ip has */
  170. /* no #include SYMBOL lines */
  171. /* and is not a .c file */
  172. if (warn_multiple)
  173. {
  174. warning("%s includes %s more than once!\n",
  175. ip->i_file, newfile->i_file);
  176. warning1("Already have\n");
  177. for (i=0; i<ip->i_listlen; i++)
  178. warning1("\t%s\n", ip->i_list[i]->i_file);
  179. }
  180. }
  181. return;
  182. }
  183. ip->i_list = realloc(ip->i_list,
  184. sizeof(struct inclist *) * ++ip->i_listlen);
  185. ip->i_merged =
  186. realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen);
  187. }
  188. ip->i_list[ ip->i_listlen-1 ] = newfile;
  189. ip->i_merged[ ip->i_listlen-1 ] = FALSE;
  190. }
  191. void
  192. inc_clean (void)
  193. {
  194. register struct inclist *ip;
  195. for (ip = inclist; ip < inclistp; ip++) {
  196. ip->i_flags &= ~MARKED;
  197. }
  198. }
  199. struct inclist *
  200. inc_path(const char *file, const char *include, int type)
  201. {
  202. static char path[ BUFSIZ ];
  203. register const char **pp, *p;
  204. register struct inclist *ip;
  205. struct stat st;
  206. /*
  207. * Check all previously found include files for a path that
  208. * has already been expanded.
  209. */
  210. if ((type == INCLUDE) || (type == INCLUDEDOT))
  211. inclistnext = inclist;
  212. ip = inclistnext;
  213. for (; ip->i_file; ip++) {
  214. if ((strcmp(ip->i_incstring, include) == 0) &&
  215. !(ip->i_flags & INCLUDED_SYM)) {
  216. inclistnext = ip + 1;
  217. return ip;
  218. }
  219. }
  220. if (inclistnext == inclist) {
  221. /*
  222. * If the path was surrounded by "" or is an absolute path,
  223. * then check the exact path provided.
  224. */
  225. if ((type == INCLUDEDOT) ||
  226. (type == INCLUDENEXTDOT) ||
  227. (*include == '/')) {
  228. if (stat(include, &st) == 0 && !S_ISDIR(st.st_mode))
  229. return newinclude(include, include);
  230. if (show_where_not)
  231. warning1("\tnot in %s\n", include);
  232. }
  233. /*
  234. * If the path was surrounded by "" see if this include file is
  235. * in the directory of the file being parsed.
  236. */
  237. if ((type == INCLUDEDOT) || (type == INCLUDENEXTDOT)) {
  238. for (p=file+strlen(file); p>file; p--)
  239. if (*p == '/')
  240. break;
  241. if (p == file) {
  242. strcpy(path, include);
  243. } else {
  244. strncpy(path, file, (p-file) + 1);
  245. path[ (p-file) + 1 ] = '\0';
  246. strcpy(path + (p-file) + 1, include);
  247. }
  248. remove_dotdot(path);
  249. if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode))
  250. return newinclude(path, include);
  251. if (show_where_not)
  252. warning1("\tnot in %s\n", path);
  253. }
  254. }
  255. /*
  256. * Check the include directories specified. Standard include dirs
  257. * should be at the end.
  258. */
  259. if ((type == INCLUDE) || (type == INCLUDEDOT))
  260. includedirsnext = includedirs;
  261. pp = includedirsnext;
  262. for (; *pp; pp++) {
  263. snprintf(path, sizeof(path), "%s/%s", *pp, include);
  264. remove_dotdot(path);
  265. if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
  266. includedirsnext = pp + 1;
  267. return newinclude(path, include);
  268. }
  269. if (show_where_not)
  270. warning1("\tnot in %s\n", path);
  271. }
  272. return NULL;
  273. }