os/ossrv/genericopenlibs/cstdlib/LSTDIO/STDIO.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* STDIO.C
sl@0
     2
 * 
sl@0
     3
 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 * All rights reserved.
sl@0
     6
 */
sl@0
     7
sl@0
     8
/* No user fns here.  Pesch 15apr92. */
sl@0
     9
sl@0
    10
/*
sl@0
    11
 * Copyright (c) 1990 The Regents of the University of California.
sl@0
    12
 * All rights reserved.
sl@0
    13
 *
sl@0
    14
 * Redistribution and use in source and binary forms are permitted
sl@0
    15
 * provided that the above copyright notice and this paragraph are
sl@0
    16
 * duplicated in all such forms and that any documentation,
sl@0
    17
 * advertising materials, and other materials related to such
sl@0
    18
 * distribution and use acknowledge that the software was developed
sl@0
    19
 * by the University of California, Berkeley.  The name of the
sl@0
    20
 * University may not be used to endorse or promote products derived
sl@0
    21
 * from this software without specific prior written permission.
sl@0
    22
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0
    23
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0
    24
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    25
 */
sl@0
    26
sl@0
    27
#include <stdio.h>
sl@0
    28
#include <sys/types.h>
sl@0
    29
#include <fcntl.h>
sl@0
    30
#include <sys/unistd.h>
sl@0
    31
#include "LOCAL.H"
sl@0
    32
sl@0
    33
/*
sl@0
    34
 * Small standard I/O/seek/close functions.
sl@0
    35
 * These maintain the `known seek offset' for seek optimisation.
sl@0
    36
 */
sl@0
    37
sl@0
    38
int
sl@0
    39
__sread (void* cookie,char *buf,int n)
sl@0
    40
{
sl@0
    41
  register FILE *fp = (FILE *) cookie;
sl@0
    42
  register int ret;
sl@0
    43
sl@0
    44
  ret = _read_r(fp->_data, fp->_file, buf, n);
sl@0
    45
sl@0
    46
  /* If the read succeeded, update the current offset.  */
sl@0
    47
sl@0
    48
  if (ret >= 0)
sl@0
    49
    fp->_offset += ret;
sl@0
    50
  else
sl@0
    51
    fp->_flags &= ~__SOFF;	/* paranoia */
sl@0
    52
  return ret;
sl@0
    53
}
sl@0
    54
sl@0
    55
int
sl@0
    56
__swrite (void* cookie,char const *buf,int n)
sl@0
    57
{
sl@0
    58
  register FILE *fp = (FILE *) cookie;
sl@0
    59
sl@0
    60
  if (fp->_flags & __SAPP)
sl@0
    61
    (void) _lseek_r (fp->_data, fp->_file, (off_t) 0, SEEK_END);
sl@0
    62
  fp->_flags &= ~__SOFF;	/* in case O_APPEND mode is set */
sl@0
    63
  return _write_r (fp->_data, fp->_file, buf, n);
sl@0
    64
}
sl@0
    65
sl@0
    66
fpos_t
sl@0
    67
__sseek (void* cookie,fpos_t offset,int whence)
sl@0
    68
{
sl@0
    69
  register FILE *fp = (FILE *) cookie;
sl@0
    70
  register off_t ret;
sl@0
    71
sl@0
    72
  ret = _lseek_r (fp->_data, fp->_file, (off_t) offset, whence);
sl@0
    73
  if (ret == -1L)
sl@0
    74
    fp->_flags &= ~__SOFF;
sl@0
    75
  else
sl@0
    76
    {
sl@0
    77
      fp->_flags |= __SOFF;
sl@0
    78
      fp->_offset = ret;
sl@0
    79
    }
sl@0
    80
  return ret;
sl@0
    81
}
sl@0
    82
sl@0
    83
int
sl@0
    84
__sclose (void* cookie)
sl@0
    85
{
sl@0
    86
  FILE *fp = (FILE *) cookie;
sl@0
    87
sl@0
    88
  return _close_r (fp->_data, fp->_file);
sl@0
    89
}