const char *prepend_dir_to_path(const char *dir) { const char *saved_path; char *new_path; resources_get_string("Directory", &saved_path); saved_path = lib_stralloc(saved_path); if (dir && *dir) { new_path = util_concat(dir, ARCHDEP_FINDPATH_SEPARATOR_STRING, saved_path, NULL); } else { char *current_dir = ioutil_current_dir(); new_path = util_concat(current_dir, ARCHDEP_FINDPATH_SEPARATOR_STRING, saved_path, NULL); lib_free(current_dir); } resources_set_string("Directory", new_path); lib_free(new_path); return saved_path; }
char *archdep_get_current_drive(void) { char *p = ioutil_current_dir(); char *p2 = strchr(p, '\\'); p2[0] = '/'; p2[1] = '\0'; return p; }
static int set_system_path(const char *val, void *param) { char *tmp_path, *tmp_path_save, *p, *s, *current_dir; util_string_set(&system_path, val); lib_free(expanded_system_path); expanded_system_path = NULL; /* will subsequently be replaced */ tmp_path_save = util_subst(system_path, "$$", default_path); /* malloc'd */ current_dir = ioutil_current_dir(); tmp_path = tmp_path_save; /* tmp_path points into tmp_path_save */ do { p = strstr(tmp_path, ARCHDEP_FINDPATH_SEPARATOR_STRING); if (p != NULL) { *p = 0; } if (!archdep_path_is_relative(tmp_path)) { /* absolute path */ if (expanded_system_path == NULL) { s = util_concat(tmp_path, NULL); /* concat allocs a new str. */ } else { s = util_concat(expanded_system_path, ARCHDEP_FINDPATH_SEPARATOR_STRING, tmp_path, NULL ); } } else { /* relative path */ if (expanded_system_path == NULL) { s = util_concat(current_dir, FSDEV_DIR_SEP_STR, tmp_path, NULL ); } else { s = util_concat(expanded_system_path, ARCHDEP_FINDPATH_SEPARATOR_STRING, current_dir, FSDEV_DIR_SEP_STR, tmp_path, NULL ); } } lib_free(expanded_system_path); expanded_system_path = s; tmp_path = p + strlen(ARCHDEP_FINDPATH_SEPARATOR_STRING); } while (p != NULL); lib_free(current_dir); lib_free(tmp_path_save); DBG(("set_system_path -> expanded_system_path:'%s'\n", expanded_system_path)); return 0; }
/* return malloc'd version of full pathname of orig_name */ int archdep_expand_path(char **return_path, const char *orig_name) { /* Unix version. */ if (*orig_name == '/') { *return_path = lib_stralloc(orig_name); } else { static char *cwd; cwd = ioutil_current_dir(); *return_path = util_concat(cwd, "/", orig_name, NULL); lib_free(cwd); } return 0; }
static struct file_list *file_list_read_nolfn(const char *path, const char *pattern) { char *cwd = ioutil_current_dir(); struct file_list *fl = NULL; struct find_t f; if (cwd == NULL) { return NULL; } if (ioutil_chdir(path) < 0) { goto end; } if (_dos_findfirst("*.*", (_A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH), &f)) { goto end; } fl = file_list_create(); /* (We skip `.' here.) */ while (!_dos_findnext(&f)) { strlwr(f.name); if (pattern == NULL || (f.attrib & _A_SUBDIR)) { file_list_add_item(fl, f.name, (f.attrib & _A_SUBDIR) ? FT_DIR : FT_NORMAL); continue; } { char *p = lib_stralloc(pattern); char *element; element = strtok(p, ";"); do { if (fnmatch(element, f.name, FNM_NOCASE) == 0) { file_list_add_item(fl, f.name, (f.attrib & _A_SUBDIR) ? FT_DIR : FT_NORMAL); } element = strtok(NULL, ";"); } while (element != NULL); lib_free(p); } } file_list_sort(fl); end: ioutil_chdir(cwd); return fl; }
static int fsdevice_flush_cd(vdrive_t* vdrive, char *arg) { int er; er = CBMDOS_IPE_OK; if (ioutil_chdir(fsdevice_get_path(vdrive->unit)) || ioutil_chdir(arg)) { er = CBMDOS_IPE_NOT_FOUND; if (ioutil_errno(IOUTIL_ERRNO_EPERM)) { er = CBMDOS_IPE_PERMISSION; } } else { /* get full path and save */ arg = ioutil_current_dir(); fsdevice_set_directory(arg, vdrive->unit); lib_free(arg); } return er; }
const char *archdep_boot_path(void) { //HANDLE snap; //MODULEENTRY32 ment; //HANDLE hproc; //int cbneed; //int i; //_CreateToolhelp32Snapshot func_CreateToolhelp32Snapshot = NULL; //_Module32First func_Module32First = NULL; //_Module32Next func_Module32Next = NULL; //_EnumProcessModules func_EnumProcessModules = NULL; //_GetModuleFileNameEx func_GetModuleFileNameEx = NULL; //char *possible_trojan_path; //possible_trojan_path = NULL; //if (boot_path == NULL) { // hkernel = LoadLibrary(TEXT("kernel32.dll")); // if (hkernel) { // OutputDebugString(TEXT("DLL: kernel32.dll loaded\n")); // OutputDebugString(TEXT("DLL: getting address for CreateToolhelp32Snapshot")); // func_CreateToolhelp32Snapshot = (_CreateToolhelp32Snapshot)GetProcAddress(hkernel, TEXT("CreateToolhelp32Snapshot")); // if (func_CreateToolhelp32Snapshot) { // OutputDebugString(TEXT("CreateToolhelp32Snaphshot success\n")); // } // else { // OutputDebugString(TEXT("CreateToolhelp32Snaphshot fail\n")); // } // OutputDebugString(TEXT("DLL: getting address for Module32First")); // func_Module32First = (_Module32First)GetProcAddress(hkernel, TEXT("Module32First")); // if (func_Module32First) { // OutputDebugString(TEXT("Module32First success\n")); // } // else { // OutputDebugString(TEXT("Module32First fail\n")); // } // OutputDebugString(TEXT("DLL: getting address for Module32Next")); // func_Module32Next = (_Module32Next)GetProcAddress(hkernel, TEXT("Module32Next")); // if (func_Module32Next) { // OutputDebugString(TEXT("Module32Next success\n")); // } // else { // OutputDebugString(TEXT("Module32Next fail\n")); // } // } // hpsapi = LoadLibrary(TEXT("psapi.dll")); // if (hpsapi) { // OutputDebugString(TEXT("DLL: psapi.dll loaded")); // OutputDebugString(TEXT("DLL: getting address for EnumProcessModules")); // func_EnumProcessModules = (_EnumProcessModules)GetProcAddress(hpsapi, TEXT("EnumProcessModules")); // if (func_EnumProcessModules) { // OutputDebugString(TEXT("EnumProcessModules success\n")); // } // else { // OutputDebugString(TEXT("EnumProcessModules fail\n")); // } // OutputDebugString(TEXT("DLL: getting address for GetModuleFileNameEx")); // func_GetModuleFileNameEx = (_GetModuleFileNameEx)GetProcAddress(hpsapi, TEXT("GetModuleFileNameExA")); // if (func_GetModuleFileNameEx) { // OutputDebugString(TEXT("GetModuleFileNameEx success\n")); // } // else { // OutputDebugString(TEXT("GetModuleFileNameEx fail\n")); // } // } // if (func_EnumProcessModules) { // OutputDebugString(TEXT("BOOT path NT method\n")); // hproc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId()); // cbneed = 0; // func_EnumProcessModules(hproc, NULL, 0, (LPDWORD)&cbneed); // if (cbneed) { // HMODULE *modules = lib_malloc(cbneed); // int n_modules = cbneed / sizeof(HMODULE); // if (func_EnumProcessModules(hproc, modules, cbneed, (LPDWORD)&cbneed)) { // for (i = 0; i < n_modules; i++) { // TCHAR st_temp[MAX_PATH]; // char temp[MAX_PATH]; // if (func_GetModuleFileNameEx(hproc, modules[i], st_temp, MAX_PATH)) { // system_wcstombs(temp, st_temp, MAX_PATH); // OutputDebugString(st_temp); // if (verify_exe(temp)) { // util_fname_split(temp, &boot_path, NULL); // break; // } else if (i == 0) { // possible_trojan_path = lib_stralloc(temp); // } // } // } // } // lib_free(modules); // } // CloseHandle(hproc); // } else if (func_CreateToolhelp32Snapshot && func_Module32First && func_Module32Next) { // OutputDebugString(TEXT("BOOT path Win9x method\n")); // snap = func_CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId()); // memset(&ment, 0, sizeof(MODULEENTRY32)); // ment.dwSize = sizeof(MODULEENTRY32); // func_Module32First(snap, &ment); // OutputDebugString(ment.szExePath); // if (verify_exe(ment.szExePath)) { // util_fname_split(ment.szExePath, &boot_path, NULL); // } else { // possible_trojan_path = lib_stralloc(ment.szExePath); // while (func_Module32Next(snap, &ment)) { // OutputDebugString(ment.szExePath); // if (verify_exe(ment.szExePath)) { // util_fname_split(ment.szExePath, &boot_path, NULL); // break; // } // } // } // CloseHandle(snap); // } else { // TCHAR st_temp[MAX_PATH]; // char temp[MAX_PATH]; // OutputDebugString(TEXT("BOOT path NT4 without PSAPI\n")); // if (GetModuleFileName(NULL, st_temp, MAX_PATH)) { // system_wcstombs(temp, st_temp, MAX_PATH); // if (!verify_exe(temp)) { // possible_trojan_path = lib_stralloc(temp); // } // util_fname_split(temp, &boot_path, NULL); // } else { // OutputDebugString(TEXT("Module file name could not be obtained\n")); // } // } // OutputDebugString(TEXT("boot path:")); // OutputDebugString(boot_path); // OutputDebugString(TEXT("\n")); /* This should not happen, but you never know... */ if (boot_path == NULL) { boot_path = ioutil_current_dir (); //lib_stralloc("."); } //} /*if (possible_trojan_path) { archdep_startup_log_error("Vice detected a possible trojan running in its process space\n\n%s\n\nPlease run your favourite Antivirus software!", possible_trojan_path); lib_free(possible_trojan_path); }*/ return boot_path; }
/* FIXME: documentation. */ char *tui_file_selector(const char *title, const char *directory, const char *pattern, const char *default_item, read_contents_func_type contents_func, char **browse_file_return, unsigned int *browse_file_number_return) { static char *return_path = NULL; struct file_list *fl; int curr_item, first_item, need_update; int x, y, width, height, num_cols, num_lines, field_width; int num_files; char str[0x100]; int str_len = 0; tui_area_t backing_store = NULL; if (contents_func != NULL) { *browse_file_return = NULL; } if (browse_file_number_return != NULL) { *browse_file_number_return = 0; } if (directory != NULL) { return_path = lib_stralloc(directory); } else { return_path = ioutil_current_dir(); } slashize_path(&return_path); fl = file_list_read(return_path, pattern); if (fl == NULL) { return NULL; } first_item = curr_item = 0; num_cols = 4; field_width = 18; num_lines = 17; height = num_lines + 2; width = field_width * num_cols + 4; num_files = num_cols * num_lines; if (default_item != NULL && *default_item) { int i; for (i = 0; i < fl->num_items; i++) { if (!strcasecmp(default_item, fl->items[i].name)) { curr_item = i; while (curr_item - first_item >= num_files) { first_item += num_lines; } break; } } } x = CENTER_X(width); y = CENTER_Y(height); need_update = 1; tui_area_get(&backing_store, x, y, width + 2, height + 1); tui_display_window(x, y, width, height, MENU_BORDER, MENU_BACK, title, NULL); while (1) { int key; tui_set_attr(MENU_FORE, MENU_BACK, 0); if (need_update) { file_selector_display_path(return_path, x + 1, y + height - 1, width - 2); file_selector_update(fl, first_item, x + 2, y + 1, field_width, num_lines, num_cols); tui_set_attr(FIRST_LINE_FORE, FIRST_LINE_BACK, 0); tui_display(0, tui_num_lines() - 1, tui_num_cols(), "\030\031\033\032: Move <Enter>: Select %s<Alt>-<letter>: Change drive", contents_func != NULL ? "<Space>: Preview " : ""); need_update = 0; } tui_set_attr(MENU_FORE, MENU_HIGHLIGHT, 0); file_selector_display_item(fl, curr_item, first_item, x + 2, y + 1, field_width, num_lines, num_cols); key = getkey(); tui_set_attr(MENU_FORE, MENU_BACK, 0); file_selector_display_item(fl, curr_item, first_item, x + 2, y + 1, field_width, num_lines, num_cols); switch (key) { case K_Escape: tui_area_put(backing_store, x, y); tui_area_free(backing_store); return NULL; case K_Left: str_len = 0; if (curr_item - num_lines >= 0) { curr_item -= num_lines; if (curr_item < first_item) { if (first_item >= num_lines) { first_item -= num_lines; need_update = 1; } else { curr_item += num_lines; } } } break; case K_Up: str_len = 0; if (curr_item > 0) { curr_item--; if (curr_item < first_item) { first_item = curr_item; need_update = 1; } } break; case K_Right: str_len = 0; if (curr_item + num_lines < fl->num_used_items) { curr_item += num_lines; if (curr_item - first_item >= num_files) { first_item += num_lines; need_update = 1; } } break; case K_Down: str_len = 0; if (curr_item < fl->num_used_items - 1) { curr_item++; if (curr_item == first_item + num_files) { first_item++; need_update = 1; } } break; case K_PageDown: str_len = 0; if (curr_item + num_files < fl->num_used_items) { curr_item += num_files; first_item += num_files; } need_update = 1; break; case K_PageUp: str_len = 0; if (curr_item - num_files >= 0) { curr_item -= num_files; first_item -= num_files; if (first_item < 0) { first_item = 0; } need_update = 1; } break; case K_Home: str_len = 0; curr_item = 0; if (first_item != 0) { first_item = 0; need_update = 1; } break; case K_End: str_len = 0; curr_item = fl->num_used_items - 1; first_item = curr_item - num_files + 1; if (first_item < 0) { first_item = 0; } need_update = 1; break; case K_Return: str_len = 0; if (fl->items[curr_item].type == FT_DIR) { struct file_list *new_fl; char *new_path; new_path = change_path(fl, return_path, curr_item); new_fl = file_list_read(new_path, pattern); if (new_fl != NULL) { file_list_free(fl); fl = new_fl; first_item = curr_item = 0; lib_free(return_path); return_path = new_path; need_update = 1; ioutil_chdir(return_path); } else { lib_free(new_path); } } else { char *p = util_concat(return_path, fl->items[curr_item].name, NULL); lib_free(return_path); return_path = p; tui_area_put(backing_store, x, y); tui_area_free(backing_store); return return_path; } break; case K_BackSpace: if (str_len > 1) { int n; str_len--; n = file_list_find(fl, str, str_len); if (n >= 0) { curr_item = n; if (curr_item < first_item) { first_item = curr_item; need_update = 1; } else if (first_item + num_files <= curr_item) { first_item = curr_item - num_files + 1; need_update = 1; } } } else { str_len = 0; curr_item = 0; if (first_item != 0) { first_item = 0; need_update = 1; } } break; case ' ': if (contents_func != NULL && fl->items[curr_item].type != FT_DIR && browse_file_return != NULL) { tui_display(0, tui_num_lines() - 1, tui_num_cols(), ""); *browse_file_return = tui_image_browser(fl->items[curr_item].name, contents_func, browse_file_number_return); if (*browse_file_return != NULL) { char *p = util_concat(return_path, fl->items[curr_item].name, NULL); lib_free(return_path); return_path = p; tui_area_put(backing_store, x, y); tui_area_free(backing_store); return return_path; } need_update = 1; break; } else { tui_beep(); } default: { int drive_num; drive_num = alt_key_to_drive_num(key); if (drive_num > 0) { /* `A-a' ... `A-z' change the current drive. */ int num_available_drives; int current_drive; _dos_getdrive(¤t_drive); _dos_setdrive(current_drive, &num_available_drives); if (drive_num <= num_available_drives) { char *new_path; /* FIXME: This is a hack... Maybe there is a cleaner way to do it, but for now I just don't know. */ _dos_setdrive(drive_num, &num_available_drives); new_path = ioutil_current_dir(); if (new_path != NULL) { slashize_path(&new_path); _dos_setdrive(current_drive, &num_available_drives); if (new_path != NULL) { struct file_list *new_fl; new_fl = file_list_read(new_path, pattern); if (new_fl != NULL) { file_list_free(fl); fl = new_fl; first_item = curr_item = 0; lib_free(return_path); return_path = new_path; need_update = 1; ioutil_chdir(return_path); } else { lib_free(new_path); } } } else { _dos_setdrive(current_drive, &num_available_drives); tui_beep(); } } else { tui_beep(); } } else if (isprint(key) && str_len < 0x100) { int n; str[str_len] = key; n = file_list_find(fl, str, str_len + 1); if (n < 0) { tui_beep(); } else { str_len++; curr_item = n; if (curr_item < first_item) { first_item = curr_item; need_update = 1; } else if (first_item + num_files <= curr_item) { first_item = curr_item - num_files + 1; need_update = 1; } } } } break; } } }