Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. /*
  19. FUNCTION
  20. <<fwrite>>---write array elements
  21.  
  22. INDEX
  23.         fwrite
  24. INDEX
  25.         _fwrite_r
  26.  
  27. ANSI_SYNOPSIS
  28.         #include <stdio.h>
  29.         size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
  30.                       size_t <[count]>, FILE *restrict <[fp]>);
  31.  
  32.         #include <stdio.h>
  33.         size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
  34.                       size_t <[count]>, FILE *restrict <[fp]>);
  35.  
  36. TRAD_SYNOPSIS
  37.         #include <stdio.h>
  38.         size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
  39.         char *<[buf]>;
  40.         size_t <[size]>;
  41.         size_t <[count]>;
  42.         FILE *<[fp]>;
  43.  
  44.         #include <stdio.h>
  45.         size_t _fwrite_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
  46.         struct _reent *<[ptr]>;
  47.         char *<[buf]>;
  48.         size_t <[size]>;
  49.         size_t <[count]>;
  50.         FILE *<[fp]>;
  51.  
  52. DESCRIPTION
  53. <<fwrite>> attempts to copy, starting from the memory location
  54. <[buf]>, <[count]> elements (each of size <[size]>) into the file or
  55. stream identified by <[fp]>.  <<fwrite>> may copy fewer elements than
  56. <[count]> if an error intervenes.
  57.  
  58. <<fwrite>> also advances the file position indicator (if any) for
  59. <[fp]> by the number of @emph{characters} actually written.
  60.  
  61. <<_fwrite_r>> is simply the reentrant version of <<fwrite>> that
  62. takes an additional reentrant structure argument: <[ptr]>.
  63.  
  64. RETURNS
  65. If <<fwrite>> succeeds in writing all the elements you specify, the
  66. result is the same as the argument <[count]>.  In any event, the
  67. result is the number of complete elements that <<fwrite>> copied to
  68. the file.
  69.  
  70. PORTABILITY
  71. ANSI C requires <<fwrite>>.
  72.  
  73. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  74. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  75. */
  76.  
  77. #if defined(LIBC_SCCS) && !defined(lint)
  78. static char sccsid[] = "%W% (Berkeley) %G%";
  79. #endif /* LIBC_SCCS and not lint */
  80.  
  81. #include <_ansi.h>
  82. #include <stdio.h>
  83. #include <string.h>
  84. #if 0
  85. #include <sys/stdc.h>
  86. #endif
  87. #include "local.h"
  88. #if 1
  89. #include "fvwrite.h"
  90. #endif
  91.  
  92. /*
  93.  * Write `count' objects (each size `size') from memory to the given file.
  94.  * Return the number of whole objects written.
  95.  */
  96.  
  97. size_t
  98. _DEFUN(_fwrite_r, (ptr, buf, size, count, fp),
  99.        struct _reent * ptr _AND
  100.        _CONST _PTR __restrict buf _AND
  101.        size_t size     _AND
  102.        size_t count    _AND
  103.        FILE * __restrict fp)
  104. {
  105.   size_t n;
  106. #ifdef _FVWRITE_IN_STREAMIO
  107.   struct __suio uio;
  108.   struct __siov iov;
  109.  
  110.   iov.iov_base = buf;
  111.   uio.uio_resid = iov.iov_len = n = count * size;
  112.   uio.uio_iov = &iov;
  113.   uio.uio_iovcnt = 1;
  114.  
  115.   /*
  116.    * The usual case is success (__sfvwrite_r returns 0);
  117.    * skip the divide if this happens, since divides are
  118.    * generally slow and since this occurs whenever size==0.
  119.    */
  120.  
  121.   CHECK_INIT(ptr, fp);
  122.  
  123.   _newlib_flockfile_start (fp);
  124.   ORIENT (fp, -1);
  125.   if (__sfvwrite_r (ptr, fp, &uio) == 0)
  126.     {
  127.       _newlib_flockfile_exit (fp);
  128.       return count;
  129.     }
  130.   _newlib_flockfile_end (fp);
  131.   return (n - uio.uio_resid) / size;
  132. #else
  133.   size_t i = 0;
  134.   _CONST char *p = buf;
  135.   n = count * size;
  136.   CHECK_INIT (ptr, fp);
  137.  
  138.   _newlib_flockfile_start (fp);
  139.   ORIENT (fp, -1);
  140.   /* Make sure we can write.  */
  141.   if (cantwrite (ptr, fp))
  142.     goto ret;
  143.  
  144.   while (i < n)
  145.     {
  146.       if (__sputc_r (ptr, p[i], fp) == EOF)
  147.         break;
  148.  
  149.       i++;
  150.     }
  151.  
  152. ret:
  153.   _newlib_flockfile_end (fp);
  154.   return i / size;
  155. #endif
  156. }
  157.  
  158. #ifndef _REENT_ONLY
  159. size_t
  160. _DEFUN(fwrite, (buf, size, count, fp),
  161.        _CONST _PTR __restrict buf _AND
  162.        size_t size     _AND
  163.        size_t count    _AND
  164.        FILE * fp)
  165. {
  166.   return _fwrite_r (_REENT, buf, size, count, fp);
  167. }
  168. #endif
  169.