genericopenlibs/cstdlib/LSTDIO/FWRITE.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* FWRITE.C
       
     2  * 
       
     3  * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     4  * All rights reserved.
       
     5  */
       
     6 
       
     7 /*
       
     8  * Copyright (c) 1990 The Regents of the University of California.
       
     9  * All rights reserved.
       
    10  *
       
    11  * Redistribution and use in source and binary forms are permitted
       
    12  * provided that the above copyright notice and this paragraph are
       
    13  * duplicated in all such forms and that any documentation,
       
    14  * advertising materials, and other materials related to such
       
    15  * distribution and use acknowledge that the software was developed
       
    16  * by the University of California, Berkeley.  The name of the
       
    17  * University may not be used to endorse or promote products derived
       
    18  * from this software without specific prior written permission.
       
    19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
       
    20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
       
    21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
       
    22  */
       
    23 
       
    24 /*
       
    25 FUNCTION
       
    26 <<fwrite>>---write array elements
       
    27 
       
    28 INDEX
       
    29 	fwrite
       
    30 
       
    31 ANSI_SYNOPSIS
       
    32 	#include <stdio.h>
       
    33 	size_t fwrite(const void *<[buf]>, size_t <[size]>,
       
    34 		      size_t <[count]>, FILE *<[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 DESCRIPTION
       
    45 <<fwrite>> attempts to copy, starting from the memory location
       
    46 <[buf]>, <[count]> elements (each of size <[size]>) into the file or
       
    47 stream identified by <[fp]>.  <<fwrite>> may copy fewer elements than
       
    48 <[count]> if an error intervenes.
       
    49 
       
    50 <<fwrite>> also advances the file position indicator (if any) for
       
    51 <[fp]> by the number of @emph{characters} actually written.
       
    52 
       
    53 RETURNS
       
    54 If <<fwrite>> succeeds in writing all the elements you specify, the
       
    55 result is the same as the argument <[count]>.  In any event, the
       
    56 result is the number of complete elements that <<fwrite>> copied to
       
    57 the file.
       
    58 
       
    59 PORTABILITY
       
    60 ANSI C requires <<fwrite>>.
       
    61 
       
    62 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
       
    63 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
       
    64 */
       
    65 
       
    66 #include <stdio.h>
       
    67 #include <string.h>
       
    68 #include "LOCAL.H"
       
    69 #include "FVWRITE.H"
       
    70 
       
    71 /*
       
    72  * Write `count' objects (each size `size') from memory to the given file.
       
    73  * Return the number of whole objects written.
       
    74  */
       
    75 
       
    76 EXPORT_C size_t
       
    77 fwrite (const void *buf, size_t size, size_t count, FILE * fp)
       
    78 {
       
    79   size_t n;
       
    80   struct __suio uio;
       
    81   struct __siov iov;
       
    82 
       
    83   iov.iov_base = buf;
       
    84   uio.uio_resid = iov.iov_len = n = count * size;
       
    85   uio.uio_iov = &iov;
       
    86   uio.uio_iovcnt = 1;
       
    87 
       
    88   /*
       
    89    * The usual case is success (__sfvwrite returns 0);
       
    90    * skip the divide if this happens, since divides are
       
    91    * generally slow and since this occurs whenever size==0.
       
    92    */
       
    93 
       
    94   if (__sfvwrite (fp, &uio) == 0)
       
    95     return count;
       
    96   return (n - uio.uio_resid) / size;
       
    97 }