home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / libnix-0.7-src.lha / libnix-0.7 / sources / nix / stdio / fwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  876 b   |  42 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. extern void __chkabort(void);
  7. extern int __fflush(FILE *stream);
  8.  
  9. size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream)
  10. {
  11.   signed long subsize;
  12.   unsigned long total;
  13.   unsigned char *b;
  14.   __chkabort();
  15.   if (!(total=size*nmemb))
  16.     return 0;
  17.   b=(unsigned char *)ptr;
  18.   if(stream->flags&64) /* Error on stream */
  19.   { errno=EPERM;
  20.     return 0; }
  21.   if(stream->flags&8)
  22.   { if(__fflush(stream))
  23.       return 0;
  24.   }else if(stream->flags&4)
  25.   {
  26.     stream->incount=0; /* throw away input buffer */
  27.     stream->tmpp=NULL;
  28.     stream->flags&=~4;
  29.   }
  30.   while(total)
  31.   { /* write buffer */
  32.     if((subsize=write(stream->file,b,total))<0)
  33.     {
  34.       stream->flags|=64; /* error flag */
  35.       break;
  36.     }
  37.     total=total-subsize;
  38.     b=b+subsize;
  39.   }
  40.   return (b-(unsigned char *)ptr)/size;
  41. }
  42.