1 /*
2 ** Copyright 2000 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #include "config.h"
7 #include "maildirmisc.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <errno.h>
13
14
maildir_shared_fopen(const char * maildir,const char * mode)15 FILE *maildir_shared_fopen(const char *maildir, const char *mode)
16 {
17 char *m;
18 FILE *fp;
19
20 m=malloc(strlen(maildir)+sizeof("/shared-maildirs"));
21 if (!m)
22 {
23 perror("malloc");
24 return (0);
25 }
26 strcat(strcpy(m, maildir), "/shared-maildirs");
27
28 fp=fopen(m, mode);
29 free(m);
30 return (fp);
31 }
32
maildir_shared_fparse(char * p,char ** name,char ** dir)33 void maildir_shared_fparse(char *p, char **name, char **dir)
34 {
35 char *q;
36
37 *name=0;
38 *dir=0;
39
40 if ((q=strchr(p, '\n')) != 0) *q=0;
41 if ((q=strchr(p, '#')) != 0) *q=0;
42
43 for (q=p; *q; q++)
44 if (isspace((int)(unsigned char)*q)) break;
45 if (!*q) return;
46 *q++=0;
47 while (*q && isspace((int)(unsigned char)*q))
48 ++q;
49 if (*q)
50 {
51 *name=p;
52 *dir=q;
53 }
54 }
55
maildir_shareddir(const char * maildir,const char * sharedname)56 char *maildir_shareddir(const char *maildir, const char *sharedname)
57 {
58 char *p, *q;
59 const char *r;
60
61 if (!maildir) maildir=".";
62
63 if (strchr(sharedname, '.') == 0 || *sharedname == '.' ||
64 strchr(sharedname, '/'))
65 {
66 errno=EINVAL;
67 return (0);
68 }
69
70 for (r=sharedname; *r; r++)
71 {
72 if (*r == '.' && (r[1] == '.' || r[1] == '\0'))
73 {
74 errno=EINVAL;
75 return (0);
76 }
77 }
78
79 p=malloc(strlen(maildir)+sizeof("/" SHAREDSUBDIR "/")+strlen(sharedname));
80 if (!p) return (0);
81
82 *p=0;
83 if (strcmp(maildir, "."))
84 strcat(strcpy(p, maildir), "/");
85 strcat(p, SHAREDSUBDIR "/");
86 q=p+strlen(p);
87 strcpy(q, sharedname);
88 *strchr(q, '.')='/';
89 return (p);
90 }
91