1 /* distsbr.c - routines to do additional "dist-style" processing */
3 static char ident[] = "@(#)$Id: distsbr.c,v 1.6 1993/08/25 22:26:24 jromine Exp $";
9 #include <sys/types.h> /* for off_t */
13 static int hdrfd = NOTOK;
14 static int txtfd = NOTOK;
21 #define BADHDR "please re-edit %s to remove the ``%s'' header!"
22 #define BADTXT "please re-edit %s to consist of headers only!"
23 #define BADMSG "please re-edit %s to include a ``Resent-To:''!"
24 #define BADRFT "please re-edit %s and fix that header!"
26 int distout (drft, msgnam, backup)
39 if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
40 adios (backup, "unable to rename %s to",drft);
41 if ((ifp = fopen (backup, "r")) == NULL)
42 adios (backup, "unable to read");
44 if ((ofp = fopen (drft, "w")) == NULL)
45 adios (drft, "unable to create temporary file");
46 (void) chmod (drft, m_gmprot ());
49 (void) lseek (hdrfd, (off_t)0, 0); /* msgnam not accurate */
50 cpydata (hdrfd, fileno (ofp), msgnam, drft);
54 for (state = FLD, resent = NULL;;)
56 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
60 if (uprf (name, "distribute-"))
61 (void) sprintf (name, "%s%s", "Resent", &name[10]);
62 if (uprf (name, "distribution-"))
63 (void) sprintf (name, "%s%s", "Resent", &name[12]);
64 if (!uprf (name, "resent")) {
65 advise (NULLCP, BADHDR, "draft", name);
69 resent = add (":", add (name, resent));
70 resent = add (buffer, resent);
71 fprintf (ofp, "%s: %s", name, buffer);
72 while (state == FLDPLUS) {
73 state = m_getfld (state, name,
74 buffer, sizeof buffer, ifp);
75 resent = add (buffer, resent);
84 for (dp = buffer; *dp; dp++)
86 advise (NULLCP, BADTXT, "draft");
95 advise (NULLCP, BADRFT, "draft");
100 if (rename (backup, drft) == NOTOK)
101 adios (drft, "unable to rename %s to", backup);
105 adios (NULLCP, "getfld() returned %d", state);
114 advise (NULLCP, BADMSG, "draft");
116 (void) unlink (drft);
117 if (rename (backup, drft) == NOTOK)
118 adios (drft, "unable to rename %s to", backup);
123 if (txtfd != NOTOK) {
124 (void) lseek (txtfd, (off_t)0, 0); /* msgnam not accurate */
125 cpydata (txtfd, fileno (ofp), msgnam, drft);
135 static ready_msg (msgnam)
136 register char *msgnam;
147 (void) close (hdrfd), hdrfd = NOTOK;
149 (void) close (txtfd), txtfd = NOTOK;
151 if ((ifp = fopen (msgnam, "r")) == NULL)
152 adios (msgnam, "unable to open message");
154 (void) strcpy (tmpfil, m_tmpfil ("dist"));
155 if ((hdrfd = creat (tmpfil, 0600)) == NOTOK)
156 adios (tmpfil, "unable to create temporary file");
157 (void) close (hdrfd);
158 if ((hdrfd = open (tmpfil, 2)) == NOTOK)
159 adios (tmpfil, "unable to re-open temporary file");
160 if ((out = dup (hdrfd)) == NOTOK
161 || (ofp = fdopen (out, "w")) == NULL)
162 adios (NULLCP, "no file descriptors -- you lose big");
163 (void) unlink (tmpfil);
169 m_getfld (state, name, buffer, sizeof buffer, ifp)) {
173 if (uprf (name, "resent"))
174 fprintf (ofp, "Prev-");
175 fprintf (ofp, "%s: %s", name, buffer);
176 while (state == FLDPLUS) {
177 state = m_getfld (state, name,
178 buffer, sizeof buffer, ifp);
189 (void) strcpy (tmpfil, m_tmpfil ("dist"));
190 if ((txtfd = creat (tmpfil, 0600)) == NOTOK)
191 adios (tmpfil, "unable to create temporary file");
192 (void) close (txtfd);
193 if ((txtfd = open (tmpfil, 2)) == NOTOK)
194 adios (tmpfil, "unable to re-open temporary file");
195 if ((out = dup (txtfd)) == NOTOK
196 || (ofp = fdopen (out, "w")) == NULL)
197 adios (NULLCP, "no file descriptors -- you lose big");
198 (void) unlink (tmpfil);
199 fprintf (ofp, "\n%s", buffer);
200 while (state == BODY) {
201 state = m_getfld (state, name,
202 buffer, sizeof buffer, ifp);
210 adios (NULLCP, "format error in message %s", msgnam);
213 adios (NULLCP, "getfld() returned %d", state);