aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-11-20 13:41:43 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-11-20 13:41:43 -0200
commite4830ddce3d477f51ecf1da447dd8e361d74a1a0 (patch)
tree8caff56d2056e11e8cb9a2af7722d3fb8e45e688 /liolib.c
parent758e330d6e5ef83c1fc3bfd58c3d626d5968d12c (diff)
downloadlua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.gz
lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.bz2
lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.zip
new option "mode" in "readfrom", "writeto" and "appendto" (for
binary files).
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/liolib.c b/liolib.c
index 2aba847c..83a57d48 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.24 1998/08/30 20:25:24 roberto Exp roberto $ 2** $Id: liolib.c,v 1.25 1998/09/07 18:59:59 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -62,6 +62,7 @@ static void pushresult (int i)
62 else { 62 else {
63 lua_pushnil(); 63 lua_pushnil();
64 lua_pushstring(strerror(errno)); 64 lua_pushstring(strerror(errno));
65 lua_pushnumber(errno);
65 } 66 }
66} 67}
67 68
@@ -102,6 +103,15 @@ static FILE *getfileparam (char *name, int *arg) {
102} 103}
103 104
104 105
106static char *getmode (char mode) {
107 static char m[3];
108 m[0] = mode;
109 m[1] = (*luaL_opt_string(FIRSTARG+1, "text") == 'b') ? 'b' : '\0';
110 m[2] = '\0';
111 return m;
112}
113
114
105static void closefile (char *name) 115static void closefile (char *name)
106{ 116{
107 FILE *f = getfilebyname(name); 117 FILE *f = getfilebyname(name);
@@ -140,7 +150,7 @@ static void io_readfrom (void)
140 current = lua_getuserdata(f); 150 current = lua_getuserdata(f);
141 else { 151 else {
142 char *s = luaL_check_string(FIRSTARG); 152 char *s = luaL_check_string(FIRSTARG);
143 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); 153 current = (*s == '|') ? popen(s+1, "r") : fopen(s, getmode('r'));
144 if (current == NULL) { 154 if (current == NULL) {
145 pushresult(0); 155 pushresult(0);
146 return; 156 return;
@@ -162,7 +172,7 @@ static void io_writeto (void)
162 current = lua_getuserdata(f); 172 current = lua_getuserdata(f);
163 else { 173 else {
164 char *s = luaL_check_string(FIRSTARG); 174 char *s = luaL_check_string(FIRSTARG);
165 current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); 175 current = (*s == '|') ? popen(s+1,"w") : fopen(s,getmode('w'));
166 if (current == NULL) { 176 if (current == NULL) {
167 pushresult(0); 177 pushresult(0);
168 return; 178 return;
@@ -175,7 +185,7 @@ static void io_writeto (void)
175static void io_appendto (void) 185static void io_appendto (void)
176{ 186{
177 char *s = luaL_check_string(FIRSTARG); 187 char *s = luaL_check_string(FIRSTARG);
178 FILE *fp = fopen (s, "a"); 188 FILE *fp = fopen (s, getmode('a'));
179 if (fp != NULL) 189 if (fp != NULL)
180 setreturn(fp, FOUTPUT); 190 setreturn(fp, FOUTPUT);
181 else 191 else