Removed the space between function names and the opening parenthesis.
[mmh] / sbr / seq_save.c
1 /*
2 ** seq_save.c -- 1) synchronize sequences
3 **            -- 2) save public sequences
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12
13
14 /*
15 ** 1.  If sequence is public and folder is readonly,
16 **     then change it to be private
17 ** 2a. If sequence is public, then add it to the sequences file
18 **     in folder (name specified by mh-sequences profile entry).
19 ** 2b. If sequence is private, then add it to the
20 **     context file.
21 */
22
23 void
24 seq_save(struct msgs *mp)
25 {
26         int i;
27         char flags, *cp, attr[BUFSIZ], seqfile[PATH_MAX];
28         FILE *fp;
29         sigset_t set, oset;
30
31         /* check if sequence information has changed */
32         if (!(mp->msgflags & SEQMOD))
33                 return;
34         mp->msgflags &= ~SEQMOD;
35
36         fp = NULL;
37         flags = mp->msgflags;  /* record folder flags */
38
39         /*
40         ** If no mh-sequences file is defined, or if a mh-sequences file
41         ** is defined but empty (*mh_seq == '\0'), then pretend folder
42         ** is readonly.  This will force all sequences to be private.
43         */
44         if (mh_seq == NULL || *mh_seq == '\0')
45                 set_readonly(mp);
46         else
47                 snprintf(seqfile, sizeof(seqfile), "%s/%s", mp->foldpath,
48                                 mh_seq);
49
50         for (i = 0; mp->msgattrs[i]; i++) {
51                 snprintf(attr, sizeof(attr), "atr-%s-%s", mp->msgattrs[i],
52                                 mp->foldpath);
53
54                 /* get space separated list of sequence ranges */
55                 if (!(cp = seq_list(mp, mp->msgattrs[i]))) {
56                         context_del(attr);  /* delete sequence from context */
57                         continue;
58                 }
59
60                 if (is_readonly(mp) || is_seq_private(mp, i)) {
61 priv:
62                         /* sequence is private */
63                         context_replace(attr, cp);  /* update seq in ctx */
64                 } else {
65                         /* sequence is public */
66                         context_del(attr);  /* delete sequence from context */
67
68                         if (!fp) {
69                                 /*
70                                 ** Attempt to open file for public sequences.
71                                 ** If that fails (probably because folder is
72                                 ** readonly), then make sequence private.
73                                 */
74                                 if ((fp = lkfopen(seqfile, "w")) == NULL
75                                                 && (unlink(seqfile) == -1 ||
76                                                 (fp = lkfopen(seqfile, "w"))
77                                                 == NULL)) {
78                                         admonish(attr, "unable to write");
79                                         goto priv;
80                                 }
81
82                                 /* block a few signals */
83                                 sigemptyset(&set);
84                                 sigaddset(&set, SIGHUP);
85                                 sigaddset(&set, SIGINT);
86                                 sigaddset(&set, SIGQUIT);
87                                 sigaddset(&set, SIGTERM);
88                                 SIGPROCMASK(SIG_BLOCK, &set, &oset);
89                         }
90                         fprintf(fp, "%s: %s\n", mp->msgattrs[i], cp);
91                 }
92         }
93
94         if (fp) {
95                 lkfclose(fp, seqfile);
96                 SIGPROCMASK(SIG_SETMASK, &oset, &set);  /* reset signal mask */
97         } else {
98                 /*
99                 ** If folder is not readonly, and we didn't save any
100                 ** public sequences, then remove that file.
101                 */
102                 if (!is_readonly(mp))
103                         unlink(seqfile);
104         }
105
106         /*
107         ** Reset folder flag, since we may be
108         ** pretending that folder is readonly.
109         */
110         mp->msgflags = flags;
111 }