sl@0: /* FWRITE.C sl@0: * sl@0: * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: /* sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms are permitted sl@0: * provided that the above copyright notice and this paragraph are sl@0: * duplicated in all such forms and that any documentation, sl@0: * advertising materials, and other materials related to such sl@0: * distribution and use acknowledge that the software was developed sl@0: * by the University of California, Berkeley. The name of the sl@0: * University may not be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. sl@0: */ sl@0: sl@0: /* sl@0: FUNCTION sl@0: <>---write array elements sl@0: sl@0: INDEX sl@0: fwrite sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: size_t fwrite(const void *<[buf]>, size_t <[size]>, sl@0: size_t <[count]>, FILE *<[fp]>); sl@0: sl@0: TRAD_SYNOPSIS sl@0: #include sl@0: size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>) sl@0: char *<[buf]>; sl@0: size_t <[size]>; sl@0: size_t <[count]>; sl@0: FILE *<[fp]>; sl@0: sl@0: DESCRIPTION sl@0: <> attempts to copy, starting from the memory location sl@0: <[buf]>, <[count]> elements (each of size <[size]>) into the file or sl@0: stream identified by <[fp]>. <> may copy fewer elements than sl@0: <[count]> if an error intervenes. sl@0: sl@0: <> also advances the file position indicator (if any) for sl@0: <[fp]> by the number of @emph{characters} actually written. sl@0: sl@0: RETURNS sl@0: If <> succeeds in writing all the elements you specify, the sl@0: result is the same as the argument <[count]>. In any event, the sl@0: result is the number of complete elements that <> copied to sl@0: the file. sl@0: sl@0: PORTABILITY sl@0: ANSI C requires <>. sl@0: sl@0: Supporting OS subroutines required: <>, <>, <>, sl@0: <>, <>, <>, <>. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "LOCAL.H" sl@0: #include "FVWRITE.H" sl@0: sl@0: /* sl@0: * Write `count' objects (each size `size') from memory to the given file. sl@0: * Return the number of whole objects written. sl@0: */ sl@0: sl@0: EXPORT_C size_t sl@0: fwrite (const void *buf, size_t size, size_t count, FILE * fp) sl@0: { sl@0: size_t n; sl@0: struct __suio uio; sl@0: struct __siov iov; sl@0: sl@0: iov.iov_base = buf; sl@0: uio.uio_resid = iov.iov_len = n = count * size; sl@0: uio.uio_iov = &iov; sl@0: uio.uio_iovcnt = 1; sl@0: sl@0: /* sl@0: * The usual case is success (__sfvwrite returns 0); sl@0: * skip the divide if this happens, since divides are sl@0: * generally slow and since this occurs whenever size==0. sl@0: */ sl@0: sl@0: if (__sfvwrite (fp, &uio) == 0) sl@0: return count; sl@0: return (n - uio.uio_resid) / size; sl@0: }