OSDN Git Service

Rename hinfo -> dtable. Name the former dtable array 'fdtab'.
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / pipe.cc
1 /* pipe.cc: pipe for Cygwin.
2
3    Copyright 1996, 1998, 1999, 2000 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <unistd.h>
13 #include <sys/fcntl.h>
14 #include <errno.h>
15
16 static int
17 make_pipe (int fildes[2], unsigned int psize, int mode)
18 {
19   SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
20
21   HANDLE r, w;
22   int  fdr, fdw;
23   SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ?  &sec_none_nih : &sec_none;
24
25   if ((fdr = fdtab.find_unused_handle ()) < 0)
26     set_errno (ENMFILE);
27   else if ((fdw = fdtab.find_unused_handle (fdr + 1)) < 0)
28     set_errno ( ENMFILE);
29   else if (!CreatePipe (&r, &w, sa, psize))
30     __seterrno ();
31   else
32     {
33       fhandler_base *fhr = fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
34       fhandler_base *fhw = fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
35
36       int binmode = mode & O_TEXT ? 0 : 1;
37       fhr->init (r, GENERIC_READ, binmode);
38       fhw->init (w, GENERIC_WRITE, binmode);
39       if (mode & O_NOINHERIT)
40        {
41          fhr->set_close_on_exec_flag (1);
42          fhw->set_close_on_exec_flag (1);
43        }
44
45       fildes[0] = fdr;
46       fildes[1] = fdw;
47
48       debug_printf ("0 = pipe (%p) (%d:%p, %d:%p)", fildes,
49                     fdr, fhr->get_handle (), fdw, fhw->get_handle ());
50
51       ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
52       return 0;
53     }
54
55   syscall_printf ("-1 = pipe (%p)", fildes);
56   ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
57   return -1;
58 }
59
60 extern "C" int
61 pipe (int filedes[2])
62 {
63   return make_pipe (filedes, 16384, (!__fmode || __fmode == O_BINARY) ? O_BINARY : O_TEXT);
64 }
65
66 extern "C" int
67 _pipe (int filedes[2], unsigned int psize, int mode)
68 {
69   int res = make_pipe (filedes, psize, mode);
70   /* This type of pipe is not interruptible so set the appropriate flag. */
71   if (!res)
72     fdtab[filedes[0]]->set_r_no_interrupt (1);
73   return res;
74 }
75
76 int
77 dup (int fd)
78 {
79   int res;
80   SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
81
82   res = dup2 (fd, fdtab.find_unused_handle ());
83
84   ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
85
86   return res;
87 }
88
89 int
90 dup2 (int oldfd, int newfd)
91 {
92   return fdtab.dup2 (oldfd, newfd);
93 }