da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1985-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Glenn Fowler
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Research
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * include style search support
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define directory(p,s) (stat((p),(s))>=0&&S_ISDIR((s)->st_mode))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define regular(p,s) (stat((p),(s))>=0&&(S_ISREG((s)->st_mode)||streq(p,"/dev/null")))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic struct /* directory list state */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * append dir to pathfind() include list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (dir && *dir && !streq(dir, ".") && directory(dir, &st))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * return path to name using pathinclude() list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * path placed in <buf,size>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * if lib!=0 then pathpath() attempted after include search
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * if type!=0 and name has no '.' then file.type also attempted
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * any *: prefix in lib is ignored (discipline library dictionary support)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpathfind(const char* name, const char* lib, const char* type, char* buf, size_t size)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* s;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (((s = strrchr(name, '/')) || (s = (char*)name)) && strchr(s, '.'))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * always check the unadorned path first
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * this handles . and absolute paths
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * check the directory of the including file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * on the assumption that error_info.file is properly stacked
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (error_info.file && (s = strrchr(error_info.file, '/')))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfsprintf(buf, size, "%-.*s%s", s - error_info.file + 1, error_info.file, name);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfsprintf(buf, size, "%-.*s%s%.s", s - error_info.file + 1, error_info.file, name, type);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * check the include dir list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfsprintf(tmp, sizeof(tmp), "%s/%s.%s", dp->dir, name, type);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * finally a lib related search on PATH