home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / __fioctl.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  156 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __fioctl.c,v 1.1.1.1 1994/04/04 04:29:44 amiga Exp
  20.  *
  21.  *  __fioctl.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:29:44  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/17  21:00:51  mwild
  26.  *  Initial revision
  27.  *
  28.  * Revision 1.1  1992/05/14  19:55:40  mwild
  29.  * Initial revision
  30.  *
  31.  */
  32.  
  33. #define _KERNEL
  34. #include "ixemul.h"
  35. #include "kprintf.h"
  36. #include <sys/ioctl.h>
  37.  
  38. extern int __read(), __aread(), __write(), __awrite();
  39.  
  40. /* IOCTLs on files in general */
  41.  
  42. int
  43. __fioctl(struct file *f, unsigned int cmd, unsigned int inout,
  44.          unsigned int arglen, unsigned int arg)
  45. {
  46.   int omask;
  47.   int result = 0;
  48.   int err=errno;
  49.   
  50.   omask = syscall (SYS_sigsetmask, ~0);
  51.   __get_file (f);
  52.  
  53.   switch (cmd)
  54.     {
  55.     case FIONREAD:
  56.       {
  57.     unsigned int *pt = (unsigned int *)arg;
  58.     if (!f->f_fh->fh_Port)
  59.       {
  60.         int this_pos, eof_pos;
  61.         /* so this must be some file-oriented device, could be
  62.          * a pipe, could be a normal file. Lets try to seek to
  63.          * the eof, if we can, we know, how many characters there
  64.          * are to be read. */
  65.         SendPacket3(f,__srwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_CURRENT);
  66.         __wait_sync_packet (&f->f_sp);
  67.         this_pos = LastResult (f);
  68.         if (LastError(f) == ERROR_ACTION_NOT_KNOWN) this_pos = -1;
  69.  
  70.         if (this_pos >= 0)
  71.           {
  72.         /* fine, the device seems at least to understand the
  73.          * Seek-Packet */
  74.         SendPacket3(f,__srwport,ACTION_SEEK,f->f_fh->fh_Arg1,0,OFFSET_END);
  75.         __wait_sync_packet (&f->f_sp);
  76.         eof_pos = LastResult (f);
  77.         /* shouldn't happen here, I'm paranoid... */
  78.             if (LastError(f) == ERROR_ACTION_NOT_KNOWN) eof_pos = -1;
  79.  
  80.         /* since this was a real seek, the device could have
  81.          * signaled an error, if it just can't seek .. */
  82.         if (eof_pos >= 0)
  83.           {
  84.             *pt = eof_pos - this_pos;
  85.             SendPacket3(f,__srwport,ACTION_SEEK,f->f_fh->fh_Arg1,this_pos,OFFSET_BEGINNING);
  86.             __wait_sync_packet (&f->f_sp);
  87.             LastResult(f) = 0;
  88.             result = 0;
  89.             goto ret;
  90.           }
  91.           }
  92.         /* well, since the device can't seek, AND it's not        
  93.          * interactive, chances are bad, we ever will get at the
  94.          * right result, but we'll try nevertheless the WaitForChar
  95.          * Packet, it can only fail... */
  96.       }
  97.     /* if the docs would all speak the same language... some
  98.      * say, that the timeout should be in 1/50s, others say
  99.      * its actually in micro/s.. who knows.. */
  100.     SendPacket1(f, __srwport, ACTION_WAIT_CHAR, 0);
  101.     __wait_sync_packet (&f->f_sp);
  102.  
  103.     *pt = LastResult(f) ? 1 : 0;
  104.     LastResult(f) = 0;
  105.     result = 0;
  106.         goto ret;
  107.       }
  108.  
  109.     case FIONBIO:
  110.       {
  111.     /* that's probably the most inefficient part of the whole
  112.      * library... */
  113.     result = f->f_flags & FNDELAY ? 1 : 0;
  114.     if (*(unsigned int *)arg)
  115.       f->f_flags |= FNDELAY;
  116.     else
  117.       f->f_flags &= ~FNDELAY;
  118.     /* I didn't find it documented in a manpage, but I assume, we
  119.      * should return the former state, not just zero.. */
  120.     goto ret;
  121.       }
  122.  
  123.     case FIOASYNC:
  124.       {
  125.     /* DOESN'T WORK YET */
  126.  
  127.     int flags = *(unsigned long*)arg;
  128.     result = f->f_flags & FASYNC ? 1 : 0;
  129.     if (flags)
  130.       f->f_flags |= FASYNC;
  131.     else
  132.       f->f_flags &= ~FASYNC;
  133.  
  134.     /* ATTENTION: have to call some function here in the future !!! */
  135.  
  136.     /* I didn't find it documented in a manpage, but I assume, we
  137.      * should return the former state, not just zero.. */
  138.     goto ret;
  139.       }
  140.  
  141.     case FIOCLEX:
  142.     case FIONCLEX:
  143.     case FIOSETOWN:
  144.     case FIOGETOWN:
  145.       /* this is no error, but nevertheless we don't take any actions.. */      
  146.       result = 0; goto ret;
  147.     }
  148.  
  149. ret:
  150.     __release_file (f);
  151.     syscall (SYS_sigsetmask, omask);
  152.     errno = err;
  153.     KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  154.     return result;
  155. }
  156.