diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 7cbdb7da..fb01d855 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -677,6 +677,7 @@ static NFnDesc* fListDesc; static NFnDesc* fMapBytesDesc; static NFnDesc* createdirDesc; static NFnDesc* renameDesc; +static NFnDesc* removeDesc; B list_c1(B d, B x) { return path_list(path_rel(nfn_objU(d), x)); @@ -694,6 +695,11 @@ B rename_c2(B d, B w, B x) { thrM("(file).Rename: Failed to rename file"); } +B remove_c1(B d, B x) { + if (path_remove(path_rel(nfn_objU(d), x))) return m_i32(1); + thrM("(file).Remove: Failed to remove file"); +} + B ftype_c1(B d, B x) { char ty = path_type(path_rel(nfn_objU(d), x)); if (ty==0) thrM("•file.Type: Error while accessing file"); @@ -1110,7 +1116,7 @@ B sys_c1(B t, B x) { if(!fileNS.u) { REQ_PATH; #define F(X) m_nfn(X##Desc, inc(path)) - fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(fList), F(fBytes), F(fChars), F(fLines), F(fType), F(fExists), inc(bi_fName), F(fMapBytes), F(createdir), F(rename)); + fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(fList), F(fBytes), F(fChars), F(fLines), F(fType), F(fExists), inc(bi_fName), F(fMapBytes), F(createdir), F(rename), F(remove)); #undef F } cr = inc(fileNS); @@ -1180,12 +1186,13 @@ void sysfn_init() { fTypeDesc = registerNFn(m_str8l("(file).Type"), ftype_c1, c2_bad); createdirDesc = registerNFn(m_str8l("(file).CreateDir"), createdir_c1, c2_bad); renameDesc = registerNFn(m_str8l("(file).Rename"), c1_bad, rename_c2); + removeDesc = registerNFn(m_str8l("(file).Remove"), remove_c1, c2_bad); fMapBytesDesc = registerNFn(m_str8l("(file).MapBytes"), mapBytes_c1, c2_bad); fExistsDesc = registerNFn(m_str8l("(file).Exists"), fexists_c1, c2_bad); importDesc = registerNFn(m_str32(U"•Import"), import_c1, import_c2); reBQNDesc = registerNFn(m_str8l("(REPL)"), repl_c1, repl_c2); } void sysfnPost_init() { - file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes","createdir","rename"); + file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes","createdir","rename","remove"); c(BMd1,bi_bitcast)->im = bitcast_im; } diff --git a/src/utils/file.c b/src/utils/file.c index 72150801..27ac52f9 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -324,6 +324,14 @@ bool path_rename(B old_path, B new_path) { return ok; } +bool path_remove(B path) { + char* p = toCStr(path); + bool ok = unlink(p) == 0; + freeCStr(p); + dec(path); + return ok; +} + char path_type(B path) { char* p = toCStr(path); struct stat path_stat; diff --git a/src/utils/file.h b/src/utils/file.h index 3a6e69bc..1bf1281a 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -16,6 +16,7 @@ I8Arr* stream_bytes(FILE* f); B mmap_file(B path); // consumes bool dir_create(B path); // doesn't consume bool path_rename(B old_path, B new_path); // consumes only old_path +bool path_remove(B path); // consumes void path_wChars(B path, B x); // consumes path void path_wBytes(B path, B x); // consumes path