From e4830ddce3d477f51ecf1da447dd8e361d74a1a0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 Nov 1998 13:41:43 -0200 Subject: new option "mode" in "readfrom", "writeto" and "appendto" (for binary files). --- liolib.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'liolib.c') diff --git a/liolib.c b/liolib.c index 2aba847c..83a57d48 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.24 1998/08/30 20:25:24 roberto Exp roberto $ +** $Id: liolib.c,v 1.25 1998/09/07 18:59:59 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -62,6 +62,7 @@ static void pushresult (int i) else { lua_pushnil(); lua_pushstring(strerror(errno)); + lua_pushnumber(errno); } } @@ -102,6 +103,15 @@ static FILE *getfileparam (char *name, int *arg) { } +static char *getmode (char mode) { + static char m[3]; + m[0] = mode; + m[1] = (*luaL_opt_string(FIRSTARG+1, "text") == 'b') ? 'b' : '\0'; + m[2] = '\0'; + return m; +} + + static void closefile (char *name) { FILE *f = getfilebyname(name); @@ -140,7 +150,7 @@ static void io_readfrom (void) current = lua_getuserdata(f); else { char *s = luaL_check_string(FIRSTARG); - current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); + current = (*s == '|') ? popen(s+1, "r") : fopen(s, getmode('r')); if (current == NULL) { pushresult(0); return; @@ -162,7 +172,7 @@ static void io_writeto (void) current = lua_getuserdata(f); else { char *s = luaL_check_string(FIRSTARG); - current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); + current = (*s == '|') ? popen(s+1,"w") : fopen(s,getmode('w')); if (current == NULL) { pushresult(0); return; @@ -175,7 +185,7 @@ static void io_writeto (void) static void io_appendto (void) { char *s = luaL_check_string(FIRSTARG); - FILE *fp = fopen (s, "a"); + FILE *fp = fopen (s, getmode('a')); if (fp != NULL) setreturn(fp, FOUTPUT); else -- cgit v1.2.3-55-g6feb