|
|
1.1 ! root 1: #ident "@(#)edit.c 1.3 'attmail mail(1) command'" ! 2: #ident "@(#)mailx:edit.c 1.8.1.1" ! 3: /* Copyright (c) 1984 AT&T */ ! 4: /* All Rights Reserved */ ! 5: ! 6: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */ ! 7: /* The copyright notice above does not evidence any */ ! 8: /* actual or intended publication of such source code. */ ! 9: ! 10: #ident "@(#)mailx:edit.c 1.8" ! 11: ! 12: #include "rcv.h" ! 13: ! 14: /* ! 15: * mailx -- a modified version of a University of California at Berkeley ! 16: * mail program ! 17: * ! 18: * Perform message editing functions. ! 19: */ ! 20: ! 21: static void edit1(); ! 22: ! 23: /* ! 24: * Edit a message list. ! 25: */ ! 26: ! 27: editor(msgvec) ! 28: int *msgvec; ! 29: { ! 30: char *edname; ! 31: ! 32: if ((edname = value("EDITOR")) == NOSTR) ! 33: edname = EDITOR; ! 34: edit1(msgvec, edname); ! 35: return(0); ! 36: } ! 37: ! 38: /* ! 39: * Invoke the visual editor on a message list. ! 40: */ ! 41: ! 42: visual(msgvec) ! 43: int *msgvec; ! 44: { ! 45: char *edname; ! 46: ! 47: if ((edname = value("VISUAL")) == NOSTR) ! 48: edname = VISUAL; ! 49: edit1(msgvec, edname); ! 50: return(0); ! 51: } ! 52: ! 53: /* ! 54: * Edit a message by writing the message into a funnily-named file ! 55: * (which should not exist) and forking an editor on it. ! 56: * We get the editor from the stuff above. ! 57: */ ! 58: ! 59: static void ! 60: edit1(msgvec, ed) ! 61: int *msgvec; ! 62: char *ed; ! 63: { ! 64: register int c; ! 65: pid_t pid; ! 66: int *ip, mesg; ! 67: long ms, lines; ! 68: void (*sigint)(), (*sigquit)(); ! 69: FILE *ibuf, *obuf; ! 70: struct message *mp; ! 71: off_t size; ! 72: struct stat statb; ! 73: long modtime; ! 74: ! 75: /* ! 76: * Set signals; locate editor. ! 77: */ ! 78: ! 79: sigint = sigset(SIGINT, SIG_IGN); ! 80: sigquit = sigset(SIGQUIT, SIG_IGN); ! 81: ed = expand(ed); ! 82: ! 83: /* ! 84: * Deal with each message to be edited . . . ! 85: */ ! 86: ! 87: for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { ! 88: mesg = *ip; ! 89: mp = &message[mesg-1]; ! 90: if (mp->m_text) { ! 91: mp->m_flag |= MODIFY; ! 92: ! 93: if (!access(tempZedit, 2)) { ! 94: printf("%s: file exists\n", tempZedit); ! 95: goto out; ! 96: } ! 97: ! 98: /* ! 99: * Copy the message into the edit file. ! 100: */ ! 101: ! 102: close(creat(tempZedit, TEMPPERM)); ! 103: if ((obuf = fopen(tempZedit, "w")) == NULL) { ! 104: perror(tempZedit); ! 105: goto out; ! 106: } ! 107: if (send(mp, obuf, 0) < 0) { ! 108: perror(tempZedit); ! 109: fclose(obuf); ! 110: removefile(tempZedit); ! 111: goto out; ! 112: } ! 113: fflush(obuf); ! 114: if (ferror(obuf)) { ! 115: removefile(tempZedit); ! 116: fclose(obuf); ! 117: goto out; ! 118: } ! 119: fclose(obuf); ! 120: ! 121: /* ! 122: * If we are in read only mode, make the ! 123: * temporary message file readonly as well. ! 124: */ ! 125: ! 126: if (readonly) ! 127: chmod(tempZedit, 0400); ! 128: ! 129: /* ! 130: * Fork/execl the editor on the edit file. ! 131: */ ! 132: ! 133: if (stat(tempZedit, &statb) < 0) ! 134: modtime = 0; ! 135: modtime = statb.st_mtime; ! 136: pid = fork(); ! 137: if (pid == (pid_t)-1) { ! 138: perror("fork"); ! 139: removefile(tempZedit); ! 140: goto out; ! 141: } ! 142: if (pid == 0) { ! 143: sigchild(); ! 144: if (sigint != (void (*)())SIG_IGN) /* adb */ ! 145: sigset(SIGINT, SIG_DFL); ! 146: if (sigquit != (void (*)())SIG_IGN) /* adb */ ! 147: sigset(SIGQUIT, SIG_DFL); ! 148: execlp(ed, ed, tempZedit, (char *)0); ! 149: perror(ed); ! 150: _exit(1); ! 151: } ! 152: while (wait(&mesg) != pid) ! 153: ; ! 154: ! 155: /* ! 156: * If in read only mode, just remove the editor ! 157: * temporary and return. ! 158: */ ! 159: ! 160: if (readonly) { ! 161: removefile(tempZedit); ! 162: continue; ! 163: } ! 164: ! 165: /* ! 166: * Now copy the message to the end of the ! 167: * temp file. ! 168: */ ! 169: ! 170: if (stat(tempZedit, &statb) < 0) { ! 171: perror(tempZedit); ! 172: goto out; ! 173: } ! 174: if (modtime == statb.st_mtime) { ! 175: removefile(tempZedit); ! 176: goto out; ! 177: } ! 178: if ((ibuf = fopen(tempZedit, "r")) == NULL) { ! 179: perror(tempZedit); ! 180: removefile(tempZedit); ! 181: goto out; ! 182: } ! 183: removefile(tempZedit); ! 184: fseek(otf, (long) 0, 2); ! 185: size = fsize(otf); ! 186: mp->m_block = blockof(size); ! 187: mp->m_offset = offsetofaddr(size); ! 188: ms = 0L; ! 189: lines = 0; ! 190: while ((c = getc(ibuf)) != EOF) { ! 191: if (c == '\n') ! 192: lines++; ! 193: putc(c, otf); ! 194: if (ferror(otf)) ! 195: break; ! 196: ms++; ! 197: } ! 198: mp->m_size = ms; ! 199: mp->m_lines = lines; ! 200: if (ferror(otf)) ! 201: perror("/tmp"); ! 202: fclose(ibuf); ! 203: } else { ! 204: printf("\n%s\n", binmsg); ! 205: } ! 206: } ! 207: ! 208: /* ! 209: * Restore signals and return. ! 210: */ ! 211: ! 212: out: ! 213: sigset(SIGINT, sigint); ! 214: sigset(SIGQUIT, sigquit); ! 215: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.