1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-18 10:35:55 +00:00

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.
This commit is contained in:
Marcel Moolenaar 2013-07-10 21:37:50 +00:00
parent 374931bdc1
commit 68fd965f98
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=253172

View File

@ -404,6 +404,34 @@ static void displayCellNoPad(FICL_VM *pVM)
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 *pSys)
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);