int main(int ac, const char **av) { char mbpwd[MAXPATHLEN]; wchar_t pwd[MAXPATHLEN]; size_t len; char *t; int found_repo = 0; if (ac != 1) { printf("usage: prwd\n"); exit(-1); } setlocale(LC_ALL, ""); /* Populate $HOME */ t = getenv("HOME"); mbstowcs(home, t, MAXPATHLEN); if (home == NULL || *home == '\0') errx(0, "Unknown variable '$HOME'."); /* Get the working directory */ t = getcwd(NULL, MAXPATHLEN); if (t == NULL) errx(0, "Unable to get current working directory."); mbstowcs(pwd, t, MAXPATHLEN); free(t); read_config(); /* Replace the beginning with ~ for directories within $HOME. */ add_alias(L"~", home, 0); /* Alias handling */ replace_aliases(pwd); /* Newsgroup mode, keep only the first letters. */ if (cfg_newsgroup == 1) newsgroupize(pwd); /* If the path is still too long, crop it. */ len = wcslen(pwd); if (cfg_maxpwdlen > 0 && len > cfg_maxpwdlen) { if (cfg_cleancut == 1 && cfg_newsgroup != 1) { cleancut(pwd); } else { quickcut(pwd, len); } } /* If mercurial is enabled, show the branch */ if (cfg_mercurial == 1) { found_repo = add_branch(pwd, VCS_MERCURIAL); } if (found_repo == 0 && cfg_git == 1) { add_branch(pwd, VCS_GIT); } wcstombs(mbpwd, pwd, MAXPATHLEN); puts(mbpwd); return 0; }
/* * Main prwd functionality, prints a reduced working directory. */ void prwd(void) { size_t len; int found_repo = 0; char *wd = NULL; char *wd_env = NULL; char mbs_wd[MAX_OUTPUT_LEN]; wchar_t wcs_wd[MAX_OUTPUT_LEN]; struct stat sa, sb; wd = getcwd(NULL, MAXPATHLEN); if (wd == NULL) errx(100, "unable to get current working directory"); if (stat(wd, &sa) == -1) err(100, "stat(wd_real)"); /* * If we can get a valid PWD from the environment, that turns out to be * the same directory, then we should use it, it provides more context * if the shell is located in a symlink. */ wd_env = getenv("PWD"); if (wd_env != NULL && stat(wd_env, &sb) == 0) { if (sa.st_ino == sb.st_ino && sa.st_dev == sb.st_dev) { free(wd); wd = wd_env; } } mbstowcs(wcs_wd, wd, MAX_OUTPUT_LEN); /* Replace the beginning with ~ for directories within $HOME. */ add_alias(L"~", home, 0); /* Alias handling */ replace_aliases(wcs_wd); /* Newsgroup mode, keep only the first letters. */ if (cfg_newsgroup == 1) newsgroupize(wcs_wd); /* If the path is still too long, crop it. */ len = wcslen(wcs_wd); if (cfg_maxpwdlen > 0 && len > cfg_maxpwdlen) { if (cfg_cleancut == 1 && cfg_newsgroup != 1) { cleancut(wcs_wd); } else { quickcut(wcs_wd, len); } } /* If mercurial or git is enabled, show the branch */ if (cfg_mercurial == 1) { found_repo = add_branch(wcs_wd, VCS_MERCURIAL); } if (found_repo == 0 && cfg_git == 1) { add_branch(wcs_wd, VCS_GIT); } /* Do we show the hostname? */ if (cfg_hostname == 1) { add_hostname(wcs_wd); } /* Add the '$' or '#' character depending if your root. */ if (cfg_uid_indicator == 1) { add_uid_indicator(wcs_wd); } wcstombs(mbs_wd, wcs_wd, MAX_OUTPUT_LEN); puts(mbs_wd); }