filename.c
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2009,
2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <mailutils/types.h>
#include <mailutils/util.h>
#include <mailutils/mu_auth.h>
char *
mu_get_homedir (void)
{
char *homedir = getenv ("HOME");
if (homedir)
homedir = strdup (homedir);
else
{
struct mu_auth_data *auth = mu_get_auth_by_uid (geteuid ());
if (!auth)
return NULL;
homedir = strdup (auth->dir);
mu_auth_data_free (auth);
}
return homedir;
}
char *
mu_get_full_path (const char *file)
{
char *p = NULL;
if (!file)
p = mu_getcwd ();
else if (*file != '/')
{
char *cwd = mu_getcwd ();
if (cwd)
{
p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
if (p)
sprintf (p, "%s/%s", cwd, file);
free (cwd);
}
}
if (!p)
p = strdup (file);
return p;
}
/* mu_normalize_path: convert pathname containig relative paths specs (../)
into an equivalent absolute path. Strip trailing delimiter if present,
unless it is the only character left. E.g.:
/home/user/../smith --> /home/smith
/home/user/../.. --> /
*/
char *
mu_normalize_path (char *path)
{
int len;
char *p;
if (!path)
return path;
len = strlen (path);
/* Empty string is returned as is */
if (len == 0)
return path;
/* delete trailing delimiter if any */
if (len && path[len-1] == '/')
path[len-1] = 0;
/* Eliminate any /../ */
for (p = strchr (path, '.'); p; p = strchr (p, '.'))
{
if (p > path && p[-1] == '/')
{
if (p[1] == '.' && (p[2] == 0 || p[2] == '/'))
/* found */
{
char *q, *s;
/* Find previous delimiter */
for (q = p-2; *q != '/' && q >= path; q--)
;
if (q < path)
break;
/* Copy stuff */
s = p + 2;
p = q;
while ((*q++ = *s++))
;
continue;
}
}
p++;
}
if (path[0] == 0)
{
path[0] = '/';
path[1] = 0;
}
return path;
}
/* Expand a PATTERN to the pathname. PATTERN may contain the following
macro-notations:
---------+------------
notation | expands to
---------+------------
%u user name
%h user's home dir
~ Likewise
---------+------------
Allocates memory.
*/
char *
mu_expand_path_pattern (const char *pattern, const char *username)
{
const char *p;
char *q;
char *path;
size_t len = 0;
struct mu_auth_data *auth = NULL;
for (p = pattern; *p; p++)
{
if (*p == '~')
{
if (!auth)
{
auth = mu_get_auth_by_name (username);
if (!auth)
return NULL;
}
len += strlen (auth->dir);
}
else if (*p == '%')
switch (*++p)
{
case 'u':
len += strlen (username);
break;
case 'h':
if (!auth)
{
auth = mu_get_auth_by_name (username);
if (!auth)
return NULL;
}
len += strlen (auth->dir);
break;
case '%':
len++;
break;
default:
len += 2;
}
else
len++;
}
path = malloc (len + 1);
if (!path)
return NULL;
p = pattern;
q = path;
while (*p)
{
size_t off = strcspn (p, "~%");
memcpy (q, p, off);
q += off;
p += off;
if (*p == '~')
{
strcpy (q, auth->dir);
q += strlen (auth->dir);
p++;
}
else if (*p == '%')
{
switch (*++p)
{
case 'u':
strcpy (q, username);
q += strlen (username);
break;
case 'h':
strcpy (q, auth->dir);
q += strlen (auth->dir);
break;
case '%':
*q++ = '%';
break;
default:
*q++ = '%';
*q++ = *p;
}
p++;
}
}
*q = 0;
if (auth)
mu_auth_data_free (auth);
return path;
}