Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Jul 2013 21:37:51 +0000 (UTC)
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r253172 - head/sys/boot/ficl
Message-ID:  <201307102137.r6ALbpl7094267@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307102137.r6ALbpl7094267>