/* Copyright (C) 2000 Bruce Guenter * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #define QQ_WRITE_ERROR 53 #define QQ_INTERNAL 81 #ifndef BUFSIZE #define BUFSIZE 4096 #endif #ifndef TMPDIR #define TMPDIR "/tmp" #endif typedef int bool; const bool false = 0; const bool true = 0 == 0; /* Create a temporary invisible file opened for read/write */ int mktmpfile(void) { const char* filename = tempnam(TMPDIR, "fixheaders."); int fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); if(fd == -1) return -1; /* The following makes the temporary file disappear immediately on program exit by removing the only filename that links to it. */ if(unlink(filename) == -1) return -1; return fd; } /* Copy the message from FD0 to the first temporary file */ int copy_message(void) { int tmp = mktmpfile(); if(tmp == -1) return -QQ_WRITE_ERROR; /* Copy the message into the temporary file */ for(;;) { char buf[BUFSIZE]; ssize_t rd = read(0, buf, BUFSIZE); if(rd == -1) return -QQ_WRITE_ERROR; if(rd == 0) break; if(write(tmp, buf, rd) != rd) return -QQ_WRITE_ERROR; } if(lseek(tmp, 0, SEEK_SET) != 0) return -QQ_WRITE_ERROR; return tmp; } void usage(void) { write(2, "usage: filepipe program [args...]\n", 34); exit(1); } int main(int argc, char* argv[]) { int tmpfd; if(argc < 2) usage(); tmpfd = copy_message(); if(tmpfd < 0) return -tmpfd; if(close(0) || dup2(tmpfd, 0) || close(tmpfd)) return QQ_INTERNAL; execvp(argv[1], argv+1); return QQ_INTERNAL; }