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