1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Caml Special Light */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1995 Institut National de Recherche en Informatique et */
|
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $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>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include <string.h>
|
1995-08-10 02:16:58 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
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
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "instruct.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "stacks.h"
|
1995-08-08 06:37:34 -07:00
|
|
|
#ifdef HAS_UI
|
|
|
|
#include "ui.h"
|
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
extern int errno;
|
|
|
|
|
|
|
|
#ifdef HAS_STRERROR
|
|
|
|
|
|
|
|
extern char * strerror();
|
|
|
|
|
|
|
|
char * error_message()
|
|
|
|
{
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
extern int sys_nerr;
|
|
|
|
extern char * sys_errlist [];
|
|
|
|
|
|
|
|
char * error_message()
|
|
|
|
{
|
|
|
|
if (errno < 0 || errno >= sys_nerr)
|
|
|
|
return "unknown error";
|
|
|
|
else
|
|
|
|
return sys_errlist[errno];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAS_STRERROR */
|
|
|
|
|
|
|
|
void sys_error(arg)
|
|
|
|
char * arg;
|
|
|
|
{
|
|
|
|
char * err = error_message();
|
|
|
|
int err_len = strlen(err);
|
|
|
|
int arg_len;
|
|
|
|
value str;
|
|
|
|
|
|
|
|
if (arg == NULL) {
|
|
|
|
str = alloc_string(err_len);
|
|
|
|
bcopy(err, &Byte(str, 0), err_len);
|
|
|
|
} else {
|
|
|
|
arg_len = strlen(arg);
|
|
|
|
str = alloc_string(arg_len + 2 + err_len);
|
|
|
|
bcopy(arg, &Byte(str, 0), arg_len);
|
|
|
|
bcopy(": ", &Byte(str, arg_len), 2);
|
|
|
|
bcopy(err, &Byte(str, arg_len + 2), err_len);
|
|
|
|
}
|
|
|
|
raise_sys_error(str);
|
|
|
|
}
|
|
|
|
|
1996-02-18 06:44:59 -08:00
|
|
|
value sys_exit(retcode) /* ML */
|
1995-05-04 03:15:53 -07:00
|
|
|
value retcode;
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
static int sys_open_flags[] = {
|
1995-08-10 05:18:40 -07:00
|
|
|
O_RDONLY, O_WRONLY, O_APPEND, O_CREAT, O_TRUNC, O_EXCL, O_BINARY, O_TEXT
|
1995-05-04 03:15:53 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
value sys_open(path, flags, perm) /* ML */
|
|
|
|
value path, flags, perm;
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = open(String_val(path), convert_flag_list(flags, sys_open_flags),
|
|
|
|
Int_val(perm));
|
|
|
|
if (ret == -1) sys_error(String_val(path));
|
|
|
|
return Val_long(ret);
|
|
|
|
}
|
|
|
|
|
1995-05-04 05:48:07 -07:00
|
|
|
value sys_file_exists(name) /* ML */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
value sys_remove(name) /* ML */
|
|
|
|
value name;
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = unlink(String_val(name));
|
|
|
|
if (ret != 0) sys_error(String_val(name));
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
value sys_rename(oldname, newname) /* ML */
|
|
|
|
value oldname, newname;
|
|
|
|
{
|
|
|
|
if (rename(String_val(oldname), String_val(newname)) != 0)
|
|
|
|
sys_error(String_val(oldname));
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
value sys_chdir(dirname) /* ML */
|
|
|
|
value dirname;
|
|
|
|
{
|
|
|
|
if (chdir(String_val(dirname)) != 0) sys_error(String_val(dirname));
|
1995-06-18 07:44:56 -07:00
|
|
|
return Val_unit;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
1995-08-23 04:54:56 -07:00
|
|
|
value sys_getcwd(unit) /* ML */
|
|
|
|
value unit;
|
|
|
|
{
|
|
|
|
char buff[4096];
|
1995-09-25 07:42:51 -07:00
|
|
|
#ifdef HAS_GETCWD
|
1995-08-23 04:54:56 -07:00
|
|
|
if (getcwd(buff, sizeof(buff)) == 0) sys_error(NULL);
|
1995-09-25 07:42:51 -07:00
|
|
|
#else
|
|
|
|
if (getwd(buff) == 0) sys_error(NULL);
|
|
|
|
#endif
|
1995-08-23 04:54:56 -07:00
|
|
|
return copy_string(buff);
|
|
|
|
}
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
value sys_getenv(var) /* ML */
|
|
|
|
value var;
|
|
|
|
{
|
|
|
|
char * res;
|
|
|
|
|
|
|
|
res = getenv(String_val(var));
|
|
|
|
if (res == 0) raise_not_found();
|
|
|
|
return copy_string(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ** main_argv;
|
|
|
|
|
|
|
|
value sys_get_argv(unit) /* ML */
|
|
|
|
value unit;
|
|
|
|
{
|
|
|
|
return copy_string_array(main_argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_init(argv)
|
|
|
|
char ** argv;
|
|
|
|
{
|
|
|
|
main_argv = argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
value sys_system_command(command) /* ML */
|
|
|
|
value command;
|
|
|
|
{
|
|
|
|
int retcode = system(String_val(command));
|
|
|
|
if (retcode == -1) sys_error(String_val(command));
|
|
|
|
return Val_int(retcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search path function */
|
|
|
|
|
1995-08-10 02:16:58 -07:00
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
char * searchpath(name)
|
|
|
|
char * name;
|
|
|
|
{
|
|
|
|
static char fullname[512];
|
|
|
|
char * path;
|
|
|
|
char * p;
|
|
|
|
char * q;
|
1995-08-10 02:16:58 -07:00
|
|
|
struct stat st;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/') return name;
|
|
|
|
}
|
|
|
|
path = getenv("PATH");
|
1995-08-10 05:18:40 -07:00
|
|
|
if (path == NULL) return 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
while(1) {
|
|
|
|
p = fullname;
|
|
|
|
while (*path != 0 && *path != ':') {
|
|
|
|
*p++ = *path++;
|
|
|
|
}
|
|
|
|
if (p != fullname) *p++ = '/';
|
|
|
|
q = name;
|
|
|
|
while (*q != 0) {
|
|
|
|
*p++ = *q++;
|
|
|
|
}
|
|
|
|
*p = 0;
|
1995-08-10 02:16:58 -07:00
|
|
|
if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode)) return fullname;
|
1995-05-04 03:15:53 -07:00
|
|
|
if (*path == 0) return 0;
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
}
|