home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / library / __fselect.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  109 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.  *  __fselect.c,v 1.1.1.1 1994/04/04 04:29:45 amiga Exp
  20.  *
  21.  *  __fselect.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:29:45  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.2  1993/11/05  21:49:09  mw
  26.  *  socket-changes
  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.  
  37. #include "select.h"
  38.  
  39. /* don't use the `normal' packet port for select. We need synchronous
  40.  * notification to be able to Wait() for multiple replies */
  41.  
  42. /*
  43.  * select operation on a "normal" AmigaOS filehandle. Normal means,
  44.  * we only have the WaitForChar() call available to find out, whether
  45.  * a read would block. Write() cannot be caught, so we always allow it.
  46.  * Request for exceptional data is routed to read.
  47.  */
  48.  
  49. int
  50. __fselect (struct file *f, int select_cmd, int io_mode,
  51.        fd_set *ignored, u_long *also_ignored)
  52. {
  53.   int result;
  54.  
  55.   if (select_cmd == SELCMD_PREPARE)
  56.     {
  57.       /* always possible... perhaps return ~0 in this case ???? */
  58.       if (io_mode != SELMODE_IN) return 0;
  59.  
  60.       SelLastError(f) = 0;
  61.  
  62.       if (!SELPKT_IN_USE(f))
  63.         SelSendPacket1(f, __selport, ACTION_WAIT_CHAR, SELTIMEOUT);
  64.       return 1 << __selport->mp_SigBit;
  65.     }
  66.   else if (select_cmd == SELCMD_CHECK)
  67.     {
  68.       /* only read is supported, other modes default to `ok' */
  69.       if (io_mode != SELMODE_IN) return 1;
  70.  
  71.       if (SELPKT_IN_USE(f))
  72.         return 0;
  73.       /* there are two possible answers: error (packet not supported) 
  74.        * and the `real' answer.
  75.        * An error is treated as to allow input, so select() won't block
  76.        * indefinitely...
  77.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  78.        */
  79.       result = SelLastError(f) ? 1 : (SelLastResult(f) & 1);
  80.       /* don't make __write() think its last packet failed.. */
  81.       SelLastError(f) = 0;
  82.       return result;
  83.     }
  84.   else if (select_cmd == SELCMD_POLL)
  85.     {
  86.       /* only read is supported, other modes default to `ok' */
  87.       if (io_mode != SELMODE_IN) return 1;
  88.  
  89.       __get_file (f);
  90.  
  91.       LastError(f) = 0;
  92.       SendPacket1(f, __srwport, ACTION_WAIT_CHAR, 0);
  93.       __wait_sync_packet (&f->f_sp);
  94.       /* there are two possible answers: error (packet not supported) 
  95.        * and the `real' answer.
  96.        * An error is treated as to allow input, so select() won't block
  97.        * indefinitely...
  98.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  99.        */
  100.       result = LastError(f) ? 1 : (LastResult(f) & 1);
  101.       /* don't make __write() think its last packet failed.. */
  102.       LastError(f) = 0;
  103.       __release_file (f);
  104.       return result;
  105.     }
  106.   else
  107.     return 0;
  108. }
  109.