source: trunk/third/nmh/uip/distsbr.c @ 12455

Revision 12455, 4.8 KB checked in by danw, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12454, which included commits to RCS files with non-trunk default branches.
Line 
1
2/*
3 * distsbr.c -- routines to do additional "dist-style" processing
4 *
5 * $Id: distsbr.c,v 1.1.1.1 1999-02-07 18:14:13 danw Exp $
6 */
7
8#include <h/mh.h>
9#include <fcntl.h>
10
11static int  hdrfd = NOTOK;
12static int  txtfd = NOTOK;
13
14#define BADHDR  "please re-edit %s to remove the ``%s'' header!"
15#define BADTXT  "please re-edit %s to consist of headers only!"
16#define BADMSG  "please re-edit %s to include a ``Resent-To:''!"
17#define BADRFT  "please re-edit %s and fix that header!"
18
19/*
20 * static prototypes
21 */
22static void ready_msg(char *);
23
24int
25distout (char *drft, char *msgnam, char *backup)
26{
27    int state;
28    register char *dp, *resent;
29    char name[NAMESZ], buffer[BUFSIZ];
30    register FILE *ifp, *ofp;
31
32    if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
33        adios (backup, "unable to rename %s to",drft);
34    if ((ifp = fopen (backup, "r")) == NULL)
35        adios (backup, "unable to read");
36
37    if ((ofp = fopen (drft, "w")) == NULL)
38        adios (drft, "unable to create temporary file");
39    chmod (drft, m_gmprot ());
40
41    ready_msg (msgnam);
42    lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
43    cpydata (hdrfd, fileno (ofp), msgnam, drft);
44
45    for (state = FLD, resent = NULL;;)
46        switch (state =
47                m_getfld (state, name, buffer, sizeof buffer, ifp)) {
48            case FLD:
49            case FLDPLUS:
50            case FLDEOF:
51                if (uprf (name, "distribute-"))
52                    snprintf (name, sizeof(name), "%s%s", "Resent", &name[10]);
53                if (uprf (name, "distribution-"))
54                    snprintf (name, sizeof(name), "%s%s", "Resent", &name[12]);
55                if (!uprf (name, "resent")) {
56                    advise (NULL, BADHDR, "draft", name);
57                    goto leave_bad;
58                }
59                if (state == FLD)
60                    resent = add (":", add (name, resent));
61                resent = add (buffer, resent);
62                fprintf (ofp, "%s: %s", name, buffer);
63                while (state == FLDPLUS) {
64                    state = m_getfld (state, name,
65                            buffer, sizeof buffer, ifp);
66                    resent = add (buffer, resent);
67                    fputs (buffer, ofp);
68                }
69                if (state == FLDEOF)
70                    goto process;
71                break;
72
73            case BODY:
74            case BODYEOF:
75                for (dp = buffer; *dp; dp++)
76                    if (!isspace (*dp)) {
77                        advise (NULL, BADTXT, "draft");
78                        goto leave_bad;
79                    }
80
81            case FILEEOF:
82                goto process;
83
84            case LENERR:
85            case FMTERR:
86                advise (NULL, BADRFT, "draft");
87        leave_bad: ;
88                fclose (ifp);
89                fclose (ofp);
90                unlink (drft);
91                if (rename (backup, drft) == NOTOK)
92                    adios (drft, "unable to rename %s to", backup);
93                return NOTOK;
94
95            default:
96                adios (NULL, "getfld() returned %d", state);
97        }
98process: ;
99    fclose (ifp);
100    fflush (ofp);
101
102    if (!resent) {
103        advise (NULL, BADMSG, "draft");
104        fclose (ofp);
105        unlink (drft);
106        if (rename (backup, drft) == NOTOK)
107            adios (drft, "unable to rename %s to", backup);
108        return NOTOK;
109    }
110    free (resent);
111
112    if (txtfd != NOTOK) {
113        lseek (txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
114        cpydata (txtfd, fileno (ofp), msgnam, drft);
115    }
116
117    fclose (ofp);
118
119    return OK;
120}
121
122
123static void
124ready_msg (char *msgnam)
125{
126    int state, out;
127    char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ];
128    register FILE *ifp, *ofp;
129
130    if (hdrfd != NOTOK)
131        close (hdrfd), hdrfd = NOTOK;
132    if (txtfd != NOTOK)
133        close (txtfd), txtfd = NOTOK;
134
135    if ((ifp = fopen (msgnam, "r")) == NULL)
136        adios (msgnam, "unable to open message");
137
138    strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
139    if ((hdrfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
140        adios (tmpfil, "unable to re-open temporary file");
141    if ((out = dup (hdrfd)) == NOTOK
142            || (ofp = fdopen (out, "w")) == NULL)
143        adios (NULL, "no file descriptors -- you lose big");
144    unlink (tmpfil);
145
146    for (state = FLD;;)
147        switch (state =
148                m_getfld (state, name, buffer, sizeof buffer, ifp)) {
149            case FLD:
150            case FLDPLUS:
151            case FLDEOF:
152                if (uprf (name, "resent"))
153                    fprintf (ofp, "Prev-");
154                fprintf (ofp, "%s: %s", name, buffer);
155                while (state == FLDPLUS) {
156                    state = m_getfld (state, name,
157                            buffer, sizeof buffer, ifp);
158                    fputs (buffer, ofp);
159                }
160                if (state == FLDEOF)
161                    goto process;
162                break;
163
164            case BODY:
165            case BODYEOF:
166                fclose (ofp);
167
168                strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
169                if ((txtfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
170                    adios (tmpfil, "unable to open temporary file");
171                if ((out = dup (txtfd)) == NOTOK
172                        || (ofp = fdopen (out, "w")) == NULL)
173                    adios (NULL, "no file descriptors -- you lose big");
174                unlink (tmpfil);
175                fprintf (ofp, "\n%s", buffer);
176                while (state == BODY) {
177                    state = m_getfld (state, name,
178                            buffer, sizeof buffer, ifp);
179                    fputs (buffer, ofp);
180                }
181            case FILEEOF:
182                goto process;
183
184            case LENERR:
185            case FMTERR:
186                adios (NULL, "format error in message %s", msgnam);
187
188            default:
189                adios (NULL, "getfld() returned %d", state);
190        }
191process: ;
192    fclose (ifp);
193    fclose (ofp);
194}
Note: See TracBrowser for help on using the repository browser.