home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / unistd / dup2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-29  |  608 b   |  29 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <dpmi.h>
  6. #include <errno.h>
  7. #include <io.h>
  8. #include <libc/dosio.h>
  9.  
  10. int
  11. dup2(int fd, int newfd)
  12. {
  13.   __dpmi_regs r;
  14.   if (fd == newfd)
  15.     return newfd;
  16.   __file_handle_set(newfd, __file_handle_modes[fd] ^ (O_BINARY|O_TEXT));
  17.   r.h.ah = 0x46;
  18.   r.x.bx = fd;
  19.   r.x.cx = newfd;
  20.   __dpmi_int(0x21, &r);
  21.   if (r.x.flags & 1)
  22.   {
  23.     errno = __doserr_to_errno(r.x.ax);
  24.     return -1;
  25.   }
  26.   setmode(newfd, __file_handle_modes[fd]);
  27.   return newfd;
  28. }
  29.