char *sh_compiler_get_global_path(const char *file) { size_t file_length = strlen(file); char *current_path; size_t path_length; size_t total_length; char *global_path; char additional = '\0'; if (_is_absolute(file, file_length)) return strdup(file); current_path = malloc((PATH_MAX + 1) * sizeof *current_path); if (current_path == NULL) return NULL; if (!_get_current_dir(current_path, PATH_MAX)) /* if couldn't retrieve the path, just ignore the path */ current_path[0] = '\0'; path_length = strlen(current_path); total_length = path_length + file_length; if (path_length > 0 && !_is_path_separator(current_path[path_length - 1])) { additional = PATH_SEPARATOR; ++total_length; } global_path = malloc((total_length + 1) * sizeof *global_path); if (global_path == NULL) { /* If couldn't allocate, at least try the local name */ global_path = strdup(file); goto exit_no_mem; } strcpy(global_path, current_path); if (additional != '\0') global_path[path_length++] = additional; strcpy(global_path + path_length, file); exit_no_mem: free(current_path); return global_path; }
static void insert_wd(unsigned char **up, unsigned char *cwd) { unsigned char *url = *up; if (!url || !cwd || !*cwd) return; if (casecmp(url, cast_uchar "file://", 7)) return; if (dir_sep(url[7])) return; #ifdef DOS_FS if (upcase(url[7]) >= 'A' && upcase(url[7]) <= 'Z' && url[8] == ':' && dir_sep(url[9])) return; #endif #ifdef SPAD if (_is_absolute(cast_const_char(url + 7)) != _ABS_NO) return; #endif url = mem_alloc(strlen(cast_const_char *up) + strlen(cast_const_char cwd) + 2); memcpy(url, *up, 7); strcpy(cast_char(url + 7), cast_const_char cwd); if (!dir_sep(cwd[strlen(cast_const_char cwd) - 1])) strcat(cast_char url, "/"); strcat(cast_char url, cast_const_char(*up + 7)); mem_free(*up); *up = url; }