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.
16 static char *expath(char *, int);
17 static void packpath(char *);
22 return path(name + 1, *name == '+' ? TFOLDER : TSUBCWF);
26 path(char *name, int type)
30 if ((cp = expath(name, type)) &&
31 (ep = cp+strlen(cp)-1) > cp &&
41 expath(char *name, int type)
46 if (type == TSUBCWF) {
47 /* @folder to +folder */
48 snprintf(buffer, sizeof(buffer), "%s/%s", getfolder(1), name);
49 name = m_mailpath(buffer);
51 snprintf(buffer, sizeof(buffer), "%s/", m_maildir(""));
52 if (isprefix(buffer, name)) {
54 name = getcpy(name + strlen(buffer));
64 if (type == TFOLDER &&
65 (strncmp(name, "./", 2) && strcmp(name, ".") &&
66 strcmp(name, "..") && strncmp(name, "../", 3))) {
68 ** FIXME: Seems as if this check does not catch names like:
78 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0) {
82 ep = pwds + strlen(pwds);
83 if ((cp = strrchr(pwds, '/')) == NULL) {
85 } else if (cp == pwds) {
89 if (strncmp(name, "./", 2) == 0) {
93 if (strcmp(name, "..") == 0 || strcmp(name, "../") == 0) {
94 snprintf(buffer, sizeof(buffer), "%.*s",
95 (int)(cp - pwds), pwds);
96 return getcpy(buffer);
99 if (strncmp(name, "../", 3) == 0) {
105 snprintf(buffer, sizeof(buffer), "%.*s/%s",
106 (int)(cp - pwds), pwds, name);
107 return getcpy(buffer);
112 ** Compactify an absolute path name by removing unneccessary parts.
113 ** Removes trailing slashes, but not if it would empty the string then.
125 abspath = (*f == '/');
129 /* Skip. Interesting places are only after slashes. */
130 /* We don't care about "./" beginnings */
135 /* Let's see what follows the slash ... */
139 continue; /* ... and thus exit the loop */
142 /* reduce subsequent slashes to one */
143 for (dp = cp; *dp == '/'; dp++) {
148 continue; /* ... at the slash */
151 if (cp[1] == '/' || cp[1] == '\0') {
152 /* one-dot element */
156 } else if ((strncmp(cp, "../", 3) == 0) ||
157 (strcmp(cp, "..") == 0)) {
158 /* dot-dot element */
159 /* crop out previous path element */
160 for (dp=cp-2; dp>f && *dp!='/'; dp--) {
164 /* path starts with "/.." */
171 /* a normal hidden file */
177 /* nothing special */
184 /* We have removed everything, but need something. */
185 strcpy(f, abspath ? "/" : ".");