diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-03-14 12:55:18 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-03-14 12:55:18 -0300 |
commit | 675e608325ace43e4350bcde8cfb6ec93b46788f (patch) | |
tree | 95417666b915b252fa94c527203095338764056e | |
parent | 1dc0e82aeb43d2265fcb6834430e8471729c77e2 (diff) | |
download | lua-675e608325ace43e4350bcde8cfb6ec93b46788f.tar.gz lua-675e608325ace43e4350bcde8cfb6ec93b46788f.tar.bz2 lua-675e608325ace43e4350bcde8cfb6ec93b46788f.zip |
new functions "rename" and "tmpname".
-rw-r--r-- | iolib.c | 42 |
1 files changed, 26 insertions, 16 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Input/output library to LUA | 3 | ** Input/output library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 13:14:52 roberto Exp roberto $"; | 6 | char *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 | ||
33 | static void pushresult (int i) | ||
34 | { | ||
35 | if (i) | ||
36 | lua_pushnumber (1); | ||
37 | else | ||
38 | lua_pushnil(); | ||
39 | } | ||
40 | |||
33 | static void closeread (void) | 41 | static 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 | ||
374 | static int write_quoted (int just, int m) | 382 | static 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 | */ |
473 | static void io_remove (void) | 473 | static 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(); | 478 | static 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 | |||
485 | static void io_tmpname (void) | ||
486 | { | ||
487 | lua_pushstring(tmpnam(NULL)); | ||
479 | } | 488 | } |
480 | 489 | ||
481 | static void io_errorno (void) | 490 | static void io_errorno (void) |
@@ -490,8 +499,7 @@ static void io_errorno (void) | |||
490 | static void io_getenv (void) | 499 | static 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); |