]> diplodocus.org Git - nmh/blobdiff - uip/distsbr.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / distsbr.c
index bda98c8f120b9266c6ddad80c6075673bc5c142c..305237785accf02bb2a55ee925436da00e6b2ecf 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * distsbr.c -- routines to do additional "dist-style" processing
  *
- * $Id$
- *
  * 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.
@@ -11,6 +9,7 @@
 
 #include <h/mh.h>
 #include <fcntl.h>
+#include <h/utils.h>
 
 static int  hdrfd = NOTOK;
 static int  txtfd = NOTOK;
@@ -29,9 +28,10 @@ int
 distout (char *drft, char *msgnam, char *backup)
 {
     int state;
-    register char *dp, *resent;
+    char *dp, *resent;
     char name[NAMESZ], buffer[BUFSIZ];
-    register FILE *ifp, *ofp;
+    FILE *ifp, *ofp;
+    m_getfld_state_t gstate = 0;
 
     if (rename (drft, strcpy (backup, m_backup (drft))) == NOTOK)
        adios (backup, "unable to rename %s to",drft);
@@ -46,12 +46,11 @@ distout (char *drft, char *msgnam, char *backup)
     lseek (hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
     cpydata (hdrfd, fileno (ofp), msgnam, drft);
 
-    for (state = FLD, resent = NULL;;)
-       switch (state =
-               m_getfld (state, name, buffer, sizeof buffer, ifp)) {
+    for (resent = NULL;;) {
+       int buffersz = sizeof buffer;
+       switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) {
            case FLD: 
            case FLDPLUS: 
-           case FLDEOF: 
                if (uprf (name, "distribute-"))
                    snprintf (name, sizeof(name), "%s%s", "Resent", &name[10]);
                if (uprf (name, "distribution-"))
@@ -65,19 +64,16 @@ distout (char *drft, char *msgnam, char *backup)
                resent = add (buffer, resent);
                fprintf (ofp, "%s: %s", name, buffer);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    resent = add (buffer, resent);
                    fputs (buffer, ofp);
                }
-               if (state == FLDEOF)
-                   goto process;
                break;
 
            case BODY: 
-           case BODYEOF: 
                for (dp = buffer; *dp; dp++)
-                   if (!isspace (*dp)) {
+                   if (!isspace ((unsigned char) *dp)) {
                        advise (NULL, BADTXT, "draft");
                        goto leave_bad;
                    }
@@ -91,7 +87,7 @@ distout (char *drft, char *msgnam, char *backup)
        leave_bad: ;
                fclose (ifp);
                fclose (ofp);
-               unlink (drft);
+               (void) m_unlink (drft);
                if (rename (backup, drft) == NOTOK)
                    adios (drft, "unable to rename %s to", backup);
                return NOTOK;
@@ -99,14 +95,16 @@ distout (char *drft, char *msgnam, char *backup)
            default: 
                adios (NULL, "getfld() returned %d", state);
        }
+    }
 process: ;
+    m_getfld_state_destroy (&gstate);
     fclose (ifp);
     fflush (ofp);
 
     if (!resent) {
        advise (NULL, BADMSG, "draft");
        fclose (ofp);
-       unlink (drft);
+       (void) m_unlink (drft);
        if (rename (backup, drft) == NOTOK)
            adios (drft, "unable to rename %s to", backup);
        return NOTOK;
@@ -129,7 +127,9 @@ ready_msg (char *msgnam)
 {
     int state, out;
     char name[NAMESZ], buffer[BUFSIZ], tmpfil[BUFSIZ];
-    register FILE *ifp, *ofp;
+    FILE *ifp, *ofp;
+    char *cp = NULL;
+    m_getfld_state_t gstate = 0;
 
     if (hdrfd != NOTOK)
        close (hdrfd), hdrfd = NOTOK;
@@ -139,47 +139,49 @@ ready_msg (char *msgnam)
     if ((ifp = fopen (msgnam, "r")) == NULL)
        adios (msgnam, "unable to open message");
 
-    strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
-    if ((hdrfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
-       adios (tmpfil, "unable to re-open temporary file");
+    cp = m_mktemp2(NULL, "dist", &hdrfd, NULL);
+    if (cp == NULL) {
+       adios(NULL, "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");
-    unlink (tmpfil);
+    (void) m_unlink (tmpfil);
 
-    for (state = FLD;;)
-       switch (state =
-               m_getfld (state, name, buffer, sizeof buffer, ifp)) {
+    for (;;) {
+       int buffersz = sizeof buffer;
+       switch (state = m_getfld (&gstate, name, buffer, &buffersz, ifp)) {
            case FLD: 
            case FLDPLUS: 
-           case FLDEOF: 
                if (uprf (name, "resent"))
                    fprintf (ofp, "Prev-");
                fprintf (ofp, "%s: %s", name, buffer);
                while (state == FLDPLUS) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    fputs (buffer, ofp);
                }
-               if (state == FLDEOF)
-                   goto process;
                break;
 
            case BODY: 
-           case BODYEOF: 
                fclose (ofp);
 
-               strncpy (tmpfil, m_tmpfil ("dist"), sizeof(tmpfil));
-               if ((txtfd = open (tmpfil, O_RDWR | O_CREAT | O_TRUNC, 0600)) == NOTOK)
-                   adios (tmpfil, "unable to open temporary file");
+                cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
+                if (cp == NULL) {
+                   adios(NULL, "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");
-               unlink (tmpfil);
+               (void) m_unlink (tmpfil);
                fprintf (ofp, "\n%s", buffer);
                while (state == BODY) {
-                   state = m_getfld (state, name,
-                           buffer, sizeof buffer, ifp);
+                   buffersz = sizeof buffer;
+                   state = m_getfld (&gstate, name, buffer, &buffersz, ifp);
                    fputs (buffer, ofp);
                }
            case FILEEOF: 
@@ -192,7 +194,9 @@ ready_msg (char *msgnam)
            default: 
                adios (NULL, "getfld() returned %d", state);
        }
+    }
 process: ;
+    m_getfld_state_destroy (&gstate);
     fclose (ifp);
     fclose (ofp);
 }