|
|
1.1 ! root 1: #ident "@(#)cmd4.c 1.3 'attmail mail(1) command'" ! 2: #ident "@(#)mailx:cmd4.c 1.4.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:cmd4.c 1.4" ! 11: ! 12: #include "rcv.h" ! 13: ! 14: /* ! 15: * mailx -- a modified version of a University of California at Berkeley ! 16: * mail program ! 17: * ! 18: * More commands.. ! 19: */ ! 20: ! 21: ! 22: /* ! 23: * pipe messages to cmd. ! 24: */ ! 25: ! 26: dopipe(str) ! 27: char str[]; ! 28: { ! 29: register int *ip, mesg; ! 30: register struct message *mp; ! 31: char *cp, *cmd; ! 32: int f, *msgvec, nowait=0; ! 33: void (*sigint)(), (*sigpipe)(); ! 34: long lc, cc, t; ! 35: register pid_t pid; ! 36: int page, s, pivec[2]; ! 37: char *Shell; ! 38: FILE *pio; ! 39: ! 40: msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec); ! 41: if ((cmd = snarf(str, &f, 0)) == NOSTR) { ! 42: if (f == -1) { ! 43: printf("pipe command error\n"); ! 44: return(1); ! 45: } ! 46: if ( (cmd = value("cmd")) == NOSTR) { ! 47: printf("\"cmd\" not set, ignored.\n"); ! 48: return(1); ! 49: } ! 50: } ! 51: if (!f) { ! 52: *msgvec = first(0, MMNORM); ! 53: if (*msgvec == NULL) { ! 54: printf("No messages to pipe.\n"); ! 55: return(1); ! 56: } ! 57: msgvec[1] = NULL; ! 58: } ! 59: if (f && getmsglist(str, msgvec, 0) < 0) ! 60: return(1); ! 61: if (*(cp=cmd+strlen(cmd)-1)=='&'){ ! 62: *cp=0; ! 63: nowait++; ! 64: } ! 65: printf("Pipe to: \"%s\"\n", cmd); ! 66: flush(); ! 67: ! 68: /* setup pipe */ ! 69: if (pipe(pivec) < 0) { ! 70: perror("pipe"); ! 71: return(0); ! 72: } ! 73: ! 74: if ((pid = fork()) == 0) { ! 75: close(pivec[1]); /* child */ ! 76: fclose(stdin); ! 77: dup(pivec[0]); ! 78: close(pivec[0]); ! 79: if ((Shell = value("SHELL")) == NOSTR || *Shell=='\0') ! 80: Shell = SHELL; ! 81: execlp(Shell, Shell, "-c", cmd, (char *)0); ! 82: perror(Shell); ! 83: _exit(1); ! 84: } ! 85: if (pid == (pid_t)-1) { /* error */ ! 86: perror("fork"); ! 87: close(pivec[0]); ! 88: close(pivec[1]); ! 89: return(0); ! 90: } ! 91: ! 92: close(pivec[0]); /* parent */ ! 93: pio=fdopen(pivec[1],"w"); ! 94: sigint = sigset(SIGINT, SIG_IGN); ! 95: sigpipe = sigset(SIGPIPE, SIG_IGN); ! 96: ! 97: /* send all messages to cmd */ ! 98: page = (value("page")!=NOSTR); ! 99: lc = cc = 0; ! 100: for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { ! 101: mesg = *ip; ! 102: touch(mesg); ! 103: mp = &message[mesg-1]; ! 104: if ((t = send(mp, pio, 0)) < 0) { ! 105: perror(cmd); ! 106: sigset(SIGPIPE, sigpipe); ! 107: sigset(SIGINT, sigint); ! 108: return(1); ! 109: } ! 110: lc += t; ! 111: cc += mp->m_size; ! 112: if (page) putc('\f', pio); ! 113: } ! 114: ! 115: fflush(pio); ! 116: if (ferror(pio)) ! 117: perror(cmd); ! 118: fclose(pio); ! 119: ! 120: /* wait */ ! 121: if (!nowait) { ! 122: while (wait(&s) != pid); ! 123: if (s != 0) ! 124: printf("Pipe to \"%s\" failed\n", cmd); ! 125: } ! 126: if (nowait || s == 0) ! 127: printf("\"%s\" %ld/%ld\n", cmd, lc, cc); ! 128: sigset(SIGPIPE, sigpipe); ! 129: sigset(SIGINT, sigint); ! 130: return(0); ! 131: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.