char *expandtilde(char *str) { static char retval[MAXINPUTLINE]; char tempstr[MAXINPUTLINE]; char *homedir; char *tempptr; int counter; if (str[0] != '~') return str; /* No tilde -- no expansion. */ strcpy(tempstr, (str + 1)); /* Make a temporary copy of the string */ if ((tempstr[0] == '/') || (tempstr[0] == 0)) tempptr = (char *)NULL; else { /* Only parse up to a slash */ /* strtok() cannot be used here because it is being used in the function that calls expandtilde(). Therefore, use a simple substitute. */ if (strstr(tempstr, "/")) *(strstr(tempstr, "/")) = 0; tempptr = tempstr; } if ((!tempptr) || !tempptr[0]) { /* Get user's own homedir */ homedir = gethomedir(); } else { /* Get specified user's homedir */ homedir = getuserhomedir(tempptr); } /* Now generate the output string in retval. */ strcpy(retval, homedir); /* Put the homedir in there */ /* Now take care of adding in the rest of the parameter */ counter = 1; while ((str[counter]) && (str[counter] != '/')) counter++; strcat(retval, (str + counter)); return retval; }
char* wexpandpath(char *path) { char *origpath = path; char buffer2[PATH_MAX+2]; char buffer[PATH_MAX+2]; int i; memset(buffer, 0, PATH_MAX+2); if (*path=='~') { char *home; path++; if (*path=='/' || *path==0) { home = wgethomedir(); if (strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } else { int j; j = 0; while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *path; buffer2[j] = 0; path++; } home = getuserhomedir(buffer2); if (!home || strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } } i = strlen(buffer); while (*path!=0 && i <= PATH_MAX) { char *tmp; if (*path=='$') { int j = 0; path++; /* expand $(HOME) or $HOME style environment variables */ if (*path=='(') { path++; while (*path!=0 && *path!=')') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } if (*path==')') { path++; tmp = getenv(buffer2); } else { tmp = NULL; } if (!tmp) { if ((i += strlen(buffer2)+2) > PATH_MAX) goto error; buffer[i] = 0; strcat(buffer, "$("); strcat(buffer, buffer2); if (*(path-1)==')') { if (++i > PATH_MAX) goto error; strcat(buffer, ")"); } } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } else { while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } tmp = getenv(buffer2); if (!tmp) { if ((i += strlen(buffer2)+1) > PATH_MAX) goto error; strcat(buffer, "$"); strcat(buffer, buffer2); } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } } else { buffer[i++] = *path; path++; } } if (*path!=0) goto error; return wstrdup(buffer); error: errno = ENAMETOOLONG; wsyserror(_("could not expand %s"), origpath); /* FIXME: too many functions handle a return value of NULL incorrectly */ exit(1); }
char *wexpandpath(const char *path) { const char *origpath = path; char buffer2[PATH_MAX + 2]; char buffer[PATH_MAX + 2]; int i; memset(buffer, 0, PATH_MAX + 2); if (*path == '~') { const char *home; path++; if (*path == '/' || *path == 0) { home = wgethomedir(); if (strlen(home) > PATH_MAX || wstrlcpy(buffer, home, sizeof(buffer)) >= sizeof(buffer)) goto error; } else { int j; j = 0; while (*path != 0 && *path != '/') { if (j > PATH_MAX) goto error; buffer2[j++] = *path; buffer2[j] = 0; path++; } home = getuserhomedir(buffer2); if (!home || wstrlcat(buffer, home, sizeof(buffer)) >= sizeof(buffer)) goto error; } } i = strlen(buffer); while (*path != 0 && i <= PATH_MAX) { char *tmp; if (*path == '$') { int j; path++; /* expand $(HOME) or $HOME style environment variables */ if (*path == '(') { path++; j = 0; while (*path != 0 && *path != ')') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); } buffer2[j] = 0; if (*path == ')') { path++; tmp = getenv(buffer2); } else { tmp = NULL; } if (!tmp) { if ((i += strlen(buffer2) + 2) > PATH_MAX) goto error; buffer[i] = 0; if (wstrlcat(buffer, "$(", sizeof(buffer)) >= sizeof(buffer) || wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer)) goto error; if (*(path-1)==')') { if (++i > PATH_MAX || wstrlcat(buffer, ")", sizeof(buffer)) >= sizeof(buffer)) goto error; } } else { if ((i += strlen(tmp)) > PATH_MAX || wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer)) goto error; } } else { j = 0; while (*path != 0 && *path != '/') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); } buffer2[j] = 0; tmp = getenv(buffer2); if (!tmp) { if ((i += strlen(buffer2) + 1) > PATH_MAX || wstrlcat(buffer, "$", sizeof(buffer)) >= sizeof(buffer) || wstrlcat(buffer, buffer2, sizeof(buffer)) >= sizeof(buffer)) goto error; } else { if ((i += strlen(tmp)) > PATH_MAX || wstrlcat(buffer, tmp, sizeof(buffer)) >= sizeof(buffer)) goto error; } } } else { buffer[i++] = *path; path++; } } if (*path!=0) goto error; return wstrdup(buffer); error: errno = ENAMETOOLONG; werror(_("could not expand %s"), origpath); return NULL; }