diff --git a/src/file.c b/src/file.c new file mode 100644 index 00000000..5c069711 --- /dev/null +++ b/src/file.c @@ -0,0 +1,42 @@ +#include + +#include "picrin.h" + +pic_value +pic_file_exists_p(pic_state *pic) +{ + char *fname; + size_t size; + FILE *fp; + + pic_get_args(pic, "s", &fname, &size); + + fp = fopen(fname, "r"); + if (fp) { + fclose(fp); + return pic_true_value(); + } else { + return pic_false_value(); + } +} + +pic_value +pic_file_delete(pic_state *pic) +{ + char *fname; + size_t size; + + pic_get_args(pic, "s", &fname, &size); + + if (remove(fname) != 0) { + pic_error(pic, "file cannot be deleted"); + } + return pic_true_value(); +} + +void +pic_init_file(pic_state *pic) +{ + pic_defun(pic, "file-exists?", pic_file_exists_p); + pic_defun(pic, "delete-file", pic_file_delete); +} diff --git a/src/init.c b/src/init.c index a4d0407d..77c845d3 100644 --- a/src/init.c +++ b/src/init.c @@ -4,6 +4,7 @@ void pic_init_port(pic_state *); void pic_init_number(pic_state *); void pic_init_time(pic_state *); void pic_init_system(pic_state *); +void pic_init_file(pic_state *); #define DONE pic_gc_arena_restore(pic, ai); @@ -17,4 +18,5 @@ pic_init_core(pic_state *pic) pic_init_number(pic); DONE; pic_init_time(pic); DONE; pic_init_system(pic); DONE; + pic_init_file(pic); DONE; }