Raymond Toy pushed to branch master at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • src/lisp/lisp.c
    ... ... @@ -127,175 +127,104 @@ int debug_lisp_search = FALSE;
    127 127
     
    
    128 128
     /*
    
    129 129
      * From the current location of the lisp executable, create a suitable
    
    130
    - * default for CMUCLLIB
    
    130
    + * default for CMUCLLIB.  The result is a colon-separated list
    
    131
    + * directories to be used for finding the core file and for the cmucl
    
    132
    + * libraries.
    
    133
    + *
    
    134
    + * The caller must free the returned string.
    
    131 135
      */
    
    132
    -static const char *
    
    136
    +static char *
    
    133 137
     default_cmucllib(const char *argv0arg)
    
    134 138
     {
    
    135
    -    char *p0;
    
    136
    -    char *defpath;
    
    137 139
         char *cwd;
    
    138
    -    char *argv0_dir = strdup(argv0arg);
    
    140
    +    int cwd_len;
    
    141
    +    char **ptr;
    
    142
    +    char *path;
    
    143
    +    int path_len;
    
    144
    +    char *slash;
    
    139 145
     
    
    140
    -    /*
    
    141
    -     * From argv[0], create the appropriate directory by lopping off the
    
    142
    -     * executable name
    
    143
    -     */
    
    146
    +    cwd = realpath(argv0arg, NULL);
    
    144 147
     
    
    145
    -    p0 = strrchr(argv0_dir, '/');
    
    146
    -    if (p0 == NULL) {
    
    147
    -	*argv0_dir = '\0';
    
    148
    -    } else if (p0 != argv0_dir) {
    
    149
    -	*p0 = '\0';
    
    148
    +    if (debug_lisp_search) {
    
    149
    +	fprintf(stderr, "Realpath of %s = %s\n", argv0arg, cwd);
    
    150
    +    }
    
    151
    +
    
    152
    +    if (!cwd) {
    
    153
    +	perror("Cannot determine realpath of lisp executable");
    
    154
    +	exit(1);
    
    150 155
         }
    
    151 156
     
    
    152 157
         /*
    
    153
    -     * Create the full pathname of the directory containing the
    
    154
    -     * executable.  argv[0] can be an absolute or relative path.
    
    158
    +     * Delete the binary name from the full path, leaving just the
    
    159
    +     * full directory to the executable.
    
    155 160
          */
    
    156
    -    if (debug_lisp_search) {
    
    157
    -	fprintf(stderr, "argv[0] = %s\n", argv0arg);
    
    158
    -	fprintf(stderr, "argv_dir = %s\n", argv0_dir);
    
    161
    +    slash = strrchr(cwd, '/');
    
    162
    +    if (slash) {
    
    163
    +	slash[1] = '\0';
    
    159 164
         }
    
    160 165
     
    
    166
    +    if (debug_lisp_search) {
    
    167
    +	fprintf(stderr, "Executable path %s\n", cwd);
    
    168
    +    }
    
    161 169
     
    
    162
    -    if (argv0_dir[0] == '/') {
    
    163
    -	cwd = malloc(strlen(argv0_dir) + 2);
    
    164
    -	strcpy(cwd, argv0_dir);
    
    165
    -	strcat(cwd, "/");
    
    166
    -	if (debug_lisp_search) {
    
    167
    -	    fprintf(stderr, "absolute path, argv[0] = %s\n", cwd);
    
    168
    -	}
    
    169
    -
    
    170
    -    } else if (*argv0_dir != '\0') {
    
    171
    -	/*
    
    172
    -	 * argv[0] is a relative path.  Get the current directory and
    
    173
    -	 * append argv[0], after stripping off the executable name.
    
    174
    -	 */
    
    175
    -	cwd = malloc(FILENAME_MAX + strlen(argv0_dir) + 100);
    
    176
    -	getcwd_or_die(cwd, FILENAME_MAX);
    
    177
    -	strcat(cwd, "/");
    
    178
    -	if (*argv0_dir != '\0') {
    
    179
    -	    strcat(cwd, argv0_dir);
    
    180
    -	    strcat(cwd, "/");
    
    181
    -	}
    
    182
    -	if (debug_lisp_search) {
    
    183
    -	    fprintf(stderr, "relative path, argv[0] = %s\n", cwd);
    
    184
    -	}
    
    185
    -    } else {
    
    186
    -	/*
    
    187
    -	 * argv[0] is someplace on the user's PATH
    
    188
    -	 *
    
    189
    -	 */
    
    190
    -	char *path = getenv("PATH");
    
    191
    -	char *p1, *p2 = NULL;
    
    192
    -	struct stat buf;
    
    193
    -
    
    194
    -	if (debug_lisp_search) {
    
    195
    -	    fprintf(stderr, "User's PATH = %s\n", path ? path : "<NULL>");
    
    196
    -	}
    
    197
    -
    
    198
    -	cwd = malloc(FILENAME_MAX + strlen(argv0arg) + 100);
    
    199
    -	cwd[0] = '\0';
    
    200
    -
    
    201
    -	if (path) {
    
    202
    -            const char *ptr = (p0 != NULL) ? p0 : argv0arg;
    
    203
    -
    
    204
    -	    for (p1 = path; *p1 != '\0'; p1 = p2) {
    
    205
    -		p2 = strchr(p1, ':');
    
    206
    -		if (p2 == NULL)
    
    207
    -		    p2 = p1 + strlen(p1);
    
    208
    -		strncpy(cwd, p1, p2 - p1);
    
    209
    -		cwd[p2 - p1] = '/';
    
    210
    -		cwd[p2 - p1 + 1] = '\0';
    
    211
    -		strcpy(cwd + (p2 - p1 + 1), ptr);
    
    212
    -
    
    213
    -		if (debug_lisp_search) {
    
    214
    -		    fprintf(stderr, "User's PATH, trying %s\n", cwd);
    
    215
    -		}
    
    216
    -
    
    217
    -		if (stat(cwd, &buf) == 0) {
    
    218
    -
    
    219
    -		    if (debug_lisp_search) {
    
    220
    -			fprintf(stderr, "User's PATH, found %s\n", cwd);
    
    221
    -		    }
    
    222
    -		    if (access(cwd, X_OK) == 0) {
    
    223
    -			break;
    
    224
    -		    } else {
    
    225
    -			if (debug_lisp_search) {
    
    226
    -			    fprintf(stderr,
    
    227
    -				    " But not executable.  Continuing...\n");
    
    228
    -			}
    
    229
    -		    }
    
    170
    +    /*
    
    171
    +     * Create the appropriate value for CMUCLLIB by adding the
    
    172
    +     * executable path (if needed) to each entry in
    
    173
    +     * cmucllib_search_list.
    
    174
    +     */
    
    230 175
     
    
    231
    -		}
    
    176
    +    /* First figure out how much space we need */
    
    232 177
     
    
    233
    -		if (*p2 == ':') {
    
    234
    -		    p2++;
    
    235
    -		}
    
    178
    +    path_len = 0;
    
    179
    +    cwd_len = strlen(cwd);
    
    236 180
     
    
    237
    -	    }
    
    238
    -	    if ((p1 == p2) || (p2 == NULL)) {
    
    239
    -		cwd[0] = '\0';
    
    240
    -	    } else {
    
    241
    -		cwd[p2 - p1 + 1] = '\0';
    
    242
    -	    }
    
    243
    -	    if (debug_lisp_search) {
    
    244
    -		fprintf(stderr, "User's PATH, Final cwd %s\n", cwd);
    
    245
    -	    }
    
    181
    +    ptr = cmucllib_search_list;
    
    246 182
     
    
    247
    -	}
    
    183
    +    while (*ptr != NULL) {
    
    184
    +	/*
    
    185
    +	 * Plus 2 for the ":" and "/" we need to add and the cwd that
    
    186
    +	 * might be added.
    
    187
    +	 */
    
    188
    +	path_len += strlen(*ptr) + cwd_len + 2;
    
    189
    +	++ptr;
    
    248 190
         }
    
    249 191
     
    
    250
    -    /* Create the appropriate value for CMUCLLIB */
    
    251
    -
    
    252
    -    {
    
    253
    -	char **ptr;
    
    254
    -	int total_len;
    
    255
    -	int cwd_len;
    
    256
    -
    
    257
    -	/* First figure out how much space we need */
    
    192
    +    /* Create the colon separated list of directories */
    
    258 193
     
    
    259
    -	total_len = 0;
    
    260
    -	cwd_len = strlen(cwd);
    
    261
    -
    
    262
    -	ptr = cmucllib_search_list;
    
    194
    +    path = malloc(path_len + 1);
    
    195
    +    if (!path) {
    
    196
    +	perror("Failed to malloc space for cmucllib");
    
    197
    +	exit(1);
    
    198
    +    }
    
    199
    +    
    
    200
    +    *path = '\0';
    
    263 201
     
    
    264
    -	while (*ptr != NULL) {
    
    265
    -	    /* Plus 2 for the ":" and "/" we need to add */
    
    266
    -	    total_len += strlen(*ptr) + cwd_len + 2;
    
    267
    -	    ++ptr;
    
    202
    +    ptr = cmucllib_search_list;
    
    203
    +    while (*ptr != NULL) {
    
    204
    +	/*
    
    205
    +	 * If it's relative, add the full executable path first to
    
    206
    +	 * make the path absolute.
    
    207
    +	 */
    
    208
    +	if (*ptr[0] != '/') {
    
    209
    +	    strcat(path, cwd);
    
    268 210
     	}
    
    269 211
     
    
    270
    -	/* Create the colon separated list of directories */
    
    271
    -
    
    272
    -	defpath = malloc(total_len + 1);
    
    273
    -	*defpath = '\0';
    
    274
    -
    
    275
    -	ptr = cmucllib_search_list;
    
    276
    -	while (*ptr != NULL) {
    
    277
    -	    if (*ptr[0] != '/') {
    
    278
    -		strcat(defpath, cwd);
    
    279
    -	    }
    
    280
    -
    
    281
    -	    strcat(defpath, *ptr);
    
    282
    -
    
    283
    -	    if (ptr[1] != NULL) {
    
    284
    -		strcat(defpath, ":");
    
    285
    -	    }
    
    212
    +	strcat(path, *ptr);
    
    286 213
     
    
    287
    -	    ++ptr;
    
    214
    +	/* Add a colon if we're not at the last entry of the search list */
    
    215
    +	if (ptr[1] != NULL) {
    
    216
    +	    strcat(path, ":");
    
    288 217
     	}
    
    289 218
     
    
    290
    -	if (strlen(defpath) > total_len) {
    
    291
    -	    abort();
    
    292
    -	}
    
    219
    +	++ptr;
    
    293 220
         }
    
    294 221
     
    
    295
    -    free(argv0_dir);
    
    296
    -    free(cwd);
    
    222
    +    if (strlen(path) > path_len) {
    
    223
    +	abort();
    
    224
    +    }
    
    297 225
     
    
    298
    -    return (const char *) defpath;
    
    226
    +    free(cwd);
    
    227
    +    return path;
    
    299 228
     }
    
    300 229
     
    
    301 230
     /*