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