move the defines to the resource header and include that where necessary.
[AROS.git] / compiler / posixc / pipe.c
blobf41f45515c0c846f2ecd634d4ef2072492146f48
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <exec/exec.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <errno.h>
14 #include "__fdesc.h"
16 /*****************************************************************************
18 NAME */
19 #include <unistd.h>
21 int pipe(
23 /* SYNOPSIS */
24 int *pipedes)
26 /* FUNCTION
28 INPUTS
30 RESULT
32 NOTES
34 EXAMPLE
36 BUGS
38 SEE ALSO
40 INTERNALS
42 ******************************************************************************/
44 BPTR reader, writer;
45 fcb *rfcb = NULL, *wfcb = NULL;
46 fdesc *rdesc = NULL, *wdesc = NULL;
47 /* PIPE:cpipe-%08x-%d, where %x is the getpid(), %d is the nth pipe */
48 char pipe_name[5 + 6 + 8 + 1 + 16 + 1];
49 static int pipeno = 0;
51 if (!pipedes)
53 errno = EFAULT;
55 return -1;
58 if (
59 (rfcb = AllocVec(sizeof(fcb), MEMF_ANY | MEMF_CLEAR)) == NULL ||
60 (rdesc = __alloc_fdesc()) == NULL ||
61 (wfcb = AllocVec(sizeof(fcb), MEMF_ANY | MEMF_CLEAR)) == NULL ||
62 (wdesc = __alloc_fdesc()) == NULL
65 FreeVec(rfcb);
66 if(rdesc)
67 __free_fdesc(rdesc);
68 FreeVec(wfcb);
69 if(wdesc)
70 __free_fdesc(wdesc);
71 errno = ENOMEM;
72 return -1;
75 /* Get the next pipe number */
76 Forbid();
77 pipeno++;
78 Permit();
80 snprintf(pipe_name, sizeof(pipe_name), "PIPE:cpipe-%08lx-%d",
81 (unsigned long)getpid(), pipeno);
82 pipe_name[sizeof(pipe_name)-1] = 0;
84 writer = Open(pipe_name, MODE_NEWFILE);
85 if (writer)
87 reader = Open(pipe_name, MODE_OLDFILE);
88 if (!reader)
90 DeleteFile(pipe_name);
91 Close(writer);
92 writer = BNULL;
96 if (!writer)
98 errno = __stdc_ioerr2errno(IoErr());
99 __free_fdesc(rdesc);
100 __free_fdesc(wdesc);
101 return -1;
104 pipedes[0] = __getfdslot(__getfirstfd(0));
105 rdesc->fdflags = 0;
106 rdesc->fcb = rfcb;
107 rdesc->fcb->handle = reader;
108 rdesc->fcb->flags = O_RDONLY;
109 rdesc->fcb->opencount = 1;
110 __setfdesc(pipedes[0], rdesc);
112 pipedes[1] = __getfdslot(__getfirstfd(pipedes[0]));
113 wdesc->fdflags = 0;
114 wdesc->fcb = wfcb;
115 wdesc->fcb->handle = writer;
116 wdesc->fcb->flags = O_WRONLY;
117 wdesc->fcb->opencount = 1;
118 __setfdesc(pipedes[1], wdesc);
120 return 0;