home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / DUP2.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  2.6 KB  |  97 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - dup2.cas
  3.  *
  4.  * function(s)
  5.  *        dup  - duplicate a file handle
  6.  *        dup2 - duplicate a file handle
  7.  *--------------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #pragma inline
  19. #include <io.h>
  20. #include <_io.h>
  21. #include <RtlData.h>
  22.  
  23. extern  void    (*_exitopen)();
  24. extern  void    _xclose();
  25.  
  26.  
  27. /*--------------------------------------------------------------------------*
  28.  
  29. Name            dup - duplicate a file handle
  30.  
  31. Usage           int   dup(int handle);
  32.                 int   dup2(int oldhandle, int newhandle);
  33.  
  34. Prototype in    io.h
  35.  
  36. Description     dup and dup2 each return a new file handle that has the
  37.                 following in common with the original file handle:
  38.  
  39.                 . same open file or device
  40.                 . same file pointer(that is, changing the file pointer of
  41.                   one changes th other
  42.                 . same access mode(read,write,read/write)
  43.  
  44.                 dup returns the next file handle available; dup2 returns
  45.                 a new handle with the value of newhandle.  if the file
  46.                 associated with newhandle is open when dup2 is called, it
  47.                 is closed.
  48.  
  49. Return value    Upon successful completion, dup returns the new file handle,
  50.                 Otherwise it returns -1.
  51.  
  52.                 dup2 returns 0 on successful completion, -1 otherwise.
  53.  
  54.                 In the event of an error, errno is set to one of the
  55.                 following:
  56.                     EMFILE  Too many open files
  57.                     EBADF   Bad file number
  58.  
  59. *---------------------------------------------------------------------------*/
  60. int dup2 (register int oldhandle, register int newhandle)
  61. {
  62.     _QRTLDataBlock;
  63. asm mov ah,46h
  64. asm mov bx,oldhandle
  65. asm mov cx,newhandle
  66. asm int 21h
  67. asm jc  dup2Failed
  68.  
  69.     _QRTLInstanceData(_openfd) [newhandle] =
  70.         _QRTLInstanceData(_openfd) [oldhandle];
  71.     _exitopen = _xclose;
  72.     return 0;
  73.  
  74. dup2Failed:
  75.     return __IOerror (_AX);
  76. }
  77.  
  78.  
  79. int dup (int handle)
  80. {
  81.     register int     fd;
  82.     _QRTLDataBlock;
  83.  
  84. asm mov ah,45h
  85. asm mov bx,handle
  86. asm int 21h
  87. asm jc  dupFailed
  88.     fd = _AX;
  89.  
  90.     _QRTLInstanceData(_openfd) [fd] = _QRTLInstanceData(_openfd) [handle];
  91.     _exitopen = _xclose;
  92.     return (fd);
  93.  
  94. dupFailed:
  95.     return __IOerror (_AX);
  96. }
  97.