home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / dos / io / setmode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  1.5 KB  |  61 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <sys/exceptn.h>
  4. #include <dpmi.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <io.h>
  8.  
  9. #include <libc/dosio.h>
  10. #include <libc/ttyprvt.h>
  11.  
  12. void (*__setmode_stdio_hook)(int fd, int mode); /* BSS to zero */
  13.  
  14. int
  15. setmode(int handle, int mode)
  16. {
  17.   __dpmi_regs regs;
  18.   int oldmode, newmode;
  19.  
  20.   regs.x.ax = 0x4400;
  21.   regs.x.bx = handle;
  22.   regs.x.dx = 0;        /* Clear upper e-word for return */
  23.   __dpmi_int(0x21, ®s);
  24.   if (regs.x.flags & 1)
  25.   {
  26.     errno = __doserr_to_errno(regs.x.ax);
  27.     return -1;
  28.   }
  29.   oldmode = newmode = regs.x.dx;
  30.  
  31.   if (mode & O_BINARY)
  32.     newmode |= 0x20;
  33.   else
  34.     newmode &= ~0x20;
  35.  
  36.   if (__libc_read_termios_hook != NULL
  37.       && (oldmode & 0x80) && (oldmode & 0x03) && handle == 0) /* for termios */
  38.     __djgpp_set_ctrl_c (! (mode & O_BINARY));
  39.   else if (oldmode & 0x80)    /* Only for character dev */
  40.   {
  41.     regs.x.ax = 0x4401;
  42.     regs.x.bx = handle;
  43.     regs.x.dx = newmode & 0xff;           /* Force upper byte zero */
  44.     __dpmi_int(0x21, ®s);
  45.     if (regs.x.flags & 1)
  46.     {
  47.       errno = __doserr_to_errno(regs.x.ax);
  48.       return -1;
  49.     }
  50.     if (handle == 0)
  51.       __djgpp_set_ctrl_c(!(mode & O_BINARY));
  52.   }
  53.  
  54.   oldmode = __file_handle_modes[handle];
  55.   newmode = (oldmode & ~(O_BINARY|O_TEXT)) | (mode & (O_BINARY|O_TEXT));
  56.   __file_handle_set (handle, newmode);
  57.   oldmode &= (O_BINARY|O_TEXT);
  58.  
  59.   return oldmode;
  60. }
  61.