CLIte/clite/libs/libstdio.js
2023-12-17 14:11:07 +10:00

165 lines
3.5 KiB
JavaScript

clite.libs.data = function() {
clite.libs.load('libio','stdio',function(io,env) {
function randChar() {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return characters.charAt(Math.floor(Math.random() * characters.length));
}
return Object.create({
types:clite.io.types,
flags:clite.io.flags,
modes:clite.io.modes,
creat:function(path,type) {
return clite.io.creat(io.pid,path,type);
},
open:function(path,flags,cb) {
return clite.io.open(io.pid,path,flags,cb);
},
close:function(fd) {
return clite.io.close(io.pid,fd);
},
read:function(fd,cb) {
return clite.io.read(io.pid,fd,cb);
},
readLine:function(fd,cb) {
return clite.io.readLine(io.pid,fd,cb);
},
readAll:function(fd) {
return clite.io.readAll(io.pid,fd);
},
write:function(fd,data) {
return clite.io.write(io.pid,fd,data);
},
ftruncate:function(fd,len) {
return clite.io.ftruncate(io.pid,fd,len);
},
truncate:function(path,len) {
return clite.io.truncate(io.pid,path,len);
},
seek:function(fd,pos) {
return clite.io.seek(io.pid,fd,pos);
},
remove:function(path) {
return clite.io.remove(io.pid,path);
},
link:function(path,target) {
return clite.io.link(io.pid,path,target);
},
fstatat:function(fd,flags) {
return clite.io.fstatat(io.pid,fd,flags);
},
stat:function(path) {
var fd = clite.io.open(0,path,clite.io.flags.O_RDONLY|clite.io.flags.O_NONBLOCK);
if (!fd)
return null;
var st = clite.io.fstatat(io.pid,fd,0);
clite.io.close(io.pid,fd);
return st;
},
lstat:function(path) {
var fd = clite.io.open(0,path,clite.io.flags.O_RDONLY|clite.io.flags.O_NOFOLLOW|clite.io.flags.O_NONBLOCK);
if (!fd)
return null;
var st = clite.io.fstatat(io.pid,fd,clite.io.flags.AT_SYMLINK_NOFOLLOW);
clite.io.close(io.pid,fd);
return st;
},
chmod:function(path,mode) {
return clite.io.chmod(io.pid,path,mode);
},
fchmod:function(fd,mode) {
return clite.io.fchmod(io.pid,fd,mode);
},
chown:function(path,uid,gid) {
return clite.io.chown(io.pid,path,uid,gid);
},
lchown:function(path,uid,gid) {
return clite.io.lchown(io.pid,path,uid,gid);
},
isatty:function(fd) {
return clite.io.isatty(io.pid,fd);
},
isreadable(fd) {
return clite.lib.checkNodeReadable(fd.node,io.pid);
},
iswritable(fd) {
return clite.lib.checkNodeWritable(fd.node,io.pid);
},
isexecutable(fd) {
return clite.lib.checkNodeExecutable(fd.node,io.pid);
},
mkdir:function(path,mode) {
return clite.io.mkdir(io.pid,path,mode);
},
mkstemp:function(template,rc) {
if (typeof template === 'undefined')
template = '/tmp/tmpfileXXXXXX';
if (typeof rc === 'undefined')
rc = 0;
if (rc > 4 || template[0] != '/')
return null;
var path = template;
var ind;
while ((ind = path.lastIndexOf('X')) > 4) {
var t = path.substring(0,ind)+randChar()+path.substring(ind+1);
path = t;
}
if (path == template)
return this.mkstemp(template+'XXXXXX',rc+1);
var fd = clite.io.open(io.pid,path,clite.io.flags.O_RDWR|clite.io.flags.O_CREAT|clite.io.flags.O_EXCL|clite.io.flags.O_SYNC);
if (!fd)
return this.mkstemp(template,rc+1);
return fd;
},
vfprintf:function(fd,fmt,args) {
return clite.io.vfprintf(io.pid,fd,fmt,args);
},
fprintf:function(fd,fmt) {
var args = Array.from(arguments);
args.shift();
args.shift();
return clite.io.vfprintf(io.pid,fd,fmt,args);
},
printf:function(fmt) {
var args = Array.from(arguments);
args.shift();
return clite.io.vfprintf(io.pid,io.stdout,fmt,args);
}
});
});
}