aboutsummaryrefslogtreecommitdiff
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
parent758e330d6e5ef83c1fc3bfd58c3d626d5968d12c (diff)
downloadlua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.gz
lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.bz2
lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.zip
new option "mode" in "readfrom", "writeto" and "appendto" (for
binary files).
-rw-r--r--liolib.c18
-rw-r--r--manual.tex16
2 files changed, 25 insertions, 9 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
diff --git a/manual.tex b/manual.tex
index 8a6925b8..8e6e4786 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.19 1998/08/24 20:14:56 roberto Exp roberto $ 1% $Id: manual.tex,v 1.20 1998/11/13 16:48:48 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -41,7 +41,7 @@ Waldemar Celes
41\tecgraf\ --- Computer Science Department --- PUC-Rio 41\tecgraf\ --- Computer Science Department --- PUC-Rio
42} 42}
43 43
44%\date{\small \verb$Date: 1998/08/24 20:14:56 $} 44%\date{\small \verb$Date: 1998/11/13 16:48:48 $}
45 45
46\maketitle 46\maketitle
47 47
@@ -2615,12 +2615,14 @@ Unless otherwise stated,
2615all I/O functions return \nil\ on failure and 2615all I/O functions return \nil\ on failure and
2616some value different from \nil\ on success. 2616some value different from \nil\ on success.
2617 2617
2618\subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} 2618\subsubsection*{\ff \T{readfrom (filename [, mode])}}\Deffunc{readfrom}
2619 2619
2620This function may be called in two ways. 2620This function may be called in two ways.
2621When called with a file name, it opens the named file, 2621When called with a file name, it opens the named file,
2622sets its handle as the value of \verb|_INPUT|, 2622sets its handle as the value of \verb|_INPUT|,
2623and returns this value. 2623and returns this value.
2624An optional \verb|mode| argument with the string \verb|"binary"|
2625opens file in binary mode (where this applies).
2624It does not close the current input file. 2626It does not close the current input file.
2625When called without parameters, 2627When called without parameters,
2626it closes the \verb|_INPUT| file, 2628it closes the \verb|_INPUT| file,
@@ -2639,13 +2641,15 @@ the number of files that can be open at the same time is
2639usually limited and depends on the system. 2641usually limited and depends on the system.
2640\end{quotation} 2642\end{quotation}
2641 2643
2642\subsubsection*{\ff \T{writeto (filename)}}\Deffunc{writeto} 2644\subsubsection*{\ff \T{writeto (filename [, mode])}}\Deffunc{writeto}
2643 2645
2644This function may be called in two ways. 2646This function may be called in two ways.
2645When called with a file name, 2647When called with a file name,
2646it opens the named file, 2648it opens the named file,
2647sets its handle as the value of \verb|_OUTPUT|, 2649sets its handle as the value of \verb|_OUTPUT|,
2648and returns this value. 2650and returns this value.
2651An optional \verb|mode| argument with the string \verb|"binary"|
2652opens file in binary mode (where this applies).
2649It does not close the current output file. 2653It does not close the current output file.
2650Note that, if the file already exists, 2654Note that, if the file already exists,
2651then it will be \emph{completely erased} with this operation. 2655then it will be \emph{completely erased} with this operation.
@@ -2667,10 +2671,12 @@ the number of files that can be open at the same time is
2667usually limited and depends on the system. 2671usually limited and depends on the system.
2668\end{quotation} 2672\end{quotation}
2669 2673
2670\subsubsection*{\ff \T{appendto (filename)}}\Deffunc{appendto} 2674\subsubsection*{\ff \T{appendto (filename [, mode])}}\Deffunc{appendto}
2671 2675
2672Opens a file named \verb|filename| and sets it as the 2676Opens a file named \verb|filename| and sets it as the
2673value of \verb|_OUTPUT|. 2677value of \verb|_OUTPUT|.
2678An optional \verb|mode| argument with the string \verb|"binary"|
2679opens file in binary mode (where this applies).
2674Unlike the \verb|writeto| operation, 2680Unlike the \verb|writeto| operation,
2675this function does not erase any previous content of the file. 2681this function does not erase any previous content of the file.
2676If this function fails, it returns \nil, 2682If this function fails, it returns \nil,