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