2 ** distsbr.c -- routines to do additional "dist-style" processing
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
16 static int hdrfd = NOTOK;
17 static int txtfd = NOTOK;
22 static int ready_msg(char *);
25 distout(char *drft, char *msgnam, char *backup)
28 struct field f = {{0}};
33 strcpy(backup, m_mktemp(toabsdir(invo_name), NULL, NULL));
34 if (rename(drft, backup) == NOTOK) {
35 advise(backup, "unable to rename %s to",drft);
38 if (!(ifp = fopen(backup, "r"))) {
39 advise(backup, "unable to read");
43 if (!(ofp = fopen(drft, "w"))) {
44 advise(drft, "unable to create temporary file");
47 chmod(drft, m_gmprot());
49 if (ready_msg(msgnam) != OK) {
52 lseek(hdrfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
53 cpydata(hdrfd, fileno(ofp), msgnam, drft);
57 switch (state = m_getfld2(state, &f, ifp)) {
59 if (!uprf(f.name, "resent")) {
60 advise(NULL, "Please re-edit draft to remove the ``%s'' header.", f.name);
64 fprintf(ofp, "%s: %s", f.name, f.value);
68 for (dp = f.value; *dp; dp++) {
70 advise(NULL, "Please re-edit draft to consist of headers only.");
81 advise(NULL, "Please re-edit draft and fix that header.");
86 if (rename(backup, drft) == NOTOK) {
87 advise(drft, "unable to rename %s to", backup);
92 advise(NULL, "getfld() returned %d", state);
102 advise(NULL, "Please re-edit draft to include a ``Resent-To:'' header.");
105 if (rename(backup, drft) == NOTOK) {
106 advise(drft, "unable to rename %s to", backup);
111 if (txtfd != NOTOK) {
112 lseek(txtfd, (off_t) 0, SEEK_SET); /* msgnam not accurate */
113 cpydata(txtfd, fileno(ofp), msgnam, drft);
123 ready_msg(char *msgnam)
126 struct field f = {{0}};
132 if (hdrfd != NOTOK) {
136 if (txtfd != NOTOK) {
140 if (!(ifp = fopen(msgnam, "r"))) {
141 advise(msgnam, "unable to open message");
145 cp = m_mktemp2(NULL, "dist", &hdrfd, NULL);
147 advise("distsbr", "unable to create temporary file");
151 strncpy(tmpfil, cp, sizeof(tmpfil));
152 if ((out = dup(hdrfd)) == NOTOK || !(ofp = fdopen(out, "w"))) {
153 advise(NULL, "no file descriptors -- you lose big");
160 state = m_getfld2(state, &f, ifp);
163 if (uprf(f.name, "resent")) {
164 fprintf(ofp, "Prev-");
166 fprintf(ofp, "%s: %s", f.name, f.value);
172 cp = m_mktemp2(NULL, "dist", &txtfd, NULL);
174 advise("distsbr", "unable to create temp file");
178 strncpy(tmpfil, cp, sizeof(tmpfil));
179 if ((out = dup(txtfd)) == NOTOK ||
180 !(ofp = fdopen(out, "w"))) {
181 advise(NULL, "no file descriptors -- you lose");
185 fprintf(ofp, "\n%s", f.value);
186 while (state == BODY2) {
187 state = m_getfld2(state, &f, ifp);
198 advise(NULL, "format error in message %s", msgnam);
202 advise(NULL, "getfld() returned %d", state);