]> diplodocus.org Git - nmh/blob - sbr/path.c
d425dc8bb6cb59ea7ceac9f1745af422464e1fc7
[nmh] / sbr / path.c
1 /* path.c -- return a pathname
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "path.h"
10 #include "h/utils.h"
11 #include "m_maildir.h"
12
13 #define CWD "./"
14 #define DOT "."
15 #define DOTDOT ".."
16 #define PWD "../"
17
18 static char *pwds;
19
20 /*
21 * static prototypes
22 */
23 static char *expath(char *,int);
24 static void compath(char *);
25
26
27 /* Return value must be free(3)'d. */
28 char *
29 pluspath(char *name)
30 {
31 return path(name + 1, *name == '+' ? TFOLDER : TSUBCWF);
32 }
33
34
35 /* Return value must be free(3)'d. */
36 char *
37 path(char *name, int flag)
38 {
39 char *p, *last;
40
41 p = expath(name, flag);
42 last = p + strlen(p) - 1;
43 if (last > p && *last == '/')
44 *last = '\0';
45
46 return p;
47 }
48
49
50 /* Return value must be free(3)'d. */
51 static char *
52 expath (char *name, int flag)
53 {
54 char *cp, *ep;
55 char buffer[BUFSIZ];
56
57 if (flag == TSUBCWF) {
58 snprintf (buffer, sizeof(buffer), "%s/%s", getfolder (1), name);
59 name = m_mailpath (buffer);
60 compath (name);
61 snprintf (buffer, sizeof(buffer), "%s/", m_maildir (""));
62 if (ssequal (buffer, name)) {
63 cp = name;
64 name = mh_xstrdup(name + strlen(buffer));
65 free (cp);
66 }
67 flag = TFOLDER;
68 }
69
70 if (*name == '/'
71 || (flag == TFOLDER
72 && (!has_prefix(name, CWD)
73 && strcmp (name, DOT)
74 && strcmp (name, DOTDOT)
75 && !has_prefix(name, PWD))))
76 return mh_xstrdup(name);
77
78 if (pwds == NULL)
79 pwds = pwd ();
80
81 if (strcmp (name, DOT) == 0 || strcmp (name, CWD) == 0)
82 return mh_xstrdup(pwds);
83
84 ep = pwds + strlen (pwds);
85 if ((cp = strrchr(pwds, '/')) == NULL)
86 cp = ep;
87 else if (cp == pwds)
88 cp++;
89
90 if (has_prefix(name, CWD))
91 name += LEN(CWD);
92
93 if (strcmp (name, DOTDOT) == 0 || strcmp (name, PWD) == 0) {
94 snprintf (buffer, sizeof(buffer), "%.*s", (int)(cp - pwds), pwds);
95 return mh_xstrdup(buffer);
96 }
97
98 if (has_prefix(name, PWD))
99 name += LEN(PWD);
100 else
101 cp = ep;
102
103 snprintf (buffer, sizeof(buffer), "%.*s/%s", (int)(cp - pwds), pwds, name);
104 return mh_xstrdup(buffer);
105 }
106
107
108 static void
109 compath (char *f)
110 {
111 char *cp, *dp;
112
113 if (*f != '/')
114 return;
115
116 for (cp = f; *cp;) {
117 if (*cp != '/') {
118 cp++;
119 continue;
120 }
121
122 switch (*++cp) {
123 case 0:
124 if (--cp > f)
125 *cp = '\0';
126 return;
127
128 case '/':
129 for (dp = cp; *dp == '/'; dp++)
130 continue;
131 strcpy (cp--, dp);
132 continue;
133
134 case '.':
135 if (strcmp (cp, DOT) == 0) {
136 if (cp > f + 1)
137 cp--;
138 *cp = '\0';
139 return;
140 }
141 if (strcmp (cp, DOTDOT) == 0) {
142 for (cp -= 2; cp > f; cp--)
143 if (*cp == '/')
144 break;
145 if (cp <= f)
146 cp = f + 1;
147 *cp = '\0';
148 return;
149 }
150 if (has_prefix(cp, PWD)) {
151 for (dp = cp - 2; dp > f; dp--)
152 if (*dp == '/')
153 break;
154 if (dp <= f)
155 dp = f;
156 strcpy (dp, cp + LEN(PWD) - 1);
157 cp = dp;
158 continue;
159 }
160 if (has_prefix(cp, CWD)) {
161 strcpy (cp - 1, cp + LEN(CWD) - 1);
162 cp--;
163 continue;
164 }
165 continue;
166
167 default:
168 cp++;
169 continue;
170 }
171 }
172 }