static bool InFlashNamed(const TCHAR *path, const TCHAR *name) { size_t name_length = _tcslen(name); return is_dir_separator(path[0]) && memcmp(path + 1, name, name_length * sizeof(name[0])) == 0 && is_dir_separator(path[1 + name_length]); }
/** * Check whether path is an absolute path. */ bool is_absolute_path(const char *path) { g_assert(path != NULL); if (is_dir_separator(path[0])) return TRUE; /* On Windows also check for something like C:\ and x:/ */ return is_running_on_mingw() && is_ascii_alpha(path[0]) && ':' == path[1] && is_dir_separator(path[2]); }
static const char * filepath_directory_end(const char *pathname, char separator) { const char *p; p = strrchr(pathname, separator); if (p) { while (p != pathname && is_dir_separator(p[-1])) { p--; } } return p; }
void ContractLocalPath(TCHAR* filein) { TCHAR output[MAX_PATH]; // Get the relative file name and location (ptr) const TCHAR *ptr = string_after_prefix(filein, data_path); if (ptr == NULL || !is_dir_separator(*ptr)) return; // Replace the full local path by the code "%LOCAL_PATH%\\" (output) _stprintf(output, _T("%s%s"), local_path_code, ptr + 1); // ... and copy it to the buffer (filein) _tcscpy(filein, output); }
/* drops the last component of the path, leaving only the directory */ char * directory_path (char *s) { char *t, *tail = NULL; char path[MAXPATHLEN]; for (t = s; *t; t++) { if (is_dir_separator(*t)) tail = t; } if (tail == NULL) { return NULL; } else { return substring_copy (s, 0, tail-s); } }
/* drops path prefix in string */ extern string drop_path (string s) { string t, tail = NULL; for (t = s; *t; t++) { if (is_dir_separator (*t)) tail = t; } if (tail == NULL) { return s; /* no path prefix */ } else { tail++; /* skip the slash */ return tail; /* points inside s, not new string! */ } }
// Get program path from PATH variable. static char * get_executable_dir_from_path(char *name) { if (is_dir_separator (name[0])) return name; #if defined(_WIN32) else if (name[1] == ':' && is_dir_separator(name[2])) return name; #endif else { /* relative path */ char *path = getenv("PATH"); if (path != NULL) { char *p = string_copy(path); char *tmp; char *dir; #if defined(_WIN32) #if defined(__MINGW32__) /* mingw uses semicolon as separator */ while ((dir = strtok(p, ";")) != NULL) { #else /* strtok_r doesn't exist */ while ((dir = strtok(p, ":")) != NULL) { #endif /* __MINGW32__ */ #else while ((dir = strtok_r(p, ":", &tmp)) != NULL) { #endif /* _WIN32 */ if (is_directory(dir)) { char filename[MAXPATHLEN]; snprintf(filename, MAXPATHLEN, "%s%s%s", dir, DIR_SEPARATOR_STR, name); if (is_executable(filename)) { return string_copy(filename); } #if defined(_WIN32) else { /* if not found, then try again with or without .exe suffix, * depending on whether the original filename had one; * e.g. if invoked opencc when opencc.exe is full name. */ char *suffix = get_suffix(filename); if (suffix != NULL && strcmp(suffix, "exe") == 0) { /* has suffix so try without suffix */ *(suffix-1) = NIL; } else { /* add exe suffix */ strcat(filename, ".exe"); } if (is_executable(filename)) { return string_copy(filename); } } #endif } p = NULL; } } } return name; } char * get_executable_dir (void) { char path[MAXPATHLEN]; int rval = 0; int i; #if defined(linux) /* Look in this special place for a link to the executable. This only works on Linux, but it is benign if we try it elsewhere. */ rval = readlink ("/proc/self/exe", path, sizeof(path)); #endif if (rval <= 0) { // If can't read /proc/self/exe, get program path from PATH // variable. char *p = get_executable_dir_from_path(saved_orig_program_name); strncpy(path, p, sizeof(path)); rval = strlen(path); } else { path[rval] = '\0'; // readlink doesn't append NULL } if (rval > 0) { for (i=rval-1; i >= 0; i--) { if (is_dir_separator(path[i])) break; } if (i > 0) { /* Overwrite the trailing slash, giving the directory portion of the path. */ path[i] = '\0'; } else if (i == 0) { /* Directory is the root */ strcpy (path, DIR_SEPARATOR_STR); } if (is_directory (path)) { /* Verify that it is a directory */ return string_copy (path); } } /* TBD: try to extract the name from argv0 */ /* Can't get anything reasonable. */ #ifdef TARG_NVISA internal_error("unable to find $PATH for nvopencc"); #endif return NULL; } void dump_file_to_stdout(char *filename) { char buf[buf_size]; FILE *f; int n; if (filename == NULL || !file_exists(filename)) internal_error("file does not exist"); f = fopen(filename, "r"); if (f == NULL) internal_error("cannot open file for read"); // Copy the content of file to stdout. while ((n = fread(buf, 1, buf_size, f)) > 0) { if (-1 == write(1, buf, n)) { internal_error("cannot write to file"); break; } } fclose(f); }