mbx_default.c
shortcut for getenv(MAIL) etc ... to get the default mailbox. And could serve as a hook to Jeff/Jakob idea of config file.
Showing
1 changed file
with
60 additions
and
0 deletions
mailbox/mbx_default.c
0 → 100644
1 | #include <mailbox.h> | ||
2 | #include <string.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <paths.h> | ||
5 | #include <errno.h> | ||
6 | #include <stdio.h> | ||
7 | |||
8 | #ifndef _PATH_MAILDIR | ||
9 | # define _PATH_MAILDIR "/var/spool/mail" | ||
10 | #endif | ||
11 | |||
12 | int | ||
13 | mailbox_create_default (mailbox_t *pmbox, const char *mail) | ||
14 | { | ||
15 | const char *user = NULL; | ||
16 | |||
17 | if (pmbox == NULL) | ||
18 | return EINVAL; | ||
19 | |||
20 | if (mail == NULL) | ||
21 | mail = getenv ("MAIL"); | ||
22 | |||
23 | if (mail) | ||
24 | { | ||
25 | /* is it a fullpath ? */ | ||
26 | if (mail[0] != '/') | ||
27 | { | ||
28 | /* is it a URL ? */ | ||
29 | if (strchr (mail, ':') == NULL) | ||
30 | { | ||
31 | /* a user name */ | ||
32 | user = mail; | ||
33 | mail = NULL; | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | if (mail == NULL) | ||
39 | { | ||
40 | int status; | ||
41 | char *mail0; | ||
42 | if (user == NULL) | ||
43 | { | ||
44 | user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER"); | ||
45 | if (user == NULL) | ||
46 | { | ||
47 | fprintf (stderr, "who am I?\n"); | ||
48 | return EINVAL; | ||
49 | } | ||
50 | } | ||
51 | mail0 = malloc (strlen (user) + strlen (_PATH_MAILDIR) + 2); | ||
52 | if (mail0 == NULL) | ||
53 | return ENOMEM; | ||
54 | sprintf (mail0, "%s/%s", _PATH_MAILDIR, user); | ||
55 | status = mailbox_create (pmbox, mail0, 0); | ||
56 | free (mail0); | ||
57 | return status; | ||
58 | } | ||
59 | return mailbox_create (pmbox, mail, 0); | ||
60 | } |
-
Please register or sign in to post a comment