You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

include.c 7.2KB

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