From: "Mark E." To: djgpp-workers AT delorie DOT com Date: Fri, 23 Mar 2001 00:37:10 -0500 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: pipe() emulation Message-ID: <3ABA9AB6.9970.50AA59@localhost> X-mailer: Pegasus Mail for Win32 (v3.12c) Reply-To: djgpp-workers AT delorie DOT com After looking over the POSIX docs for pipe, I took out the code that moved up the handles since it goes against at least two sentences in the draft standard. I also added docs. To make the emulation more complete, I could even add code to prevent lseek from working with files opened with pipe since POSIX explicitly forbids that. Index: pipe.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/posix/unistd/pipe.c,v retrieving revision 1.1 diff -c -p -r1.1 pipe.c *** pipe.c 1995/04/01 23:49:02 1.1 --- pipe.c 2001/03/23 05:32:46 *************** *** 1,10 **** /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include ! #include int ! pipe(int _fildes[2]) { ! errno = EACCES; ! return -1; } --- 1,34 ---- + /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #include ! #include ! #include ! #include ! #include + /* Emulate a pipe using a temporary file. */ int ! pipe(int fildes[2]) { ! int ifd, ofd; ! char temp_name[FILENAME_MAX + 1]; ! char *tname; ! ! tname = tmpnam(temp_name); ! if (tname == NULL) ! return -1; ! ! ofd = open(tname, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND | O_TEMPORARY, ! S_IWUSR); ! if (ofd < 0) ! return -1; ! ! ifd = open(tname, O_RDONLY | O_TEMPORARY); ! if (ifd < 0) ! return -1; ! ! fildes[0] = ifd; ! fildes[1] = ofd; ! ! return 0; } Index: pipe.txh =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/posix/unistd/pipe.txh,v retrieving revision 1.2 diff -c -p -r1.2 pipe.txh *** pipe.txh 1998/09/27 15:22:26 1.2 --- pipe.txh 2001/03/23 05:32:56 *************** int pipe(int fildes[2]); *** 9,17 **** @subheading Description ! This function is provided only to assist in porting from Unix. It ! always returns an error condition. @subheading Portability --- 9,27 ---- @subheading Description ! This function creates a pipe and places a file descriptor for the read end ! of the pipe in @var{fildes[0]}, and another for the write end in ! @var{fildes[1]}. Data written to @var{fildes[1]} will be read from ! @var{fildes[0]} in a first-in first-out (FIFO) basis. + Note that DOS doesn't support pipes so they are emulated with temporary + files. This function should only be used in porting Unix and POSIX programs + that use it. + + @subheading Return Value + + Zero for success, otherwise -1 is returned and @var{errno} is set to indicate + the error. @subheading Portability