Removed unnecessary null-pointer checks before calling FreeVec() and
[AROS.git] / compiler / clib / pipe.c
blobadd05b11053900269a15baab5c7d7e6866230a05
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 <stdlib.h>
11 #include <fcntl.h>
12 #include <stdio.h> /* For snprintf */
13 #include <errno.h>
15 #include "__fdesc.h"
17 /*****************************************************************************
19 NAME */
20 #include <unistd.h>
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 FreeVec(rfcb);
67 if(rdesc)
68 __free_fdesc(rdesc);
69 FreeVec(wfcb);
70 if(wdesc)
71 __free_fdesc(wdesc);
72 errno = ENOMEM;
73 return -1;
76 /* Get the next pipe number */
77 Forbid();
78 pipeno++;
79 Permit();
81 snprintf(pipe_name, sizeof(pipe_name), "PIPE:cpipe-%08x-%d",
82 (unsigned long)getpid(), pipeno);
83 pipe_name[sizeof(pipe_name)-1] = 0;
85 writer = Open(pipe_name, MODE_NEWFILE);
86 if (writer)
88 reader = Open(pipe_name, MODE_OLDFILE);
89 if (!reader)
91 DeleteFile(pipe_name);
92 Close(writer);
93 writer = BNULL;
97 if (!writer)
99 errno = __arosc_ioerr2errno(IoErr());
100 __free_fdesc(rdesc);
101 __free_fdesc(wdesc);
102 return -1;
105 pipedes[0] = __getfdslot(__getfirstfd(0));
106 rdesc->fdflags = 0;
107 rdesc->fcb = rfcb;
108 rdesc->fcb->fh = reader;
109 rdesc->fcb->flags = O_RDONLY;
110 rdesc->fcb->opencount = 1;
111 __setfdesc(pipedes[0], rdesc);
113 pipedes[1] = __getfdslot(__getfirstfd(pipedes[0]));
114 wdesc->fdflags = 0;
115 wdesc->fcb = wfcb;
116 wdesc->fcb->fh = writer;
117 wdesc->fcb->flags = O_WRONLY;
118 wdesc->fcb->opencount = 1;
119 __setfdesc(pipedes[1], wdesc);
121 return 0;