home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / library / rcs / __fioctl.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  4.4 KB  |  174 lines

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