Browse Source

1) execute retorna resultado Unix da execucao do comando.
2) correcao parcial da read: retorna nil quando encontra EOF.

Roberto Ierusalimschy 32 years ago
parent
commit
8886f221bc
1 changed files with 14 additions and 11 deletions
  1. 14 11
      iolib.c

+ 14 - 11
iolib.c

@@ -3,7 +3,7 @@
 ** Input/output library to LUA
 ** Input/output library to LUA
 */
 */
 
 
-char *rcs_iolib="$Id: $";
+char *rcs_iolib="$Id: iolib.c,v 1.1 1993/12/17 18:41:19 celes Exp roberto $";
 
 
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
@@ -272,22 +272,25 @@ static void io_read (void)
     case 'i':
     case 'i':
     {
     {
      long int l;
      long int l;
-     fscanf (in, "%ld", &l);
-     lua_pushnumber(l);
+     if (fscanf (in, "%ld", &l) == EOF)
+       lua_pushnil();
+       else lua_pushnumber(l);
     }
     }
     break;
     break;
     case 'f': case 'g': case 'e':
     case 'f': case 'g': case 'e':
     {
     {
      float f;
      float f;
-     fscanf (in, "%f", &f);
-     lua_pushnumber(f);
+     if (fscanf (in, "%f", &f) == EOF)
+       lua_pushnil();
+       else lua_pushnumber(f);
     }
     }
     break;
     break;
     default: 
     default: 
     {
     {
      char s[256];
      char s[256];
-     fscanf (in, "%s", s);
-     lua_pushstring(s);
+     if (fscanf (in, "%s", s) == EOF)
+       lua_pushnil();
+       else lua_pushstring(s);
     }
     }
     break;
     break;
    }
    }
@@ -406,8 +409,8 @@ static void io_write (void)
 }
 }
 
 
 /*
 /*
-** Execute a executable program using "sustem".
-** On error put 0 on stack, otherwise put 1.
+** Execute a executable program using "system".
+** Return the result of execution.
 */
 */
 void io_execute (void)
 void io_execute (void)
 {
 {
@@ -419,8 +422,8 @@ void io_execute (void)
  }
  }
  else
  else
  {
  {
-  system(lua_getstring(o));
-  lua_pushnumber (1);
+  int res = system(lua_getstring(o));
+  lua_pushnumber (res);
  }
  }
  return;
  return;
 }
 }