home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / mntlib16.lzh / MNTLIB16 / IOCTL.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  2KB  |  107 lines

  1. /*
  2.  * ioctl() emulation for MiNT; written by Eric R. Smith and placed
  3.  * in the public domain
  4.  */
  5.  
  6. #include <errno.h>
  7. #include <mintbind.h>
  8. #include <ioctl.h>
  9. #include <fcntl.h>    /* for __open_stat */
  10. #include <unistd.h>
  11. #include <linea.h>    /* for TIOCGWINSZ under TOS */
  12.  
  13. extern int __mint;    /* MiNT version */
  14. int _ttydisc = NTTYDISC;
  15. int _ldisc = LLITOUT;
  16.  
  17. /* in read.c */
  18. extern struct tchars __tchars;
  19. extern struct ltchars __ltchars;
  20.  
  21. int ioctl(fd, cmd, arg)
  22.     int fd, cmd;
  23.     void *arg;
  24. {
  25.     long r;
  26.     int istty = isatty(fd);
  27.     struct sgttyb *sg = arg;
  28.  
  29.     if (istty) {
  30.         if (cmd == TIOCGETD) {
  31.             *((int *)arg) = _ttydisc;
  32.             return 0;
  33.         } else if (cmd == TIOCSETD) {
  34.             _ttydisc = *((int *)arg);
  35.             return 0;
  36.         } else if (cmd == TIOCLGET) {
  37.             *((int *)arg) = _ldisc;
  38.             return 0;
  39.         } else if (cmd == TIOCLSET) {
  40.             _ldisc = *((int *)arg);
  41.             return 0;
  42.         } else if (cmd == TIOCSWINSZ && __mint < 9) {
  43.             return 0;
  44.         } else if (cmd == TIOCGWINSZ && __mint < 9) {
  45.             struct winsize *win = (struct winsize *)arg;
  46.  
  47.             (void)linea0();
  48.             win->ws_row = V_CEL_MY + 1;
  49.             win->ws_col = V_CEL_MX + 1;
  50.             win->ws_xpixel = V_X_MAX;
  51.             win->ws_ypixel = V_Y_MAX;
  52.             return 0;
  53.         }
  54.     }
  55.  
  56.     if (__mint) {
  57.         r = Fcntl(fd, arg, cmd);
  58.     }
  59.     else if (istty) {
  60.         r = 0;
  61.         switch(cmd) {
  62.         case TIOCSETP:
  63.             fd = __OPEN_INDEX(fd);
  64.             if (fd < 0 || fd >= __NHANDLES)
  65.                 fd = __NHANDLES - 1;
  66.             __open_stat[fd].flags = sg->sg_flags;
  67.             break;
  68.         case TIOCGETP:
  69.             fd = __OPEN_INDEX(fd);
  70.             if (fd < 0 || fd >= __NHANDLES)
  71.                 fd = __NHANDLES - 1;
  72.             sg->sg_flags = __open_stat[fd].flags;
  73.             sg->sg_ispeed = sg->sg_ospeed = 0;
  74.             sg->sg_erase = 'H' & 0x1f;
  75.             sg->sg_kill = 'U' & 0x1f;
  76.             break;
  77.         case TIOCGETC:
  78.             *((struct tchars *)arg) = __tchars;
  79.             break;
  80.         case TIOCSETC:
  81.             __tchars = *((struct tchars *)arg);
  82.             break;
  83.         case TIOCGLTC:
  84.             *((struct ltchars *)arg) = __ltchars;
  85.             break;
  86.         case TIOCSLTC:
  87.             __ltchars = *((struct ltchars *)arg);
  88.             break;
  89.         case TIOCGPGRP:
  90.             *((long *)arg) = 0;
  91.             break;
  92.         case TIOCSPGRP:
  93.             break;
  94.         default:
  95.             r = -EINVAL;
  96.         }
  97.     }
  98.     else
  99.         r = -EINVAL;
  100.  
  101.     if (r < 0) {
  102.         errno = -r;
  103.         return -1;
  104.     }
  105.     return r;
  106. }
  107.