home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / ioctl.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  3KB  |  133 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>
  10. #include <unistd.h>
  11. #include <linea.h>    /* for TIOCGWINSZ under TOS */
  12. #include "lib.h"    /* for __open_stat */
  13.  
  14. extern int __mint;    /* MiNT version */
  15. int _ttydisc = NTTYDISC;
  16. int _ldisc = LLITOUT;
  17.  
  18. /* in read.c */
  19. extern struct tchars __tchars;
  20. extern struct ltchars __ltchars;
  21.  
  22. int ioctl(fd, cmd, arg)
  23.     int fd, cmd;
  24.     void *arg;
  25. {
  26.     long r;
  27.     int istty = isatty(fd);
  28.     struct sgttyb *sg = (struct sgttyb *) arg;
  29.     int null_fd;
  30.  
  31.     if (istty) {
  32.         if (cmd == TIOCGETD) {
  33.             *((int *)arg) = _ttydisc;
  34.             return 0;
  35.         } else if (cmd == TIOCSETD) {
  36.             _ttydisc = *((int *)arg);
  37.             return 0;
  38.         } else if (cmd == TIOCLGET) {
  39.             *((int *)arg) = _ldisc;
  40.             return 0;
  41.         } else if (cmd == TIOCLSET) {
  42.             _ldisc = *((int *)arg);
  43.             return 0;
  44.         } else if (cmd == TIOCSWINSZ && __mint < 9) {
  45.             return 0;
  46.         } else if (cmd == TIOCGWINSZ && __mint < 9) {
  47.             struct winsize *win = (struct winsize *)arg;
  48.  
  49. #ifndef __SOZOBON__
  50.             (void)linea0();
  51. #else /* __SOZOBON__ */
  52.             linea0();
  53. #endif /* __SOZOBON__ */
  54.             win->ws_row = V_CEL_MY + 1;
  55.             win->ws_col = V_CEL_MX + 1;
  56.             win->ws_xpixel = V_X_MAX;
  57.             win->ws_ypixel = V_Y_MAX;
  58.             return 0;
  59.         }
  60. #ifdef __MINT__
  61.         else if (cmd == TIOCNOTTY && __mint) {
  62.             if ((fd < 0) || !(_isctty(fd))) {
  63.                 errno = EBADF;
  64.                 return -1;
  65.             }
  66.             (void) close(fd);
  67.             null_fd = open("/dev/null", O_RDWR | O_NOCTTY);
  68.             (void) Fforce(-1, null_fd);
  69.             __open_stat[__OPEN_INDEX(-1)] =
  70.                 __open_stat[__OPEN_INDEX(null_fd)];
  71.             if (null_fd != fd) {
  72.                 (void) Fforce(fd, null_fd);
  73.                 __open_stat[__OPEN_INDEX(fd)] =
  74.                     __open_stat[__OPEN_INDEX(null_fd)];
  75.                 (void) close(null_fd);
  76.             }
  77.             return 0;
  78.         }
  79. #endif /* __MINT__ */
  80.     }
  81.  
  82.     if (__mint) {
  83.         r = Fcntl(fd, arg, cmd);
  84.     }
  85.     else if (istty) {
  86.         r = 0;
  87.         switch(cmd) {
  88.         case TIOCSETP:
  89.             fd = __OPEN_INDEX(fd);
  90.             if (fd < 0 || fd >= __NHANDLES)
  91.                 fd = __NHANDLES - 1;
  92.             __open_stat[fd].flags = sg->sg_flags;
  93.             break;
  94.         case TIOCGETP:
  95.             fd = __OPEN_INDEX(fd);
  96.             if (fd < 0 || fd >= __NHANDLES)
  97.                 fd = __NHANDLES - 1;
  98.             sg->sg_flags = __open_stat[fd].flags;
  99.             sg->sg_ispeed = sg->sg_ospeed = 0;
  100.             sg->sg_erase = 'H' & 0x1f;
  101.             sg->sg_kill = 'U' & 0x1f;
  102.             break;
  103.         case TIOCGETC:
  104.             *((struct tchars *)arg) = __tchars;
  105.             break;
  106.         case TIOCSETC:
  107.             __tchars = *((struct tchars *)arg);
  108.             break;
  109.         case TIOCGLTC:
  110.             *((struct ltchars *)arg) = __ltchars;
  111.             break;
  112.         case TIOCSLTC:
  113.             __ltchars = *((struct ltchars *)arg);
  114.             break;
  115.         case TIOCGPGRP:
  116.             *((long *)arg) = 0;
  117.             break;
  118.         case TIOCSPGRP:
  119.             break;
  120.         default:
  121.             r = -EINVAL;
  122.         }
  123.     }
  124.     else
  125.         r = -EINVAL;
  126.  
  127.     if (r < 0) {
  128.         errno = (int) -r;
  129.         return -1;
  130.     }
  131.     return (int) r;
  132. }
  133.