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