sl@0: /* STDIO.C
sl@0:  * 
sl@0:  * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0:  * All rights reserved.
sl@0:  * All rights reserved.
sl@0:  */
sl@0: 
sl@0: /* No user fns here.  Pesch 15apr92. */
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: #include <stdio.h>
sl@0: #include <sys/types.h>
sl@0: #include <fcntl.h>
sl@0: #include <sys/unistd.h>
sl@0: #include "LOCAL.H"
sl@0: 
sl@0: /*
sl@0:  * Small standard I/O/seek/close functions.
sl@0:  * These maintain the `known seek offset' for seek optimisation.
sl@0:  */
sl@0: 
sl@0: int
sl@0: __sread (void* cookie,char *buf,int n)
sl@0: {
sl@0:   register FILE *fp = (FILE *) cookie;
sl@0:   register int ret;
sl@0: 
sl@0:   ret = _read_r(fp->_data, fp->_file, buf, n);
sl@0: 
sl@0:   /* If the read succeeded, update the current offset.  */
sl@0: 
sl@0:   if (ret >= 0)
sl@0:     fp->_offset += ret;
sl@0:   else
sl@0:     fp->_flags &= ~__SOFF;	/* paranoia */
sl@0:   return ret;
sl@0: }
sl@0: 
sl@0: int
sl@0: __swrite (void* cookie,char const *buf,int n)
sl@0: {
sl@0:   register FILE *fp = (FILE *) cookie;
sl@0: 
sl@0:   if (fp->_flags & __SAPP)
sl@0:     (void) _lseek_r (fp->_data, fp->_file, (off_t) 0, SEEK_END);
sl@0:   fp->_flags &= ~__SOFF;	/* in case O_APPEND mode is set */
sl@0:   return _write_r (fp->_data, fp->_file, buf, n);
sl@0: }
sl@0: 
sl@0: fpos_t
sl@0: __sseek (void* cookie,fpos_t offset,int whence)
sl@0: {
sl@0:   register FILE *fp = (FILE *) cookie;
sl@0:   register off_t ret;
sl@0: 
sl@0:   ret = _lseek_r (fp->_data, fp->_file, (off_t) offset, whence);
sl@0:   if (ret == -1L)
sl@0:     fp->_flags &= ~__SOFF;
sl@0:   else
sl@0:     {
sl@0:       fp->_flags |= __SOFF;
sl@0:       fp->_offset = ret;
sl@0:     }
sl@0:   return ret;
sl@0: }
sl@0: 
sl@0: int
sl@0: __sclose (void* cookie)
sl@0: {
sl@0:   FILE *fp = (FILE *) cookie;
sl@0: 
sl@0:   return _close_r (fp->_data, fp->_file);
sl@0: }