void init_homedir(void) { // In case we are called a second time. xfree(homedir); homedir = NULL; const char *var = os_getenv("HOME"); #ifdef WIN32 // Typically, $HOME is not defined on Windows, unless the user has // specifically defined it for Vim's sake. However, on Windows NT // platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for // each user. Try constructing $HOME from these. if (var == NULL) { const char *homedrive = os_getenv("HOMEDRIVE"); const char *homepath = os_getenv("HOMEPATH"); if (homepath == NULL) { homepath = "\\"; } if (homedrive != NULL && strlen(homedrive) + strlen(homepath) < MAXPATHL) { snprintf(os_buf, MAXPATHL, "%s%s", homedrive, homepath); if (os_buf[0] != NUL) { var = os_buf; vim_setenv("HOME", os_buf); } } } #endif if (var != NULL) { #ifdef UNIX // Change to the directory and get the actual path. This resolves // links. Don't do it when we can't return. if (os_dirname((char_u *)os_buf, MAXPATHL) == OK && os_chdir(os_buf) == 0) { if (!os_chdir(var) && os_dirname(IObuff, IOSIZE) == OK) { var = (char *)IObuff; } if (os_chdir(os_buf) != 0) { EMSG(_(e_prev_dir)); } } #endif homedir = xstrdup(var); } }
int vifm_chdir(const char path[]) { char curr_path[PATH_MAX]; if(getcwd(curr_path, sizeof(curr_path)) == curr_path) { if(stroscmp(curr_path, path) == 0) { return 0; } } return os_chdir(path); }
/// Get the absolute name of the given relative directory. /// /// @param directory Directory name, relative to current directory. /// @return `FAIL` for failure, `OK` for success. int os_full_dir_name(char *directory, char *buffer, int len) { int retval = OK; if (STRLEN(directory) == 0) { return os_dirname((char_u *) buffer, len); } char old_dir[MAXPATHL]; // Get current directory name. if (os_dirname((char_u *) old_dir, MAXPATHL) == FAIL) { return FAIL; } // We have to get back to the current dir at the end, check if that works. if (os_chdir(old_dir) != 0) { return FAIL; } if (os_chdir(directory) != 0) { // Do not return immediatly since we may be in the wrong directory. retval = FAIL; } if (retval == FAIL || os_dirname((char_u *) buffer, len) == FAIL) { // Do not return immediatly since we are in the wrong directory. retval = FAIL; } if (os_chdir(old_dir) != 0) { // That shouldn't happen, since we've tested if it works. retval = FAIL; EMSG(_(e_prev_dir)); } return retval; }
/* RMBIN -- Remove all binaries in a directory or in a directory tree. * We chdir to each directory to minimize path searches. */ static void rmbin ( char *dir, int recurse, char *path /* pathname of current directory */ ) { char newpath[SZ_PATHNAME+1]; char fname[SZ_PATHNAME+1]; int dp, ftype; if ((dp = os_diropen (dir)) == ERR) { fprintf (stderr, "cannot open directory `%s'\n", dir); fflush (stderr); return; } sprintf (newpath, "%s%s/", path, dir); /* Descend into the subdirectory. */ if (strcmp (dir, ".") != 0) if (os_chdir (dir) == ERR) { os_dirclose (dp); fprintf (stderr, "cannot change directory to `%s'\n", newpath); fflush (stderr); return; } /* Scan through the directory. */ while (os_gfdir (dp, fname, SZ_PATHNAME) > 0) { if (os_symlink (fname, 0, 0)) continue; if ((ftype = os_filetype (fname)) == DIRECTORY_FILE) rmbin (fname, recurse, newpath); else { if (only[0] != NULL) { if (exclude_file (fname)) continue; } else if (ftype != BINARY_FILE || exclude_file (fname)) continue; /* We have a binary file which is not excluded from deletion * by its extension, so delete it. */ if (interactive && (verify_delete (fname, newpath) == NO)) continue; if (verbose) { printf ("%s%s\n", newpath, fname); fflush (stdout); } if (execute) if (os_delete (fname) == ERR) { fprintf (stderr, "cannot delete `%s'\n", fname); fflush (stderr); } } } /* Return from the subdirectory. */ if (strcmp (dir, ".") != 0) if (os_chdir ("..") == ERR) { fprintf (stderr, "cannot return from subdirectory `%s'\n", newpath); fflush (stderr); } os_dirclose (dp); }