1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FWRITE.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,97 @@
1.4 +/* FWRITE.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +/*
1.28 +FUNCTION
1.29 +<<fwrite>>---write array elements
1.30 +
1.31 +INDEX
1.32 + fwrite
1.33 +
1.34 +ANSI_SYNOPSIS
1.35 + #include <stdio.h>
1.36 + size_t fwrite(const void *<[buf]>, size_t <[size]>,
1.37 + size_t <[count]>, FILE *<[fp]>);
1.38 +
1.39 +TRAD_SYNOPSIS
1.40 + #include <stdio.h>
1.41 + size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
1.42 + char *<[buf]>;
1.43 + size_t <[size]>;
1.44 + size_t <[count]>;
1.45 + FILE *<[fp]>;
1.46 +
1.47 +DESCRIPTION
1.48 +<<fwrite>> attempts to copy, starting from the memory location
1.49 +<[buf]>, <[count]> elements (each of size <[size]>) into the file or
1.50 +stream identified by <[fp]>. <<fwrite>> may copy fewer elements than
1.51 +<[count]> if an error intervenes.
1.52 +
1.53 +<<fwrite>> also advances the file position indicator (if any) for
1.54 +<[fp]> by the number of @emph{characters} actually written.
1.55 +
1.56 +RETURNS
1.57 +If <<fwrite>> succeeds in writing all the elements you specify, the
1.58 +result is the same as the argument <[count]>. In any event, the
1.59 +result is the number of complete elements that <<fwrite>> copied to
1.60 +the file.
1.61 +
1.62 +PORTABILITY
1.63 +ANSI C requires <<fwrite>>.
1.64 +
1.65 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.66 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.67 +*/
1.68 +
1.69 +#include <stdio.h>
1.70 +#include <string.h>
1.71 +#include "LOCAL.H"
1.72 +#include "FVWRITE.H"
1.73 +
1.74 +/*
1.75 + * Write `count' objects (each size `size') from memory to the given file.
1.76 + * Return the number of whole objects written.
1.77 + */
1.78 +
1.79 +EXPORT_C size_t
1.80 +fwrite (const void *buf, size_t size, size_t count, FILE * fp)
1.81 +{
1.82 + size_t n;
1.83 + struct __suio uio;
1.84 + struct __siov iov;
1.85 +
1.86 + iov.iov_base = buf;
1.87 + uio.uio_resid = iov.iov_len = n = count * size;
1.88 + uio.uio_iov = &iov;
1.89 + uio.uio_iovcnt = 1;
1.90 +
1.91 + /*
1.92 + * The usual case is success (__sfvwrite returns 0);
1.93 + * skip the divide if this happens, since divides are
1.94 + * generally slow and since this occurs whenever size==0.
1.95 + */
1.96 +
1.97 + if (__sfvwrite (fp, &uio) == 0)
1.98 + return count;
1.99 + return (n - uio.uio_resid) / size;
1.100 +}