aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-08-24 17:14:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-08-24 17:14:56 -0300
commitee22af5ced0b14c2a445124fc0efffc5f44ed49a (patch)
tree4dea69431fe478cf8583038226f57567a7548c94 /liolib.c
parentcc117253c83ec32d226a8f2226d5ca144804f873 (diff)
downloadlua-ee22af5ced0b14c2a445124fc0efffc5f44ed49a.tar.gz
lua-ee22af5ced0b14c2a445124fc0efffc5f44ed49a.tar.bz2
lua-ee22af5ced0b14c2a445124fc0efffc5f44ed49a.zip
new functions "seek" and "flush".
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c49
1 files changed, 40 insertions, 9 deletions
diff --git a/liolib.c b/liolib.c
index 0fbc425f..6f89bbdf 100644
--- a/liolib.c
+++ b/liolib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: liolib.c,v 1.21 1998/06/18 17:04:28 roberto Exp roberto $ 2** $Id: liolib.c,v 1.22 1998/08/21 17:43:44 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*/
@@ -76,7 +76,7 @@ static int ishandler (lua_Object f)
76 else return 0; 76 else return 0;
77} 77}
78 78
79static FILE *getfile (char *name) 79static FILE *getfilebyname (char *name)
80{ 80{
81 lua_Object f = lua_getglobal(name); 81 lua_Object f = lua_getglobal(name);
82 if (!ishandler(f)) 82 if (!ishandler(f))
@@ -85,21 +85,26 @@ static FILE *getfile (char *name)
85} 85}
86 86
87 87
88static FILE *getfileparam (char *name, int *arg) 88static FILE *getfile (int arg) {
89{ 89 lua_Object f = lua_getparam(arg);
90 lua_Object f = lua_getparam(*arg); 90 return (ishandler(f)) ? lua_getuserdata(f) : NULL;
91 if (ishandler(f)) { 91}
92
93
94static FILE *getfileparam (char *name, int *arg) {
95 FILE *f = getfile(*arg);
96 if (f) {
92 (*arg)++; 97 (*arg)++;
93 return lua_getuserdata(f); 98 return f;
94 } 99 }
95 else 100 else
96 return getfile(name); 101 return getfilebyname(name);
97} 102}
98 103
99 104
100static void closefile (char *name) 105static void closefile (char *name)
101{ 106{
102 FILE *f = getfile(name); 107 FILE *f = getfilebyname(name);
103 if (f == stdin || f == stdout) return; 108 if (f == stdin || f == stdout) return;
104 if (pclose(f) == -1) 109 if (pclose(f) == -1)
105 fclose(f); 110 fclose(f);
@@ -271,6 +276,30 @@ static void io_write (void)
271} 276}
272 277
273 278
279static void io_seek (void) {
280 static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
281 static char *modenames[] = {"set", "cur", "end", NULL};
282 FILE *f = getfile(FIRSTARG-1+1);
283 int op = luaL_findstring(luaL_opt_string(FIRSTARG-1+2, "cur"), modenames);
284 long offset = luaL_opt_number(FIRSTARG-1+3, 0);
285 luaL_arg_check(f, 1, "invalid file handler");
286 luaL_arg_check(op != -1, 2, "invalid mode");
287 op = fseek(f, offset, mode[op]);
288 if (op)
289 pushresult(0); /* error */
290 else
291 lua_pushnumber(ftell(f));
292}
293
294
295static void io_flush (void) {
296 FILE *f = getfile(FIRSTARG);
297 luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, 1,
298 "invalid file handler");
299 pushresult(fflush(f) == 0);
300}
301
302
274static void io_execute (void) 303static void io_execute (void)
275{ 304{
276 lua_pushnumber(system(luaL_check_string(1))); 305 lua_pushnumber(system(luaL_check_string(1)));
@@ -420,7 +449,9 @@ static struct luaL_reg iolibtag[] = {
420 {"readfrom", io_readfrom}, 449 {"readfrom", io_readfrom},
421 {"writeto", io_writeto}, 450 {"writeto", io_writeto},
422 {"appendto", io_appendto}, 451 {"appendto", io_appendto},
452 {"flush", io_flush},
423 {"read", io_read}, 453 {"read", io_read},
454 {"seek", io_seek},
424 {"write", io_write} 455 {"write", io_write}
425}; 456};
426 457