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>
|
1996-11-02 10:00:46 -08:00
|
|
|
#if !macintosh
|
1995-08-10 02:16:58 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
1996-11-02 10:00:46 -08:00
|
|
|
#endif
|
1998-12-02 08:11:37 -08:00
|
|
|
#if !macintosh && !_WIN32
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
1999-11-29 11:03:05 -08:00
|
|
|
#if macintosh
|
|
|
|
#include "macintosh.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
|
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"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "stacks.h"
|
1996-10-01 02:46:42 -07:00
|
|
|
#include "sys.h"
|
1995-08-08 06:37:34 -07:00
|
|
|
#ifdef HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#ifdef HAS_STRERROR
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
#ifndef _WIN32
|
1997-09-02 05:55:01 -07:00
|
|
|
extern char * strerror(int);
|
2001-08-28 07:47:48 -07:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
char * error_message(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
extern int sys_nerr;
|
|
|
|
extern char * sys_errlist [];
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
char * error_message(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (errno < 0 || errno >= sys_nerr)
|
|
|
|
return "unknown error";
|
|
|
|
else
|
|
|
|
return sys_errlist[errno];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAS_STRERROR */
|
|
|
|
|
1998-11-20 07:36:27 -08:00
|
|
|
#ifndef EAGAIN
|
|
|
|
#define EAGAIN (-1)
|
|
|
|
#endif
|
|
|
|
#ifndef EWOULDBLOCK
|
|
|
|
#define EWOULDBLOCK (-1)
|
|
|
|
#endif
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void 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);
|
1996-10-01 02:46:42 -07:00
|
|
|
|
1998-11-20 07:36:27 -08:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
raise_sys_blocked_io();
|
1995-05-04 03:15:53 -07:00
|
|
|
} else {
|
1998-11-20 07:36:27 -08:00
|
|
|
err = error_message();
|
|
|
|
if (arg == NO_ARG) {
|
|
|
|
str = copy_string(err);
|
|
|
|
} else {
|
|
|
|
int err_len = strlen(err);
|
|
|
|
int arg_len = string_length(arg);
|
1997-05-26 10:16:31 -07:00
|
|
|
str = alloc_string(arg_len + 2 + err_len);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&Byte(str, 0), String_val(arg), arg_len);
|
|
|
|
memmove(&Byte(str, arg_len), ": ", 2);
|
|
|
|
memmove(&Byte(str, arg_len + 2), err, err_len);
|
1998-11-20 07:36:27 -08:00
|
|
|
}
|
|
|
|
raise_sys_error(str);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_exit(value retcode)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-01-02 06:15:42 -08:00
|
|
|
#ifndef NATIVE_CODE
|
1996-11-29 10:36:42 -08:00
|
|
|
debugger(PROGRAM_EXIT);
|
1997-01-02 06:15:42 -08:00
|
|
|
#endif
|
1995-08-08 06:37:34 -07:00
|
|
|
#ifdef HAS_UI
|
|
|
|
ui_exit(Int_val(retcode));
|
|
|
|
#else
|
1995-05-04 03:15:53 -07:00
|
|
|
exit(Int_val(retcode));
|
1995-08-08 06:37:34 -07:00
|
|
|
#endif
|
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[] = {
|
1996-04-29 06:18:36 -07:00
|
|
|
O_RDONLY, O_WRONLY, O_APPEND, O_CREAT, O_TRUNC, O_EXCL,
|
|
|
|
O_BINARY, O_TEXT, O_NONBLOCK
|
1995-05-04 03:15:53 -07:00
|
|
|
};
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_open(value path, value flags, value perm)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int ret;
|
1996-11-02 10:00:46 -08:00
|
|
|
ret = open(String_val(path), convert_flag_list(flags, sys_open_flags)
|
|
|
|
#if !macintosh
|
1997-06-13 08:49:36 -07:00
|
|
|
, Int_val(perm)
|
1996-11-02 10:00:46 -08:00
|
|
|
#endif
|
1997-05-19 08:42:21 -07:00
|
|
|
);
|
1996-10-01 02:46:42 -07:00
|
|
|
if (ret == -1) sys_error(path);
|
1995-05-04 03:15:53 -07:00
|
|
|
return Val_long(ret);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_close(value fd)
|
1996-10-31 08:01:50 -08:00
|
|
|
{
|
|
|
|
close(Int_val(fd));
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_file_exists(value name)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-11-02 10:00:46 -08:00
|
|
|
#if macintosh
|
|
|
|
int f;
|
|
|
|
f = open (String_val (name), O_RDONLY);
|
|
|
|
if (f == -1) return (Val_bool (0));
|
|
|
|
close (f);
|
|
|
|
return (Val_bool (1));
|
|
|
|
#else
|
1995-08-10 02:16:58 -07:00
|
|
|
struct stat st;
|
|
|
|
return Val_bool(stat(String_val(name), &st) == 0);
|
1996-11-02 10:00:46 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_remove(value name)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = unlink(String_val(name));
|
1996-10-01 02:46:42 -07:00
|
|
|
if (ret != 0) sys_error(name);
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_rename(value oldname, value newname)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
if (rename(String_val(oldname), String_val(newname)) != 0)
|
1996-10-01 02:46:42 -07:00
|
|
|
sys_error(oldname);
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_chdir(value dirname)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1996-10-01 02:46:42 -07:00
|
|
|
if (chdir(String_val(dirname)) != 0) sys_error(dirname);
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value 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
|
1996-10-01 02:46:42 -07:00
|
|
|
if (getcwd(buff, sizeof(buff)) == 0) sys_error(NO_ARG);
|
1995-09-25 07:42:51 -07:00
|
|
|
#else
|
1996-10-01 02:46:42 -07:00
|
|
|
if (getwd(buff) == 0) sys_error(NO_ARG);
|
1996-11-02 10:00:46 -08:00
|
|
|
#endif /* HAS_GETCWD */
|
1995-08-23 04:54:56 -07:00
|
|
|
return copy_string(buff);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_getenv(value var)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char * res;
|
|
|
|
|
|
|
|
res = getenv(String_val(var));
|
|
|
|
if (res == 0) raise_not_found();
|
|
|
|
return copy_string(res);
|
|
|
|
}
|
|
|
|
|
2001-02-19 04:29:00 -08:00
|
|
|
char ** caml_main_argv;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_get_argv(value unit)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2001-02-19 04:29:00 -08:00
|
|
|
return copy_string_array((char const **) caml_main_argv);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void sys_init(char **argv)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2001-02-19 04:29:00 -08:00
|
|
|
caml_main_argv = argv;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
#ifdef _WIN32
|
|
|
|
extern int win32_system(char * command);
|
|
|
|
#endif
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_system_command(value command)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1998-12-02 08:11:37 -08:00
|
|
|
int status, retcode;
|
1999-11-29 11:03:05 -08:00
|
|
|
|
|
|
|
enter_blocking_section ();
|
1997-12-10 05:27:48 -08:00
|
|
|
#ifndef _WIN32
|
1998-12-02 08:11:37 -08:00
|
|
|
status = system(String_val(command));
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
retcode = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
retcode = 255;
|
1997-12-10 05:27:48 -08:00
|
|
|
#else
|
1998-12-02 08:11:37 -08:00
|
|
|
status = retcode = win32_system(String_val(command));
|
1997-12-10 05:27:48 -08:00
|
|
|
#endif
|
1999-11-29 11:03:05 -08:00
|
|
|
leave_blocking_section ();
|
1998-12-02 08:11:37 -08:00
|
|
|
if (status == -1) sys_error(command);
|
1995-05-04 03:15:53 -07:00
|
|
|
return Val_int(retcode);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_time(value unit)
|
1998-02-25 02:20:38 -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 copy_double((double)(t.tms_utime + t.tms_stime) / CLK_TCK);
|
|
|
|
#else
|
|
|
|
/* clock() is standard ANSI C */
|
|
|
|
return copy_double((double)clock() / CLOCKS_PER_SEC);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value sys_random_seed (value unit)
|
1999-07-22 05:59:43 -07:00
|
|
|
{
|
1999-11-23 02:49:40 -08:00
|
|
|
#ifdef HAS_GETTIMEOFDAY
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return Val_int(tv.tv_sec ^ tv.tv_usec);
|
|
|
|
#else
|
|
|
|
return Val_int(time (NULL));
|
|
|
|
#endif
|
1999-07-22 05:59:43 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value 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
|
|
|
|
|
|
|
ostype = copy_string(OCAML_OS_TYPE);
|
1999-11-29 11:03:05 -08:00
|
|
|
result = alloc_small (2, 0);
|
|
|
|
Field(result, 0) = ostype;
|
|
|
|
Field(result, 1) = Val_long (8 * sizeof(value));
|
|
|
|
CAMLreturn (result);
|
1996-10-06 09:36:37 -07:00
|
|
|
}
|
|
|
|
|