From owner-svn-src-projects@FreeBSD.ORG Mon Jan 11 10:30:58 2010 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4643F106566C; Mon, 11 Jan 2010 10:30:58 +0000 (UTC) (envelope-from rodrigc@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 365878FC08; Mon, 11 Jan 2010 10:30:58 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0BAUwgR029578; Mon, 11 Jan 2010 10:30:58 GMT (envelope-from rodrigc@svn.freebsd.org) Received: (from rodrigc@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0BAUwXX029575; Mon, 11 Jan 2010 10:30:58 GMT (envelope-from rodrigc@svn.freebsd.org) Message-Id: <201001111030.o0BAUwXX029575@svn.freebsd.org> From: Craig Rodrigues Date: Mon, 11 Jan 2010 10:30:58 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202075 - projects/jbuild/usr.bin/make X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Jan 2010 10:30:58 -0000 Author: rodrigc Date: Mon Jan 11 10:30:57 2010 New Revision: 202075 URL: http://svn.freebsd.org/changeset/base/202075 Log: Add Dir_FindHereOrAbove() function from NetBSD: revision 1.40 date: 2004/02/03 19:25:29; author: chuck; state: Exp; lines: +86 -3 add parent directory search for make as discussed on tech-toolchain. - new dir.c function: Dir_FindHereOrAbove: Search for a path in the current directory and then all the directories above it in turn until the path is found or we reach the root ("/"). Submitted by: obrien, Simon Gerarty Obtained from: NetBSD Modified: projects/jbuild/usr.bin/make/dir.c projects/jbuild/usr.bin/make/dir.h Modified: projects/jbuild/usr.bin/make/dir.c ============================================================================== --- projects/jbuild/usr.bin/make/dir.c Mon Jan 11 10:16:12 2010 (r202074) +++ projects/jbuild/usr.bin/make/dir.c Mon Jan 11 10:30:57 2010 (r202075) @@ -61,6 +61,10 @@ __FBSDID("$FreeBSD$"); * If it exists, the entire path is returned. * Otherwise NULL is returned. * + * Dir_FindHereOrAbove Search for a path in the current directory and + * then all the directories above it in turn until + * the path is found or we reach the root ("/"). + * * Dir_MTime Return the modification time of a node. The file * is searched for along the default search path. * The path and mtime fields of the node are filled in. @@ -83,7 +87,7 @@ __FBSDID("$FreeBSD$"); * Dir_PrintDirectories Print stats about the directory cache. */ -#include +#include #include #include #include @@ -655,7 +659,7 @@ Path_FindFile(char *name, struct Path *p * (fish.c) and what pmake finds (./fish.c). */ if ((!hasSlash || (cp - name == 2 && *name == '.')) && - (Hash_FindEntry(&dot->files, cp) != NULL)) { + (dot != NULL && Hash_FindEntry(&dot->files, cp) != NULL)) { DEBUGF(DIR, ("in '.'\n")); hits += 1; dot->hits += 1; @@ -851,6 +855,83 @@ Path_FindFile(char *name, struct Path *p /*- *----------------------------------------------------------------------- + * Dir_FindHereOrAbove -- + * search for a path starting at a given directory and then working + * our way up towards the root. + * + * Input: + * here starting directory + * search_path the path we are looking for + * result the result of a successful search is placed here + * rlen the length of the result buffer + * (typically MAXPATHLEN + 1) + * + * Results: + * 0 on failure, 1 on success [in which case the found path is put + * in the result buffer]. + * + * Side Effects: + *----------------------------------------------------------------------- + */ +int +Dir_FindHereOrAbove(char *here, char *search_path, char *result, int rlen) +{ + struct stat st; + char dirbase[MAXPATHLEN + 1], *db_end; + char try[MAXPATHLEN + 1], *try_end; + + /* copy out our starting point */ + snprintf(dirbase, sizeof(dirbase), "%s", here); + db_end = dirbase + strlen(dirbase); + + /* loop until we determine a result */ + while (1) { + /* try and stat(2) it ... */ + snprintf(try, sizeof(try), "%s/%s", dirbase, search_path); + if (stat(try, &st) != -1) { + /* + * Success! If we found a file, chop off + * the filename so we return a directory. + */ + if ((st.st_mode & S_IFMT) != S_IFDIR) { + try_end = try + strlen(try); + while (try_end > try && *try_end != '/') + try_end--; + if (try_end > try) + *try_end = 0; /* chop! */ + } + + /* + * Done! + */ + snprintf(result, rlen, "%s", try); + return(1); + } + + /* + * Nope, we didn't find it. If we used up dirbase we've + * reached the root and failed. + */ + if (db_end == dirbase) + break; /* Failed! */ + + /* + * truncate dirbase from the end to move up a dir + */ + while (db_end > dirbase && *db_end != '/') + db_end--; + *db_end = 0; /* chop! */ + + } /* while (1) */ + + /* + * We failed... + */ + return(0); +} + +/*- + *----------------------------------------------------------------------- * Dir_MTime -- * Find the modification time of the file described by gn along the * search path dirSearchPath. Modified: projects/jbuild/usr.bin/make/dir.h ============================================================================== --- projects/jbuild/usr.bin/make/dir.h Mon Jan 11 10:16:12 2010 (r202074) +++ projects/jbuild/usr.bin/make/dir.h Mon Jan 11 10:30:57 2010 (r202075) @@ -72,5 +72,6 @@ void Path_Print(const struct Path *); typedef void path_listcb_t(char *, size_t, const char *); void Path_List(const struct Path *, path_listcb_t *, char *, size_t); #endif +int Dir_FindHereOrAbove(char *, char *, char *, int); #endif /* dir_h_6002e3b8 */