1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2002-03-02 01:16:39 -08:00
|
|
|
#include <errno.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <mlvalues.h>
|
1998-06-09 08:06:39 -07:00
|
|
|
#include <memory.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <alloc.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
#include "cst2constr.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2002-03-02 01:16:39 -08:00
|
|
|
#include <io.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
#ifndef S_IFLNK
|
|
|
|
#define S_IFLNK 0
|
|
|
|
#endif
|
|
|
|
#ifndef S_IFIFO
|
|
|
|
#define S_IFIFO 0
|
|
|
|
#endif
|
|
|
|
#ifndef S_IFSOCK
|
|
|
|
#define S_IFSOCK 0
|
|
|
|
#endif
|
1996-09-04 07:15:31 -07:00
|
|
|
#ifndef S_IFBLK
|
|
|
|
#define S_IFBLK 0
|
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
|
2002-03-02 01:16:39 -08:00
|
|
|
#ifndef EOVERFLOW
|
|
|
|
#define EOVERFLOW ERANGE
|
|
|
|
#endif
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
static int file_kind_table[] = {
|
|
|
|
S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK
|
|
|
|
};
|
|
|
|
|
2002-03-06 08:52:12 -08:00
|
|
|
static value stat_aux(int use_64, struct stat *buf)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2003-05-05 07:20:58 -07:00
|
|
|
CAMLparam0();
|
|
|
|
CAMLlocal5(atime, mtime, ctime, offset, v);
|
1995-05-08 08:18:32 -07:00
|
|
|
|
2003-05-05 07:20:58 -07:00
|
|
|
atime = copy_double((double) buf->st_atime);
|
|
|
|
mtime = copy_double((double) buf->st_mtime);
|
|
|
|
ctime = copy_double((double) buf->st_ctime);
|
|
|
|
offset = use_64 ? Val_file_offset(buf->st_size) : Val_int (buf->st_size);
|
|
|
|
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) = offset;
|
|
|
|
Field (v, 9) = atime;
|
|
|
|
Field (v, 10) = mtime;
|
|
|
|
Field (v, 11) = ctime;
|
|
|
|
CAMLreturn(v);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_stat(value path)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
ret = stat(String_val(path), &buf);
|
|
|
|
if (ret == -1) uerror("stat", path);
|
2002-09-27 01:32:28 -07:00
|
|
|
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
|
|
|
|
unix_error(EOVERFLOW, "stat", path);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(0, &buf);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_lstat(value path)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
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);
|
2002-09-27 01:32:28 -07:00
|
|
|
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
|
|
|
|
unix_error(EOVERFLOW, "lstat", path);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(0, &buf);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_fstat(value fd)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
ret = fstat(Int_val(fd), &buf);
|
|
|
|
if (ret == -1) uerror("fstat", Nothing);
|
2002-09-27 01:32:28 -07:00
|
|
|
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
|
|
|
|
unix_error(EOVERFLOW, "fstat", Nothing);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(0, &buf);
|
2002-03-02 01:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value unix_stat_64(value path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
ret = stat(String_val(path), &buf);
|
|
|
|
if (ret == -1) uerror("stat", path);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(1, &buf);
|
2002-03-02 01:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(1, &buf);
|
2002-03-02 01:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value unix_fstat_64(value fd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat buf;
|
|
|
|
ret = fstat(Int_val(fd), &buf);
|
|
|
|
if (ret == -1) uerror("fstat", Nothing);
|
2002-03-06 08:52:12 -08:00
|
|
|
return stat_aux(1, &buf);
|
2002-03-02 01:16:39 -08:00
|
|
|
}
|