Added the following file system primitives

- file-readable? path -> boolean
 - file-writable? path -> boolean
 - file-executable? path -> boolean
 - file-size path -> integer
 - rename-file source dest -> void
(thanks to Andreas Rottmann)
This commit is contained in:
Abdulaziz Ghuloum
2008-11-10 23:36:11 -05:00
parent 2903b7f9e0
commit 25f40fefb0
7 changed files with 149 additions and 16 deletions
+4
View File
@@ -220,6 +220,10 @@ ikptr ik_underflow_handler(ikpcb*);
ikptr ik_unsafe_alloc(ikpcb* pcb, int size);
ikptr ik_safe_alloc(ikpcb* pcb, int size);
ikptr u_to_number(unsigned long, ikpcb*);
ikptr ull_to_number(unsigned long long, ikpcb*);
ikptr normalize_bignum(long int limbs, int sign, ikptr r);
#define IK_HEAP_EXT_SIZE (32 * 4096)
#define IK_HEAPSIZE (1024 * ((wordsize==4)?1:2) * 4096) /* 4/8 MB */
+13
View File
@@ -3,6 +3,7 @@
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include <gmp.h>
ikptr
ikrt_isapointer(ikptr x, ikpcb* pcb){
@@ -262,6 +263,18 @@ u_to_number(unsigned long n, ikpcb* pcb) {
return bn+vector_tag;
}
ikptr
ull_to_number(unsigned long long n, ikpcb* pcb) {
unsigned long long mxn = ((unsigned long)-1)>>(fx_shift+1);
if (n <= mxn) {
return fix(n);
}
ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data+sizeof(long long)));
bcopy((char*)(&n), (char*)(bn+disp_bignum_data), sizeof(long long));
return normalize_bignum(sizeof(long long)/sizeof(mp_limb_t), 0, bn);
}
ikptr
d_to_number(double n, ikpcb* pcb) {
ikptr x = ik_safe_alloc(pcb, flonum_size) + vector_tag;
+59
View File
@@ -1037,3 +1037,62 @@ ikrt_debug(ikptr x){
fprintf(stderr, "DEBUG 0x%016lx\n", (long int)x);
return 0;
}
ikptr
ikrt_access(ikptr filename, ikptr how /*, ikpcb* pcb */){
char* fn = (char*)(filename + off_bytevector_data);
int r;
int ik_how;
int c_how;
ik_how = unfix(how);
if (ik_how == 0) {
c_how = F_OK;
} else {
c_how = 0;
if (ik_how & 1) c_how |= R_OK;
if (ik_how & 2) c_how |= W_OK;
if (ik_how & 4) c_how |= X_OK;
}
r = access(fn, c_how);
if (r == 0) {
return true_object;
} else if ((errno == EACCES) ||
(errno == EROFS) ||
(errno == ETXTBSY)) {
return false_object;
} else {
return ik_errno_to_code();
}
}
ikptr
ikrt_file_size(ikptr filename, ikpcb* pcb){
char* fn = (char*)(filename + off_bytevector_data);
struct stat s;
int r = stat(fn, &s);
if (r == 0) {
if (sizeof(off_t) == sizeof(long)) {
return u_to_number(s.st_size, pcb);
} else if (sizeof(off_t) == sizeof(long long)) {
return ull_to_number(s.st_size, pcb);
} else {
fprintf(stderr, "internal error in ikarus: invalid off_t size\n");
exit(-1);
}
} else {
return ik_errno_to_code();
}
}
ikptr
ikrt_rename_file(ikptr src, ikptr dst /* ikpcb* pcb */){
int err = rename((char*)(src + off_bytevector_data),
(char*)(dst + off_bytevector_data));
if (err == 0) {
return true_object;
} else {
return ik_errno_to_code();
}
}