Subversion Repositories Kolibri OS

Rev

Rev 4921 | 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>>, <<fwrite_unlocked>>---write array elements
  21.  
  22. INDEX
  23.         fwrite
  24. INDEX
  25.         fwrite_unlocked
  26. INDEX
  27.         _fwrite_r
  28. INDEX
  29.         _fwrite_unlocked_r
  30.  
  31. ANSI_SYNOPSIS
  32.         #include <stdio.h>
  33.         size_t fwrite(const void *restrict <[buf]>, size_t <[size]>,
  34.                       size_t <[count]>, FILE *restrict <[fp]>);
  35.  
  36.         #define _BSD_SOURCE
  37.         #include <stdio.h>
  38.         size_t fwrite_unlocked(const void *restrict <[buf]>, size_t <[size]>,
  39.                       size_t <[count]>, FILE *restrict <[fp]>);
  40.  
  41.         #include <stdio.h>
  42.         size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
  43.                       size_t <[count]>, FILE *restrict <[fp]>);
  44.  
  45.         #include <stdio.h>
  46.         size_t _fwrite_unlocked_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>,
  47.                       size_t <[count]>, FILE *restrict <[fp]>);
  48.  
  49. TRAD_SYNOPSIS
  50.         #include <stdio.h>
  51.         size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
  52.         char *<[buf]>;
  53.         size_t <[size]>;
  54.         size_t <[count]>;
  55.         FILE *<[fp]>;
  56.  
  57.         #define _BSD_SOURCE
  58.         #include <stdio.h>
  59.         size_t fwrite_unlocked(<[buf]>, <[size]>, <[count]>, <[fp]>)
  60.         char *<[buf]>;
  61.         size_t <[size]>;
  62.         size_t <[count]>;
  63.         FILE *<[fp]>;
  64.  
  65.         #include <stdio.h>
  66.         size_t _fwrite_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
  67.         struct _reent *<[ptr]>;
  68.         char *<[buf]>;
  69.         size_t <[size]>;
  70.         size_t <[count]>;
  71.         FILE *<[fp]>;
  72.  
  73.         #include <stdio.h>
  74.         size_t _fwrite_unlocked_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
  75.         struct _reent *<[ptr]>;
  76.         char *<[buf]>;
  77.         size_t <[size]>;
  78.         size_t <[count]>;
  79.         FILE *<[fp]>;
  80.  
  81. DESCRIPTION
  82. <<fwrite>> attempts to copy, starting from the memory location
  83. <[buf]>, <[count]> elements (each of size <[size]>) into the file or
  84. stream identified by <[fp]>.  <<fwrite>> may copy fewer elements than
  85. <[count]> if an error intervenes.
  86.  
  87. <<fwrite>> also advances the file position indicator (if any) for
  88. <[fp]> by the number of @emph{characters} actually written.
  89.  
  90. <<fwrite_unlocked>> is a non-thread-safe version of <<fwrite>>.
  91. <<fwrite_unlocked>> may only safely be used within a scope
  92. protected by flockfile() (or ftrylockfile()) and funlockfile().  This
  93. function may safely be used in a multi-threaded program if and only
  94. if they are called while the invoking thread owns the (FILE *)
  95. object, as is the case after a successful call to the flockfile() or
  96. ftrylockfile() functions.  If threads are disabled, then
  97. <<fwrite_unlocked>> is equivalent to <<fwrite>>.
  98.  
  99. <<_fwrite_r>> and <<_fwrite_unlocked_r>> are simply reentrant versions of the
  100. above that take an additional reentrant structure argument: <[ptr]>.
  101.  
  102. RETURNS
  103. If <<fwrite>> succeeds in writing all the elements you specify, the
  104. result is the same as the argument <[count]>.  In any event, the
  105. result is the number of complete elements that <<fwrite>> copied to
  106. the file.
  107.  
  108. PORTABILITY
  109. ANSI C requires <<fwrite>>.
  110.  
  111. <<fwrite_unlocked>> is a BSD extension also provided by GNU libc.
  112.  
  113. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  114. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  115. */
  116.  
  117. #if defined(LIBC_SCCS) && !defined(lint)
  118. static char sccsid[] = "%W% (Berkeley) %G%";
  119. #endif /* LIBC_SCCS and not lint */
  120.  
  121. #include <_ansi.h>
  122. #include <stdio.h>
  123. #include <string.h>
  124. #if 0
  125. #include <sys/stdc.h>
  126. #endif
  127. #include "local.h"
  128. #if 1
  129. #include "fvwrite.h"
  130. #endif
  131.  
  132. #ifdef __IMPL_UNLOCKED__
  133. #define _fwrite_r _fwrite_unlocked_r
  134. #define fwrite fwrite_unlocked
  135. #endif
  136.  
  137. /*
  138.  * Write `count' objects (each size `size') from memory to the given file.
  139.  * Return the number of whole objects written.
  140.  */
  141.  
  142. size_t
  143. _DEFUN(_fwrite_r, (ptr, buf, size, count, fp),
  144.        struct _reent * ptr _AND
  145.        _CONST _PTR __restrict buf _AND
  146.        size_t size     _AND
  147.        size_t count    _AND
  148.        FILE * __restrict fp)
  149. {
  150.   size_t n;
  151. #ifdef _FVWRITE_IN_STREAMIO
  152.   struct __suio uio;
  153.   struct __siov iov;
  154.  
  155.   iov.iov_base = buf;
  156.   uio.uio_resid = iov.iov_len = n = count * size;
  157.   uio.uio_iov = &iov;
  158.   uio.uio_iovcnt = 1;
  159.  
  160.   /*
  161.    * The usual case is success (__sfvwrite_r returns 0);
  162.    * skip the divide if this happens, since divides are
  163.    * generally slow and since this occurs whenever size==0.
  164.    */
  165.  
  166.   CHECK_INIT(ptr, fp);
  167.  
  168.   _newlib_flockfile_start (fp);
  169.   ORIENT (fp, -1);
  170.   if (__sfvwrite_r (ptr, fp, &uio) == 0)
  171.     {
  172.       _newlib_flockfile_exit (fp);
  173.       return count;
  174.     }
  175.   _newlib_flockfile_end (fp);
  176.   return (n - uio.uio_resid) / size;
  177. #else
  178.   size_t i = 0;
  179.   _CONST char *p = buf;
  180.   n = count * size;
  181.   CHECK_INIT (ptr, fp);
  182.  
  183.   _newlib_flockfile_start (fp);
  184.   ORIENT (fp, -1);
  185.   /* Make sure we can write.  */
  186.   if (cantwrite (ptr, fp))
  187.     goto ret;
  188.  
  189.   while (i < n)
  190.     {
  191.       if (__sputc_r (ptr, p[i], fp) == EOF)
  192.         break;
  193.  
  194.       i++;
  195.     }
  196.  
  197. ret:
  198.   _newlib_flockfile_end (fp);
  199.   return i / size;
  200. #endif
  201. }
  202.  
  203. #ifndef _REENT_ONLY
  204. size_t
  205. _DEFUN(fwrite, (buf, size, count, fp),
  206.        _CONST _PTR __restrict buf _AND
  207.        size_t size     _AND
  208.        size_t count    _AND
  209.        FILE * fp)
  210. {
  211.   return _fwrite_r (_REENT, buf, size, count, fp);
  212. }
  213. #endif
  214.