diff options
author | jhb <jhb@FreeBSD.org> | 2001-12-11 00:49:34 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2001-12-11 00:49:34 +0000 |
commit | 8c6afa35bea1a5ea8a94a3c60d1988999cb41bcc (patch) | |
tree | 3d16cb8ac2303ccc50640d839778a81b153c89ec /sys/boot/ficl | |
parent | 991ffbae5691ddb93149381b3c23fcf0763b63bd (diff) | |
download | FreeBSD-src-8c6afa35bea1a5ea8a94a3c60d1988999cb41bcc.zip FreeBSD-src-8c6afa35bea1a5ea8a94a3c60d1988999cb41bcc.tar.gz |
- Add 'fwrite' and 'fseek' words for writing to and seeking on files.
- Change the 'fopen' keyword to accept a mode parameter. Note that this
will break existing 4th scripts that use fopen. Thus, the loader
version has been bumped and loader.4th has been changed to check for a
sufficient version on i386 and alpha. Be sure that you either do a full
world build or install or full build and install of sys/boot after this
since loader.old won't work with the new 4th files and vice versa.
PR: kern/32389
Submitted by: Jonathan Mini <mini@haikugeek.com>
Sponsored by: ClickArray, Inc.
Diffstat (limited to 'sys/boot/ficl')
-rw-r--r-- | sys/boot/ficl/loader.c | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/sys/boot/ficl/loader.c b/sys/boot/ficl/loader.c index 43db67f..54380b2 100644 --- a/sys/boot/ficl/loader.c +++ b/sys/boot/ficl/loader.c @@ -371,22 +371,37 @@ static void displayCellNoPad(FICL_VM *pVM) /* fopen - open a file and return new fd on stack. * - * fopen ( count ptr -- fd ) + * fopen ( ptr count mode -- fd ) */ static void pfopen(FICL_VM *pVM) { - int fd; - char *p; + int mode, fd, count; + char *ptr, *name; #if FICL_ROBUST > 1 - vmCheckStack(pVM, 2, 1); + vmCheckStack(pVM, 3, 1); #endif - (void)stackPopINT(pVM->pStack); /* don't need count value */ - p = stackPopPtr(pVM->pStack); - fd = open(p, O_RDONLY); + + mode = stackPopINT(pVM->pStack); /* get mode */ + count = stackPopINT(pVM->pStack); /* get count */ + ptr = stackPopPtr(pVM->pStack); /* get ptr */ + + if ((count < 0) || (ptr == NULL)) { + stackPushINT(pVM->pStack, -1); + return; + } + + /* ensure that the string is null terminated */ + name = (char *)malloc(count+1); + bcopy(ptr,name,count); + name[count] = 0; + + /* open the file */ + fd = open(name, mode); + free(name); stackPushINT(pVM->pStack, fd); return; - } +} /* fclose - close a file who's fd is on stack. * @@ -444,6 +459,46 @@ static void pfload(FICL_VM *pVM) return; } +/* fwrite - write file contents + * + * fwrite ( fd buf nbytes -- nwritten ) + */ +static void pfwrite(FICL_VM *pVM) +{ + int fd, len; + char *buf; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 3, 1); +#endif + len = stackPopINT(pVM->pStack); /* get number of bytes to read */ + buf = stackPopPtr(pVM->pStack); /* get buffer */ + fd = stackPopINT(pVM->pStack); /* get fd */ + if (len > 0 && buf && fd != -1) + stackPushINT(pVM->pStack, write(fd, buf, len)); + else + stackPushINT(pVM->pStack, -1); + return; +} + +/* fseek - seek to a new position in a file + * + * fseek ( fd ofs whence -- pos ) + */ +static void pfseek(FICL_VM *pVM) +{ + int fd, pos, whence; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 3, 1); +#endif + whence = stackPopINT(pVM->pStack); + pos = stackPopINT(pVM->pStack); + fd = stackPopINT(pVM->pStack); + stackPushINT(pVM->pStack, lseek(fd, pos, whence)); + return; +} + /* key - get a character from stdin * * key ( -- char ) @@ -568,6 +623,8 @@ void ficlCompilePlatform(FICL_SYSTEM *pSys) dictAppendWord(dp, "fread", pfread, FW_DEFAULT); dictAppendWord(dp, "fload", pfload, FW_DEFAULT); dictAppendWord(dp, "fkey", fkey, FW_DEFAULT); + dictAppendWord(dp, "fseek", pfseek, FW_DEFAULT); + dictAppendWord(dp, "fwrite", pfwrite, FW_DEFAULT); dictAppendWord(dp, "key", key, FW_DEFAULT); dictAppendWord(dp, "key?", keyQuestion, FW_DEFAULT); dictAppendWord(dp, "ms", ms, FW_DEFAULT); |