Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / distsbr.c,v
1 head    1.6;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.6
9 date    93.08.25.22.26.24;      author jromine; state Exp;
10 branches;
11 next    1.5;
12
13 1.5
14 date    93.08.25.17.24.48;      author jromine; state Exp;
15 branches;
16 next    1.4;
17
18 1.4
19 date    92.12.15.00.20.22;      author jromine; state Exp;
20 branches;
21 next    1.3;
22
23 1.3
24 date    90.04.05.15.00.50;      author sources; state Exp;
25 branches;
26 next    1.2;
27
28 1.2
29 date    90.02.06.13.17.31;      author sources; state Exp;
30 branches;
31 next    1.1;
32
33 1.1
34 date    90.02.06.13.17.06;      author sources; state Exp;
35 branches;
36 next    ;
37
38
39 desc
40 @@
41
42
43 1.6
44 log
45 @use sys/types
46 @
47 text
48 @/* distsbr.c - routines to do additional "dist-style" processing */
49 #ifndef lint
50 static char ident[] = "@@(#)$Id: distsbr.c,v 1.5 1993/08/25 17:24:48 jromine Exp jromine $";
51 #endif  /* lint */
52
53 #include "../h/mh.h"
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <sys/types.h>  /* for off_t */
57
58 static          ready_msg();
59
60 static int  hdrfd = NOTOK;
61 static int  txtfd = NOTOK;
62
63
64 off_t   lseek ();
65
66 /* \f */
67
68 #define BADHDR  "please re-edit %s to remove the ``%s'' header!"
69 #define BADTXT  "please re-edit %s to consist of headers only!"
70 #define BADMSG  "please re-edit %s to include a ``Resent-To:''!"
71 #define BADRFT  "please re-edit %s and fix that header!"
72
73 int     distout (drft, msgnam, backup)
74 register char   *drft,
75        *msgnam,
76        *backup;
77 {
78     int     state;
79     register char  *dp,
80                    *resent;
81     char    name[NAMESZ],
82             buffer[BUFSIZ];
83     register    FILE *ifp,
84                      *ofp;
85
86     if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
87         adios (backup, "unable to rename %s to",drft);
88     if ((ifp = fopen (backup, "r")) == NULL)
89         adios (backup, "unable to read");
90
91     if ((ofp = fopen (drft, "w")) == NULL)
92         adios (drft, "unable to create temporary file");
93     (void) chmod (drft, m_gmprot ());
94
95     ready_msg (msgnam);
96     (void) lseek (hdrfd, (off_t)0, 0);  /* msgnam not accurate */
97     cpydata (hdrfd, fileno (ofp), msgnam, drft);
98
99 /* \f */
100
101     for (state = FLD, resent = NULL;;)
102         switch (state =
103                 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
104             case FLD: 
105             case FLDPLUS: 
106             case FLDEOF: 
107                 if (uprf (name, "distribute-"))
108                     (void) sprintf (name, "%s%s", "Resent", &name[10]);
109                 if (uprf (name, "distribution-"))
110                     (void) sprintf (name, "%s%s", "Resent", &name[12]);
111                 if (!uprf (name, "resent")) {
112                     advise (NULLCP, BADHDR, "draft", name);
113                     goto leave_bad;
114                 }
115                 if (state == FLD)
116                     resent = add (":", add (name, resent));
117                 resent = add (buffer, resent);
118                 fprintf (ofp, "%s: %s", name, buffer);
119                 while (state == FLDPLUS) {
120                     state = m_getfld (state, name,
121                             buffer, sizeof buffer, ifp);
122                     resent = add (buffer, resent);
123                     fputs (buffer, ofp);
124                 }
125                 if (state == FLDEOF)
126                     goto process;
127                 break;
128
129             case BODY: 
130             case BODYEOF: 
131                 for (dp = buffer; *dp; dp++)
132                     if (!isspace (*dp)) {
133                         advise (NULLCP, BADTXT, "draft");
134                         goto leave_bad;
135                     }
136
137             case FILEEOF: 
138                 goto process;
139
140             case LENERR: 
141             case FMTERR: 
142                 advise (NULLCP, BADRFT, "draft");
143         leave_bad: ;
144                 (void) fclose (ifp);
145                 (void) fclose (ofp);
146                 (void) unlink (drft);
147                 if (rename (backup, drft) == NOTOK)
148                     adios (drft, "unable to rename %s to", backup);
149                 return NOTOK;
150
151             default: 
152                 adios (NULLCP, "getfld() returned %d", state);
153         }
154 process: ;
155     (void) fclose (ifp);
156     (void) fflush (ofp);
157
158 /* \f */
159
160     if (!resent) {
161         advise (NULLCP, BADMSG, "draft");
162         (void) fclose (ofp);
163         (void) unlink (drft);
164         if (rename (backup, drft) == NOTOK)
165             adios (drft, "unable to rename %s to", backup);
166         return NOTOK;
167     }
168     free (resent);
169
170     if (txtfd != NOTOK) {
171         (void) lseek (txtfd, (off_t)0, 0);      /* msgnam not accurate */
172         cpydata (txtfd, fileno (ofp), msgnam, drft);
173     }
174
175     (void) fclose (ofp);
176
177     return OK;
178 }
179
180 /* \f */
181
182 static  ready_msg (msgnam)
183 register char   *msgnam;
184 {
185     int     state,
186             out;
187     char    name[NAMESZ],
188             buffer[BUFSIZ],
189             tmpfil[BUFSIZ];
190     register    FILE *ifp,
191                      *ofp;
192
193     if (hdrfd != NOTOK)
194         (void) close (hdrfd), hdrfd = NOTOK;
195     if (txtfd != NOTOK)
196         (void) close (txtfd), txtfd = NOTOK;
197
198     if ((ifp = fopen (msgnam, "r")) == NULL)
199         adios (msgnam, "unable to open message");
200
201     (void) strcpy (tmpfil, m_tmpfil ("dist"));
202     if ((hdrfd = creat (tmpfil, 0600)) == NOTOK)
203         adios (tmpfil, "unable to create temporary file");
204     (void) close (hdrfd);
205     if ((hdrfd = open (tmpfil, 2)) == NOTOK)
206         adios (tmpfil, "unable to re-open temporary file");
207     if ((out = dup (hdrfd)) == NOTOK
208             || (ofp = fdopen (out, "w")) == NULL)
209         adios (NULLCP, "no file descriptors -- you lose big");
210     (void) unlink (tmpfil);
211
212 /* \f */
213
214     for (state = FLD;;)
215         switch (state =
216                 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
217             case FLD: 
218             case FLDPLUS: 
219             case FLDEOF: 
220                 if (uprf (name, "resent"))
221                     fprintf (ofp, "Prev-");
222                 fprintf (ofp, "%s: %s", name, buffer);
223                 while (state == FLDPLUS) {
224                     state = m_getfld (state, name,
225                             buffer, sizeof buffer, ifp);
226                     fputs (buffer, ofp);
227                 }
228                 if (state == FLDEOF)
229                     goto process;
230                 break;
231
232             case BODY: 
233             case BODYEOF: 
234                 (void) fclose (ofp);
235
236                 (void) strcpy (tmpfil, m_tmpfil ("dist"));
237                 if ((txtfd = creat (tmpfil, 0600)) == NOTOK)
238                     adios (tmpfil, "unable to create temporary file");
239                 (void) close (txtfd);
240                 if ((txtfd = open (tmpfil, 2)) == NOTOK)
241                     adios (tmpfil, "unable to re-open temporary file");
242                 if ((out = dup (txtfd)) == NOTOK
243                         || (ofp = fdopen (out, "w")) == NULL)
244                     adios (NULLCP, "no file descriptors -- you lose big");
245                 (void) unlink (tmpfil);
246                 fprintf (ofp, "\n%s", buffer);
247                 while (state == BODY) {
248                     state = m_getfld (state, name,
249                             buffer, sizeof buffer, ifp);
250                     fputs (buffer, ofp);
251                 }
252             case FILEEOF: 
253                 goto process;
254
255             case LENERR: 
256             case FMTERR: 
257                 adios (NULLCP, "format error in message %s", msgnam);
258
259             default: 
260                 adios (NULLCP, "getfld() returned %d", state);
261         }
262 process: ;
263     (void) fclose (ifp);
264     (void) fclose (ofp);
265 }
266 @
267
268
269 1.5
270 log
271 @off_t fixes for BSD44
272 @
273 text
274 @d3 1
275 a3 1
276 static char ident[] = "@@(#)$Id: distsbr.c,v 1.4 1992/12/15 00:20:22 jromine Exp jromine $";
277 d9 1
278 @
279
280
281 1.4
282 log
283 @endif sugar
284 @
285 text
286 @d3 1
287 a3 1
288 static char ident[] = "@@(#)$Id: distsbr.c,v 1.3 1990/04/05 15:00:50 sources Exp jromine $";
289 d16 1
290 a16 1
291 long    lseek ();
292 d48 1
293 a48 1
294     (void) lseek (hdrfd, 0L, 0);        /* msgnam not accurate */
295 d123 1
296 a123 1
297         (void) lseek (txtfd, 0L, 0);    /* msgnam not accurate */
298 @
299
300
301 1.3
302 log
303 @add ID
304 @
305 text
306 @d3 2
307 a4 2
308 static char ident[] = "@@(#)$Id:$";
309 #endif  lint
310 @
311
312
313 1.2
314 log
315 @ANSI Compilance
316 @
317 text
318 @d2 3
319 @
320
321
322 1.1
323 log
324 @Initial revision
325 @
326 text
327 @d7 1
328 @