mutil.c
3.44 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
/* 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 <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <mailutils/argcv.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
#include <mailutils/assoc.h>
#include <mailutils/stream.h>
#include <mailutils/sql.h>
int
mu_is_proto (const char *p)
{
if (*p == '|')
return 1;
for (; *p && *p != '/'; p++)
if (*p == ':')
return 1;
return 0;
}
int
mu_mh_delim (const char *str)
{
if (str[0] == '-')
{
for (; *str == '-'; str++)
;
for (; *str == ' ' || *str == '\t'; str++)
;
}
return str[0] == '\n';
}
/* A locale-independent version of strftime */
size_t
mu_strftime (char *s, size_t max, const char *format, const struct tm *tm)
{
size_t size;
mu_set_locale ("C");
size = strftime (s, max, format, tm);
mu_restore_locale ();
return size;
}
static void
assoc_str_free (void *data)
{
free (data);
}
int
mutil_parse_field_map (const char *map, mu_assoc_t *passoc_tab, int *perr)
{
int rc;
int i;
int argc;
char **argv;
mu_assoc_t assoc_tab = NULL;
rc = mu_argcv_get (map, ":", NULL, &argc, &argv);
if (rc)
{
mu_error (_("cannot split line `%s': %s"), map, mu_strerror (rc));
return rc;
}
for (i = 0; i < argc; i += 2)
{
char *tok = argv[i];
char *p = strchr (tok, '=');
char *pptr;
if (!p)
{
rc = EINVAL;
break;
}
if (!assoc_tab)
{
rc = mu_assoc_create (&assoc_tab, sizeof(char*), 0);
if (rc)
break;
mu_assoc_set_free (assoc_tab, assoc_str_free);
*passoc_tab = assoc_tab;
}
*p++ = 0;
pptr = strdup (p);
if (!pptr)
{
rc = errno;
break;
}
rc = mu_assoc_install (assoc_tab, tok, &pptr);
if (rc)
{
free (p);
break;
}
}
mu_argcv_free (argc, argv);
if (rc && perr)
*perr = i;
return rc;
}
/* FIXME: should it be here? */
int
mu_sql_decode_password_type (const char *arg, enum mu_password_type *t)
{
if (strcmp (arg, "plain") == 0)
*t = password_plaintext;
else if (strcmp (arg, "hash") == 0)
*t = password_hash;
else if (strcmp (arg, "scrambled") == 0)
*t = password_scrambled;
else
return 1;
return 0;
}
int
mu_stream_flags_to_mode (int flags, int isdir)
{
int mode = 0;
if (flags & MU_STREAM_IRGRP)
mode |= S_IRGRP;
if (flags & MU_STREAM_IWGRP)
mode |= S_IWGRP;
if (flags & MU_STREAM_IROTH)
mode |= S_IROTH;
if (flags & MU_STREAM_IWOTH)
mode |= S_IWOTH;
if (isdir)
{
if (mode & (S_IRGRP|S_IWGRP))
mode |= S_IXGRP;
if (mode & (S_IROTH|S_IWOTH))
mode |= S_IXOTH;
}
return mode;
}