home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / DUP2.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  2.6 KB  |  94 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. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19. #pragma inline
  20. #include <io.h>
  21. #include <_io.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. asm    mov    ah,46h
  63. asm    mov    bx,oldhandle
  64. asm    mov    cx,newhandle
  65. asm    int    21h
  66. asm    jc    dup2Failed
  67.  
  68.     _openfd [newhandle] = _openfd [oldhandle];
  69.     _exitopen = _xclose;
  70.     return 0;
  71.  
  72. dup2Failed:
  73.     return __IOerror (_AX);
  74. }
  75.  
  76.  
  77. int dup (int handle)
  78. {
  79.     register int     fd;
  80.  
  81. asm    mov    ah,45h
  82. asm    mov    bx,handle
  83. asm    int    21h
  84. asm    jc    dupFailed
  85.     fd = _AX;
  86.  
  87.     _openfd [fd] = _openfd [handle];
  88.     _exitopen = _xclose;
  89.     return (fd);
  90.  
  91. dupFailed:
  92.     return __IOerror (_AX);
  93. }
  94.