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 */
|
|
|
|
/* */
|
1998-04-14 07:48:34 -07:00
|
|
|
/* Copyright 1998 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1998-04-14 07:48:34 -07:00
|
|
|
/* The launcher for bytecode executables (if #! is not working) */
|
|
|
|
|
1999-03-04 04:45:13 -08:00
|
|
|
#include <stdio.h>
|
1998-04-14 07:48:34 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../config/s.h"
|
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2014-12-27 07:04:22 -08:00
|
|
|
#include "../byterun/caml/mlvalues.h"
|
|
|
|
#include "../byterun/caml/exec.h"
|
1998-04-14 07:48:34 -07:00
|
|
|
|
1998-06-01 07:53:28 -07:00
|
|
|
char * default_runtime_path = RUNTIME_NAME;
|
|
|
|
|
2000-08-10 02:58:08 -07:00
|
|
|
#ifndef MAXPATHLEN
|
1998-04-14 07:48:34 -07:00
|
|
|
#define MAXPATHLEN 1024
|
2000-08-10 02:58:08 -07:00
|
|
|
#endif
|
1998-04-14 07:48:34 -07:00
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SEEK_END
|
|
|
|
#define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
2000-08-10 02:58:08 -07:00
|
|
|
#ifndef __CYGWIN32__
|
|
|
|
|
|
|
|
/* Normal Unix search path function */
|
|
|
|
|
1998-04-14 07:48:34 -07:00
|
|
|
static char * searchpath(char * name)
|
|
|
|
{
|
|
|
|
static char fullname[MAXPATHLEN + 1];
|
|
|
|
char * path;
|
|
|
|
char * p;
|
|
|
|
char * q;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/') return name;
|
|
|
|
}
|
|
|
|
path = getenv("PATH");
|
|
|
|
if (path == NULL) return name;
|
|
|
|
while(1) {
|
|
|
|
for (p = fullname; *path != 0 && *path != ':'; p++, path++)
|
|
|
|
if (p < fullname + MAXPATHLEN) *p = *path;
|
|
|
|
if (p != fullname && p < fullname + MAXPATHLEN)
|
|
|
|
*p++ = '/';
|
|
|
|
for (q = name; *q != 0; p++, q++)
|
|
|
|
if (p < fullname + MAXPATHLEN) *p = *q;
|
|
|
|
*p = 0;
|
|
|
|
if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode)) break;
|
|
|
|
if (*path == 0) return name;
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
return fullname;
|
|
|
|
}
|
|
|
|
|
2000-08-10 02:58:08 -07:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Special version for Cygwin32: takes care of the ".exe" implicit suffix */
|
|
|
|
|
|
|
|
static int file_ok(char * name)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
/* Cannot use stat() here because it adds ".exe" implicitly */
|
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd == -1) return 0;
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * searchpath(char * name)
|
|
|
|
{
|
|
|
|
char * path, * fullname, * p;
|
|
|
|
|
|
|
|
path = getenv("PATH");
|
|
|
|
fullname = malloc(strlen(name) + (path == NULL ? 0 : strlen(path)) + 6);
|
|
|
|
/* 6 = "/" plus ".exe" plus final "\0" */
|
|
|
|
if (fullname == NULL) return name;
|
|
|
|
/* Check for absolute path name */
|
|
|
|
for (p = name; *p != 0; p++) {
|
|
|
|
if (*p == '/' || *p == '\\') {
|
|
|
|
if (file_ok(name)) return name;
|
|
|
|
strcpy(fullname, name);
|
|
|
|
strcat(fullname, ".exe");
|
|
|
|
if (file_ok(fullname)) return fullname;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Search in path */
|
|
|
|
if (path == NULL) return name;
|
|
|
|
while(1) {
|
|
|
|
for (p = fullname; *path != 0 && *path != ':'; p++, path++) *p = *path;
|
|
|
|
if (p != fullname) *p++ = '/';
|
|
|
|
strcpy(p, name);
|
|
|
|
if (file_ok(fullname)) return fullname;
|
|
|
|
strcat(fullname, ".exe");
|
|
|
|
if (file_ok(fullname)) return fullname;
|
|
|
|
if (*path == 0) break;
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
#endif
|
2000-08-10 02:58:08 -07:00
|
|
|
|
1998-04-14 07:48:34 -07:00
|
|
|
static unsigned long read_size(char * ptr)
|
|
|
|
{
|
|
|
|
unsigned char * p = (unsigned char *) ptr;
|
|
|
|
return ((unsigned long) p[0] << 24) + ((unsigned long) p[1] << 16) +
|
|
|
|
((unsigned long) p[2] << 8) + p[3];
|
|
|
|
}
|
|
|
|
|
1998-06-01 07:53:28 -07:00
|
|
|
static char * read_runtime_path(int fd)
|
1998-04-14 07:48:34 -07:00
|
|
|
{
|
|
|
|
char buffer[TRAILER_SIZE];
|
1998-06-01 07:53:28 -07:00
|
|
|
static char runtime_path[MAXPATHLEN];
|
2000-03-29 05:10:50 -08:00
|
|
|
int num_sections, i;
|
2014-08-27 02:58:33 -07:00
|
|
|
uint32_t path_size;
|
2000-03-29 05:10:50 -08:00
|
|
|
long ofs;
|
1998-04-14 07:48:34 -07:00
|
|
|
|
|
|
|
lseek(fd, (long) -TRAILER_SIZE, SEEK_END);
|
1998-06-01 07:53:28 -07:00
|
|
|
if (read(fd, buffer, TRAILER_SIZE) < TRAILER_SIZE) return NULL;
|
2000-03-29 05:10:50 -08:00
|
|
|
num_sections = read_size(buffer);
|
|
|
|
ofs = TRAILER_SIZE + num_sections * 8;
|
|
|
|
lseek(fd, -ofs, SEEK_END);
|
|
|
|
path_size = 0;
|
|
|
|
for (i = 0; i < num_sections; i++) {
|
|
|
|
if (read(fd, buffer, 8) < 8) return NULL;
|
|
|
|
if (buffer[0] == 'R' && buffer[1] == 'N' &&
|
|
|
|
buffer[2] == 'T' && buffer[3] == 'M') {
|
|
|
|
path_size = read_size(buffer + 4);
|
|
|
|
ofs += path_size;
|
|
|
|
} else if (path_size > 0)
|
|
|
|
ofs += read_size(buffer + 4);
|
|
|
|
}
|
1998-06-01 07:53:28 -07:00
|
|
|
if (path_size == 0) return default_runtime_path;
|
2000-03-29 05:10:50 -08:00
|
|
|
if (path_size >= MAXPATHLEN) return NULL;
|
|
|
|
lseek(fd, -ofs, SEEK_END);
|
1998-06-01 07:53:28 -07:00
|
|
|
if (read(fd, runtime_path, path_size) != path_size) return NULL;
|
1998-04-14 07:48:34 -07:00
|
|
|
runtime_path[path_size - 1] = 0;
|
1998-06-01 07:53:28 -07:00
|
|
|
return runtime_path;
|
1998-04-14 07:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void errwrite(char * msg)
|
|
|
|
{
|
|
|
|
write(2, msg, strlen(msg));
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-11-28 08:21:26 -08:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
int main(int argc, char ** argv)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1998-06-01 07:53:28 -07:00
|
|
|
char * truename, * runtime_path;
|
1998-04-14 07:48:34 -07:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
truename = searchpath(argv[0]);
|
2000-11-28 08:21:26 -08:00
|
|
|
fd = open(truename, O_RDONLY | O_BINARY);
|
1998-06-01 07:53:28 -07:00
|
|
|
if (fd == -1 || (runtime_path = read_runtime_path(fd)) == NULL) {
|
1998-04-14 07:48:34 -07:00
|
|
|
errwrite(truename);
|
|
|
|
errwrite(" not found or is not a bytecode executable file\n");
|
|
|
|
return 2;
|
|
|
|
}
|
2000-08-10 02:58:08 -07:00
|
|
|
argv[0] = truename;
|
1998-04-14 07:48:34 -07:00
|
|
|
execv(runtime_path, argv);
|
|
|
|
errwrite("Cannot exec ");
|
|
|
|
errwrite(runtime_path);
|
|
|
|
errwrite("\n");
|
1995-05-04 03:15:53 -07:00
|
|
|
return 2;
|
|
|
|
}
|