]> diplodocus.org Git - nmh/blob - sbr/m_maildir.c
e72cd7806d3c1b67501fc9e2a71bb66583525b6e
[nmh] / sbr / m_maildir.c
1 /* m_maildir.c -- get the path for the mail directory
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 "context_find.h"
10 #include "path.h"
11 #include "h/utils.h"
12 #include "m_maildir.h"
13
14 #define CWD "./"
15 #define DOT "."
16 #define DOTDOT ".."
17 #define PWD "../"
18
19 static char mailfold[BUFSIZ];
20
21 /*
22 * static prototypes
23 */
24 static char *exmaildir (char *);
25
26
27 /* Returns static char[], never NULL. */
28 char *
29 m_maildir (char *folder)
30 {
31 char *cp, *ep;
32
33 if ((cp = exmaildir (folder))
34 && (ep = cp + strlen (cp) - 1) > cp
35 && *ep == '/')
36 *ep = '\0';
37
38 return cp;
39 }
40
41
42 /* Return value must be free(3)'d. */
43 char *
44 m_mailpath (char *folder)
45 {
46 char *cp;
47 char maildir[BUFSIZ];
48
49 if (*folder != '/'
50 && !has_prefix(folder, CWD)
51 && strcmp (folder, DOT)
52 && strcmp (folder, DOTDOT)
53 && !has_prefix(folder, PWD)) {
54 strncpy (maildir, mailfold, sizeof(maildir)); /* preserve... */
55 cp = mh_xstrdup(m_maildir(folder));
56 strncpy (mailfold, maildir, sizeof(mailfold));
57 } else {
58 cp = path (folder, TFOLDER);
59 }
60
61 return cp;
62 }
63
64
65 /* Returns static char[], never NULL. */
66 static char *
67 exmaildir (char *folder)
68 {
69 char *cp, *pp;
70
71 /* use current folder if none is specified */
72 if (folder == NULL)
73 folder = getfolder(1);
74
75 if (!(*folder != '/'
76 && !has_prefix(folder, CWD)
77 && strcmp (folder, DOT)
78 && strcmp (folder, DOTDOT)
79 && !has_prefix(folder, PWD))) {
80 strncpy (mailfold, folder, sizeof(mailfold));
81 return mailfold;
82 }
83
84 cp = mailfold;
85 if ((pp = context_find ("path")) && *pp) {
86 if (*pp != '/') {
87 snprintf(cp, sizeof mailfold, "%s/", mypath);
88 cp += strlen (cp);
89 }
90 cp = stpcpy(cp, pp);
91 } else {
92 char *p = path("./", TFOLDER);
93 cp = stpcpy(cp, p);
94 free(p);
95 }
96 if (cp[-1] != '/')
97 *cp++ = '/';
98 strcpy (cp, folder);
99
100 return mailfold;
101 }