new functions "rename" and "tmpname".

This commit is contained in:
Roberto Ierusalimschy 1996-03-14 12:55:18 -03:00
parent 1dc0e82aeb
commit 675e608325
1 changed files with 26 additions and 16 deletions

42
iolib.c
View File

@ -3,7 +3,7 @@
** Input/output library to LUA ** Input/output library to LUA
*/ */
char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 13:14:52 roberto Exp roberto $"; char *rcs_iolib="$Id: iolib.c,v 1.38 1996/03/12 15:56:03 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
@ -30,6 +30,14 @@ int pclose();
#endif #endif
static void pushresult (int i)
{
if (i)
lua_pushnumber (1);
else
lua_pushnil();
}
static void closeread (void) static void closeread (void)
{ {
if (in != stdin) if (in != stdin)
@ -373,16 +381,8 @@ static int write_string (char *s, int just, int m)
static int write_quoted (int just, int m) static int write_quoted (int just, int m)
{ {
char *s;
luaI_addchar(0); luaI_addchar(0);
luaI_addchar('"'); luaI_addquoted(lua_check_string(1, "write"));
for (s = lua_check_string(1, "write"); *s; s++)
{
if (*s == '"' || *s == '\\' || *s == '\n')
luaI_addchar('\\');
luaI_addchar(*s);
}
luaI_addchar('"');
return write_string(luaI_addchar(0), just, m); return write_string(luaI_addchar(0), just, m);
} }
@ -472,10 +472,19 @@ static void io_execute (void)
*/ */
static void io_remove (void) static void io_remove (void)
{ {
if (remove(lua_check_string(1, "remove")) == 0) pushresult(remove(lua_check_string(1, "remove")) == 0);
lua_pushnumber (1); }
else
lua_pushnil(); static void io_rename (void)
{
char *f1 = lua_check_string(1, "rename");
char *f2 = lua_check_string(2, "rename");
pushresult(rename(f1, f2) == 0);
}
static void io_tmpname (void)
{
lua_pushstring(tmpnam(NULL));
} }
static void io_errorno (void) static void io_errorno (void)
@ -490,8 +499,7 @@ static void io_errorno (void)
static void io_getenv (void) static void io_getenv (void)
{ {
char *env = getenv(lua_check_string(1, "getenv")); char *env = getenv(lua_check_string(1, "getenv"));
if (env == NULL) lua_pushnil(); lua_pushstring(env); /* if NULL push nil */
else lua_pushstring(env);
} }
/* /*
@ -602,6 +610,8 @@ void iolib_open (void)
lua_register ("write", io_write); lua_register ("write", io_write);
lua_register ("execute", io_execute); lua_register ("execute", io_execute);
lua_register ("remove", io_remove); lua_register ("remove", io_remove);
lua_register ("rename", io_rename);
lua_register ("tmpname", io_tmpname);
lua_register ("ioerror", io_errorno); lua_register ("ioerror", io_errorno);
lua_register ("getenv", io_getenv); lua_register ("getenv", io_getenv);
lua_register ("date", io_date); lua_register ("date", io_date);