include.c 7.9 KB

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