|
| 1 | +/***********************************************************************/ |
| 2 | +/* */ |
| 3 | +/* OCaml */ |
| 4 | +/* */ |
| 5 | +/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */ |
| 6 | +/* */ |
| 7 | +/* Copyright 1998 Institut National de Recherche en Informatique et */ |
| 8 | +/* en Automatique. All rights reserved. This file is distributed */ |
| 9 | +/* under the terms of the GNU Library General Public License, with */ |
| 10 | +/* the special exception on linking described in file ../LICENSE. */ |
| 11 | +/* */ |
| 12 | +/***********************************************************************/ |
| 13 | + |
| 14 | +/* The launcher for bytecode executables (if #! is not working) */ |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <string.h> |
| 19 | +#include "../config/s.h" |
| 20 | +#ifdef HAS_UNISTD |
| 21 | +#include <unistd.h> |
| 22 | +#endif |
| 23 | +#include <fcntl.h> |
| 24 | +#include <sys/types.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include "../byterun/caml/mlvalues.h" |
| 27 | +#include "../byterun/caml/exec.h" |
| 28 | + |
| 29 | +char * default_runtime_path = RUNTIME_NAME; |
| 30 | + |
| 31 | +#ifndef MAXPATHLEN |
| 32 | +#define MAXPATHLEN 1024 |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifndef S_ISREG |
| 36 | +#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) |
| 37 | +#endif |
| 38 | + |
| 39 | +#ifndef SEEK_END |
| 40 | +#define SEEK_END 2 |
| 41 | +#endif |
| 42 | + |
| 43 | +#ifndef __CYGWIN__ |
| 44 | + |
| 45 | +/* Normal Unix search path function */ |
| 46 | + |
| 47 | +static char * searchpath(char * name) |
| 48 | +{ |
| 49 | + static char fullname[MAXPATHLEN + 1]; |
| 50 | + char * path; |
| 51 | + char * p; |
| 52 | + char * q; |
| 53 | + struct stat st; |
| 54 | + |
| 55 | + for (p = name; *p != 0; p++) { |
| 56 | + if (*p == '/') return name; |
| 57 | + } |
| 58 | + path = getenv("PATH"); |
| 59 | + if (path == NULL) return name; |
| 60 | + while(1) { |
| 61 | + for (p = fullname; *path != 0 && *path != ':'; p++, path++) |
| 62 | + if (p < fullname + MAXPATHLEN) *p = *path; |
| 63 | + if (p != fullname && p < fullname + MAXPATHLEN) |
| 64 | + *p++ = '/'; |
| 65 | + for (q = name; *q != 0; p++, q++) |
| 66 | + if (p < fullname + MAXPATHLEN) *p = *q; |
| 67 | + *p = 0; |
| 68 | + if (stat(fullname, &st) == 0 && S_ISREG(st.st_mode)) break; |
| 69 | + if (*path == 0) return name; |
| 70 | + path++; |
| 71 | + } |
| 72 | + return fullname; |
| 73 | +} |
| 74 | + |
| 75 | +#else |
| 76 | + |
| 77 | +/* Special version for Cygwin32: takes care of the ".exe" implicit suffix */ |
| 78 | + |
| 79 | +static int file_ok(char * name) |
| 80 | +{ |
| 81 | + int fd; |
| 82 | + /* Cannot use stat() here because it adds ".exe" implicitly */ |
| 83 | + fd = open(name, O_RDONLY); |
| 84 | + if (fd == -1) return 0; |
| 85 | + close(fd); |
| 86 | + return 1; |
| 87 | +} |
| 88 | + |
| 89 | +static char * searchpath(char * name) |
| 90 | +{ |
| 91 | + char * path, * fullname, * p; |
| 92 | + |
| 93 | + path = getenv("PATH"); |
| 94 | + fullname = malloc(strlen(name) + (path == NULL ? 0 : strlen(path)) + 6); |
| 95 | + /* 6 = "/" plus ".exe" plus final "\0" */ |
| 96 | + if (fullname == NULL) return name; |
| 97 | + /* Check for absolute path name */ |
| 98 | + for (p = name; *p != 0; p++) { |
| 99 | + if (*p == '/' || *p == '\\') { |
| 100 | + if (file_ok(name)) return name; |
| 101 | + strcpy(fullname, name); |
| 102 | + strcat(fullname, ".exe"); |
| 103 | + if (file_ok(fullname)) return fullname; |
| 104 | + return name; |
| 105 | + } |
| 106 | + } |
| 107 | + /* Search in path */ |
| 108 | + if (path == NULL) return name; |
| 109 | + while(1) { |
| 110 | + for (p = fullname; *path != 0 && *path != ':'; p++, path++) *p = *path; |
| 111 | + if (p != fullname) *p++ = '/'; |
| 112 | + strcpy(p, name); |
| 113 | + if (file_ok(fullname)) return fullname; |
| 114 | + strcat(fullname, ".exe"); |
| 115 | + if (file_ok(fullname)) return fullname; |
| 116 | + if (*path == 0) break; |
| 117 | + path++; |
| 118 | + } |
| 119 | + return name; |
| 120 | +} |
| 121 | + |
| 122 | +#endif |
| 123 | + |
| 124 | +static unsigned long read_size(char * ptr) |
| 125 | +{ |
| 126 | + unsigned char * p = (unsigned char *) ptr; |
| 127 | + return ((unsigned long) p[0] << 24) + ((unsigned long) p[1] << 16) + |
| 128 | + ((unsigned long) p[2] << 8) + p[3]; |
| 129 | +} |
| 130 | + |
| 131 | +static char * read_runtime_path(int fd) |
| 132 | +{ |
| 133 | + char buffer[TRAILER_SIZE]; |
| 134 | + static char runtime_path[MAXPATHLEN]; |
| 135 | + int num_sections, i; |
| 136 | + uint32 path_size; |
| 137 | + long ofs; |
| 138 | + |
| 139 | + lseek(fd, (long) -TRAILER_SIZE, SEEK_END); |
| 140 | + if (read(fd, buffer, TRAILER_SIZE) < TRAILER_SIZE) return NULL; |
| 141 | + num_sections = read_size(buffer); |
| 142 | + ofs = TRAILER_SIZE + num_sections * 8; |
| 143 | + lseek(fd, -ofs, SEEK_END); |
| 144 | + path_size = 0; |
| 145 | + for (i = 0; i < num_sections; i++) { |
| 146 | + if (read(fd, buffer, 8) < 8) return NULL; |
| 147 | + if (buffer[0] == 'R' && buffer[1] == 'N' && |
| 148 | + buffer[2] == 'T' && buffer[3] == 'M') { |
| 149 | + path_size = read_size(buffer + 4); |
| 150 | + ofs += path_size; |
| 151 | + } else if (path_size > 0) |
| 152 | + ofs += read_size(buffer + 4); |
| 153 | + } |
| 154 | + if (path_size == 0) return default_runtime_path; |
| 155 | + if (path_size >= MAXPATHLEN) return NULL; |
| 156 | + lseek(fd, -ofs, SEEK_END); |
| 157 | + if (read(fd, runtime_path, path_size) != path_size) return NULL; |
| 158 | + runtime_path[path_size - 1] = 0; |
| 159 | + return runtime_path; |
| 160 | +} |
| 161 | + |
| 162 | +static void errwrite(char * msg) |
| 163 | +{ |
| 164 | + write(2, msg, strlen(msg)); |
| 165 | +} |
| 166 | + |
| 167 | +#ifndef O_BINARY |
| 168 | +#define O_BINARY 0 |
| 169 | +#endif |
| 170 | + |
| 171 | +int main(int argc, char ** argv) |
| 172 | +{ |
| 173 | + char * truename, * runtime_path; |
| 174 | + int fd; |
| 175 | + |
| 176 | + truename = searchpath(argv[0]); |
| 177 | + fd = open(truename, O_RDONLY | O_BINARY); |
| 178 | + if (fd == -1 || (runtime_path = read_runtime_path(fd)) == NULL) { |
| 179 | + errwrite(truename); |
| 180 | + errwrite(" not found or is not a bytecode executable file\n"); |
| 181 | + return 2; |
| 182 | + } |
| 183 | + argv[0] = truename; |
| 184 | + execv(runtime_path, argv); |
| 185 | + errwrite("Cannot exec "); |
| 186 | + errwrite(runtime_path); |
| 187 | + errwrite("\n"); |
| 188 | + return 2; |
| 189 | +} |
0 commit comments