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