aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-14 12:55:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-14 12:55:18 -0300
commit675e608325ace43e4350bcde8cfb6ec93b46788f (patch)
tree95417666b915b252fa94c527203095338764056e
parent1dc0e82aeb43d2265fcb6834430e8471729c77e2 (diff)
downloadlua-675e608325ace43e4350bcde8cfb6ec93b46788f.tar.gz
lua-675e608325ace43e4350bcde8cfb6ec93b46788f.tar.bz2
lua-675e608325ace43e4350bcde8cfb6ec93b46788f.zip
new functions "rename" and "tmpname".
-rw-r--r--iolib.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/iolib.c b/iolib.c
index 46adf86a..ed6ead8d 100644
--- a/iolib.c
+++ b/iolib.c
@@ -3,7 +3,7 @@
3** Input/output library to LUA 3** Input/output library to LUA
4*/ 4*/
5 5
6char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 13:14:52 roberto Exp roberto $"; 6char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 15:56:03 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <ctype.h> 9#include <ctype.h>
@@ -30,6 +30,14 @@ int pclose();
30#endif 30#endif
31 31
32 32
33static void pushresult (int i)
34{
35 if (i)
36 lua_pushnumber (1);
37 else
38 lua_pushnil();
39}
40
33static void closeread (void) 41static void closeread (void)
34{ 42{
35 if (in != stdin) 43 if (in != stdin)
@@ -373,16 +381,8 @@ static int write_string (char *s, int just, int m)
373 381
374static int write_quoted (int just, int m) 382static int write_quoted (int just, int m)
375{ 383{
376 char *s;
377 luaI_addchar(0); 384 luaI_addchar(0);
378 luaI_addchar('"'); 385 luaI_addquoted(lua_check_string(1, "write"));
379 for (s = lua_check_string(1, "write"); *s; s++)
380 {
381 if (*s == '"' || *s == '\\' || *s == '\n')
382 luaI_addchar('\\');
383 luaI_addchar(*s);
384 }
385 luaI_addchar('"');
386 return write_string(luaI_addchar(0), just, m); 386 return write_string(luaI_addchar(0), just, m);
387} 387}
388 388
@@ -472,10 +472,19 @@ static void io_execute (void)
472*/ 472*/
473static void io_remove (void) 473static void io_remove (void)
474{ 474{
475 if (remove(lua_check_string(1, "remove")) == 0) 475 pushresult(remove(lua_check_string(1, "remove")) == 0);
476 lua_pushnumber (1); 476}
477 else 477
478 lua_pushnil(); 478static void io_rename (void)
479{
480 char *f1 = lua_check_string(1, "rename");
481 char *f2 = lua_check_string(2, "rename");
482 pushresult(rename(f1, f2) == 0);
483}
484
485static void io_tmpname (void)
486{
487 lua_pushstring(tmpnam(NULL));
479} 488}
480 489
481static void io_errorno (void) 490static void io_errorno (void)
@@ -490,8 +499,7 @@ static void io_errorno (void)
490static void io_getenv (void) 499static void io_getenv (void)
491{ 500{
492 char *env = getenv(lua_check_string(1, "getenv")); 501 char *env = getenv(lua_check_string(1, "getenv"));
493 if (env == NULL) lua_pushnil(); 502 lua_pushstring(env); /* if NULL push nil */
494 else lua_pushstring(env);
495} 503}
496 504
497/* 505/*
@@ -602,6 +610,8 @@ void iolib_open (void)
602 lua_register ("write", io_write); 610 lua_register ("write", io_write);
603 lua_register ("execute", io_execute); 611 lua_register ("execute", io_execute);
604 lua_register ("remove", io_remove); 612 lua_register ("remove", io_remove);
613 lua_register ("rename", io_rename);
614 lua_register ("tmpname", io_tmpname);
605 lua_register ("ioerror", io_errorno); 615 lua_register ("ioerror", io_errorno);
606 lua_register ("getenv", io_getenv); 616 lua_register ("getenv", io_getenv);
607 lua_register ("date", io_date); 617 lua_register ("date", io_date);