Raymond Toy pushed to branch issue-437-find-self at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • src/lisp/lisp.c
    ... ... @@ -303,64 +303,89 @@ default_cmucllib(const char *argv0arg)
    303 303
     static const char *
    
    304 304
     default_cmucllib(const char *argv0arg)
    
    305 305
     {
    
    306
    -    extern void findyourself_init(const char *argv0);
    
    307
    -    extern int find_yourself(char *result, size_t size_of_result);
    
    308
    -    
    
    309
    -    int rc;
    
    306
    +    int total_len;
    
    307
    +    int cwd_len;
    
    308
    +    char **ptr;
    
    310 309
         char *defpath;
    
    311 310
         char *cwd;
    
    312
    -    char* newpath = malloc(PATH_MAX);
    
    313
    -    
    
    314
    -    realpath(argv0arg, newpath);
    
    315
    -    printf("argv[0] = %s\n", argv0arg);
    
    316
    -    printf("realpath = %s\n", newpath);
    
    311
    +    char *newpath = malloc(PATH_MAX);
    
    312
    +
    
    313
    +    cwd = realpath(argv0arg, newpath);
    
    314
    +
    
    315
    +    if (debug_lisp_search) {
    
    316
    +	fprintf(stderr, "Realpath of %s = %s\n", argv0arg, newpath);
    
    317
    +    }
    
    318
    +
    
    319
    +    if (!cwd) {
    
    320
    +	perror("Cannot determine realpath of lisp executable");
    
    321
    +	exit(1);
    
    322
    +    }
    
    317 323
     
    
    324
    +    /*
    
    325
    +     * Delete the binary name from the full path, leaving just the
    
    326
    +     * full directory to the executable.
    
    327
    +     */
    
    318 328
         cwd = strrchr(newpath, '/');
    
    319
    -    cwd[1] = '\0';
    
    320
    -    printf("cwd = %s\n", newpath);
    
    321
    -    
    
    329
    +    if (cwd) {
    
    330
    +	cwd[1] = '\0';
    
    331
    +    }
    
    332
    +
    
    333
    +    /* cwd is the path directory of the executable */
    
    322 334
         cwd = newpath;
    
    323
    -    {
    
    324
    -	char **ptr;
    
    325
    -	int total_len;
    
    326
    -	int cwd_len;
    
    327 335
     
    
    328
    -	/* First figure out how much space we need */
    
    336
    +    if (debug_lisp_search) {
    
    337
    +	fprintf(stderr, "Executable path %s\n", cwd);
    
    338
    +    }
    
    329 339
     
    
    330
    -	total_len = 0;
    
    331
    -	cwd_len = strlen(newpath);
    
    340
    +    /*
    
    341
    +     * Create the appropriate value for CMUCLLIB by adding the
    
    342
    +     * executable path (if needed) to each entry in
    
    343
    +     * cmucllib_search_list.
    
    344
    +     */
    
    332 345
     
    
    333
    -	ptr = cmucllib_search_list;
    
    346
    +    /* First figure out how much space we need */
    
    334 347
     
    
    335
    -	while (*ptr != NULL) {
    
    336
    -	    /* Plus 2 for the ":" and "/" we need to add */
    
    337
    -	    total_len += strlen(*ptr) + cwd_len + 2;
    
    338
    -	    ++ptr;
    
    339
    -	}
    
    348
    +    total_len = 0;
    
    349
    +    cwd_len = strlen(cwd);
    
    340 350
     
    
    341
    -	/* Create the colon separated list of directories */
    
    351
    +    ptr = cmucllib_search_list;
    
    342 352
     
    
    343
    -	defpath = malloc(total_len + 1);
    
    344
    -	*defpath = '\0';
    
    353
    +    while (*ptr != NULL) {
    
    354
    +	/*
    
    355
    +	 * Plus 2 for the ":" and "/" we need to add and the cwd that
    
    356
    +	 * might be added.
    
    357
    +	 */
    
    358
    +	total_len += strlen(*ptr) + cwd_len + 2;
    
    359
    +	++ptr;
    
    360
    +    }
    
    345 361
     
    
    346
    -	ptr = cmucllib_search_list;
    
    347
    -	while (*ptr != NULL) {
    
    348
    -	    if (*ptr[0] != '/') {
    
    349
    -		strcat(defpath, cwd);
    
    350
    -	    }
    
    362
    +    /* Create the colon separated list of directories */
    
    351 363
     
    
    352
    -	    strcat(defpath, *ptr);
    
    364
    +    defpath = malloc(total_len + 1);
    
    365
    +    *defpath = '\0';
    
    353 366
     
    
    354
    -	    if (ptr[1] != NULL) {
    
    355
    -		strcat(defpath, ":");
    
    356
    -	    }
    
    357
    -
    
    358
    -	    ++ptr;
    
    367
    +    ptr = cmucllib_search_list;
    
    368
    +    while (*ptr != NULL) {
    
    369
    +	/*
    
    370
    +	 * If it's relative, add the full executable path first to
    
    371
    +	 * make the path absolute.
    
    372
    +	 */
    
    373
    +	if (*ptr[0] != '/') {
    
    374
    +	    strcat(defpath, cwd);
    
    359 375
     	}
    
    360 376
     
    
    361
    -	if (strlen(defpath) > total_len) {
    
    362
    -	    abort();
    
    377
    +	strcat(defpath, *ptr);
    
    378
    +
    
    379
    +	/* Add a colon if we're not at the last entry of the search list */
    
    380
    +	if (ptr[1] != NULL) {
    
    381
    +	    strcat(defpath, ":");
    
    363 382
     	}
    
    383
    +
    
    384
    +	++ptr;
    
    385
    +    }
    
    386
    +
    
    387
    +    if (strlen(defpath) > total_len) {
    
    388
    +	abort();
    
    364 389
         }
    
    365 390
     
    
    366 391
         free(newpath);