aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-02-08 14:39:42 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-02-08 14:39:42 -0200
commit74f1c3d025c6d8a714454470a953f383a1c6a641 (patch)
treecc89dfa109c64ed928217d777959ffc7ace12501 /liolib.c
parent1f691a4fcd32b706c79e4c8cbff17c6ae985bc34 (diff)
downloadlua-74f1c3d025c6d8a714454470a953f383a1c6a641.tar.gz
lua-74f1c3d025c6d8a714454470a953f383a1c6a641.tar.bz2
lua-74f1c3d025c6d8a714454470a953f383a1c6a641.zip
small changes for "clean C"
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/liolib.c b/liolib.c
index e9f114fe..0d75ac0d 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.55 1999/12/30 18:28:40 roberto Exp roberto $ 2** $Id: liolib.c,v 1.56 2000/01/19 12:00:45 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*/
@@ -80,28 +80,30 @@ static int gettag (lua_State *L) {
80} 80}
81 81
82 82
83static int ishandle (lua_State *L, lua_Object f) { 83static FILE *gethandle (lua_State *L, lua_Object f) {
84 if (lua_isuserdata(L, f)) { 84 if (lua_isuserdata(L, f)) {
85 int tag = gettag(L); 85 int ftag = lua_tag(L, f);
86 if (lua_tag(L, f) == CLOSEDTAG(L, tag)) 86 int iotag = gettag(L);
87 if (ftag == iotag)
88 return (FILE *)lua_getuserdata(L, f);
89 if (ftag == CLOSEDTAG(L, iotag))
87 lua_error(L, "cannot access a closed file"); 90 lua_error(L, "cannot access a closed file");
88 return lua_tag(L, f) == tag; 91 /* else go through */
89 } 92 }
90 else return 0; 93 return NULL;
91} 94}
92 95
93 96
94static FILE *getfilebyname (lua_State *L, const char *name) { 97static FILE *getfilebyname (lua_State *L, const char *name) {
95 lua_Object f = lua_rawgetglobal(L, name); 98 FILE *handle = gethandle(L, lua_rawgetglobal(L, name));
96 if (!ishandle(L, f)) 99 if (!handle)
97 luaL_verror(L, "global variable `%.50s' is not a file handle", name); 100 luaL_verror(L, "global variable `%.50s' is not a file handle", name);
98 return lua_getuserdata(L, f); 101 return handle;
99} 102}
100 103
101 104
102static FILE *getfile (lua_State *L, int arg) { 105static FILE *getfile (lua_State *L, int arg) {
103 lua_Object f = lua_getparam(L, arg); 106 return gethandle(L, lua_getparam(L, arg));
104 return (ishandle(L, f)) ? lua_getuserdata(L, f) : NULL;
105} 107}
106 108
107 109
@@ -182,7 +184,7 @@ static void io_readfrom (lua_State *L) {
182 current = NULL; /* to signal error */ 184 current = NULL; /* to signal error */
183 } 185 }
184 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */ 186 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */
185 current = lua_getuserdata(L, f); 187 current = (FILE *)lua_getuserdata(L, f);
186 else { 188 else {
187 const char *s = luaL_check_string(L, FIRSTARG); 189 const char *s = luaL_check_string(L, FIRSTARG);
188 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); 190 current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
@@ -201,7 +203,7 @@ static void io_writeto (lua_State *L) {
201 current = NULL; /* to signal error */ 203 current = NULL; /* to signal error */
202 } 204 }
203 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */ 205 else if (lua_tag(L, f) == gettag(L)) /* deprecated option */
204 current = lua_getuserdata(L, f); 206 current = (FILE *)lua_getuserdata(L, f);
205 else { 207 else {
206 const char *s = luaL_check_string(L, FIRSTARG); 208 const char *s = luaL_check_string(L, FIRSTARG);
207 current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w"); 209 current = (*s == '|') ? popen(s+1,"w") : fopen(s, "w");
@@ -301,7 +303,7 @@ static int read_number (lua_State *L, FILE *f) {
301 303
302static void read_word (lua_State *L, FILE *f) { 304static void read_word (lua_State *L, FILE *f) {
303 int c; 305 int c;
304 do { c = fgetc(f); } while isspace(c); /* skip spaces */ 306 do { c = fgetc(f); } while (isspace(c)); /* skip spaces */
305 while (c != EOF && !isspace(c)) { 307 while (c != EOF && !isspace(c)) {
306 luaL_addchar(L, c); 308 luaL_addchar(L, c);
307 c = fgetc(f); 309 c = fgetc(f);
@@ -477,10 +479,10 @@ static void io_clock (lua_State *L) {
477static void io_date (lua_State *L) { 479static void io_date (lua_State *L) {
478 char b[256]; 480 char b[256];
479 const char *s = luaL_opt_string(L, 1, "%c"); 481 const char *s = luaL_opt_string(L, 1, "%c");
480 struct tm *tm; 482 struct tm *stm;
481 time_t t; 483 time_t t;
482 time(&t); tm = localtime(&t); 484 time(&t); stm = localtime(&t);
483 if (strftime(b,sizeof(b),s,tm)) 485 if (strftime(b, sizeof(b), s, stm))
484 lua_pushstring(L, b); 486 lua_pushstring(L, b);
485 else 487 else
486 lua_error(L, "invalid `date' format"); 488 lua_error(L, "invalid `date' format");