Fixed filesystem reads, utils

This commit is contained in:
Łukasz Magiera 2016-01-13 01:12:01 +01:00
parent 03f4203fa2
commit cf19c634a2
4 changed files with 84 additions and 10 deletions

View File

@ -16,6 +16,49 @@
#include <limits.h>
#include <linux/kd.h>
void logn (const char *message) {
FILE *file;
file = fopen("lupi.log", "a");
if (file == NULL) {
return;
} else {
fputs(message, file);
fputs("\n", file);
fclose(file);
}
}
void logi (int message) {
FILE *file;
file = fopen("lupi.log", "a");
if (file == NULL) {
return;
} else {
char str[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
sprintf(str, "%d", message);
fputs(str, file);
fclose(file);
}
}
void logm (const char *message) {
FILE *file;
file = fopen("lupi.log", "a");
if (file == NULL) {
return;
} else {
fputs(message, file);
fclose(file);
}
}
static int l_sleep (lua_State *L) {
unsigned int t = lua_tonumber(L, 1);
usleep(t);
@ -73,14 +116,20 @@ static int l_fs_spaceUsed (lua_State *L) {
static int l_fs_open (lua_State *L) {
const char* fname = lua_tostring(L, 1);
const char* mode = lua_tostring(L, 2);
logm("Open file: ");
logn(fname);
int m = 0;
if(mode[0] == 'r') m = O_RDONLY;
else if(mode[0] == 'w') m = O_WRONLY | O_CREAT /*| O_DIRECT*/;
else if(mode[0] == 'a') m = O_WRONLY | O_APPEND | O_CREAT /*| O_DIRECT*/;
else return 0;
int fd = open(fname, m, 644);
if(fd == -1) return 0;
if(fd == -1) return 0;
logm("FD ");
logi(fd);
logm(" for ");
logn(fname);
lua_pushnumber(L, fd);
return 1;
}
@ -145,8 +194,10 @@ static int l_fs_list (lua_State *L) {
lua_newtable(L);
int n = 1;
while ((ent = readdir(dir)) != NULL) { //TODO: Check if it should be freed
lua_pushstring(L, ent->d_name);
lua_rawseti(L, -2, n++);
if(strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
lua_pushstring(L, ent->d_name);
lua_rawseti(L, -2, n++);
}
}
closedir(dir);
} else {
@ -213,11 +264,18 @@ static int l_fs_size (lua_State *L) {
}
static int l_fs_read (lua_State *L) {
int fd = lua_tonumber(L, 1);
int count = lua_tonumber(L, 2);
unsigned int fd = lua_tonumber(L, 1);
unsigned int count = lua_tonumber(L, 2);
void* buf = malloc(count);
size_t res = read(fd, buf, count);
if(res != -1) {
logm("read(");
logi(fd);
logm(") bytes:");
logi(res);
logm(" of ");
logi(count);
logn("");
if(res > 0) {
lua_pushlstring(L, buf, res);
free(buf);
return 1;
@ -267,6 +325,7 @@ void luanative_start(lua_State *L) {
pushctuple(L, "fs_write", l_fs_write);
pushctuple(L, "fs_spaceTotal", l_fs_spaceTotal);
pushctuple(L, "fs_rename", l_fs_rename);
pushctuple(L, "fs_list", l_fs_list);
pushctuple(L, "fs_lastModified", l_fs_lastModified);
pushctuple(L, "fs_remove", l_fs_remove);
pushctuple(L, "fs_close", l_fs_close);

View File

@ -10,10 +10,21 @@ function api.pushSignal(...)
--FIXME: ASAP: Implement
end
function api.pullSignal(timeout)
if type(timeout) == "number" then
native.sleep(timeout * 1000000);
end
--print(debug.traceback())
end
function api.uptime()
return native.uptime()
end
function api.beep(freq, time)
if not freq then freq = 1000 end
if not time then time = 0.2 end
native.beep(freq, time * 1000)
if not freq then freq = 1000 end
if not time then time = 0.2 end
native.beep(freq, time * 1000)
end
return computer

View File

@ -144,6 +144,9 @@ function filesystem.register(basePath)
return native.fs_size(realpath(path))
end
function fs.read(handle, count) --FIXME: Hudgeread, fix in general
if count == math.huge then
count = 4294967295
end
checkArg(1, handle, "number")
checkArg(2, count, "number")
return native.fs_read(handle, count)

View File

@ -110,7 +110,7 @@ function textgpu.start()
checkArg(3, w, "number")
checkArg(4, h, "number")
checkArg(5, ch, "string")
ch = ch:sub(1, 1):rep(w)
ch = ch:sub(1, 1):rep(math.floor(w))
for i=1, h do
gpu.set(x, y + i - 1, ch)
end
@ -122,6 +122,7 @@ function textgpu.start()
gpu.setBackground(0x000000)
modules.component.api.register(nil, "gpu", gpu)
modules.component.api.register(nil, "screen", {getKeyboards = function() return {} end}) --verry dummy screen, TODO: make it better
deadhooks[#deadhooks + 1] = function()
io.write("\x1b[?25h") --Enable cursor on quit