security/nss/coreconf/mkdepend/include.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/coreconf/mkdepend/include.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,337 @@
     1.4 +/* $Xorg: include.c,v 1.4 2001/02/09 02:03:16 xorgcvs Exp $ */
     1.5 +/*
     1.6 +
     1.7 +Copyright (c) 1993, 1994, 1998 The Open Group
     1.8 +
     1.9 +Permission to use, copy, modify, distribute, and sell this software and its
    1.10 +documentation for any purpose is hereby granted without fee, provided that
    1.11 +the above copyright notice appear in all copies and that both that
    1.12 +copyright notice and this permission notice appear in supporting
    1.13 +documentation.
    1.14 +
    1.15 +The above copyright notice and this permission notice shall be included in
    1.16 +all copies or substantial portions of the Software.
    1.17 +
    1.18 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.19 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.20 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    1.21 +OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    1.22 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    1.23 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.24 +
    1.25 +Except as contained in this notice, the name of The Open Group shall not be
    1.26 +used in advertising or otherwise to promote the sale, use or other dealings
    1.27 +in this Software without prior written authorization from The Open Group.
    1.28 +
    1.29 +*/
    1.30 +/* $XFree86: xc/config/makedepend/include.c,v 3.7 2001/12/14 19:53:20 dawes Exp $ */
    1.31 +
    1.32 +
    1.33 +#include "def.h"
    1.34 +
    1.35 +#ifdef _MSC_VER
    1.36 +#include <windows.h>
    1.37 +static int
    1.38 +does_file_exist(char *file)
    1.39 +{
    1.40 +  WIN32_FILE_ATTRIBUTE_DATA data;
    1.41 +  BOOL b = GetFileAttributesExA(file, GetFileExInfoStandard, &data);
    1.42 +  if (!b)
    1.43 +    return 0;
    1.44 +  return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
    1.45 +}
    1.46 +#else
    1.47 +static int
    1.48 +does_file_exist(char *file)
    1.49 +{
    1.50 +  struct stat sb;
    1.51 +  return stat(file, &sb) == 0 && !S_ISDIR(sb.st_mode);
    1.52 +}
    1.53 +#endif
    1.54 +
    1.55 +extern struct	inclist	inclist[ MAXFILES ],
    1.56 +			*inclistp, *inclistnext;
    1.57 +extern char	*includedirs[ ],
    1.58 +		**includedirsnext;
    1.59 +extern char	*notdotdot[ ];
    1.60 +extern boolean show_where_not;
    1.61 +extern boolean warn_multiple;
    1.62 +
    1.63 +static boolean
    1.64 +isdot(char *p)
    1.65 +{
    1.66 +	if(p && *p++ == '.' && *p++ == '\0')
    1.67 +		return(TRUE);
    1.68 +	return(FALSE);
    1.69 +}
    1.70 +
    1.71 +static boolean
    1.72 +isdotdot(char *p)
    1.73 +{
    1.74 +	if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
    1.75 +		return(TRUE);
    1.76 +	return(FALSE);
    1.77 +}
    1.78 +
    1.79 +static boolean
    1.80 +issymbolic(char *dir, char *component)
    1.81 +{
    1.82 +#ifdef S_IFLNK
    1.83 +	struct stat	st;
    1.84 +	char	buf[ BUFSIZ ], **pp;
    1.85 +
    1.86 +	sprintf(buf, "%s%s%s", dir, *dir ? "/" : "", component);
    1.87 +	for (pp=notdotdot; *pp; pp++)
    1.88 +		if (strcmp(*pp, buf) == 0)
    1.89 +			return (TRUE);
    1.90 +	if (lstat(buf, &st) == 0
    1.91 +	&& (st.st_mode & S_IFMT) == S_IFLNK) {
    1.92 +		*pp++ = copy(buf);
    1.93 +		if (pp >= &notdotdot[ MAXDIRS ])
    1.94 +			fatalerr("out of .. dirs, increase MAXDIRS\n");
    1.95 +		return(TRUE);
    1.96 +	}
    1.97 +#endif
    1.98 +	return(FALSE);
    1.99 +}
   1.100 +
   1.101 +/*
   1.102 + * Occasionally, pathnames are created that look like .../x/../y
   1.103 + * Any of the 'x/..' sequences within the name can be eliminated.
   1.104 + * (but only if 'x' is not a symbolic link!!)
   1.105 + */
   1.106 +static void
   1.107 +remove_dotdot(char *path)
   1.108 +{
   1.109 +	register char	*end, *from, *to, **cp;
   1.110 +	char		*components[ MAXFILES ],
   1.111 +			newpath[ BUFSIZ ];
   1.112 +	boolean		component_copied;
   1.113 +
   1.114 +	/*
   1.115 +	 * slice path up into components.
   1.116 +	 */
   1.117 +	to = newpath;
   1.118 +	if (*path == '/')
   1.119 +		*to++ = '/';
   1.120 +	*to = '\0';
   1.121 +	cp = components;
   1.122 +	for (from=end=path; *end; end++)
   1.123 +		if (*end == '/') {
   1.124 +			while (*end == '/')
   1.125 +				*end++ = '\0';
   1.126 +			if (*from)
   1.127 +				*cp++ = from;
   1.128 +			from = end;
   1.129 +		}
   1.130 +	*cp++ = from;
   1.131 +	*cp = NULL;
   1.132 +
   1.133 +	/*
   1.134 +	 * Recursively remove all 'x/..' component pairs.
   1.135 +	 */
   1.136 +	cp = components;
   1.137 +	while(*cp) {
   1.138 +		if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
   1.139 +		    && !issymbolic(newpath, *cp))
   1.140 +		{
   1.141 +		    char **fp = cp + 2;
   1.142 +		    char **tp = cp;
   1.143 +
   1.144 +		    do 
   1.145 +			*tp++ = *fp; /* move all the pointers down */
   1.146 +		    while (*fp++);
   1.147 +		    if (cp != components)
   1.148 +			cp--;	/* go back and check for nested ".." */
   1.149 +		} else {
   1.150 +		    cp++;
   1.151 +		}
   1.152 +	}
   1.153 +	/*
   1.154 +	 * Concatenate the remaining path elements.
   1.155 +	 */
   1.156 +	cp = components;
   1.157 +	component_copied = FALSE;
   1.158 +	while(*cp) {
   1.159 +		if (component_copied)
   1.160 +			*to++ = '/';
   1.161 +		component_copied = TRUE;
   1.162 +		for (from = *cp; *from; )
   1.163 +			*to++ = *from++;
   1.164 +		*to = '\0';
   1.165 +		cp++;
   1.166 +	}
   1.167 +	*to++ = '\0';
   1.168 +
   1.169 +	/*
   1.170 +	 * copy the reconstituted path back to our pointer.
   1.171 +	 */
   1.172 +	strcpy(path, newpath);
   1.173 +}
   1.174 +
   1.175 +/*
   1.176 + * Add an include file to the list of those included by 'file'.
   1.177 + */
   1.178 +struct inclist *
   1.179 +newinclude(char *newfile, char *incstring)
   1.180 +{
   1.181 +	register struct inclist	*ip;
   1.182 +
   1.183 +	/*
   1.184 +	 * First, put this file on the global list of include files.
   1.185 +	 */
   1.186 +	ip = inclistp++;
   1.187 +	if (inclistp == inclist + MAXFILES - 1)
   1.188 +		fatalerr("out of space: increase MAXFILES\n");
   1.189 +	ip->i_file = copy(newfile);
   1.190 +
   1.191 +	if (incstring == NULL)
   1.192 +		ip->i_incstring = ip->i_file;
   1.193 +	else
   1.194 +		ip->i_incstring = copy(incstring);
   1.195 +
   1.196 +	inclistnext = inclistp;
   1.197 +	return(ip);
   1.198 +}
   1.199 +
   1.200 +void
   1.201 +included_by(struct inclist *ip, struct inclist *newfile)
   1.202 +{
   1.203 +	register int i;
   1.204 +
   1.205 +	if (ip == NULL)
   1.206 +		return;
   1.207 +	/*
   1.208 +	 * Put this include file (newfile) on the list of files included
   1.209 +	 * by 'file'.  If 'file' is NULL, then it is not an include
   1.210 +	 * file itself (i.e. was probably mentioned on the command line).
   1.211 +	 * If it is already on the list, don't stick it on again.
   1.212 +	 */
   1.213 +	if (ip->i_list == NULL) {
   1.214 +		ip->i_list = (struct inclist **)
   1.215 +			malloc(sizeof(struct inclist *) * ++ip->i_listlen);
   1.216 +		ip->i_merged = (boolean *)
   1.217 +		    malloc(sizeof(boolean) * ip->i_listlen);
   1.218 +	} else {
   1.219 +		for (i=0; i<ip->i_listlen; i++)
   1.220 +			if (ip->i_list[ i ] == newfile) {
   1.221 +			    i = strlen(newfile->i_file);
   1.222 +			    if (!(ip->i_flags & INCLUDED_SYM) &&
   1.223 +				!(i > 2 &&
   1.224 +				  newfile->i_file[i-1] == 'c' &&
   1.225 +				  newfile->i_file[i-2] == '.'))
   1.226 +			    {
   1.227 +				/* only bitch if ip has */
   1.228 +				/* no #include SYMBOL lines  */
   1.229 +				/* and is not a .c file */
   1.230 +				if (warn_multiple)
   1.231 +				{
   1.232 +					warning("%s includes %s more than once!\n",
   1.233 +						ip->i_file, newfile->i_file);
   1.234 +					warning1("Already have\n");
   1.235 +					for (i=0; i<ip->i_listlen; i++)
   1.236 +						warning1("\t%s\n", ip->i_list[i]->i_file);
   1.237 +				}
   1.238 +			    }
   1.239 +			    return;
   1.240 +			}
   1.241 +		ip->i_list = (struct inclist **) realloc(ip->i_list,
   1.242 +			sizeof(struct inclist *) * ++ip->i_listlen);
   1.243 +		ip->i_merged = (boolean *)
   1.244 +		    realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen);
   1.245 +	}
   1.246 +	ip->i_list[ ip->i_listlen-1 ] = newfile;
   1.247 +	ip->i_merged[ ip->i_listlen-1 ] = FALSE;
   1.248 +}
   1.249 +
   1.250 +void
   1.251 +inc_clean (void)
   1.252 +{
   1.253 +	register struct inclist *ip;
   1.254 +
   1.255 +	for (ip = inclist; ip < inclistp; ip++) {
   1.256 +		ip->i_flags &= ~MARKED;
   1.257 +	}
   1.258 +}
   1.259 +
   1.260 +struct inclist *
   1.261 +inc_path(char *file, char *include, int type)
   1.262 +{
   1.263 +	static char		path[ BUFSIZ ];
   1.264 +	register char		**pp, *p;
   1.265 +	register struct inclist	*ip;
   1.266 +
   1.267 +	/*
   1.268 +	 * Check all previously found include files for a path that
   1.269 +	 * has already been expanded.
   1.270 +	 */
   1.271 +	if ((type == INCLUDE) || (type == INCLUDEDOT))
   1.272 +		inclistnext = inclist;
   1.273 +	ip = inclistnext;
   1.274 +
   1.275 +	for (; ip->i_file; ip++) {
   1.276 +		if ((strcmp(ip->i_incstring, include) == 0) &&
   1.277 +		    !(ip->i_flags & INCLUDED_SYM)) {
   1.278 +			inclistnext = ip + 1;
   1.279 +			return ip;
   1.280 +		}
   1.281 +	}
   1.282 +
   1.283 +	if (inclistnext == inclist) {
   1.284 +		/*
   1.285 +		 * If the path was surrounded by "" or is an absolute path,
   1.286 +		 * then check the exact path provided.
   1.287 +		 */
   1.288 +		if ((type == INCLUDEDOT) ||
   1.289 +		    (type == INCLUDENEXTDOT) ||
   1.290 +		    (*include == '/')) {
   1.291 +			if (does_file_exist(include))
   1.292 +				return newinclude(include, include);
   1.293 +			if (show_where_not)
   1.294 +				warning1("\tnot in %s\n", include);
   1.295 +		}
   1.296 +
   1.297 +		/*
   1.298 +		 * If the path was surrounded by "" see if this include file is
   1.299 +		 * in the directory of the file being parsed.
   1.300 +		 */
   1.301 +		if ((type == INCLUDEDOT) || (type == INCLUDENEXTDOT)) {
   1.302 +			for (p=file+strlen(file); p>file; p--)
   1.303 +				if (*p == '/')
   1.304 +					break;
   1.305 +			if (p == file) {
   1.306 +				strcpy(path, include);
   1.307 +			} else {
   1.308 +				strncpy(path, file, (p-file) + 1);
   1.309 +				path[ (p-file) + 1 ] = '\0';
   1.310 +				strcpy(path + (p-file) + 1, include);
   1.311 +			}
   1.312 +			remove_dotdot(path);
   1.313 +			if (does_file_exist(path))
   1.314 +				return newinclude(path, include);
   1.315 +			if (show_where_not)
   1.316 +				warning1("\tnot in %s\n", path);
   1.317 +		}
   1.318 +	}
   1.319 +
   1.320 +	/*
   1.321 +	 * Check the include directories specified.  Standard include dirs
   1.322 +	 * should be at the end.
   1.323 +	 */
   1.324 +	if ((type == INCLUDE) || (type == INCLUDEDOT))
   1.325 +		includedirsnext = includedirs;
   1.326 +	pp = includedirsnext;
   1.327 +
   1.328 +	for (; *pp; pp++) {
   1.329 +		sprintf(path, "%s/%s", *pp, include);
   1.330 +		remove_dotdot(path);
   1.331 +		if (does_file_exist(path)) {
   1.332 +			includedirsnext = pp + 1;
   1.333 +			return newinclude(path, include);
   1.334 +		}
   1.335 +		if (show_where_not)
   1.336 +			warning1("\tnot in %s\n", path);
   1.337 +	}
   1.338 +
   1.339 +	return NULL;
   1.340 +}

mercurial