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