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