1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
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$ */
|
|
|
|
|
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>
|
2004-01-08 14:28:48 -08:00
|
|
|
#if !_WIN32
|
1998-12-02 08:11:37 -08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "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
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "alloc.h"
|
1996-11-29 10:36:42 -08:00
|
|
|
#include "debugger.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "fail.h"
|
|
|
|
#include "instruct.h"
|
|
|
|
#include "mlvalues.h"
|
2003-03-03 09:16:15 -08:00
|
|
|
#include "osdeps.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "signals.h"
|
|
|
|
#include "stacks.h"
|
1996-10-01 02:46:42 -07:00
|
|
|
#include "sys.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
#ifndef _WIN32
|
1995-05-04 03:15:53 -07:00
|
|
|
extern int errno;
|
2001-08-28 07:47:48 -07:00
|
|
|
#endif
|
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
|
|
|
{
|
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;
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
p = caml_stat_alloc(caml_string_length(path) + 1);
|
2003-02-11 06:30:44 -08:00
|
|
|
strcpy(p, 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);
|
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);
|
2002-03-11 05:13:55 -08:00
|
|
|
#if defined(F_SETFD) && defined(FD_CLOEXEC)
|
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
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
|
|
|
{
|
|
|
|
close(Int_val(fd));
|
|
|
|
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
|
|
|
{
|
1995-08-10 02:16:58 -07:00
|
|
|
struct stat st;
|
|
|
|
return Val_bool(stat(String_val(name), &st) == 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)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(String_val(name), &st) == -1) caml_sys_error(name);
|
|
|
|
#ifdef S_ISDIR
|
|
|
|
return Val_bool(S_ISDIR(st.st_mode));
|
|
|
|
#else
|
|
|
|
return Val_bool(st.st_mode & S_IFDIR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
CAMLprim value caml_sys_remove(value name)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = unlink(String_val(name));
|
2003-12-15 08:29:53 -08:00
|
|
|
if (ret != 0) caml_sys_error(name);
|
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_rename(value oldname, value newname)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (rename(String_val(oldname), String_val(newname)) != 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
|
|
|
{
|
2003-12-15 08:29:53 -08:00
|
|
|
if (chdir(String_val(dirname)) != 0) caml_sys_error(dirname);
|
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_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;
|
|
|
|
static 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;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat len;
|
2006-09-20 04:14:37 -07:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
len = caml_string_length (command);
|
2003-12-31 06:20:40 -08:00
|
|
|
buf = caml_stat_alloc (len + 1);
|
2002-12-12 02:46:15 -08:00
|
|
|
memmove (buf, String_val (command), len + 1);
|
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
|
|
|
|
extern intnat caml_win32_random_seed (void);
|
|
|
|
#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
|
|
|
{
|
2007-03-01 05:37:39 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
return Val_long(caml_win32_random_seed());
|
|
|
|
#else
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat seed;
|
1999-11-23 02:49:40 -08:00
|
|
|
#ifdef HAS_GETTIMEOFDAY
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
2002-04-15 04:41:55 -07:00
|
|
|
seed = tv.tv_sec ^ tv.tv_usec;
|
1999-11-23 02:49:40 -08:00
|
|
|
#else
|
2002-04-15 04:41:55 -07:00
|
|
|
seed = time (NULL);
|
1999-11-23 02:49:40 -08:00
|
|
|
#endif
|
2002-04-15 04:41:55 -07:00
|
|
|
#ifdef HAS_UNISTD
|
2007-03-01 05:37:39 -08:00
|
|
|
seed ^= (getppid() << 16) ^ getpid();
|
2002-04-15 04:41:55 -07:00
|
|
|
#endif
|
|
|
|
return Val_long(seed);
|
2007-03-01 05:37:39 -08:00
|
|
|
#endif
|
1999-07-22 05:59:43 -07: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);
|
|
|
|
result = caml_alloc_small (2, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(result, 0) = ostype;
|
|
|
|
Field(result, 1) = Val_long (8 * sizeof(value));
|
|
|
|
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;
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_ext_table_init(&tbl, 50);
|
2006-09-20 04:14:37 -07:00
|
|
|
if (caml_read_directory(String_val(path), &tbl) == -1){
|
|
|
|
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);
|
|
|
|
}
|