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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Basic system calls */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
1995-05-04 05:48:07 -07:00
|
|
|
#include <stdlib.h>
|
1997-06-13 08:49:36 -07:00
|
|
|
#include <stdio.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <string.h>
|
1998-02-25 02:20:38 -08:00
|
|
|
#include <time.h>
|
1995-08-10 02:16:58 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-08-15 08:57:47 -07:00
|
|
|
#if _WIN32
|
|
|
|
#include <io.h> /* for isatty */
|
|
|
|
#else
|
1998-12-02 08:11:37 -08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/config.h"
|
1996-02-21 02:49:46 -08:00
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-02-25 02:20:38 -08:00
|
|
|
#ifdef HAS_TIMES
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
2006-01-04 08:55:50 -08:00
|
|
|
#ifdef HAS_GETRUSAGE
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
1999-11-23 02:49:40 -08:00
|
|
|
#ifdef HAS_GETTIMEOFDAY
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/alloc.h"
|
|
|
|
#include "caml/debugger.h"
|
|
|
|
#include "caml/fail.h"
|
|
|
|
#include "caml/instruct.h"
|
|
|
|
#include "caml/mlvalues.h"
|
|
|
|
#include "caml/osdeps.h"
|
|
|
|
#include "caml/signals.h"
|
|
|
|
#include "caml/stacks.h"
|
|
|
|
#include "caml/sys.h"
|
|
|
|
#include "caml/gc_ctrl.h"
|
2015-08-15 08:57:47 -07:00
|
|
|
#include "caml/io.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-15 08:29:53 -08:00
|
|
|
static char * error_message(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
1998-11-20 07:36:27 -08:00
|
|
|
#ifndef EAGAIN
|
|
|
|
#define EAGAIN (-1)
|
|
|
|
#endif
|
|
|
|
#ifndef EWOULDBLOCK
|
|
|
|
#define EWOULDBLOCK (-1)
|
|
|
|
#endif
|
|
|
|
|
2003-12-15 08:29:53 -08:00
|
|
|
CAMLexport void caml_sys_error(value arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (arg);
|
1998-11-20 07:36:27 -08:00
|
|
|
char * err;
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLlocal1 (str);
|
2006-09-20 04:14:37 -07:00
|
|
|
|
2007-02-25 04:38:36 -08:00
|
|
|
err = error_message();
|
|
|
|
if (arg == NO_ARG) {
|
|
|
|
str = caml_copy_string(err);
|
|
|
|
} else {
|
|
|
|
int err_len = strlen(err);
|
|
|
|
int arg_len = caml_string_length(arg);
|
|
|
|
str = caml_alloc_string(arg_len + 2 + err_len);
|
|
|
|
memmove(&Byte(str, 0), String_val(arg), arg_len);
|
|
|
|
memmove(&Byte(str, arg_len), ": ", 2);
|
|
|
|
memmove(&Byte(str, arg_len + 2), err, err_len);
|
|
|
|
}
|
|
|
|
caml_raise_sys_error(str);
|
|
|
|
CAMLnoreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport void caml_sys_io_error(value arg)
|
|
|
|
{
|
1998-11-20 07:36:27 -08:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_sys_blocked_io();
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
2007-02-25 04:38:36 -08:00
|
|
|
caml_sys_error(arg);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-15 10:37:24 -08:00
|
|
|
CAMLprim value caml_sys_exit(value retcode)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2014-10-09 04:50:23 -07:00
|
|
|
if ((caml_verb_gc & 0x400) != 0) {
|
|
|
|
/* cf caml_gc_counters */
|
|
|
|
double minwords = caml_stat_minor_words
|
2014-12-12 07:18:04 -08:00
|
|
|
+ (double) (caml_young_end - caml_young_ptr);
|
2014-10-09 04:50:23 -07:00
|
|
|
double prowords = caml_stat_promoted_words;
|
|
|
|
double majwords = caml_stat_major_words + (double) caml_allocated_words;
|
|
|
|
double allocated_words =
|
|
|
|
minwords + majwords - prowords;
|
2015-09-11 04:58:31 -07:00
|
|
|
caml_gc_message(0x400, "## Total allocated words: %ld\n",
|
|
|
|
(long)allocated_words);
|
2014-10-09 04:50:23 -07:00
|
|
|
}
|
|
|
|
|
1997-01-02 06:15:42 -08:00
|
|
|
#ifndef NATIVE_CODE
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_debugger(PROGRAM_EXIT);
|
1997-01-02 06:15:42 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
exit(Int_val(retcode));
|
1996-02-18 06:44:59 -08:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_TEXT
|
|
|
|
#define O_TEXT 0
|
|
|
|
#endif
|
1996-04-29 06:18:36 -07:00
|
|
|
#ifndef O_NONBLOCK
|
|
|
|
#ifdef O_NDELAY
|
|
|
|
#define O_NONBLOCK O_NDELAY
|
|
|
|
#else
|
|
|
|
#define O_NONBLOCK 0
|
|
|
|
#endif
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
static int sys_open_flags[] = {
|
2002-03-11 00:39:50 -08:00
|
|
|
O_RDONLY, O_WRONLY, O_APPEND | O_WRONLY, O_CREAT, O_TRUNC, O_EXCL,
|
1996-04-29 06:18:36 -07:00
|
|
|
O_BINARY, O_TEXT, O_NONBLOCK
|
1995-05-04 03:15:53 -07:00
|
|
|
};
|
|
|
|
|
2005-10-13 07:47:05 -07:00
|
|
|
CAMLprim value caml_sys_open(value path, value vflags, value vperm)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-10-13 07:47:05 -07:00
|
|
|
CAMLparam3(path, vflags, vperm);
|
|
|
|
int fd, flags, perm;
|
2003-02-11 06:30:44 -08:00
|
|
|
char * p;
|
|
|
|
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(path));
|
2005-10-13 07:47:05 -07:00
|
|
|
flags = caml_convert_flag_list(vflags, sys_open_flags);
|
|
|
|
perm = Int_val(vperm);
|
2003-02-11 06:30:44 -08:00
|
|
|
/* open on a named FIFO can block (PR#1533) */
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_enter_blocking_section();
|
2005-10-13 07:47:05 -07:00
|
|
|
fd = open(p, flags, perm);
|
2011-07-20 02:17:07 -07:00
|
|
|
/* fcntl on a fd can block (PR#5069)*/
|
|
|
|
#if defined(F_SETFD) && defined(FD_CLOEXEC)
|
|
|
|
if (fd != -1)
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_leave_blocking_section();
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(p);
|
2003-12-15 08:29:53 -08:00
|
|
|
if (fd == -1) caml_sys_error(path);
|
2003-02-11 06:30:44 -08:00
|
|
|
CAMLreturn(Val_long(fd));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_close(value fd)
|
1996-10-31 08:01:50 -08:00
|
|
|
{
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
1996-10-31 08:01:50 -08:00
|
|
|
close(Int_val(fd));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_leave_blocking_section();
|
1996-10-31 08:01:50 -08:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_file_exists(value name)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2014-04-22 02:47:42 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct _stati64 st;
|
|
|
|
#else
|
1995-08-10 02:16:58 -07:00
|
|
|
struct stat st;
|
2014-04-22 02:47:42 -07:00
|
|
|
#endif
|
2013-12-23 08:24:50 -08:00
|
|
|
char * p;
|
|
|
|
int ret;
|
|
|
|
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(name));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
2014-04-22 02:47:42 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
ret = _stati64(p, &st);
|
2014-04-22 03:14:07 -07:00
|
|
|
#else
|
|
|
|
ret = stat(p, &st);
|
2014-04-22 02:47:42 -07:00
|
|
|
#endif
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p);
|
|
|
|
|
|
|
|
return Val_bool(ret == 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2007-02-26 06:21:57 -08:00
|
|
|
CAMLprim value caml_sys_is_directory(value name)
|
|
|
|
{
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLparam1(name);
|
2014-04-22 02:47:42 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct _stati64 st;
|
|
|
|
#else
|
2007-02-26 06:21:57 -08:00
|
|
|
struct stat st;
|
2014-04-22 02:47:42 -07:00
|
|
|
#endif
|
2013-12-23 08:24:50 -08:00
|
|
|
char * p;
|
|
|
|
int ret;
|
|
|
|
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(name));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
2014-04-22 02:47:42 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
ret = _stati64(p, &st);
|
2014-04-22 03:14:07 -07:00
|
|
|
#else
|
|
|
|
ret = stat(p, &st);
|
2014-04-22 02:47:42 -07:00
|
|
|
#endif
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p);
|
|
|
|
|
|
|
|
if (ret == -1) caml_sys_error(name);
|
2007-02-26 06:21:57 -08:00
|
|
|
#ifdef S_ISDIR
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLreturn(Val_bool(S_ISDIR(st.st_mode)));
|
2007-02-26 06:21:57 -08:00
|
|
|
#else
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLreturn(Val_bool(st.st_mode & S_IFDIR));
|
2007-02-26 06:21:57 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_remove(value name)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLparam1(name);
|
|
|
|
char * p;
|
1995-05-04 03:15:53 -07:00
|
|
|
int ret;
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(name));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
|
|
|
ret = unlink(p);
|
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p);
|
2003-12-15 08:29:53 -08:00
|
|
|
if (ret != 0) caml_sys_error(name);
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLreturn(Val_unit);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_rename(value oldname, value newname)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2013-12-23 08:24:50 -08:00
|
|
|
char * p_old;
|
|
|
|
char * p_new;
|
|
|
|
int ret;
|
2014-04-15 10:09:13 -07:00
|
|
|
p_old = caml_strdup(String_val(oldname));
|
|
|
|
p_new = caml_strdup(String_val(newname));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
|
|
|
ret = rename(p_old, p_new);
|
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p_new);
|
|
|
|
caml_stat_free(p_old);
|
|
|
|
if (ret != 0)
|
2004-04-15 09:18:16 -07:00
|
|
|
caml_sys_error(NO_ARG);
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_chdir(value dirname)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2013-12-23 08:24:50 -08:00
|
|
|
CAMLparam1(dirname);
|
|
|
|
char * p;
|
|
|
|
int ret;
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(dirname));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
|
|
|
ret = chdir(p);
|
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p);
|
|
|
|
if (ret != 0) caml_sys_error(dirname);
|
|
|
|
CAMLreturn(Val_unit);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_getcwd(value unit)
|
1995-08-23 04:54:56 -07:00
|
|
|
{
|
|
|
|
char buff[4096];
|
1995-09-25 07:42:51 -07:00
|
|
|
#ifdef HAS_GETCWD
|
2003-12-15 08:29:53 -08:00
|
|
|
if (getcwd(buff, sizeof(buff)) == 0) caml_sys_error(NO_ARG);
|
1995-09-25 07:42:51 -07:00
|
|
|
#else
|
2003-12-15 08:29:53 -08:00
|
|
|
if (getwd(buff) == 0) caml_sys_error(NO_ARG);
|
1996-11-02 10:00:46 -08:00
|
|
|
#endif /* HAS_GETCWD */
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_copy_string(buff);
|
1995-08-23 04:54:56 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_getenv(value var)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char * res;
|
|
|
|
|
|
|
|
res = getenv(String_val(var));
|
2004-01-01 08:42:43 -08:00
|
|
|
if (res == 0) caml_raise_not_found();
|
2003-12-29 14:15:02 -08:00
|
|
|
return caml_copy_string(res);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2002-02-11 05:51:40 -08:00
|
|
|
char * caml_exe_name;
|
2015-07-17 07:31:05 -07:00
|
|
|
char ** caml_main_argv;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_get_argv(value unit)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2002-02-11 05:51:40 -08:00
|
|
|
CAMLparam0 (); /* unit is unused */
|
|
|
|
CAMLlocal3 (exe_name, argv, res);
|
2003-12-29 14:15:02 -08:00
|
|
|
exe_name = caml_copy_string(caml_exe_name);
|
|
|
|
argv = caml_copy_string_array((char const **) caml_main_argv);
|
|
|
|
res = caml_alloc_small(2, 0);
|
2002-02-11 05:51:40 -08:00
|
|
|
Field(res, 0) = exe_name;
|
|
|
|
Field(res, 1) = argv;
|
|
|
|
CAMLreturn(res);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
void caml_sys_init(char * exe_name, char **argv)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2002-02-11 05:51:40 -08:00
|
|
|
caml_exe_name = exe_name;
|
2001-02-19 04:29:00 -08:00
|
|
|
caml_main_argv = argv;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-03-24 07:24:51 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIFEXITED(status) 1
|
|
|
|
#define WEXITSTATUS(status) (status)
|
|
|
|
#else
|
1998-12-02 08:11:37 -08:00
|
|
|
#if !(defined(WIFEXITED) && defined(WEXITSTATUS))
|
|
|
|
/* Assume old-style V7 status word */
|
|
|
|
#define WIFEXITED(status) (((status) & 0xFF) == 0)
|
|
|
|
#define WEXITSTATUS(status) (((status) >> 8) & 0xFF)
|
|
|
|
#endif
|
2000-04-04 06:19:12 -07:00
|
|
|
#endif
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_system_command(value command)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2002-12-12 02:46:15 -08:00
|
|
|
CAMLparam1 (command);
|
1998-12-02 08:11:37 -08:00
|
|
|
int status, retcode;
|
2002-12-12 02:46:15 -08:00
|
|
|
char *buf;
|
2006-09-20 04:14:37 -07:00
|
|
|
|
2014-04-15 10:09:13 -07:00
|
|
|
buf = caml_strdup(String_val(command));
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_enter_blocking_section ();
|
2002-12-12 02:46:15 -08:00
|
|
|
status = system(buf);
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_leave_blocking_section ();
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_stat_free(buf);
|
2003-12-15 08:29:53 -08:00
|
|
|
if (status == -1) caml_sys_error(command);
|
1998-12-02 08:11:37 -08:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
retcode = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
retcode = 255;
|
2002-12-12 02:46:15 -08:00
|
|
|
CAMLreturn (Val_int(retcode));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_time(value unit)
|
1998-02-25 02:20:38 -08:00
|
|
|
{
|
2006-01-04 08:55:50 -08:00
|
|
|
#ifdef HAS_GETRUSAGE
|
|
|
|
struct rusage ru;
|
|
|
|
|
|
|
|
getrusage (RUSAGE_SELF, &ru);
|
|
|
|
return caml_copy_double (ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6
|
|
|
|
+ ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6);
|
1998-02-25 02:20:38 -08:00
|
|
|
#else
|
2006-01-04 08:55:50 -08:00
|
|
|
#ifdef HAS_TIMES
|
|
|
|
#ifndef CLK_TCK
|
|
|
|
#ifdef HZ
|
|
|
|
#define CLK_TCK HZ
|
|
|
|
#else
|
|
|
|
#define CLK_TCK 60
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
struct tms t;
|
|
|
|
times(&t);
|
|
|
|
return caml_copy_double((double)(t.tms_utime + t.tms_stime) / CLK_TCK);
|
|
|
|
#else
|
|
|
|
/* clock() is standard ANSI C */
|
|
|
|
return caml_copy_double((double)clock() / CLOCKS_PER_SEC);
|
|
|
|
#endif
|
1998-02-25 02:20:38 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-03-01 05:37:39 -08:00
|
|
|
#ifdef _WIN32
|
2012-03-14 09:12:09 -07:00
|
|
|
extern int caml_win32_random_seed (intnat data[16]);
|
2007-03-01 05:37:39 -08:00
|
|
|
#endif
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_random_seed (value unit)
|
1999-07-22 05:59:43 -07:00
|
|
|
{
|
2012-03-14 09:12:09 -07:00
|
|
|
intnat data[16];
|
|
|
|
int n, i;
|
|
|
|
value res;
|
2007-03-01 05:37:39 -08:00
|
|
|
#ifdef _WIN32
|
2012-03-14 09:12:09 -07:00
|
|
|
n = caml_win32_random_seed(data);
|
2007-03-01 05:37:39 -08:00
|
|
|
#else
|
2012-03-14 09:12:09 -07:00
|
|
|
int fd;
|
|
|
|
n = 0;
|
|
|
|
/* Try /dev/urandom first */
|
|
|
|
fd = open("/dev/urandom", O_RDONLY, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
unsigned char buffer[12];
|
|
|
|
int nread = read(fd, buffer, 12);
|
|
|
|
close(fd);
|
|
|
|
while (nread > 0) data[n++] = buffer[--nread];
|
|
|
|
}
|
|
|
|
/* If the read from /dev/urandom fully succeeded, we now have 96 bits
|
|
|
|
of good random data and can stop here. Otherwise, complement
|
|
|
|
whatever we got (probably nothing) with some not-very-random data. */
|
|
|
|
if (n < 12) {
|
1999-11-23 02:49:40 -08:00
|
|
|
#ifdef HAS_GETTIMEOFDAY
|
2012-03-14 09:12:09 -07:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
data[n++] = tv.tv_usec;
|
|
|
|
data[n++] = tv.tv_sec;
|
1999-11-23 02:49:40 -08:00
|
|
|
#else
|
2012-03-14 09:12:09 -07:00
|
|
|
data[n++] = time(NULL);
|
1999-11-23 02:49:40 -08:00
|
|
|
#endif
|
2002-04-15 04:41:55 -07:00
|
|
|
#ifdef HAS_UNISTD
|
2012-03-14 09:12:09 -07:00
|
|
|
data[n++] = getpid();
|
|
|
|
data[n++] = getppid();
|
2002-04-15 04:41:55 -07:00
|
|
|
#endif
|
2012-03-14 09:12:09 -07:00
|
|
|
}
|
2007-03-01 05:37:39 -08:00
|
|
|
#endif
|
2012-03-14 09:12:09 -07:00
|
|
|
/* Convert to an OCaml array of ints */
|
|
|
|
res = caml_alloc_small(n, 0);
|
|
|
|
for (i = 0; i < n; i++) Field(res, i) = Val_long(data[i]);
|
|
|
|
return res;
|
1999-07-22 05:59:43 -07:00
|
|
|
}
|
|
|
|
|
2012-11-29 01:55:00 -08:00
|
|
|
CAMLprim value caml_sys_const_big_endian(value unit)
|
|
|
|
{
|
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
|
|
|
return Val_true;
|
|
|
|
#else
|
|
|
|
return Val_false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* returns a value that represents a number of bits */
|
2012-11-29 01:55:00 -08:00
|
|
|
CAMLprim value caml_sys_const_word_size(value unit)
|
|
|
|
{
|
|
|
|
return Val_long(8 * sizeof(value));
|
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* returns a value that represents a number of bits */
|
2014-11-17 04:19:27 -08:00
|
|
|
CAMLprim value caml_sys_const_int_size(value unit)
|
|
|
|
{
|
|
|
|
return Val_long(8 * sizeof(value) - 1) ;
|
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* returns a value that represents a number of words */
|
2014-11-17 04:19:27 -08:00
|
|
|
CAMLprim value caml_sys_const_max_wosize(value unit)
|
|
|
|
{
|
|
|
|
return Val_long(Max_wosize) ;
|
|
|
|
}
|
|
|
|
|
2012-11-29 01:55:00 -08:00
|
|
|
CAMLprim value caml_sys_const_ostype_unix(value unit)
|
|
|
|
{
|
2015-09-11 04:58:31 -07:00
|
|
|
return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Unix"));
|
2012-11-29 01:55:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_sys_const_ostype_win32(value unit)
|
|
|
|
{
|
2015-09-11 04:58:31 -07:00
|
|
|
return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Win32"));
|
2012-11-29 01:55:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value caml_sys_const_ostype_cygwin(value unit)
|
|
|
|
{
|
2015-09-11 04:58:31 -07:00
|
|
|
return Val_bool(0 == strcmp(OCAML_OS_TYPE,"Cygwin"));
|
2012-11-29 01:55:00 -08:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_get_config(value unit)
|
1996-10-06 09:36:37 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam0 (); /* unit is unused */
|
|
|
|
CAMLlocal2 (result, ostype);
|
1997-05-26 10:16:31 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
ostype = caml_copy_string(OCAML_OS_TYPE);
|
2012-03-08 11:52:03 -08:00
|
|
|
result = caml_alloc_small (3, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(result, 0) = ostype;
|
|
|
|
Field(result, 1) = Val_long (8 * sizeof(value));
|
2012-03-08 11:52:03 -08:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
|
|
|
Field(result, 2) = Val_true;
|
|
|
|
#else
|
|
|
|
Field(result, 2) = Val_false;
|
|
|
|
#endif
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (result);
|
1996-10-06 09:36:37 -07:00
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_read_directory(value path)
|
2003-03-03 09:16:15 -08:00
|
|
|
{
|
|
|
|
CAMLparam1(path);
|
|
|
|
CAMLlocal1(result);
|
|
|
|
struct ext_table tbl;
|
2013-12-23 08:24:50 -08:00
|
|
|
char * p;
|
|
|
|
int ret;
|
2003-03-03 09:16:15 -08:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_init(&tbl, 50);
|
2014-04-15 10:09:13 -07:00
|
|
|
p = caml_strdup(String_val(path));
|
2013-12-23 08:24:50 -08:00
|
|
|
caml_enter_blocking_section();
|
|
|
|
ret = caml_read_directory(p, &tbl);
|
|
|
|
caml_leave_blocking_section();
|
|
|
|
caml_stat_free(p);
|
|
|
|
if (ret == -1){
|
2006-09-20 04:14:37 -07:00
|
|
|
caml_ext_table_free(&tbl, 1);
|
|
|
|
caml_sys_error(path);
|
|
|
|
}
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_add(&tbl, NULL);
|
|
|
|
result = caml_copy_string_array((char const **) tbl.contents);
|
|
|
|
caml_ext_table_free(&tbl, 1);
|
2003-03-03 09:16:15 -08:00
|
|
|
CAMLreturn(result);
|
|
|
|
}
|
2015-08-15 08:57:47 -07:00
|
|
|
|
|
|
|
/* Return true if the value is a filedescriptor (int) that is
|
|
|
|
* (presumably) open on an interactive terminal */
|
|
|
|
CAMLprim value caml_sys_isatty(value chan)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
value ret;
|
|
|
|
|
|
|
|
fd = (Channel(chan))->fd;
|
|
|
|
#ifdef _WIN32
|
2015-09-11 04:58:31 -07:00
|
|
|
ret = Val_bool(_isatty(fd));
|
|
|
|
/* https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx */
|
2015-08-15 08:57:47 -07:00
|
|
|
#else
|
|
|
|
ret = Val_bool(isatty(fd));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|