diff options
Diffstat (limited to 'otherlibs/unix/stat.c')
-rw-r--r-- | otherlibs/unix/stat.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/otherlibs/unix/stat.c b/otherlibs/unix/stat.c index a0f1c04a8..db9150ad0 100644 --- a/otherlibs/unix/stat.c +++ b/otherlibs/unix/stat.c @@ -13,6 +13,7 @@ /* $Id$ */ +#include <errno.h> #include <mlvalues.h> #include <memory.h> #include <alloc.h> @@ -20,6 +21,7 @@ #include "cst2constr.h" #include <sys/types.h> #include <sys/stat.h> +#include <io.h> #ifndef S_IFLNK #define S_IFLNK 0 @@ -34,6 +36,10 @@ #define S_IFBLK 0 #endif +#ifndef EOVERFLOW +#define EOVERFLOW ERANGE +#endif + static int file_kind_table[] = { S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK }; @@ -71,6 +77,7 @@ CAMLprim value unix_stat(value path) struct stat buf; ret = stat(String_val(path), &buf); if (ret == -1) uerror("stat", path); + if (buf.st_size > Max_long) unix_error(EOVERFLOW, "stat", path); return stat_aux(&buf); } @@ -84,6 +91,7 @@ CAMLprim value unix_lstat(value path) ret = stat(String_val(path), &buf); #endif if (ret == -1) uerror("lstat", path); + if (buf.st_size > Max_long) unix_error(EOVERFLOW, "lstat", path); return stat_aux(&buf); } @@ -93,5 +101,65 @@ CAMLprim value unix_fstat(value fd) struct stat buf; ret = fstat(Int_val(fd), &buf); if (ret == -1) uerror("fstat", Nothing); + if (buf.st_size > Max_long) unix_error(EOVERFLOW, "fstat", Nothing); return stat_aux(&buf); } + +static value stat_aux_64(struct stat *buf) +{ + value v; + value atime = Val_unit, mtime = Val_unit, ctime = Val_unit; + + Begin_roots3(atime,mtime,ctime) + atime = copy_double((double) buf->st_atime); + mtime = copy_double((double) buf->st_mtime); + ctime = copy_double((double) buf->st_ctime); + v = alloc_small(12, 0); + Field (v, 0) = Val_int (buf->st_dev); + Field (v, 1) = Val_int (buf->st_ino); + Field (v, 2) = cst_to_constr(buf->st_mode & S_IFMT, file_kind_table, + sizeof(file_kind_table) / sizeof(int), 0); + Field (v, 3) = Val_int(buf->st_mode & 07777); + Field (v, 4) = Val_int (buf->st_nlink); + Field (v, 5) = Val_int (buf->st_uid); + Field (v, 6) = Val_int (buf->st_gid); + Field (v, 7) = Val_int (buf->st_rdev); + Field (v, 8) = Val_file_offset (buf->st_size); + Field (v, 9) = atime; + Field (v, 10) = mtime; + Field (v, 11) = ctime; + End_roots(); + return v; +} + +CAMLprim value unix_stat_64(value path) +{ + int ret; + struct stat buf; + ret = stat(String_val(path), &buf); + if (ret == -1) uerror("stat", path); + return stat_aux_64(&buf); +} + +CAMLprim value unix_lstat_64(value path) +{ + int ret; + struct stat buf; +#ifdef HAS_SYMLINK + ret = lstat(String_val(path), &buf); +#else + ret = stat(String_val(path), &buf); +#endif + if (ret == -1) uerror("lstat", path); + return stat_aux_64(&buf); +} + +CAMLprim value unix_fstat_64(value fd) +{ + int ret; + struct stat buf; + ret = fstat(Int_val(fd), &buf); + if (ret == -1) uerror("fstat", Nothing); + return stat_aux_64(&buf); +} + |