1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 2002 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
12 /***********************************************************************/
20 #include "unixsupport.h"
21 #include "cst2constr.h"
22 #define _INTEGRAL_MAX_BITS 64
23 #include <sys/types.h>
39 static int file_kind_table
[] = {
40 S_IFREG
, S_IFDIR
, S_IFCHR
, S_IFBLK
, S_IFLNK
, S_IFIFO
, S_IFSOCK
43 static value
stat_aux(int use_64
, struct _stati64
*buf
)
48 v
= caml_alloc (12, 0);
49 Store_field (v
, 0, Val_int (buf
->st_dev
));
50 Store_field (v
, 1, Val_int (buf
->st_ino
));
51 Store_field (v
, 2, cst_to_constr (buf
->st_mode
& S_IFMT
, file_kind_table
,
52 sizeof(file_kind_table
) / sizeof(int), 0));
53 Store_field (v
, 3, Val_int(buf
->st_mode
& 07777));
54 Store_field (v
, 4, Val_int (buf
->st_nlink
));
55 Store_field (v
, 5, Val_int (buf
->st_uid
));
56 Store_field (v
, 6, Val_int (buf
->st_gid
));
57 Store_field (v
, 7, Val_int (buf
->st_rdev
));
59 use_64
? copy_int64(buf
->st_size
) : Val_int (buf
->st_size
));
60 Store_field (v
, 9, copy_double((double) buf
->st_atime
));
61 Store_field (v
, 10, copy_double((double) buf
->st_mtime
));
62 Store_field (v
, 11, copy_double((double) buf
->st_ctime
));
66 CAMLprim value
unix_stat(value path
)
71 ret
= _stati64(String_val(path
), &buf
);
72 if (ret
== -1) uerror("stat", path
);
73 if (buf
.st_size
> Max_long
) {
74 win32_maperr(ERROR_ARITHMETIC_OVERFLOW
);
77 return stat_aux(0, &buf
);
80 CAMLprim value
unix_stat_64(value path
)
84 ret
= _stati64(String_val(path
), &buf
);
85 if (ret
== -1) uerror("stat", path
);
86 return stat_aux(1, &buf
);
89 CAMLprim value
unix_fstat(value handle
)
94 ret
= _fstati64(win_CRT_fd_of_filedescr(handle
), &buf
);
95 if (ret
== -1) uerror("fstat", Nothing
);
96 if (buf
.st_size
> Max_long
) {
97 win32_maperr(ERROR_ARITHMETIC_OVERFLOW
);
98 uerror("fstat", Nothing
);
100 return stat_aux(0, &buf
);
103 CAMLprim value
unix_fstat_64(value handle
)
108 ret
= _fstati64(win_CRT_fd_of_filedescr(handle
), &buf
);
109 if (ret
== -1) uerror("fstat", Nothing
);
110 if (buf
.st_size
> Max_long
) {
111 win32_maperr(ERROR_ARITHMETIC_OVERFLOW
);
112 uerror("fstat", Nothing
);
114 return stat_aux(1, &buf
);