ansiStdio.c
上传用户:nvosite88
上传日期:2007-01-17
资源大小:4983k
文件大小:177k
- /* ansiStdio.c - ANSI `stdio' documentation */
- /* Copyright 1984-2002 Wind River Systems, Inc. */
- #include "copyright_wrs.h"
- /*
- modification history
- --------------------
- 01j,21jan02,jkf SPR#72774, fread is checking for NULL on wrong arg
- 01i,12dec01,jkf fixing SPR#72128, fread should check for NULL before bcopy.
- 01h,11nov01,jkf SPR#70967, fread() returns wrong value when 3rd arg == 0
- 01g,17mar99,jdi doc: updated w/ info about proj facility (SPR 25727).
- 01f,04feb99,dgp document errno values
- 01e,10feb95,jdi doc format tweak in fopen() and update for rhp changes in 01d.
- 01d,24jan95,rhp doc: avoid 'L' in fprintf() and fscanf(), no long doubles
- in VxWorks (see SPR#3886)
- 01c,19jul94,dvs doc tweak (SPR #2512)
- 01b,05mar93,jdi documentation cleanup for 5.1.
- 01a,24oct92,smb created.
- */
- /*
- DESCRIPTION
- The header stdio.h declares three types, several macros, and many functions
- for performing input and output.
- .SS Types
- The types declared are `size_t' and:
- .iP `FILE' 12
- object type capable of recording all the information needed to
- control a stream, including its file position indicator, a pointer to its
- associated buffer (if any), an error indicator that records whether a
- read/write error has occurred, and an end-of-file indicator that records
- whether the end of the file has been reached.
- .iP `fpos_t'
- object type capable of recording all the information needed to
- specify uniquely every position within a file.
- .SS Macros
- The macros are NULL and:
- .iP "_IOFBF, _IOLBF, _IONBF" 12 3
- expand to integral constant expressions with distinct values, suitable
- for use as the third argument to setvbuf().
- .iP BUFSIZ
- expands to an integral constant expression that is the size of the
- buffer used by setbuf().
- .iP EOF
- expands to a negative integral constant expression that is returned by
- several functions to indicate `end-of-file', that is, no more input from a
- stream.
- .iP FOPEN_MAX
- expands to an integral constant expression that is the minimum number of
- the files that the system guarantees can be open simultaneously.
- .iP FILENAME_MAX
- expands to an integral constant expression that is the size needed for an
- array of `char' large enough to hold the longest file name string that
- can be used.
- .iP L_tmpnam
- expands to an integral constant expression that is the size needed for
- an array of `char' large enough to hold a temporary file name string
- generated by tmpnam().
- .iP "SEEK_CUR, SEEK_END, SEEK_SET"
- expand to integral constant expressions with distinct values suitable
- for use as the third argument to fseek().
- .iP TMP_MAX
- expands to an integral constant expression that is the minimum number of
- file names generated by tmpnam() that will be unique.
- .iP "`stderr', `stdin', `stdout'"
- expressions of type "pointer to FILE" that point to the FILE objects
- associated, respectively, with the standard error, input, and output streams.
- STREAMS
- Input and output, whether to or from physical devices such as terminals and
- tape drives, or whether to or from files supported on structured storage
- devices, are mapped into logical data streams, whose properties are more
- uniform than their various inputs and outputs. Two forms of mapping are
- supported: for text streams and for binary streams.
- A text stream is an ordered sequence of characters composed into lines,
- each line consisting of zero or more characters plus a terminating new-line
- character. Characters may have to be added, altered, or deleted
- on input and output to conform to differing conventions for representing
- text in the host environment. Thus, there is no need for a one-to-one
- correspondence between the characters in a stream and those in the external
- representation. Data read in from a text stream will necessarily compare
- equal to the data that were earlier written out to that stream only if:
- the data consists only of printable characters and the control characters
- horizontal tab and new-line; no new-line character is immediately preceded
- by space characters; and the last character is a new-line character.
- Space characters are written out immediately before a new-line character
- appears.
- A binary stream is an ordered sequence of characters that can transparently
- record internal data. Data read in from a binary stream should compare
- equal to the data that was earlier written out to that stream, under the
- same implementation. However, such a stream may have a
- number of null characters appended to the end of the stream.
- .SS "Environmental Limits"
- VxWorks supports text files with lines containing at least 254 characters,
- including the terminating new-line character. The value of the macro
- BUFSIZ is 1024.
- FILES
- A stream is associated with an external file (which may be a physical
- device) by opening a file, which may involve creating a new file.
- Creating an existing file causes its former contents to be discarded, if
- necessary. If a file can support positioning requests (such as a disk
- file, as opposed to a terminal), then a file position indicator associated
- with the stream is positioned at the start (character number zero) of the
- file. The file position indicator is maintained by subsequent reads,
- writes, and positioning requests, to facilitate an orderly progression
- through the file. All input takes place as if characters were read by
- successive calls to fgetc(); all output takes place as if characters were
- written by successive calls to fputc().
- Binary files are not truncated, except as defined in fopen() documentation.
- When a stream is unbuffered, characters are intended to appear from the
- source or at the destination as soon as possible. Otherwise characters
- may be accumulated and transmitted to or from the host environment as a
- block. When a stream is fully buffered, characters are intended to be
- transmitted to or from the host environment as a block when the buffer is
- filled. When a stream is line buffered, characters are intended to be
- transmitted to or from the host environment as a block when a new-line
- character is encountered. Furthermore, characters are intended to be
- transmitted as a block to the host environment when a buffer is filled,
- when input is requested on an unbuffered stream, or when input is requested on
- a line-buffered stream that requires the transmission of characters from
- the host environment. VxWorks supports these characteristics via the
- setbuf() and setvbuf() functions.
- A file may be disassociated from a controlling stream by closing the file.
- Output streams are flushed (any unwritten buffer contents are transmitted
- to the host environment) before the stream is disassociated from the file.
- The value of a pointer to a FILE object is indeterminate after the associated
- file is closed (including the standard text streams).
- The file may be subsequently reopened, by the same or another program
- execution, and its contents reclaimed or modified (if it can be repositioned
- at its start).
- TASK TERMINATION
- ANSI specifies that if the main function returns to its original caller or
- if exit() is called, all open files are closed (and hence all output
- streams are flushed) before program termination. This does f3notfP
- happen in VxWorks. The exit() function does not close all files opened
- for that task. A file opened by one task may be used and closed by another.
- Unlike in UNIX, when a VxWorks task exits, it is the responsibility of the
- task to fclose() its file pointers, except `stdin', `stdout', and
- `stderr'. If a task is to be terminated asynchronously, use kill() and
- arrange for a signal handler to clean up.
- The address of the FILE object used to control a stream may be significant;
- a copy of a FILE object may not necessarily serve in place of the original.
- At program startup, three text streams are predefined and need not be opened
- explicitly: standard input (for reading conventional input), standard output
- (for writing conventional output), and standard error (for writing diagnostic
- output). When opened, the standard error stream is not fully buffered; the
- standard input and standard output streams are fully buffered if and only if
- the stream can be determined not to refer to an interactive device.
- Functions that open additional (non-temporary) files require a file name,
- which is a string. VxWorks allows the same file to be open multiple times
- simultaneously. It is up to the user to maintain synchronization between
- different tasks accessing the same file.
- FIOLIB
- Several routines normally considered part of standard I/O -- printf(),
- sprintf(), vprintf(), vsprintf(), and sscanf() -- are not implemented as part
- of the buffered standard I/O library; they are
- instead implemented in fioLib. They do not use the standard I/O buffering
- scheme. They are self-contained, formatted, but unbuffered I/O
- functions. This allows a limited amount of formatted I/O to be achieved
- without the overhead of the standard I/O library.
- SEE ALSO:
- fioLib,
- .I "American National Standard for Information Systems -"
- .I "Programming Language - C, ANSI X3.159-1989: Input/Output (stdio.h)
- INTERNAL
- This documentation module is built by appending the following files:
- clearerr.c
- fclose.c
- fdopen.c
- feof.c
- ferror.c
- fflush.c
- fgetc.c
- fgetpos.c
- fgets.c
- fileno.c
- fopen.c
- fprintf.c
- fputc.c
- fputs.c
- fread.c
- freopen.c
- fscanf.c
- fseek.c
- fsetpos.c
- ftell.c
- fwrite.c
- getc.c
- getchar.c
- gets.c
- getw.c
- perror.c
- putc.c
- putchar.c
- puts.c
- putw.c
- rewind.c
- scanf.c
- setbuf.c
- setbuffer.c
- setvbuf.c
- stdioLib.c
- stdioShow.c
- tmpfile.c
- tmpnam.c
- ungetc.c
- vfprintf.c
- */
- /* clearerr.c - clear error file for stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01d,05mar93,jdi documentation cleanup for 5.1.
- 01c,21sep92,smb corrected first line of file.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #undef clearerr
- /******************************************************************************
- *
- * clearerr - clear end-of-file and error flags for a stream (ANSI)
- *
- * This routine clears the end-of-file and error flags for a specified stream.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS: N/A
- *
- * SEE ALSO: feof(), ferror()
- */
- void clearerr
- (
- FILE * fp /* stream to clear EOF and ERROR flags for */
- )
- {
- __sclearerr(fp);
- }
- /* fclose.c - close file stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf added OBJ_VERIFY and memory reclaimation.
- +smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, stdlib.h, error.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "errno.h"
- #include "stdlib.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fclose - close a stream (ANSI)
- *
- * This routine flushes a specified stream and closes the associated file.
- * Any unwritten buffered data is delivered to the host environment to be
- * written to the file; any unread buffered data is discarded. The stream
- * is disassociated from the file. If the associated buffer was allocated
- * automatically, it is deallocated.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * Zero if the stream is closed successfully, or EOF if errors occur.
- *
- * ERRNO: EBADF
- *
- * SEE ALSO: fflush()
- */
- int fclose
- (
- FAST FILE * fp /* stream to close */
- )
- {
- FAST int r;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (EOF);
- if (fp->_flags == 0) /* not open! */
- {
- errno = EBADF;
- return (EOF);
- }
- r = (fp->_flags & __SWR) ? (__sflush(fp)) : (0);
- if (__sclose (fp) < 0)
- r = EOF;
- if (fp->_flags & __SMBF)
- free ((char *)fp->_bf._base);
- if (HASUB(fp)) /* ungetc buffer? */
- FREEUB(fp);
- if (HASLB(fp)) /* fgetline buffer? */
- FREELB(fp);
- fp->_flags = 0; /* release this FILE for reuse */
- stdioFpDestroy (fp); /* destroy the file pointer */
- return (r);
- }
- /* fdopen.c - open file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01d,07jul93,jmm fixed fdopen to validate the fd (spr 2197)
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: sys/types.h, fcntl.h, unistd.h, stdio.h, errno.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "sys/types.h"
- #include "fcntl.h"
- #include "unistd.h"
- #include "errno.h"
- #include "objLib.h"
- #include "iosLib.h"
- #include "private/stdioP.h"
- /*******************************************************************************
- *
- * fdopen - open a file specified by a file descriptor (POSIX)
- *
- * This routine opens the file specified by the file descriptor <fd> and
- * associates a stream with it.
- * The <mode> argument is used just as in the fopen() function.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS: A pointer to a stream, or a null pointer if an error occurs,
- * with `errno' set to indicate the error.
- *
- * ERRNO: EINVAL
- *
- * SEE ALSO: fopen(), freopen(),
- * .br
- * .I "Information Technology - POSIX - Part 1:"
- * .I "System API [C Language], IEEE Std 1003.1"
- */
- FILE * fdopen
- (
- int fd, /* file descriptor */
- const char * mode /* mode to open with */
- )
- {
- FAST FILE * fp;
- int flags;
- int oflags;
- /* validate the file descriptor */
- if (iosFdValue (fd) == ERROR)
- return (NULL);
-
- if ((flags = __sflags(mode, &oflags)) == 0)
- return (NULL);
- /* Make sure the mode the user wants is a subset of the actual mode. */
- #if FALSE /* XXX */
- if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
- return (NULL);
- tmp = fdflags & O_ACCMODE;
- if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE)))
- {
- errno = EINVAL;
- return (NULL);
- }
- #endif
- if ((fp = stdioFpCreate()) == NULL)
- return (NULL);
- fp->_flags = flags;
- /*
- * If opened for appending, but underlying descriptor does not have
- * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
- * end before each write.
- */
- if ((oflags & O_APPEND) /* XXX && !(fdflags & O_APPEND) */ )
- fp->_flags |= __SAPP;
- fp->_file = fd;
- return (fp);
- }
- /* feof.c - end of file indicator function. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01d,05mar93,jdi documentation cleanup for 5.1.
- 01c,21sep92,smb tweaks for mg
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #undef feof
- /******************************************************************************
- *
- * feof - test the end-of-file indicator for a stream (ANSI)
- *
- * This routine tests the end-of-file indicator for a specified stream.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * Non-zero if the end-of-file indicator is set for <fp>.
- *
- * SEE ALSO: clearerr()
- */
- int feof
- (
- FILE * fp /* stream to test */
- )
- {
- return (__sfeof(fp));
- }
- /* ferror.c - file error indicator function for stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
-
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #undef ferror
- /******************************************************************************
- *
- * ferror - test the error indicator for a file pointer (ANSI)
- *
- * This routine tests the error indicator for the stream pointed to by <fp>.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS: Non-zero if the error indicator is set for <fp>.
- *
- * SEE ALSO: clearerr()
- */
- int ferror
- (
- FILE * fp /* stream to test */
- )
- {
- return (__sferror(fp));
- }
- /* fflush.c - flush file stdio.h */
- /* Copyright 1992-1995 Wind River Systems, Inc. */
-
- /*
- modification history
- --------------------
- 01e,20jan95,ism changed FIOFLUSH to FIOSYNC
- 01d,03oct94,ism added FIOFLUSH ioctl to __sflush() as per SPR#2548
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, error.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "errno.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- #include "sys/ioctl.h"
- #include "ioLib.h"
- /******************************************************************************
- *
- * fflush - flush a stream (ANSI)
- *
- * This routine writes to the file any unwritten data for a specified output
- * or update stream for which the most recent operation was not input; for an
- * input stream the behavior is undefined.
- *
- * CAVEAT
- * ANSI specifies that if <fp> is a null pointer, fflush() performs the
- * flushing action on all streams for which the behavior is defined; however,
- * this is not implemented in VxWorks.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS: Zero, or EOF if a write error occurs.
- *
- * ERRNO: EBADF
- *
- * SEE ALSO: fclose()
- */
- int fflush
- (
- FAST FILE * fp /* stream to flush */
- )
- {
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (EOF);
- /* ANSI specifies that fflush (NULL) will flush all task's file pointers.
- * The problem is that we don't keep such a list today so we return EOF
- * for now.
- */
- if ((fp == NULL) ||
- ((fp->_flags & __SWR) == 0))
- {
- errno = EBADF;
- return (EOF);
- }
- return (__sflush(fp));
- }
- /******************************************************************************
- *
- * __sflush - flush stream pointed to by fp.
- *
- * INCLUDE: stdio.h
- *
- * RETURNS: ZERO or EOF
- * NOMANUAL
- */
- int __sflush
- (
- FAST FILE *fp
- )
- {
- FAST unsigned char *p;
- FAST int n;
- FAST int t;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (EOF);
- t = fp->_flags;
- if ((t & __SWR) == 0)
- return (0);
- if ((p = fp->_bf._base) == NULL)
- return (0);
- n = fp->_p - p; /* write this much */
- /* Set these immediately to avoid problems with longjmp and to allow
- * exchange buffering (via setvbuf) in user write function.
- */
- fp->_p = p;
- fp->_w = (t & (__SLBF|__SNBF)) ? (0) : (fp->_bf._size);
- for (; n > 0; n -= t, p += t)
- {
- t = __swrite (fp, (char *)p, n);
- if (t <= 0)
- {
- fp->_flags |= __SERR;
- return (EOF);
- }
- }
- (void)ioctl(fileno(fp), FIOSYNC, 0);
- return (0);
- }
- /* fgetc.c - get character from file -- stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
-
- /*
- modification history
- -------------------
- 01d,05mar93,jdi documentation cleanup for 5.1.
- 01c,13oct92,jdi mangen fixes.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fgetc - return the next character from a stream (ANSI)
- *
- * This routine returns the next character (converted to an `int') from the
- * specified stream, and advances the file position indicator for the stream.
- *
- * If the stream is at end-of-file, the end-of-file indicator for the stream is
- * set; if a read error occurs, the error indicator is set.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The next character from the stream, or EOF if the stream is at end-of-file
- * or a read error occurs.
- *
- * SEE ALSO: fgets(), getc()
- */
- int fgetc
- (
- FILE * fp /* stream to read from */
- )
- {
- return (getc(fp));
- }
- /* fgetpos.c - get position in file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
-
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb written.
- */
-
- /*
- DESCRIPTION
- INCLUDE FILE: stdio.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "objLib.h"
- /******************************************************************************
- *
- * fgetpos - store the current value of the file position indicator for a stream (ANSI)
- *
- * This routine stores the current value of the file position indicator for a
- * specified stream <fp> in the object pointed to by <pos>. The value stored
- * contains unspecified information usable by fsetpos() for repositioning
- * the stream to its position at the time fgetpos() was called.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * Zero, or non-zero if unsuccessful, with `errno' set to indicate the error.
- *
- * SEE ALSO: fsetpos()
- */
- int fgetpos
- (
- FILE * fp, /* stream */
- fpos_t * pos /* where to store position */
- )
- {
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (ERROR);
- return (!((*pos = ftell (fp)) != (fpos_t) -1));
- }
- /* fgets.c - Read lines of text from a stream. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
-
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, string.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "string.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fgets - read a specified number of characters from a stream (ANSI)
- *
- * This routine stores in the array <buf> up to <n>-1 characters from a
- * specified stream. No additional characters are read after a new-line or
- * end-of-line. A null character is written immediately after the last
- * character read into the array.
- *
- * If end-of-file is encountered and no characters have been read, the
- * contents of the array remain unchanged. If a read error occurs, the array
- * contents are indeterminate.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * A pointer to <buf>, or a null pointer if an error occurs or end-of-file is
- * encountered and no characters have been read.
- *
- * SEE ALSO: fread(), fgetc()
- */
- char * fgets
- (
- char * buf, /* where to store characters */
- FAST size_t n, /* no. of bytes to read + 1 */
- FAST FILE * fp /* stream to read from */
- )
- {
- FAST size_t len;
- FAST char * s;
- FAST uchar_t * p;
- FAST uchar_t * t;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (NULL);
- if (n < 2) /* sanity check */
- return (NULL);
- s = buf;
- n--; /* leave space for NULL */
- do
- {
- if ((len = fp->_r) <= 0) /* If the buffer is empty, refill it */
- {
- if (__srefill(fp)) /* EOF/error: partial or no line */
- {
- if (s == buf)
- return (NULL);
- break;
- }
- len = fp->_r;
- }
- p = fp->_p;
- /*
- * Scan through at most n bytes of the current buffer,
- * looking for 'n'. If found, copy up to and including
- * newline, and stop. Otherwise, copy entire chunk
- * and loop.
- */
- if (len > n)
- len = n;
- t = memchr ((void *)p, 'n', len);
- if (t != NULL)
- {
- len = ++t - p;
- fp->_r -= len;
- fp->_p = t;
- (void) bcopy ((void *)p, (void *)s, len);
- s[len] = 0;
- return (buf);
- }
- fp->_r -= len;
- fp->_p += len;
- (void) bcopy ((void *)p, (void *)s, len);
- s += len;
- } while ((n -= len) != 0);
- *s = 0;
- return (buf);
- }
- /* fileno.c - determine file number for stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
-
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
-
- /*
- DESCRIPTION
- *
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- #undef fileno
- /******************************************************************************
- *
- * fileno - return the file descriptor for a stream (POSIX)
- *
- * This routine returns the file descriptor associated with a specified
- * stream.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The file descriptor, or -1 if an error occurs, with `errno' set to indicate
- * the error.
- *
- * SEE ALSO:
- * .br
- * .I "Information Technology - POSIX - Part 1:"
- * .I "System API [C Language], IEEE Std 1003.1"
- */
- int fileno
- (
- FILE * fp /* stream */
- )
- {
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (ERROR);
- return (__sfileno(fp));
- }
- /* fopen.c - open a file. stdio.h */
- /* Copyright 1992-1995 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01d,10feb95,jdi doc format tweak.
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf modified to use stdioFpCreate and close memory leaks.
- smb taken from UCB stdio.
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, error.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "sys/types.h"
- #include "sys/stat.h"
- #include "ioLib.h"
- #include "fcntl.h"
- #include "errno.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fopen - open a file specified by name (ANSI)
- *
- * This routine opens a file whose name is the string pointed to by <file>
- * and associates a stream with it.
- * The argument <mode> points to a string beginning with one of the following
- * sequences:
- * .iP r "" 3
- * open text file for reading
- * .iP w
- * truncate to zero length or create text file for writing
- * .iP a
- * append; open or create text file for writing at end-of-file
- * .iP rb
- * open binary file for reading
- * .iP wb
- * truncate to zero length or create binary file for writing
- * .iP ab
- * append; open or create binary file for writing at end-of-file
- * .iP r+
- * open text file for update (reading and writing)
- * .iP w+
- * truncate to zero length or create text file for update.
- * .iP a+
- * append; open or create text file for update, writing at end-of-file
- * .iP "r+b / rb+"
- * open binary file for update (reading and writing)
- * .iP "w+b / wb+"
- * truncate to zero length or create binary file for update
- * .iP "a+b / ab+"
- * append; open or create binary file for update, writing at end-of-file
- * .LP
- *
- * Opening a file with read mode (`r' as the first character in the <mode>
- * argument) fails if the file does not exist or cannot be read.
- *
- * Opening a file with append mode (`a' as the first character in the <mode>
- * argument) causes all subsequent writes to the file to be forced to the
- * then current end-of-file, regardless of intervening calls to fseek(). In
- * some implementations, opening a binary file with append mode (`b' as the
- * second or third character in the <mode> argument) may initially position
- * the file position indicator for the stream beyond the last data written,
- * because of null character padding. In VxWorks, whether append mode is
- * supported is device-specific.
- *
- * When a file is opened with update mode (`+' as the second or third
- * character in the <mode> argument), both input and output may be performed
- * on the associated stream. However, output may not be directly followed by
- * input without an intervening call to fflush() or to a file positioning
- * function (fseek(), fsetpos(), or rewind()), and input may not be directly
- * followed by output without an intervening call to a file positioning
- * function, unless the input operation encounters end-of-file. Opening (or
- * creating) a text file with update mode may instead open (or create) a
- * binary stream in some implementations.
- *
- * When opened, a stream is fully buffered if and only if it can be determined
- * not to refer to an interactive device. The error and end-of-file
- * indicators for the stream are cleared.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * A pointer to the object controlling the stream, or a null pointer if the
- * operation fails.
- *
- * SEE ALSO: fdopen(), freopen()
- */
- FILE * fopen
- (
- const char * file, /* name of file */
- const char * mode /* mode */
- )
- {
- FAST FILE * fp;
- FAST int f;
- int flags;
- int oflags;
- if ((flags = __sflags (mode, &oflags)) == 0)
- return (NULL);
- if ((fp = stdioFpCreate ()) == NULL)
- return (NULL);
- if ((f = open (file, oflags, DEFFILEMODE )) < 0)
- {
- fp->_flags = 0x0; /* release */
- stdioFpDestroy (fp); /* destroy file pointer */
- return (NULL);
- }
- fp->_file = f;
- fp->_flags = flags;
- /*
- * When opening in append mode, even though we use O_APPEND,
- * we need to seek to the end so that ftell() gets the right
- * answer. If the user then alters the seek pointer, or
- * the file extends, this will fail, but there is not much
- * we can do about this. (We could set __SAPP and check in
- * fseek and ftell.)
- */
- if (oflags & O_APPEND)
- (void) __sseek ((void *)fp, (fpos_t)0, SEEK_END);
- return (fp);
- }
- /* fprintf.c - print to a file. stdio.h */
- /* Copyright 1992-1995 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01e,24jan95,rhp doc: avoid 'L' in fprintf(), no long doubles
- in VxWorks (see SPR#3886)
- 01d,19jul94,dvs doc tweak (SPR #2512).
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, stdarg.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdarg.h"
- #include "fioLib.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fprintf - write a formatted string to a stream (ANSI)
- *
- * This routine writes output to a specified stream under control of the string
- * <fmt>. The string <fmt> contains ordinary characters, which are written
- * unchanged, plus conversion specifications, which cause the arguments that
- * follow <fmt> to be converted and printed as part of the formatted string.
- *
- * The number of arguments for the format is arbitrary, but they must
- * correspond to the conversion specifications in <fmt>. If there are
- * insufficient arguments, the behavior is undefined. If the format is
- * exhausted while arguments remain, the excess arguments are evaluated but
- * otherwise ignored. The routine returns when the end of the format string
- * is encountered.
- *
- * The format is a multibyte character sequence, beginning and ending in its
- * initial shift state. The format is composed of zero or more directives:
- * ordinary multibyte characters (not `%') that are copied unchanged to the
- * output stream; and conversion specification, each of which results in
- * fetching zero or more subsequent arguments. Each conversion specification
- * is introduced by the `%' character. After the `%', the following appear in
- * sequence:
- * .iP "" 4
- * Zero or more flags (in any order) that modify the meaning of the
- * conversion specification.
- * .iP
- * An optional minimum field width. If the converted value has fewer
- * characters than the field width, it will be padded with spaces (by
- * default) on the left (or right, if the left adjustment flag,
- * described later, has been given) to the field width. The field
- * width takes the form of an asterisk (`*') (described later) or a decimal
- * integer.
- * .iP
- * An optional precision that gives the minimum number of digits to
- * appear for the `d', `i', `o', `u', `x', and `X' conversions, the number of
- * digits to appear after the decimal-point character for `e', `E', and `f'
- * conversions, the maximum number of significant digits for the `g' and
- * `G' conversions, or the maximum number of characters to be written
- * from a string in the `s' conversion. The precision takes the form of a
- * period (`.') followed either by an asterisk (`*') (described later) or by
- * an optional decimal integer; if only the period is specified, the
- * precision is taken as zero. If a precision appears with any other
- * conversion specifier, the behavior is undefined.
- * .iP
- * An optional `h' specifying that a following `d', `i', `o', `u', `x', and
- * `X' conversion specifier applies to a `short int' or `unsigned short int'
- * argument (the argument will have been promoted according to the integral
- * promotions, and its value converted to `short int' or
- * `unsigned short int' before printing); an optional `h' specifying that a
- * following `n' conversion specifier applies to a pointer to a `short int'
- * argument; an optional `l' (el) specifying that a following `d', `i', `o',
- * `u', `x', and `X' conversion specifier applies to a `long int' or
- * `unsigned long int' argument; or an optional `l' specifying that a following
- * `n' conversion specifier applies to a pointer to a `long int'
- * argument. If an `h' or `l' appears with any other conversion
- * specifier, the behavior is undefined.
- *
- * &WARNING: ANSI C also specifies an optional `L' in some of the same
- * contexts as `l' above, corresponding to a `long double' argument.
- * However, the current release of the VxWorks libraries does not support
- * `long double' data; using the optional `L' gives unpredictable results.
- * .iP
- * A character that specifies the type of conversion to be applied.
- * .LP
- *
- * As noted above, a field width, or precision, or both, can be indicated by
- * an asterisk (`*'). In this case, an `int' argument supplies the field width
- * or precision. The arguments specifying field width, or precision, or both,
- * should appear (in that order) before the argument (if any) to be converted.
- * A negative field width argument is taken as a `-' flag followed by a positive
- * field width. A negative precision argument is taken as if the precision
- * were omitted.
- *
- * The flag characters and their meanings are:
- * .iP `-'
- * The result of the conversion will be left-justified within the field.
- * (it will be right-justified if this flag is not specified.)
- * .iP `+'
- * The result of a signed conversion will always begin with a plus or
- * minus sign. (It will begin with a sign only when a negative value
- * is converted if this flag is not specified.)
- * .iP `space'
- * If the first character of a signed conversion is not a sign, or
- * if a signed conversion results in no characters, a space will be
- * prefixed to the result. If the `space' and `+' flags both appear, the
- * `space' flag will be ignored.
- * .iP `#'
- * The result is to be converted to an "alternate form." For `o' conversion
- * it increases the precision to force the first digit of the result to be a
- * zero. For `x' (or `X') conversion, a non-zero result will have "0x" (or
- * "0X") prefixed to it. For `e', `E', `f', `g', and `G' conversions, the
- * result will always contain a decimal-point character, even if no digits
- * follow it. (Normally, a decimal-point character appears in the result of
- * these conversions only if no digit follows it). For `g' and `G'
- * conversions, trailing zeros will not be removed from the result. For
- * other conversions, the behavior is undefined.
- * .iP `0'
- * For `d', `i', `o', `u', `x', `X', `e', `E', `f', `g', and `G' conversions,
- * leading zeros (following any indication of sign or base) are used to pad
- * to the field width; no space padding is performed. If the `0' and `-'
- * flags both appear, the `0' flag will be ignored. For `d', `i', `o', `u',
- * `x', and `X' conversions, if a precision is specified, the `0' flag will
- * be ignored. For other conversions, the behavior is undefined.
- * .LP
- *
- * The conversion specifiers and their meanings are:
- * .iP "`d', `i'"
- * The `int' argument is converted to signed decimal in the style
- * `[-]dddd'. The precision specifies the minimum number of digits
- * to appear; if the value being converted can be represented in
- * fewer digits, it will be expanded with leading zeros. The
- * default precision is 1. The result of converting a zero value
- * with a precision of zero is no characters.
- * .iP "`o', `u', `x', `X'"
- * The `unsigned int' argument is converted to unsigned octal (`o'),
- * unsigned decimal (`u'), or unsigned hexadecimal notation (`x' or `X')
- * in the style `dddd'; the letters abcdef are used for `x' conversion
- * and the letters ABCDEF for `X' conversion. The precision specifies
- * the minimum number of digits to appear; if the value being
- * converted can be represented in fewer digits, it will be
- * expanded with leading zeros. The default precision is 1. The
- * result of converting a zero value with a precision of zero is
- * no characters.
- * .iP `f'
- * The `double' argument is converted to decimal notation in the
- * style `[-]ddd.ddd', where the number of digits after the decimal
- * point character is equal to the precision specification. If the
- * precision is missing, it is taken as 6; if the precision is zero
- * and the `#' flag is not specified, no decimal-point character
- * appears. If a decimal-point character appears, at least one
- * digit appears before it. The value is rounded to the appropriate
- * number of digits.
- * .iP "`e', `E'"
- * The `double' argument is converted in the style `[-]d.ddde+/-dd',
- * where there is one digit before the decimal-point character
- * (which is non-zero if the argument is non-zero) and the number
- * of digits after it is equal to the precision; if the precision
- * is missing, it is taken as 6; if the precision is zero and the
- * `#' flag is not specified, no decimal-point character appears. The
- * value is rounded to the appropriate number of digits. The `E'
- * conversion specifier will produce a number with `E' instead of `e'
- * introducing the exponent. The exponent always contains at least
- * two digits. If the value is zero, the exponent is zero.
- * .iP "`g', `G'"
- * The `double' argument is converted in style `f' or `e' (or in style
- * `E' in the case of a `G' conversion specifier), with the precision
- * specifying the number of significant digits. If the precision
- * is zero, it is taken as 1. The style used depends on the
- * value converted; style `e' (or `E') will be used only if the
- * exponent resulting from such a conversion is less than -4 or
- * greater than or equal to the precision. Trailing zeros are
- * removed from the fractional portion of the result; a decimal-point
- * character appears only if it is followed by a digit.
- * .iP `c'
- * The `int' argument is converted to an `unsigned char', and the
- * resulting character is written.
- * .iP `s'
- * The argument should be a pointer to an array of character type.
- * Characters from the array are written up to (but not including)
- * a terminating null character; if the precision is specified,
- * no more than that many characters are written. If the precision
- * is not specified or is greater than the size of the array, the
- * array will contain a null character.
- * .iP `p'
- * The argument should be a pointer to `void'. The value of the
- * pointer is converted to a sequence of printable characters,
- * in hexadecimal representation (prefixed with "0x").
- * .iP `n'
- * The argument should be a pointer to an integer into which
- * the number of characters written to the output stream
- * so far by this call to fprintf() is written. No argument is converted.
- * .iP `%'
- * A `%' is written. No argument is converted. The complete
- * conversion specification is %%.
- * .LP
- *
- * If a conversion specification is invalid, the behavior is undefined.
- *
- * If any argument is, or points to, a union or an aggregate (except for an
- * array of character type using `s' conversion, or a pointer using `p'
- * conversion), the behavior is undefined.
- *
- * In no case does a non-existent or small field width cause truncation of a
- * field if the result of a conversion is wider than the field width, the
- * field is expanded to contain the conversion result.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The number of characters written, or a negative value if an
- * output error occurs.
- *
- * SEE ALSO: printf()
- */
- int fprintf
- (
- FILE * fp, /* stream to write to */
- const char * fmt, /* format string */
- ... /* optional arguments to format string */
- )
- {
- int ret;
- va_list vaList;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (ERROR);
- va_start (vaList, fmt);
- ret = vfprintf (fp, fmt, vaList);
- va_end (vaList);
- return (ret);
- }
- /* fputc.c - put a character to a file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fputc - write a character to a stream (ANSI)
- *
- * This routine writes a character <c> to a specified stream, at the
- * position indicated by the stream's file position indicator
- * (if defined), and advances the indicator appropriately.
- *
- * If the file cannot support positioning requests, or if the stream was opened
- * in append mode, the character is appended to the output stream.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The character written, or EOF if a write error occurs, with the error
- * indicator set for the stream.
- *
- * SEE ALSO: fputs(), putc()
- */
- int fputc
- (
- int c, /* character to write */
- FAST FILE * fp /* stream to write to */
- )
- {
- return (putc (c, fp));
- }
- /* fputs.c - put a string of characters to a file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, string.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "string.h"
- #include "objLib.h"
- #include "private/fvwriteP.h"
- /******************************************************************************
- *
- * fputs - write a string to a stream (ANSI)
- *
- * This routine writes the string <s>, minus the terminating NULL character,
- * to a specified stream.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * A non-negative value, or EOF if a write error occurs.
- *
- * SEE ALSO: fputc()
- */
- int fputs
- (
- const char * s, /* string */
- FILE * fp /* stream to write to */
- )
- {
- struct __suio uio;
- struct __siov iov;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (EOF);
- iov.iov_base = (void *)s;
- iov.iov_len = uio.uio_resid = strlen (s);
- uio.uio_iov = &iov;
- uio.uio_iovcnt = 1;
- return (__sfvwrite (fp, &uio));
- }
- /* fread.c - read a file. stdio.h */
- /* Copyright 1992-2002 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01f,21jan02,jkf SPR#72774, fread is checking for NULL on wrong arg
- 01e,12dec01,jkf fixing SPR#72128, fread should check for NULL before bcopy
- 01d,10nov01,jkf SPR#70967, fread returns wrong value when 3rd arg == 0
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, string.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "string.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fread - read data into an array (ANSI)
- *
- * This routine reads, into the array <buf>, up to <count> elements of size
- * <size>, from a specified stream <fp>. The file position indicator for the
- * stream (if defined) is advanced by the number of characters successfully
- * read. If an error occurs, the resulting value of the file position
- * indicator for the stream is indeterminate. If a partial element is read,
- * its value is indeterminate.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The number of elements successfully read, which may be less than <count>
- * if a read error or end-of-file is encountered; or zero if <size> or
- * <count> is zero, with the contents of the array and the state of the
- * stream remaining unchanged.
- */
- int fread
- (
- void * buf, /* where to copy data */
- size_t size, /* element size */
- size_t count, /* no. of elements */
- FAST FILE * fp /* stream to read from */
- )
- {
- FAST size_t resid;
- FAST char * p;
- FAST int r;
- size_t total;
- /* SPR#72128, check for NULL args before bcopy */
- if ((NULL == fp) || (NULL == buf))
- {
- return (0);
- }
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (0);
- /*
- * SPR#70967: fread returns wrong value when 3rd arg == 0
- *
- * ANSI specification declares fread() returns zero if size
- * _or_ count is zero. But the vxWorks implementation returned
- * non-zero given the following input case:
- *
- * fread ( buf , 0 ,1 ,fp );
- *
- * Old (broken) code:
- *
- * if ((resid = (count * size)) == 0)
- * return (count);
- *
- * New (fixed) code:
- *
- * if (0 == (resid = (count * size)))
- * return (0);
- */
- if (0 == (resid = (count * size)))
- return (0);
- if (fp->_r < 0)
- fp->_r = 0;
- total = resid;
- p = buf;
- while (resid > (r = fp->_r))
- {
- (void) bcopy ((void *)fp->_p, (void *)p, (size_t)r);
- fp->_p += r;
- p += r;
- resid -= r;
- if (__srefill (fp)) /* fp->_r = 0 is done */
- return ((total - resid) / size); /* partial result */
- }
- (void) bcopy ((void *)fp->_p, (void *)p, resid);
- fp->_r -= resid;
- fp->_p += resid;
- return (count);
- }
- /* freopen.c - reopen a file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf added memory reclaimation.
- +smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, sys/types.h, sys/stat.h, fcntl.h, error.h, unistd.h,
- stdlib.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "ioLib.h"
- #include "sys/types.h"
- #include "sys/stat.h"
- #include "fcntl.h"
- #include "errno.h"
- #include "unistd.h"
- #include "stdlib.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * freopen - open a file specified by name (ANSI)
- *
- * This routine opens a file whose name is the string pointed to by <file>
- * and associates it with a specified stream <fp>.
- * The <mode> argument is used just as in the fopen() function.
- *
- * This routine first attempts to close any file that is associated
- * with the specified stream. Failure to close the file successfully is
- * ignored. The error and end-of-file indicators for the stream are cleared.
- *
- * Typically, freopen() is used to attach the already-open streams
- * `stdin', `stdout', and `stderr' to other files.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The value of <fp>, or a null pointer if the open operation fails.
- *
- * SEE ALSO: fopen()
- */
- FILE * freopen
- (
- const char * file, /* name of file */
- const char * mode, /* mode */
- FAST FILE * fp /* stream */
- )
- {
- FAST int f;
- int flags;
- int oflags;
- int sverrno;
- BOOL isopen = TRUE; /* its already open */
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (NULL);
- if ((flags = __sflags (mode, &oflags)) == 0)
- {
- (void) fclose (fp);
- return (NULL);
- }
- /*
- * There are actually programs that depend on being able to "freopen"
- * descriptors that weren't originally open. Keep this from breaking.
- * Remember whether the stream was open to begin with, and which file
- * descriptor (if any) was associated with it. If it was attached to
- * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
- * should work. This is unnecessary if it was not a Unix file.
- */
- if (fp->_flags & __SWR) /* flush the stream though */
- (void) __sflush(fp); /* ANSI doesn't require this */
- if (fp->_file < 0) /* need to close the fp? */
- {
- (void) __sclose (fp);
- isopen = FALSE;
- }
- /* Get a new descriptor to refer to the new file. */
- f = open (file, oflags, DEFFILEMODE);
- if ((f < 0) && isopen)
- {
- /* If out of fd's close the old one and try again. */
- if ((errno == ENFILE) || (errno == EMFILE))
- {
- (void) __sclose (fp);
- isopen = FALSE;
- f = open (file, oflags, DEFFILEMODE);
- }
- }
- sverrno = errno;
- /*
- * Finish closing fp. Even if the open succeeded above, we cannot
- * keep fp->_base: it may be the wrong size. This loses the effect
- * of any setbuffer calls, but stdio has always done this before.
- */
- if (isopen)
- (void) __sclose (fp);
- if (fp->_flags & __SMBF)
- free ((char *)fp->_bf._base);
- fp->_w = 0;
- fp->_r = 0;
- fp->_p = NULL;
- fp->_bf._base = NULL;
- fp->_bf._size = 0;
- fp->_lbfsize = 0;
- if (HASUB(fp))
- FREEUB(fp);
- fp->_ub._size = 0;
- if (HASLB(fp))
- FREELB(fp);
- fp->_lb._size = 0;
- if ((f < 0) && (fp->_flags != 0))
- { /* did not get it after all */
- fp->_flags = 0; /* set it free */
- stdioFpDestroy (fp); /* destroy file pointer */
- errno = sverrno; /* restore in case _close clobbered */
- return (NULL);
- }
- fp->_flags = flags;
- fp->_file = f;
- return (fp);
- }
- /* fscanf.c - scan a file. stdio.h */
- /* Copyright 1992-1995 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01d,24jan95,rhp doc: avoid 'L' in fscanf(), no long doubles
- in VxWorks (see SPR#3886)
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- +smb taken from UCB stdio
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, stdarg.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "fioLib.h"
- #include "stdarg.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- /******************************************************************************
- *
- * fscanf - read and convert characters from a stream (ANSI)
- *
- * This routine reads characters from a specified stream, and interprets them
- * according to format specifications in the string <fmt>, which specifies
- * the admissible input sequences and how they are to be converted for
- * assignment, using subsequent arguments as pointers to the objects to
- * receive the converted input.
- *
- * If there are insufficient arguments for the format, the behavior is
- * undefined. If the format is exhausted while arguments remain, the excess
- * arguments are evaluated but are otherwise ignored.
- *
- * The format is a multibyte character sequence, beginning and ending in
- * its initial shift state. The format is composed of zero or more directives:
- * one or more white-space characters; an ordinary multibyte character (neither
- * `%' nor a white-space character); or a conversion specification. Each
- * conversion specification is introduced by the `%' character. After the `%',
- * the following appear in sequence:
- *
- * .iP "" 4
- * An optional assignment-suppressing character `*'.
- * .iP
- * An optional non-zero decimal integer that specifies the maximum field
- * width.
- * .iP
- * An optional `h' or `l' (el) indicating the size of the receiving
- * object. The conversion specifiers `d', `i', and `n' should be preceded by
- * `h' if the corresponding argument is a pointer to `short int' rather
- * than a pointer to `int', or by `l' if it is a pointer to `long int'.
- * Similarly, the conversion specifiers `o', `u', and `x' shall be preceded
- * by `h' if the corresponding argument is a pointer to `unsigned short int'
- * rather than a pointer to `unsigned int', or by `l' if it is a pointer to
- * `unsigned long int'. Finally, the conversion specifiers `e', `f', and `g'
- * shall be preceded by `l' if the corresponding argument is a pointer to
- * `double' rather than a pointer to `float'. If an `h' or `l' appears
- * with any other conversion specifier, the behavior is undefined.
- *
- * &WARNING: ANSI C also specifies an optional `L' in some of the same
- * contexts as `l' above, corresponding to a `long double *' argument.
- * However, the current release of the VxWorks libraries does not support
- * `long double' data; using the optional `L' gives unpredictable results.
- * .iP
- * A character that specifies the type of conversion to be applied. The
- * valid conversion specifiers are described below.
- * .LP
- *
- * The fscanf() routine executes each directive of the format in turn. If a
- * directive fails, as detailed below, fscanf() returns. Failures
- * are described as input failures (due to the unavailability of input
- * characters), or matching failures (due to inappropriate input).
- *
- * A directive composed of white-space character(s) is executed by reading
- * input up to the first non-white-space character (which remains unread),
- * or until no more characters can be read.
- *
- * A directive that is an ordinary multibyte character is executed by reading
- * the next characters of the stream. If one of the characters differs from
- * one comprising the directive, the directive fails, and the differing and
- * subsequent characters remain unread.
- *
- * A directive that is a conversion specification defines a set of matching
- * input sequences, as described below for each specifier. A conversion
- * specification is executed in the following steps:
- *
- * Input white-space characters (as specified by the isspace() function) are
- * skipped, unless the specification includes a `[', `c', or `n' specifier.
- *
- * An input item is read from the stream, unless the specification includes
- * an `n' specifier. An input item is defined as the longest matching
- * sequence of input characters, unless that exceeds a specified field width,
- * in which case it is the initial subsequence of that length in the
- * sequence. The first character, if any, after the input item remains
- * unread. If the length of the input item is zero, the execution of the
- * directive fails: this condition is a matching failure, unless an error
- * prevented input from the stream, in which case it is an input failure.
- *
- * Except in the case of a `%' specifier, the input item is converted to a
- * type appropriate to the conversion specifier. If the input item is not a
- * matching sequence, the execution of the directive fails: this condition
- * is a matching failure. Unless assignment suppression was indicated by a
- * `*', the result of the conversion is placed in the object pointed to by
- * the first argument following the <fmt> argument that has not already
- * received a conversion result. If this object does not have an appropriate
- * type, or if the result of the conversion cannot be represented in the
- * space provided, the behavior is undefined.
- *
- * The following conversion specifiers are valid:
- *
- * .iP `d'
- * Matches an optionally signed decimal integer whose format is
- * the same as expected for the subject sequence of the strtol()
- * function with the value 10 for the <base> argument. The
- * corresponding argument should be a pointer to `int'.
- * .iP `i'
- * Matches an optionally signed integer, whose format is the
- * same as expected for the subject sequence of the strtol()
- * function with the value 0 for the <base> argument. The
- * corresponding argument should be a pointer to `int'.
- * .iP `o'
- * Matches an optionally signed octal integer, whose format is the
- * same as expected for the subject sequence of the strtoul()
- * function with the value 8 for the <base> argument. The
- * corresponding argument should be a pointer to `unsigned int'.
- * .iP `u'
- * Matches an optionally signed decimal integer, whose format is
- * the same as expected for the subject sequence of the strtoul()
- * function with the value 10 for the <base> argument. The
- * corresponding argument should be a pointer to `unsigned int'.
- * .iP `x'
- * Matches an optionally signed hexadecimal integer, whose format is
- * the same as expected for the subject sequence of the strtoul()
- * function with the value 16 for the <base> argument. The
- * corresponding argument should be a pointer to `unsigned int'.
- * .iP "`e', `f', `g'"
- * Match an optionally signed floating-point number, whose format
- * is the same as expected for the subject string of the strtod()
- * function. The corresponding argument should be a pointer to `float'.
- * .iP `s'
- * Matches a sequence of non-white-space characters. The
- * corresponding argument should be a pointer to the initial
- * character of an array large enough to accept the sequence
- * and a terminating null character, which will be added
- * automatically.
- * .iP `['
- * Matches a non-empty sequence of characters from a set of
- * expected characters (the `scanset'). The corresponding argument
- * should be a pointer to the initial character of an array large
- * enough to accept the sequence and a terminating null character,
- * which is added automatically. The conversion specifier
- * includes all subsequent character in the format string, up to
- * and including the matching right bracket (`]'). The characters
- * between the brackets (the `scanlist') comprise the scanset,
- * unless the character after the left bracket is a circumflex (`^')
- * in which case the scanset contains all characters that do not
- * appear in the scanlist between the circumflex and the right
- * bracket. If the conversion specifier begins with "[]" or "[^]", the
- * right bracket character is in the scanlist and the next
- * right bracket character is the matching right bracket that ends
- * the specification; otherwise the first right bracket character
- * is the one that ends the specification.
- * .iP `c'
- * Matches a sequence of characters of the number specified by the
- * field width (1 if no field width is present in the directive).
- * The corresponding argument should be a pointer to the initial
- * character of an array large enough to accept the sequence.
- * No null character is added.
- * .iP `p'
- * Matches an implementation-defined set of sequences, which should be
- * the same as the set of sequences that may be produced by the %p
- * conversion of the fprintf() function. The corresponding argument
- * should be a pointer to a pointer to `void'. VxWorks defines its
- * pointer input field to be consistent with pointers written by the
- * fprintf() function ("0x" hexadecimal notation). If the input item is
- * a value converted earlier during the same program execution, the
- * pointer that results should compare equal to that value; otherwise
- * the behavior of the %p conversion is undefined.
- * .iP `n'
- * No input is consumed. The corresponding argument should be a pointer to
- * `int' into which the number of characters read from the input stream so
- * far by this call to fscanf() is written. Execution of a %n directive does
- * not increment the assignment count returned when fscanf() completes
- * execution.
- * .iP `%'
- * Matches a single `%'; no conversion or assignment occurs. The
- * complete conversion specification is %%.
- * .LP
- *
- * If a conversion specification is invalid, the behavior is undefined.
- *
- * The conversion specifiers `E', `G', and `X' are also valid and behave the
- * same as `e', `g', and `x', respectively.
- *
- * If end-of-file is encountered during input, conversion is terminated. If
- * end-of-file occurs before any characters matching the current directive
- * have been read (other than leading white space, where permitted), execution
- * of the current directive terminates with an input failure; otherwise, unless
- * execution of the current directive is terminated with a matching failure,
- * execution of the following directive (if any) is terminated with an input
- * failure.
- *
- * If conversion terminates on a conflicting input character, the offending
- * input character is left unread in the input stream. Trailing white space
- * (including new-line characters) is left unread unless matched by a
- * directive. The success of literal matches and suppressed assignments is
- * not directly determinable other than via the %n directive.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS:
- * The number of input items assigned, which can be fewer than provided for,
- * or even zero, in the event of an early matching failure; or EOF if an
- * input failure occurs before any conversion.
- *
- * SEE ALSO: scanf(), sscanf()
- */
- int fscanf
- (
- FILE * fp, /* stream to read from */
- char const * fmt, /* format string */
- ... /* arguments to format string */
- )
- {
- int nArgs;
- int unget;
- va_list vaList; /* vararg list */
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (EOF);
- va_start (vaList, fmt);
- nArgs = fioScanV (fmt, fgetc, (int) fp, &unget, vaList);
- va_end (vaList);
- if (unget != -1)
- ungetc (unget, fp);
- return (nArgs);
- }
- /* fseek.c - seek a position in a file. stdio.h */
- /* Copyright 1992-1993 Wind River Systems, Inc. */
- /*
- modification history
- --------------------
- 01c,05mar93,jdi documentation cleanup for 5.1.
- 01b,20sep92,smb documentation additions
- 01a,29jul92,jcf Added OBJ_VERIFY
- +smb taken from UCB stdio, removed fstat().
- */
- /*
- DESCRIPTION
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- INCLUDE FILE: stdio.h, sys/types.h, sys/stat.h, fcntl.h, stdlib.h, errno.h
- SEE ALSO: American National Standard X3.159-1989
- NOMANUAL
- */
- #include "vxWorks.h"
- #include "stdio.h"
- #include "ioLib.h"
- #include "sys/types.h"
- #include "sys/stat.h"
- #include "fcntl.h"
- #include "stdlib.h"
- #include "errno.h"
- #include "objLib.h"
- #include "private/stdioP.h"
- #define POS_ERR (-(fpos_t)1)
- /******************************************************************************
- *
- * fseek - set the file position indicator for a stream (ANSI)
- *
- * This routine sets the file position indicator for a specified stream.
- * For a binary stream, the new position, measured in characters from the
- * beginning of the file, is obtained by adding <offset> to the position
- * specified by <whence>, whose possible values are:
- * .iP SEEK_SET 16
- * the beginning of the file.
- * .iP SEEK_CUR
- * the current value of the file position indicator.
- * .iP SEEK_END
- * the end of the file.
- * .LP
- * A binary stream does not meaningfully
- * support fseek() calls with a <whence> value of SEEK_END.
- *
- * For a text stream, either <offset> is zero, or <offset> is a value
- * returned by an earlier call to ftell() on the stream, in which case
- * <whence> should be SEEK_SET.
- *
- * A successful call to fseek() clears the end-of-file indicator for the
- * stream and undoes any effects of ungetc() on the same stream. After an
- * fseek() call, the next operation on an update stream can be either input
- * or output.
- *
- * INCLUDE FILES: stdio.h
- *
- * RETURNS: Non-zero only for a request that cannot be satisfied.
- *
- * ERRNO: EINVAL
- *
- * SEE ALSO: ftell()
- */
- int fseek
- (
- FAST FILE * fp, /* stream */
- long offset, /* offset from <whence> */
- int whence /* position to offset from: */
- /* SEEK_SET = beginning */
- /* SEEK_CUR = current position */
- /* SEEK_END = end-of-file */
- )
- {
- fpos_t target;
- fpos_t curoff;
- size_t n;
- struct stat st;
- int havepos;
- BOOL doStat;
- if (OBJ_VERIFY (fp, fpClassId) != OK)
- return (ERROR);
- /*
- * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
- * After this, whence is either SEEK_SET or SEEK_END.
- */
- switch (whence)
- {
- case SEEK_CUR:
- /*
- * In order to seek relative to the current stream offset,
- * we have to first find the current stream offset a la
- * ftell (see ftell for details).
- */
- if (fp->_flags & __SOFF)
- curoff = fp->_offset;
- else
- {
- curoff = __sseek (fp, (fpos_t)0, SEEK_CUR);
- if (curoff == -1L)
- return (EOF);
- }
- if (fp->_flags & __SRD)
- {
- curoff -= fp->_r;
- if (HASUB(fp))
- curoff -= fp->_ur;
- }
- else if (fp->_flags & __SWR && fp->_p != NULL)
- curoff += fp->_p - fp->_bf._base;
- offset += curoff;
- whence = SEEK_SET;
- havepos = 1;
- break;
- case SEEK_SET:
- case SEEK_END:
- curoff = 0; /* XXX just to keep gcc quiet */
- havepos = 0;
- break;
- default:
- errno = EINVAL;
- return (EOF);
- }
- /*
- * Can only optimise if:
- * reading (and not reading-and-writing);
- * not unbuffered; and