home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / AIX / aixRTOS.c < prev    next >
C/C++ Source or Header  |  1991-09-20  |  4KB  |  147 lines

  1. /*
  2.  * $Id: aixRTOS.c,v 1.1 1991/09/20 17:58:56 mtranle Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that 
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  *
  24. */
  25. #ifdef    AIXrt
  26.  
  27. /***============================= NOTE ============================*
  28.  ***
  29.  ***    The following functions are temporarily fixed until
  30.  ***    AIX 2.2.1 available.
  31.  ***
  32.  ***===============================================================*/
  33.  
  34. #include  <errno.h>
  35. #include  <sys/types.h>
  36. #include  <sys/uio.h>
  37.  
  38. writev (sd, iov, cnt)
  39.     int     sd;
  40.     struct iovec *iov ;
  41.     int     cnt;
  42. {
  43.     int     rc;
  44.     int     i;
  45.  
  46.  
  47. #ifdef OldStuff /* that doesn't work */
  48.     for ( rc = 0; cnt--; ++iov ) {
  49.         i = write( sd, iov->iov_base, iov->iov_len );
  50.         if ( i < 0 ) {
  51.             return( i );
  52.         }
  53.         rc += i;
  54.     }
  55.     return ( rc );
  56.  
  57. #else  NewStuff /* that does works */
  58.  
  59.     int     total ;
  60.  
  61.     /* similar to readv error checking fix               */
  62.     /* probably helps to validate user's array of iovecs */
  63.     if( (cnt <= 0) || (cnt > 16) ) {
  64.         errno = EINVAL;
  65.         return( -1 );
  66.     }
  67.     /* can'g easily check the pointers, but can check the lengths */
  68.     for( i = 0; i < cnt; i++ ) {
  69.         if( iov[i].iov_len < 0 ) {
  70.             errno = EINVAL;
  71.             return( -1 );
  72.         }
  73.     }
  74.     /* more checks to come if you want emulation to be perfect ??? */
  75.  
  76.     /* write up till all user's iovec's are used... */
  77.     for (total = 0; --cnt >= 0; iov++, total += i ) {
  78.     /* for this iovec, read until it is full */
  79.     for( i=0; i < iov->iov_len; i += rc ) {
  80.         rc = write(sd, iov->iov_base + i, iov->iov_len - i);
  81.  
  82.         /* try to remember what has been writen */
  83.  
  84.         if ( rc <= 0 ) {
  85.         if (total==0)
  86.             return ( i == 0 ? rc : i );
  87.         else
  88.             return (total + i);
  89.         }
  90.     }
  91.     }
  92.     return (total);
  93.  
  94. #endif
  95. }
  96.  
  97. readv (fd, iov, iovcnt )
  98.     int     fd;
  99.     struct iovec *iov ;
  100.     int     iovcnt;
  101. {
  102.     int     rc;
  103.     int     n;
  104.     int     total;
  105.  
  106.     /* probably helps to validate user's array of iovecs */
  107.     if( (iovcnt <= 0) || (iovcnt > 16) ) {
  108.         errno = EINVAL;
  109.         return( -1 );
  110.     }
  111.     /* can'g easily check the pointers, but can check the lengths */
  112.     for( n = 0; n < iovcnt; n++ ) {
  113.         if( iov[n].iov_len < 0 ) {
  114.             errno = EINVAL;
  115.             return( -1 );
  116.         }
  117.     }
  118.  
  119.     /* read up till all user's iovec's are used... */
  120.     for (total = 0; --iovcnt >= 0; iov++, total += n ) {
  121.     /* for this iovec, read until it is full */
  122.     for( n=0; n < iov->iov_len; n += rc ) {
  123.         rc = read(fd, iov->iov_base+n, iov->iov_len-n);
  124.         /* if no more to read or error ... return */
  125.         /* note that if readv has read anything, subsequent */
  126.         /* errors must be ignored, or data gets lost */
  127.         if ( rc <= 0 ) {
  128.  
  129. #ifdef OldStuff /* that doesn't work */
  130.  
  131.         return ( total == 0 ? rc : total );
  132.  
  133. #else NewStuff /* that does work */
  134.  
  135.         if (total==0)
  136.            return ( n == 0 ? rc : n );
  137.         else
  138.            return (total + n) ;
  139. #endif
  140.         }
  141.     }
  142.     }
  143.     return (total);
  144. }
  145.  
  146. #endif /* AIXrt */
  147.