arch/m68k-amiga: Define the gcc symbol 'start' instead of using .bss
[AROS.git] / compiler / clib / pipe.c
blob99dc3fc47857832b79b8c6add3003a5d2fece36f
1 /*
2 Copyright © 1995-2011, 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 <stdlib.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <stdio.h> /* For snprintf */
15 #include "__errno.h"
16 #include "__fdesc.h"
18 /*****************************************************************************
20 NAME */
22 int pipe(
24 /* SYNOPSIS */
25 int *pipedes)
27 /* FUNCTION
29 INPUTS
31 RESULT
33 NOTES
35 EXAMPLE
37 BUGS
39 SEE ALSO
41 INTERNALS
43 ******************************************************************************/
45 BPTR reader, writer;
46 fcb *rfcb = NULL, *wfcb = NULL;
47 fdesc *rdesc = NULL, *wdesc = NULL;
48 /* PIPE:cpipe-%08x-%d, where %x is the getpid(), %d is the nth pipe */
49 char pipe_name[5 + 6 + 8 + 1 + 16 + 1];
50 static int pipeno = 0;
52 if (!pipedes)
54 errno = EFAULT;
56 return -1;
59 if (
60 (rfcb = AllocVec(sizeof(fcb), MEMF_ANY | MEMF_CLEAR)) == NULL ||
61 (rdesc = __alloc_fdesc()) == NULL ||
62 (wfcb = AllocVec(sizeof(fcb), MEMF_ANY | MEMF_CLEAR)) == NULL ||
63 (wdesc = __alloc_fdesc()) == NULL
66 if(rfcb)
67 FreeVec(rfcb);
68 if(rdesc)
69 __free_fdesc(rdesc);
70 if(wfcb)
71 FreeVec(wfcb);
72 if(wdesc)
73 __free_fdesc(wdesc);
74 errno = ENOMEM;
75 return -1;
78 /* Get the next pipe number */
79 Forbid();
80 pipeno++;
81 Permit();
83 snprintf(pipe_name, sizeof(pipe_name), "PIPE:cpipe-%08x-%d",
84 (unsigned long)getpid(), pipeno);
85 pipe_name[sizeof(pipe_name)-1] = 0;
87 writer = Open(pipe_name, MODE_NEWFILE);
88 if (writer)
90 reader = Open(pipe_name, MODE_OLDFILE);
91 if (!reader)
93 DeleteFile(pipe_name);
94 Close(writer);
95 writer = BNULL;
99 if (!writer)
101 errno = IoErr2errno(IoErr());
102 __free_fdesc(rdesc);
103 __free_fdesc(wdesc);
104 return -1;
107 pipedes[0] = __getfdslot(__getfirstfd(0));
108 rdesc->fdflags = 0;
109 rdesc->fcb = rfcb;
110 rdesc->fcb->fh = reader;
111 rdesc->fcb->flags = O_RDONLY;
112 rdesc->fcb->opencount = 1;
113 __setfdesc(pipedes[0], rdesc);
115 pipedes[1] = __getfdslot(__getfirstfd(pipedes[0]));
116 wdesc->fdflags = 0;
117 wdesc->fcb = wfcb;
118 wdesc->fcb->fh = writer;
119 wdesc->fcb->flags = O_WRONLY;
120 wdesc->fcb->opencount = 1;
121 __setfdesc(pipedes[1], wdesc);
123 return 0;