2 ** path.c -- return a pathname
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
15 ** Compactify an absolute path name by removing unneccessary parts.
16 ** Removes trailing slashes, but not if it would empty the string then.
28 abspath = (*f == '/');
32 /* Skip. Interesting places are only after slashes. */
33 /* We don't care about "./" beginnings */
38 /* Let's see what follows the slash ... */
42 continue; /* ... and thus exit the loop */
45 /* reduce subsequent slashes to one */
46 for (dp = cp; *dp == '/'; dp++) {
51 continue; /* ... at the slash */
54 if (cp[1] == '/' || cp[1] == '\0') {
59 } else if ((strncmp(cp, "../", 3) == 0) ||
60 (strcmp(cp, "..") == 0)) {
62 /* crop out previous path element */
63 for (dp=cp-2; dp>f && *dp!='/'; dp--) {
67 /* path starts with "/.." */
74 /* a normal hidden file */
87 /* We have removed everything, but need something. */
88 strcpy(f, abspath ? "/" : ".");
97 expath(char *name, int type)
102 if (type == TSUBCWF) {
103 /* @folder to +folder */
104 snprintf(buffer, sizeof(buffer), "%s/%s",
105 getfolder(FCUR), name);
106 name = m_mailpath(buffer);
108 snprintf(buffer, sizeof(buffer), "%s/", m_maildir(""));
109 if (isprefix(buffer, name)) {
111 name = getcpy(name + strlen(buffer));
121 if (type == TFOLDER &&
122 (strncmp(name, "./", 2) && strcmp(name, ".") &&
123 strcmp(name, "..") && strncmp(name, "../", 3))) {
125 ** FIXME: Seems as if this check does not catch names like:
135 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0) {
139 ep = pwds + strlen(pwds);
140 if ((cp = strrchr(pwds, '/')) == NULL) {
142 } else if (cp == pwds) {
146 if (strncmp(name, "./", 2) == 0) {
150 if (strcmp(name, "..") == 0 || strcmp(name, "../") == 0) {
151 snprintf(buffer, sizeof(buffer), "%.*s",
152 (int)(cp - pwds), pwds);
153 return getcpy(buffer);
156 if (strncmp(name, "../", 3) == 0) {
162 snprintf(buffer, sizeof(buffer), "%.*s/%s",
163 (int)(cp - pwds), pwds, name);
164 return getcpy(buffer);
172 path(char *name, int type)
176 if ((cp = expath(name, type)) &&
177 (ep = cp+strlen(cp)-1) > cp &&
187 ** Call path() appropriately for ``+folder'' or ``@folder''
192 return path(name+1, (*name == '+') ? TFOLDER : TSUBCWF);