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 */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* 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
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
1996-02-22 04:51:45 -08:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
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;
|
|
|
|
{
|
1996-02-22 04:51:45 -08:00
|
|
|
char * fullname;
|
1995-05-04 03:15:53 -07:00
|
|
|
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;
|
1996-02-23 02:28:47 -08:00
|
|
|
fullname = stat_alloc(strlen(name) + strlen(path) + 2);
|
1995-05-04 03:15:53 -07:00
|
|
|
while(1) {
|
1996-02-22 04:51:45 -08:00
|
|
|
for (p = fullname; *path != 0 && *path != ':'; p++, path++) *p = *path;
|
1995-05-04 03:15:53 -07:00
|
|
|
if (p != fullname) *p++ = '/';
|
1996-02-22 04:51:45 -08:00
|
|
|
for (q = name; *q != 0; p++, q++) *p = *q;
|
|
|
|
*p = 0;
|
|
|
|
if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode)) break;
|
|
|
|
if (*path == 0) return 0;
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
char * searchpath(name)
|
|
|
|
char * name;
|
|
|
|
{
|
|
|
|
char * fullname;
|
|
|
|
char * path;
|
|
|
|
char * p;
|
|
|
|
char * q;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/' || *p == '\\' || *p == ':') return name;
|
|
|
|
}
|
|
|
|
if (stat(name, &st) == 0) return name;
|
|
|
|
path = getenv("PATH");
|
|
|
|
if (path == NULL) return 0;
|
|
|
|
fullname = stat_alloc(strlen(name) + strlen(path) + 6);
|
1996-02-23 05:54:09 -08:00
|
|
|
strcpy(fullname, name);
|
|
|
|
strcat(fullname, ".exe");
|
|
|
|
if (stat(fullname, &st) == 0) return fullname;
|
1996-02-22 04:51:45 -08:00
|
|
|
while(1) {
|
|
|
|
for (p = fullname; *path != 0 && *path != ';'; p++, path++) *p = *path;
|
1996-02-23 05:54:09 -08:00
|
|
|
if (p != fullname && p[-1] != '\\') *p++ = '\\';
|
1996-02-22 04:51:45 -08:00
|
|
|
for (q = name; *q != 0; p++, q++) *p = *q;
|
1995-05-04 03:15:53 -07:00
|
|
|
*p = 0;
|
1996-02-22 04:51:45 -08:00
|
|
|
if (stat(fullname, &st) == 0) break;
|
|
|
|
strcpy(p, ".exe");
|
|
|
|
if (stat(fullname, &st) == 0) break;
|
1995-05-04 03:15:53 -07:00
|
|
|
if (*path == 0) return 0;
|
|
|
|
path++;
|
|
|
|
}
|
1996-02-22 04:51:45 -08:00
|
|
|
return fullname;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1996-02-22 04:51:45 -08:00
|
|
|
|
|
|
|
#endif
|