X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/8a14191c0a0ad15bb8f35b49748c75c1e0a8c2f4..1e03ea3338cc5ccd9ddf4feaaacfeda1998689cc:/uip/distsbr.c diff --git a/uip/distsbr.c b/uip/distsbr.c index bdea4b14..4e931ecb 100644 --- a/uip/distsbr.c +++ b/uip/distsbr.c @@ -1,15 +1,18 @@ - -/* - * distsbr.c -- routines to do additional "dist-style" processing +/* distsbr.c -- routines to do additional "dist-style" processing * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. */ -#include +#include "h/mh.h" +#include "sbr/cpydata.h" +#include "sbr/uprf.h" +#include "sbr/m_backup.h" +#include "sbr/error.h" #include -#include +#include "h/utils.h" +#include "sbr/m_mktemp.h" static int hdrfd = NOTOK; static int txtfd = NOTOK; @@ -29,9 +32,9 @@ distout (char *drft, char *msgnam, char *backup) { int state; char *dp, *resent; - char name[NAMESZ], buffer[BUFSIZ]; + char name[NAMESZ], buffer[NMH_BUFSIZ]; FILE *ifp, *ofp; - m_getfld_state_t gstate = 0; + m_getfld_state_t gstate; if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK) adios (backup, "unable to rename %s to",drft); @@ -43,12 +46,13 @@ distout (char *drft, char *msgnam, char *backup) chmod (drft, m_gmprot ()); ready_msg (msgnam); - lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */ + lseek(hdrfd, 0, SEEK_SET); /* msgnam not accurate */ cpydata (hdrfd, fileno (ofp), msgnam, drft); + gstate = m_getfld_state_init(ifp); for (resent = NULL;;) { int buffersz = sizeof buffer; - switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) { + switch (state = m_getfld2(&gstate, name, buffer, &buffersz)) { case FLD: case FLDPLUS: if (uprf (name, "distribute-")) @@ -65,7 +69,7 @@ distout (char *drft, char *msgnam, char *backup) fprintf (ofp, "%s: %s", name, buffer); while (state == FLDPLUS) { buffersz = sizeof buffer; - state = m_getfld (&gstate, name, buffer, &buffersz, ifp); + state = m_getfld2(&gstate, name, buffer, &buffersz); resent = add (buffer, resent); fputs (buffer, ofp); } @@ -93,7 +97,7 @@ distout (char *drft, char *msgnam, char *backup) return NOTOK; default: - adios (NULL, "getfld() returned %d", state); + die("getfld() returned %d", state); } } process: ; @@ -112,7 +116,7 @@ process: ; free (resent); if (txtfd != NOTOK) { - lseek (txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */ + lseek(txtfd, 0, SEEK_SET); /* msgnam not accurate */ cpydata (txtfd, fileno (ofp), msgnam, drft); } @@ -126,32 +130,37 @@ static void ready_msg (char *msgnam) { int state, out; - char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ]; + char name[NAMESZ], buffer[NMH_BUFSIZ], tmpfil[BUFSIZ]; FILE *ifp, *ofp; char *cp = NULL; - m_getfld_state_t gstate = 0; + m_getfld_state_t gstate; - if (hdrfd != NOTOK) - close (hdrfd), hdrfd = NOTOK; - if (txtfd != NOTOK) - close (txtfd), txtfd = NOTOK; + if (hdrfd != NOTOK) { + close (hdrfd); + hdrfd = NOTOK; + } + if (txtfd != NOTOK) { + close (txtfd); + txtfd = NOTOK; + } if ((ifp = fopen (msgnam, "r")) == NULL) adios (msgnam, "unable to open message"); cp = m_mktemp2(NULL, "dist", &hdrfd, NULL); if (cp == NULL) { - adios(NULL, "unable to create temporary file in %s", get_temp_dir()); + die("unable to create temporary file in %s", get_temp_dir()); } strncpy(tmpfil, cp, sizeof(tmpfil)); if ((out = dup (hdrfd)) == NOTOK || (ofp = fdopen (out, "w")) == NULL) - adios (NULL, "no file descriptors -- you lose big"); + die("no file descriptors -- you lose big"); (void) m_unlink (tmpfil); + gstate = m_getfld_state_init(ifp); for (;;) { int buffersz = sizeof buffer; - switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) { + switch (state = m_getfld2(&gstate, name, buffer, &buffersz)) { case FLD: case FLDPLUS: if (uprf (name, "resent")) @@ -159,7 +168,7 @@ ready_msg (char *msgnam) fprintf (ofp, "%s: %s", name, buffer); while (state == FLDPLUS) { buffersz = sizeof buffer; - state = m_getfld (&gstate, name, buffer, &buffersz, ifp); + state = m_getfld2(&gstate, name, buffer, &buffersz); fputs (buffer, ofp); } break; @@ -169,19 +178,19 @@ ready_msg (char *msgnam) cp = m_mktemp2(NULL, "dist", &txtfd, NULL); if (cp == NULL) { - adios(NULL, "unable to create temporary file in %s", + die("unable to create temporary file in %s", get_temp_dir()); } fchmod(txtfd, 0600); strncpy (tmpfil, cp, sizeof(tmpfil)); if ((out = dup (txtfd)) == NOTOK || (ofp = fdopen (out, "w")) == NULL) - adios (NULL, "no file descriptors -- you lose big"); + die("no file descriptors -- you lose big"); (void) m_unlink (tmpfil); fprintf (ofp, "\n%s", buffer); while (state == BODY) { buffersz = sizeof buffer; - state = m_getfld (&gstate, name, buffer, &buffersz, ifp); + state = m_getfld2(&gstate, name, buffer, &buffersz); fputs (buffer, ofp); } case FILEEOF: @@ -189,10 +198,10 @@ ready_msg (char *msgnam) case LENERR: case FMTERR: - adios (NULL, "format error in message %s", msgnam); + die("format error in message %s", msgnam); default: - adios (NULL, "getfld() returned %d", state); + die("getfld() returned %d", state); } } process: ;