From owner-svn-src-head@FreeBSD.ORG Wed Jul 10 21:37:51 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 6D88FB80; Wed, 10 Jul 2013 21:37:51 +0000 (UTC) (envelope-from marcel@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id 408AF1D11; Wed, 10 Jul 2013 21:37:51 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r6ALbpjk094268; Wed, 10 Jul 2013 21:37:51 GMT (envelope-from marcel@svn.freebsd.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r6ALbpl7094267; Wed, 10 Jul 2013 21:37:51 GMT (envelope-from marcel@svn.freebsd.org) Message-Id: <201307102137.r6ALbpl7094267@svn.freebsd.org> From: Marcel Moolenaar Date: Wed, 10 Jul 2013 21:37:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r253172 - head/sys/boot/ficl X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jul 2013 21:37:51 -0000 Author: marcel Date: Wed Jul 10 21:37:50 2013 New Revision: 253172 URL: http://svnweb.freebsd.org/changeset/base/253172 Log: Add 2 builtin words for working with directories: isdir? ( fd -- bool ) freaddir ( fd -- ptr len TRUE | FALSE ) The 'isdir?' word returns `true' if the file descriptor is for a directory and `false' otherwise. The 'freaddir' word reads the next directory entry and if successful, returns its name and 'true'. Otherwise 'false' is returned. These words give the loader the ability to scan directories and read files contained in them for 'rc.d'-like flexibility in handling which modules to load and/or which tunables to set. Obtained from: Juniper Networks, Inc. Modified: head/sys/boot/ficl/loader.c Modified: head/sys/boot/ficl/loader.c ============================================================================== --- head/sys/boot/ficl/loader.c Wed Jul 10 21:05:03 2013 (r253171) +++ head/sys/boot/ficl/loader.c Wed Jul 10 21:37:50 2013 (r253172) @@ -404,6 +404,34 @@ static void displayCellNoPad(FICL_VM *pV return; } +/* isdir? - Return whether an fd corresponds to a directory. + * + * isdir? ( fd -- bool ) + */ +static void isdirQuestion(FICL_VM *pVM) +{ + struct stat sb; + FICL_INT flag; + int fd; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 1, 1); +#endif + + fd = stackPopINT(pVM->pStack); + flag = FICL_FALSE; + do { + if (fd < 0) + break; + if (fstat(fd, &sb) < 0) + break; + if (!S_ISDIR(sb.st_mode)) + break; + flag = FICL_TRUE; + } while (0); + stackPushINT(pVM->pStack, flag); +} + /* fopen - open a file and return new fd on stack. * * fopen ( ptr count mode -- fd ) @@ -477,6 +505,30 @@ static void pfread(FICL_VM *pVM) return; } +/* freaddir - read directory contents + * + * freaddir ( fd -- ptr len TRUE | FALSE ) + */ +static void pfreaddir(FICL_VM *pVM) +{ + struct dirent *d; + int fd; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 1, 3); +#endif + + fd = stackPopINT(pVM->pStack); + d = readdirfd(fd); + if (d != NULL) { + stackPushPtr(pVM->pStack, d->d_name); + stackPushINT(pVM->pStack, strlen(d->d_name)); + stackPushINT(pVM->pStack, FICL_TRUE); + } else { + stackPushINT(pVM->pStack, FICL_FALSE); + } +} + /* fload - interpret file contents * * fload ( fd -- ) @@ -653,9 +705,11 @@ void ficlCompilePlatform(FICL_SYSTEM *pS assert (dp); dictAppendWord(dp, ".#", displayCellNoPad, FW_DEFAULT); + dictAppendWord(dp, "isdir?", isdirQuestion, FW_DEFAULT); dictAppendWord(dp, "fopen", pfopen, FW_DEFAULT); dictAppendWord(dp, "fclose", pfclose, FW_DEFAULT); dictAppendWord(dp, "fread", pfread, FW_DEFAULT); + dictAppendWord(dp, "freaddir", pfreaddir, FW_DEFAULT); dictAppendWord(dp, "fload", pfload, FW_DEFAULT); dictAppendWord(dp, "fkey", fkey, FW_DEFAULT); dictAppendWord(dp, "fseek", pfseek, FW_DEFAULT);