forked from KolibriOS/kolibrios
upload sdk
git-svn-id: svn://kolibrios.org@4349 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
71
contrib/sdk/sources/newlib/stdio/clearerr.c
Normal file
71
contrib/sdk/sources/newlib/stdio/clearerr.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<clearerr>>---clear file or stream error indicator
|
||||
|
||||
INDEX
|
||||
clearerr
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
void clearerr(FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
void clearerr(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
The <<stdio>> functions maintain an error indicator with each file
|
||||
pointer <[fp]>, to record whether any read or write errors have
|
||||
occurred on the associated file or stream. Similarly, it maintains an
|
||||
end-of-file indicator to record whether there is no more data in the
|
||||
file.
|
||||
|
||||
Use <<clearerr>> to reset both of these indicators.
|
||||
|
||||
See <<ferror>> and <<feof>> to query the two indicators.
|
||||
|
||||
|
||||
RETURNS
|
||||
<<clearerr>> does not return a result.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<clearerr>>.
|
||||
|
||||
No supporting OS subroutines are required.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/* A subroutine version of the macro clearerr. */
|
||||
|
||||
#undef clearerr
|
||||
|
||||
_VOID
|
||||
_DEFUN(clearerr, (fp),
|
||||
FILE * fp)
|
||||
{
|
||||
CHECK_INIT(_REENT, fp);
|
||||
_flockfile (fp);
|
||||
__sclearerr (fp);
|
||||
_funlockfile (fp);
|
||||
}
|
82
contrib/sdk/sources/newlib/stdio/diprintf.c
Normal file
82
contrib/sdk/sources/newlib/stdio/diprintf.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/* Copyright (C) 2005, 2007 Shaun Jackman
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<diprintf>>, <<vdiprintf>>---print to a file descriptor (integer only)
|
||||
|
||||
INDEX
|
||||
diprintf
|
||||
INDEX
|
||||
_diprintf_r
|
||||
INDEX
|
||||
vdiprintf
|
||||
INDEX
|
||||
_vdiprintf_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
int diprintf(int <[fd]>, const char *<[format]>, ...);
|
||||
int vdiprintf(int <[fd]>, const char *<[format]>, va_list <[ap]>);
|
||||
int _diprintf_r(struct _reent *<[ptr]>, int <[fd]>,
|
||||
const char *<[format]>, ...);
|
||||
int _vdiprintf_r(struct _reent *<[ptr]>, int <[fd]>,
|
||||
const char *<[format]>, va_list <[ap]>);
|
||||
|
||||
DESCRIPTION
|
||||
<<diprintf>> and <<vdiprintf>> are similar to <<dprintf>> and <<vdprintf>>,
|
||||
except that only integer format specifiers are processed.
|
||||
|
||||
The functions <<_diprintf_r>> and <<_vdiprintf_r>> are simply
|
||||
reentrant versions of the functions above.
|
||||
|
||||
RETURNS
|
||||
Similar to <<dprintf>> and <<vdprintf>>.
|
||||
|
||||
PORTABILITY
|
||||
This set of functions is an integer-only extension, and is not portable.
|
||||
|
||||
Supporting OS subroutines required: <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int
|
||||
_DEFUN(_diprintf_r, (ptr, fd, format),
|
||||
struct _reent *ptr _AND
|
||||
int fd _AND
|
||||
const char *format _DOTS)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
va_start (ap, format);
|
||||
n = _vdiprintf_r (ptr, fd, format, ap);
|
||||
va_end (ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(diprintf, (fd, format),
|
||||
int fd _AND
|
||||
const char *format _DOTS)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
va_start (ap, format);
|
||||
n = _vdiprintf_r (_REENT, fd, format, ap);
|
||||
va_end (ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
87
contrib/sdk/sources/newlib/stdio/dprintf.c
Normal file
87
contrib/sdk/sources/newlib/stdio/dprintf.c
Normal file
@@ -0,0 +1,87 @@
|
||||
/* Copyright 2005, 2007 Shaun Jackman
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<dprintf>>, <<vdprintf>>---print to a file descriptor
|
||||
|
||||
INDEX
|
||||
dprintf
|
||||
INDEX
|
||||
_dprintf_r
|
||||
INDEX
|
||||
vdprintf
|
||||
INDEX
|
||||
_vdprintf_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
int dprintf(int <[fd]>, const char *<[format]>, ...);
|
||||
int vdprintf(int <[fd]>, const char *<[format]>, va_list <[ap]>);
|
||||
int _dprintf_r(struct _reent *<[ptr]>, int <[fd]>,
|
||||
const char *<[format]>, ...);
|
||||
int _vdprintf_r(struct _reent *<[ptr]>, int <[fd]>,
|
||||
const char *<[format]>, va_list <[ap]>);
|
||||
|
||||
DESCRIPTION
|
||||
<<dprintf>> and <<vdprintf>> allow printing a format, similarly to
|
||||
<<printf>>, but write to a file descriptor instead of to a <<FILE>>
|
||||
stream.
|
||||
|
||||
The functions <<_dprintf_r>> and <<_vdprintf_r>> are simply
|
||||
reentrant versions of the functions above.
|
||||
|
||||
RETURNS
|
||||
The return value and errors are exactly as for <<write>>, except that
|
||||
<<errno>> may also be set to <<ENOMEM>> if the heap is exhausted.
|
||||
|
||||
PORTABILITY
|
||||
This function is originally a GNU extension in glibc and is not portable.
|
||||
|
||||
Supporting OS subroutines required: <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_dprintf_r, (ptr, fd, format),
|
||||
struct _reent *ptr _AND
|
||||
int fd _AND
|
||||
const char *format _DOTS)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
va_start (ap, format);
|
||||
n = _vdprintf_r (ptr, fd, format, ap);
|
||||
va_end (ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(dprintf, (fd, format),
|
||||
int fd _AND
|
||||
const char *format _DOTS)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
struct _reent *ptr = _REENT;
|
||||
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
va_start (ap, format);
|
||||
n = _vdprintf_r (ptr, fd, format, ap);
|
||||
va_end (ap);
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
119
contrib/sdk/sources/newlib/stdio/fclose.c
Normal file
119
contrib/sdk/sources/newlib/stdio/fclose.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fclose>>---close a file
|
||||
|
||||
INDEX
|
||||
fclose
|
||||
INDEX
|
||||
_fclose_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fclose(FILE *<[fp]>);
|
||||
int _fclose_r(struct _reent *<[reent]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fclose(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
int fclose(<[fp]>)
|
||||
struct _reent *<[reent]>
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
If the file or stream identified by <[fp]> is open, <<fclose>> closes
|
||||
it, after first ensuring that any pending data is written (by calling
|
||||
<<fflush(<[fp]>)>>).
|
||||
|
||||
The alternate function <<_fclose_r>> is a reentrant version.
|
||||
The extra argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
<<fclose>> returns <<0>> if successful (including when <[fp]> is
|
||||
<<NULL>> or not an open file); otherwise, it returns <<EOF>>.
|
||||
|
||||
PORTABILITY
|
||||
<<fclose>> is required by ANSI C.
|
||||
|
||||
Required OS subroutines: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>,
|
||||
<<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/lock.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_fclose_r, (rptr, fp),
|
||||
struct _reent *rptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
int r;
|
||||
|
||||
if (fp == NULL)
|
||||
return (0); /* on NULL */
|
||||
|
||||
CHECK_INIT (rptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
if (fp->_flags == 0) /* not open! */
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return (0);
|
||||
}
|
||||
/* Unconditionally flush to allow special handling for seekable read
|
||||
files to reposition file to last byte processed as opposed to
|
||||
last byte read ahead into the buffer. */
|
||||
r = _fflush_r (rptr, fp);
|
||||
if (fp->_close != NULL && fp->_close (rptr, fp->_cookie) < 0)
|
||||
r = EOF;
|
||||
if (fp->_flags & __SMBF)
|
||||
_free_r (rptr, (char *) fp->_bf._base);
|
||||
if (HASUB (fp))
|
||||
FREEUB (rptr, fp);
|
||||
if (HASLB (fp))
|
||||
FREELB (rptr, fp);
|
||||
__sfp_lock_acquire ();
|
||||
fp->_flags = 0; /* release this FILE for reuse */
|
||||
_funlockfile (fp);
|
||||
#ifndef __SINGLE_THREAD__
|
||||
__lock_close_recursive (fp->_lock);
|
||||
#endif
|
||||
|
||||
__sfp_lock_release ();
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fclose, (fp),
|
||||
register FILE * fp)
|
||||
{
|
||||
return _fclose_r(_REENT, fp);
|
||||
}
|
||||
|
||||
#endif
|
144
contrib/sdk/sources/newlib/stdio/fdopen.c
Normal file
144
contrib/sdk/sources/newlib/stdio/fdopen.c
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fdopen>>---turn open file into a stream
|
||||
|
||||
INDEX
|
||||
fdopen
|
||||
INDEX
|
||||
_fdopen_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *fdopen(int <[fd]>, const char *<[mode]>);
|
||||
FILE *_fdopen_r(struct _reent *<[reent]>,
|
||||
int <[fd]>, const char *<[mode]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *fdopen(<[fd]>, <[mode]>)
|
||||
int <[fd]>;
|
||||
char *<[mode]>;
|
||||
|
||||
FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
|
||||
struct _reent *<[reent]>;
|
||||
int <[fd]>;
|
||||
char *<[mode]>);
|
||||
|
||||
DESCRIPTION
|
||||
<<fdopen>> produces a file descriptor of type <<FILE *>>, from a
|
||||
descriptor for an already-open file (returned, for example, by the
|
||||
system subroutine <<open>> rather than by <<fopen>>).
|
||||
The <[mode]> argument has the same meanings as in <<fopen>>.
|
||||
|
||||
RETURNS
|
||||
File pointer or <<NULL>>, as for <<fopen>>.
|
||||
|
||||
PORTABILITY
|
||||
<<fdopen>> is ANSI.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
#include <_syslist.h>
|
||||
|
||||
FILE *
|
||||
_DEFUN(_fdopen_r, (ptr, fd, mode),
|
||||
struct _reent *ptr _AND
|
||||
int fd _AND
|
||||
_CONST char *mode)
|
||||
{
|
||||
register FILE *fp;
|
||||
int flags, oflags;
|
||||
#ifdef HAVE_FCNTL
|
||||
int fdflags, fdmode;
|
||||
#endif
|
||||
|
||||
if ((flags = __sflags (ptr, mode, &oflags)) == 0)
|
||||
return 0;
|
||||
|
||||
/* make sure the mode the user wants is a subset of the actual mode */
|
||||
#ifdef HAVE_FCNTL
|
||||
if ((fdflags = _fcntl_r (ptr, fd, F_GETFL, 0)) < 0)
|
||||
return 0;
|
||||
fdmode = fdflags & O_ACCMODE;
|
||||
if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
|
||||
{
|
||||
ptr->_errno = EBADF;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((fp = __sfp (ptr)) == 0)
|
||||
return 0;
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
fp->_flags = flags;
|
||||
/* POSIX recommends setting the O_APPEND bit on fd to match append
|
||||
streams. Someone may later clear O_APPEND on fileno(fp), but the
|
||||
stream must still remain in append mode. Rely on __sflags
|
||||
setting __SAPP properly. */
|
||||
#ifdef HAVE_FCNTL
|
||||
if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
|
||||
_fcntl_r (ptr, fd, F_SETFL, fdflags | O_APPEND);
|
||||
#endif
|
||||
fp->_file = fd;
|
||||
fp->_cookie = (_PTR) fp;
|
||||
|
||||
#undef _read
|
||||
#undef _write
|
||||
#undef _seek
|
||||
#undef _close
|
||||
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
|
||||
#ifdef __SCLE
|
||||
/* Explicit given mode results in explicit setting mode on fd */
|
||||
if (oflags & O_BINARY)
|
||||
setmode (fp->_file, O_BINARY);
|
||||
else if (oflags & O_TEXT)
|
||||
setmode (fp->_file, O_TEXT);
|
||||
if (__stextmode (fp->_file))
|
||||
fp->_flags |= __SCLE;
|
||||
#endif
|
||||
|
||||
_funlockfile (fp);
|
||||
return fp;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
FILE *
|
||||
_DEFUN(fdopen, (fd, mode),
|
||||
int fd _AND
|
||||
_CONST char *mode)
|
||||
{
|
||||
return _fdopen_r (_REENT, fd, mode);
|
||||
}
|
||||
|
||||
#endif
|
247
contrib/sdk/sources/newlib/stdio/fflush.c
Normal file
247
contrib/sdk/sources/newlib/stdio/fflush.c
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fflush>>---flush buffered file output
|
||||
|
||||
INDEX
|
||||
fflush
|
||||
INDEX
|
||||
_fflush_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fflush(FILE *<[fp]>);
|
||||
|
||||
int _fflush_r(struct _reent *<[reent]>, FILE *<[fp]>);
|
||||
|
||||
DESCRIPTION
|
||||
The <<stdio>> output functions can buffer output before delivering it
|
||||
to the host system, in order to minimize the overhead of system calls.
|
||||
|
||||
Use <<fflush>> to deliver any such pending output (for the file
|
||||
or stream identified by <[fp]>) to the host system.
|
||||
|
||||
If <[fp]> is <<NULL>>, <<fflush>> delivers pending output from all
|
||||
open files.
|
||||
|
||||
Additionally, if <[fp]> is a seekable input stream visiting a file
|
||||
descriptor, set the position of the file descriptor to match next
|
||||
unread byte, useful for obeying POSIX semantics when ending a process
|
||||
without consuming all input from the stream.
|
||||
|
||||
The alternate function <<_fflush_r>> is a reentrant version, where the
|
||||
extra argument <[reent]> is a pointer to a reentrancy structure, and
|
||||
<[fp]> must not be NULL.
|
||||
|
||||
RETURNS
|
||||
<<fflush>> returns <<0>> unless it encounters a write error; in that
|
||||
situation, it returns <<EOF>>.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fflush>>. The behavior on input streams is only
|
||||
specified by POSIX, and not all implementations follow POSIX rules.
|
||||
|
||||
No supporting OS subroutines are required.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
/* Flush a single file, or (if fp is NULL) all files. */
|
||||
|
||||
/* Core function which does not lock file pointer. This gets called
|
||||
directly from __srefill. */
|
||||
int
|
||||
_DEFUN(__sflush_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
register unsigned char *p;
|
||||
register int n, t;
|
||||
|
||||
t = fp->_flags;
|
||||
if ((t & __SWR) == 0)
|
||||
{
|
||||
/* For a read stream, an fflush causes the next seek to be
|
||||
unoptimized (i.e. forces a system-level seek). This conforms
|
||||
to the POSIX and SUSv3 standards. */
|
||||
fp->_flags |= __SNPT;
|
||||
|
||||
/* For a seekable stream with buffered read characters, we will attempt
|
||||
a seek to the current position now. A subsequent read will then get
|
||||
the next byte from the file rather than the buffer. This conforms
|
||||
to the POSIX and SUSv3 standards. Note that the standards allow
|
||||
this seek to be deferred until necessary, but we choose to do it here
|
||||
to make the change simpler, more contained, and less likely
|
||||
to miss a code scenario. */
|
||||
if ((fp->_r > 0 || fp->_ur > 0) && fp->_seek != NULL)
|
||||
{
|
||||
int tmp_errno;
|
||||
#ifdef __LARGE64_FILES
|
||||
_fpos64_t curoff;
|
||||
#else
|
||||
_fpos_t curoff;
|
||||
#endif
|
||||
|
||||
/* Save last errno and set errno to 0, so we can check if a device
|
||||
returns with a valid position -1. We restore the last errno if
|
||||
no other error condition has been encountered. */
|
||||
tmp_errno = ptr->_errno;
|
||||
ptr->_errno = 0;
|
||||
/* Get the physical position we are at in the file. */
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else
|
||||
{
|
||||
/* We don't know current physical offset, so ask for it.
|
||||
Only ESPIPE and EINVAL are ignorable. */
|
||||
#ifdef __LARGE64_FILES
|
||||
if (fp->_flags & __SL64)
|
||||
curoff = fp->_seek64 (ptr, fp->_cookie, 0, SEEK_CUR);
|
||||
else
|
||||
#endif
|
||||
curoff = fp->_seek (ptr, fp->_cookie, 0, SEEK_CUR);
|
||||
if (curoff == -1L && ptr->_errno != 0)
|
||||
{
|
||||
int result = EOF;
|
||||
if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
|
||||
{
|
||||
result = 0;
|
||||
ptr->_errno = tmp_errno;
|
||||
}
|
||||
else
|
||||
fp->_flags |= __SERR;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
if (fp->_flags & __SRD)
|
||||
{
|
||||
/* Current offset is at end of buffer. Compensate for
|
||||
characters not yet read. */
|
||||
curoff -= fp->_r;
|
||||
if (HASUB (fp))
|
||||
curoff -= fp->_ur;
|
||||
}
|
||||
/* Now physically seek to after byte last read. */
|
||||
#ifdef __LARGE64_FILES
|
||||
if (fp->_flags & __SL64)
|
||||
curoff = fp->_seek64 (ptr, fp->_cookie, curoff, SEEK_SET);
|
||||
else
|
||||
#endif
|
||||
curoff = fp->_seek (ptr, fp->_cookie, curoff, SEEK_SET);
|
||||
if (curoff != -1 || ptr->_errno == 0
|
||||
|| ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
|
||||
{
|
||||
/* Seek successful or ignorable error condition.
|
||||
We can clear read buffer now. */
|
||||
fp->_flags &= ~__SNPT;
|
||||
fp->_r = 0;
|
||||
fp->_p = fp->_bf._base;
|
||||
if ((fp->_flags & __SOFF) && (curoff != -1 || ptr->_errno == 0))
|
||||
fp->_offset = curoff;
|
||||
ptr->_errno = tmp_errno;
|
||||
if (HASUB (fp))
|
||||
FREEUB (ptr, fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if ((p = fp->_bf._base) == NULL)
|
||||
{
|
||||
/* Nothing to flush. */
|
||||
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;
|
||||
|
||||
while (n > 0)
|
||||
{
|
||||
t = fp->_write (ptr, fp->_cookie, (char *) p, n);
|
||||
if (t <= 0)
|
||||
{
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
||||
p += t;
|
||||
n -= t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_DEFUN(_fflush_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#ifdef _REENT_SMALL
|
||||
/* For REENT_SMALL platforms, it is possible we are being
|
||||
called for the first time on a std stream. This std
|
||||
stream can belong to a reentrant struct that is not
|
||||
_REENT. If CHECK_INIT gets called below based on _REENT,
|
||||
we will end up changing said file pointers to the equivalent
|
||||
std stream off of _REENT. This causes unexpected behavior if
|
||||
there is any data to flush on the _REENT std stream. There
|
||||
are two alternatives to fix this: 1) make a reentrant fflush
|
||||
or 2) simply recognize that this file has nothing to flush
|
||||
and return immediately before performing a CHECK_INIT. Choice
|
||||
2 is implemented here due to its simplicity. */
|
||||
if (fp->_bf._base == NULL)
|
||||
return 0;
|
||||
#endif /* _REENT_SMALL */
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
if (!fp->_flags)
|
||||
return 0;
|
||||
|
||||
_flockfile (fp);
|
||||
ret = __sflush_r (ptr, fp);
|
||||
_funlockfile (fp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fflush, (fp),
|
||||
register FILE * fp)
|
||||
{
|
||||
if (fp == NULL)
|
||||
return _fwalk_reent (_GLOBAL_REENT, _fflush_r);
|
||||
|
||||
return _fflush_r (_REENT, fp);
|
||||
}
|
||||
|
||||
#endif /* _REENT_ONLY */
|
106
contrib/sdk/sources/newlib/stdio/fgetc.c
Normal file
106
contrib/sdk/sources/newlib/stdio/fgetc.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fgetc>>---get a character from a file or stream
|
||||
|
||||
INDEX
|
||||
fgetc
|
||||
INDEX
|
||||
_fgetc_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fgetc(FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
int _fgetc_r(struct _reent *<[ptr]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fgetc(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
int _fgetc_r(<[ptr]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use <<fgetc>> to get the next single character from the file or stream
|
||||
identified by <[fp]>. As a side effect, <<fgetc>> advances the file's
|
||||
current position indicator.
|
||||
|
||||
For a macro version of this function, see <<getc>>.
|
||||
|
||||
The function <<_fgetc_r>> is simply a reentrant version of
|
||||
<<fgetc>> that is passed the additional reentrant structure
|
||||
pointer argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
The next character (read as an <<unsigned char>>, and cast to
|
||||
<<int>>), unless there is no more data, or the host system reports a
|
||||
read error; in either of these situations, <<fgetc>> returns <<EOF>>.
|
||||
|
||||
You can distinguish the two situations that cause an <<EOF>> result by
|
||||
using the <<ferror>> and <<feof>> functions.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fgetc>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_fgetc_r, (ptr, fp),
|
||||
struct _reent * ptr _AND
|
||||
FILE * fp)
|
||||
{
|
||||
int result;
|
||||
CHECK_INIT(ptr, fp);
|
||||
_flockfile (fp);
|
||||
result = __sgetc_r (ptr, fp);
|
||||
_funlockfile (fp);
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fgetc, (fp),
|
||||
FILE * fp)
|
||||
{
|
||||
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
|
||||
int result;
|
||||
CHECK_INIT(_REENT, fp);
|
||||
_flockfile (fp);
|
||||
result = __sgetc_r (_REENT, fp);
|
||||
_funlockfile (fp);
|
||||
return result;
|
||||
#else
|
||||
return _fgetc_r (_REENT, fp);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
187
contrib/sdk/sources/newlib/stdio/fgets.c
Normal file
187
contrib/sdk/sources/newlib/stdio/fgets.c
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fgets>>---get character string from a file or stream
|
||||
|
||||
INDEX
|
||||
fgets
|
||||
INDEX
|
||||
_fgets_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
char *_fgets_r(struct _reent *<[ptr]>, char *<[buf]>, int <[n]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
char *fgets(<[buf]>,<[n]>,<[fp]>)
|
||||
char *<[buf]>;
|
||||
int <[n]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
char *_fgets_r(<[ptr]>, <[buf]>,<[n]>,<[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[buf]>;
|
||||
int <[n]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
Reads at most <[n-1]> characters from <[fp]> until a newline
|
||||
is found. The characters including to the newline are stored
|
||||
in <[buf]>. The buffer is terminated with a 0.
|
||||
|
||||
The <<_fgets_r>> function is simply the reentrant version of
|
||||
<<fgets>> and is passed an additional reentrancy structure
|
||||
pointer: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
<<fgets>> returns the buffer passed to it, with the data
|
||||
filled in. If end of file occurs with some data already
|
||||
accumulated, the data is returned with no other indication. If
|
||||
no data are read, NULL is returned instead.
|
||||
|
||||
PORTABILITY
|
||||
<<fgets>> should replace all uses of <<gets>>. Note however
|
||||
that <<fgets>> returns all of the data, while <<gets>> removes
|
||||
the trailing newline (with no indication that it has done so.)
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Read at most n-1 characters from the given file.
|
||||
* Stop when a newline has been read, or the count runs out.
|
||||
* Return first argument, or NULL if no characters were read.
|
||||
*/
|
||||
|
||||
char *
|
||||
_DEFUN(_fgets_r, (ptr, buf, n, fp),
|
||||
struct _reent * ptr _AND
|
||||
char *buf _AND
|
||||
int n _AND
|
||||
FILE * fp)
|
||||
{
|
||||
size_t len;
|
||||
char *s;
|
||||
unsigned char *p, *t;
|
||||
|
||||
if (n < 2) /* sanity check */
|
||||
return 0;
|
||||
|
||||
s = buf;
|
||||
|
||||
CHECK_INIT(ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE)
|
||||
{
|
||||
int c;
|
||||
/* Sorry, have to do it the slow way */
|
||||
while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
|
||||
{
|
||||
*s++ = c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
if (c == EOF && s == buf)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return NULL;
|
||||
}
|
||||
*s = 0;
|
||||
_funlockfile (fp);
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
|
||||
n--; /* leave space for NUL */
|
||||
do
|
||||
{
|
||||
/*
|
||||
* If the buffer is empty, refill it.
|
||||
*/
|
||||
if ((len = fp->_r) <= 0)
|
||||
{
|
||||
if (__srefill_r (ptr, fp))
|
||||
{
|
||||
/* EOF: stop with partial or no line */
|
||||
if (s == buf)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return 0;
|
||||
}
|
||||
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 = (unsigned char *) memchr ((_PTR) p, '\n', len);
|
||||
if (t != 0)
|
||||
{
|
||||
len = ++t - p;
|
||||
fp->_r -= len;
|
||||
fp->_p = t;
|
||||
_CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
|
||||
s[len] = 0;
|
||||
_funlockfile (fp);
|
||||
return (buf);
|
||||
}
|
||||
fp->_r -= len;
|
||||
fp->_p += len;
|
||||
_CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
|
||||
s += len;
|
||||
}
|
||||
while ((n -= len) != 0);
|
||||
*s = 0;
|
||||
_funlockfile (fp);
|
||||
return buf;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
char *
|
||||
_DEFUN(fgets, (buf, n, fp),
|
||||
char *buf _AND
|
||||
int n _AND
|
||||
FILE * fp)
|
||||
{
|
||||
return _fgets_r (_REENT, buf, n, fp);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
62
contrib/sdk/sources/newlib/stdio/fileno.c
Normal file
62
contrib/sdk/sources/newlib/stdio/fileno.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fileno>>---return file descriptor associated with stream
|
||||
|
||||
INDEX
|
||||
fileno
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fileno(FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fileno(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
You can use <<fileno>> to return the file descriptor identified by <[fp]>.
|
||||
|
||||
RETURNS
|
||||
<<fileno>> returns a non-negative integer when successful.
|
||||
If <[fp]> is not an open stream, <<fileno>> returns -1.
|
||||
|
||||
PORTABILITY
|
||||
<<fileno>> is not part of ANSI C.
|
||||
POSIX requires <<fileno>>.
|
||||
|
||||
Supporting OS subroutines required: none.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(fileno, (f),
|
||||
FILE * f)
|
||||
{
|
||||
int result;
|
||||
CHECK_INIT (_REENT, f);
|
||||
_flockfile (f);
|
||||
result = __sfileno (f);
|
||||
_funlockfile (f);
|
||||
return result;
|
||||
}
|
294
contrib/sdk/sources/newlib/stdio/findfp.c
Normal file
294
contrib/sdk/sources/newlib/stdio/findfp.c
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/lock.h>
|
||||
#include "local.h"
|
||||
|
||||
#ifdef _REENT_SMALL
|
||||
const struct __sFILE_fake __sf_fake_stdin =
|
||||
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
|
||||
const struct __sFILE_fake __sf_fake_stdout =
|
||||
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
|
||||
const struct __sFILE_fake __sf_fake_stderr =
|
||||
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
|
||||
#endif
|
||||
|
||||
static _VOID
|
||||
_DEFUN(std, (ptr, flags, file, data),
|
||||
FILE *ptr _AND
|
||||
int flags _AND
|
||||
int file _AND
|
||||
struct _reent *data)
|
||||
{
|
||||
ptr->_p = 0;
|
||||
ptr->_r = 0;
|
||||
ptr->_w = 0;
|
||||
ptr->_flags = flags;
|
||||
ptr->_flags2 = 0;
|
||||
ptr->_file = file;
|
||||
ptr->_bf._base = 0;
|
||||
ptr->_bf._size = 0;
|
||||
ptr->_lbfsize = 0;
|
||||
memset (&ptr->_mbstate, 0, sizeof (_mbstate_t));
|
||||
ptr->_cookie = ptr;
|
||||
ptr->_read = __sread;
|
||||
#ifndef __LARGE64_FILES
|
||||
ptr->_write = __swrite;
|
||||
#else /* __LARGE64_FILES */
|
||||
ptr->_write = __swrite64;
|
||||
ptr->_seek64 = __sseek64;
|
||||
ptr->_flags |= __SL64;
|
||||
#endif /* __LARGE64_FILES */
|
||||
ptr->_seek = __sseek;
|
||||
ptr->_close = __sclose;
|
||||
#if !defined(__SINGLE_THREAD__) && !defined(_REENT_SMALL)
|
||||
__lock_init_recursive (ptr->_lock);
|
||||
/*
|
||||
* #else
|
||||
* lock is already initialized in __sfp
|
||||
*/
|
||||
#endif
|
||||
|
||||
#ifdef __SCLE
|
||||
if (__stextmode (ptr->_file))
|
||||
ptr->_flags |= __SCLE;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct _glue *
|
||||
_DEFUN(__sfmoreglue, (d, n),
|
||||
struct _reent *d _AND
|
||||
register int n)
|
||||
{
|
||||
struct _glue *g;
|
||||
FILE *p;
|
||||
|
||||
g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
|
||||
if (g == NULL)
|
||||
return NULL;
|
||||
p = (FILE *) (g + 1);
|
||||
g->_next = NULL;
|
||||
g->_niobs = n;
|
||||
g->_iobs = p;
|
||||
memset (p, 0, n * sizeof (FILE));
|
||||
return g;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a free FILE for fopen et al.
|
||||
*/
|
||||
|
||||
FILE *
|
||||
_DEFUN(__sfp, (d),
|
||||
struct _reent *d)
|
||||
{
|
||||
FILE *fp;
|
||||
int n;
|
||||
struct _glue *g;
|
||||
|
||||
__sfp_lock_acquire ();
|
||||
|
||||
if (!_GLOBAL_REENT->__sdidinit)
|
||||
__sinit (_GLOBAL_REENT);
|
||||
for (g = &_GLOBAL_REENT->__sglue;; g = g->_next)
|
||||
{
|
||||
for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
|
||||
if (fp->_flags == 0)
|
||||
goto found;
|
||||
if (g->_next == NULL &&
|
||||
(g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
|
||||
break;
|
||||
}
|
||||
__sfp_lock_release ();
|
||||
d->_errno = ENOMEM;
|
||||
return NULL;
|
||||
|
||||
found:
|
||||
fp->_file = -1; /* no file */
|
||||
fp->_flags = 1; /* reserve this slot; caller sets real flags */
|
||||
fp->_flags2 = 0;
|
||||
#ifndef __SINGLE_THREAD__
|
||||
__lock_init_recursive (fp->_lock);
|
||||
#endif
|
||||
__sfp_lock_release ();
|
||||
|
||||
fp->_p = NULL; /* no current pointer */
|
||||
fp->_w = 0; /* nothing to read or write */
|
||||
fp->_r = 0;
|
||||
fp->_bf._base = NULL; /* no buffer */
|
||||
fp->_bf._size = 0;
|
||||
fp->_lbfsize = 0; /* not line buffered */
|
||||
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
|
||||
/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
|
||||
fp->_ub._base = NULL; /* no ungetc buffer */
|
||||
fp->_ub._size = 0;
|
||||
fp->_lb._base = NULL; /* no line buffer */
|
||||
fp->_lb._size = 0;
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
/*
|
||||
* exit() calls _cleanup() through *__cleanup, set whenever we
|
||||
* open or buffer a file. This chicanery is done so that programs
|
||||
* that do not use stdio need not link it all in.
|
||||
*
|
||||
* The name `_cleanup' is, alas, fairly well known outside stdio.
|
||||
*/
|
||||
|
||||
_VOID
|
||||
_DEFUN(_cleanup_r, (ptr),
|
||||
struct _reent *ptr)
|
||||
{
|
||||
_CAST_VOID _fwalk(ptr, fclose);
|
||||
/* _CAST_VOID _fwalk (ptr, fflush); */ /* `cheating' */
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
_VOID
|
||||
_DEFUN_VOID(_cleanup)
|
||||
{
|
||||
_cleanup_r (_GLOBAL_REENT);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* __sinit() is called whenever stdio's internal variables must be set up.
|
||||
*/
|
||||
|
||||
_VOID
|
||||
_DEFUN(__sinit, (s),
|
||||
struct _reent *s)
|
||||
{
|
||||
__sinit_lock_acquire ();
|
||||
|
||||
if (s->__sdidinit)
|
||||
{
|
||||
__sinit_lock_release ();
|
||||
return;
|
||||
}
|
||||
|
||||
/* make sure we clean up on exit */
|
||||
s->__cleanup = _cleanup_r; /* conservative */
|
||||
s->__sdidinit = 1;
|
||||
|
||||
s->__sglue._next = NULL;
|
||||
#ifndef _REENT_SMALL
|
||||
s->__sglue._niobs = 3;
|
||||
s->__sglue._iobs = &s->__sf[0];
|
||||
#else
|
||||
s->__sglue._niobs = 0;
|
||||
s->__sglue._iobs = NULL;
|
||||
s->_stdin = __sfp(s);
|
||||
s->_stdout = __sfp(s);
|
||||
s->_stderr = __sfp(s);
|
||||
#endif
|
||||
|
||||
std (s->_stdin, __SRD, 0, s);
|
||||
|
||||
/* On platforms that have true file system I/O, we can verify
|
||||
whether stdout is an interactive terminal or not, as part of
|
||||
__smakebuf on first use of the stream. For all other platforms,
|
||||
we will default to line buffered mode here. Technically, POSIX
|
||||
requires both stdin and stdout to be line-buffered, but tradition
|
||||
leaves stdin alone on systems without fcntl. */
|
||||
#ifdef HAVE_FCNTL
|
||||
std (s->_stdout, __SWR, 1, s);
|
||||
#else
|
||||
std (s->_stdout, __SWR | __SLBF, 1, s);
|
||||
#endif
|
||||
|
||||
/* POSIX requires stderr to be opened for reading and writing, even
|
||||
when the underlying fd 2 is write-only. */
|
||||
std (s->_stderr, __SRW | __SNBF, 2, s);
|
||||
|
||||
__sinit_lock_release ();
|
||||
}
|
||||
|
||||
#ifndef __SINGLE_THREAD__
|
||||
|
||||
__LOCK_INIT_RECURSIVE(static, __sfp_lock);
|
||||
__LOCK_INIT_RECURSIVE(static, __sinit_lock);
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__sfp_lock_acquire)
|
||||
{
|
||||
__lock_acquire_recursive (__sfp_lock);
|
||||
}
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__sfp_lock_release)
|
||||
{
|
||||
__lock_release_recursive (__sfp_lock);
|
||||
}
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__sinit_lock_acquire)
|
||||
{
|
||||
__lock_acquire_recursive (__sinit_lock);
|
||||
}
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__sinit_lock_release)
|
||||
{
|
||||
__lock_release_recursive (__sinit_lock);
|
||||
}
|
||||
|
||||
/* Walkable file locking routine. */
|
||||
static int
|
||||
_DEFUN(__fp_lock, (ptr),
|
||||
FILE * ptr)
|
||||
{
|
||||
_flockfile (ptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Walkable file unlocking routine. */
|
||||
static int
|
||||
_DEFUN(__fp_unlock, (ptr),
|
||||
FILE * ptr)
|
||||
{
|
||||
_funlockfile (ptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__fp_lock_all)
|
||||
{
|
||||
__sfp_lock_acquire ();
|
||||
|
||||
_CAST_VOID _fwalk (_REENT, __fp_lock);
|
||||
}
|
||||
|
||||
_VOID
|
||||
_DEFUN_VOID(__fp_unlock_all)
|
||||
{
|
||||
_CAST_VOID _fwalk (_REENT, __fp_unlock);
|
||||
|
||||
__sfp_lock_release ();
|
||||
}
|
||||
#endif
|
55
contrib/sdk/sources/newlib/stdio/fiprintf.c
Normal file
55
contrib/sdk/sources/newlib/stdio/fiprintf.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in siprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int
|
||||
_DEFUN(_fiprintf_r, (ptr, fp, fmt),
|
||||
struct _reent *ptr _AND
|
||||
FILE * fp _AND
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = _vfiprintf_r (ptr, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fiprintf, (fp, fmt),
|
||||
FILE * fp _AND
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = _vfiprintf_r (_REENT, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
78
contrib/sdk/sources/newlib/stdio/fiscanf.c
Normal file
78
contrib/sdk/sources/newlib/stdio/fiscanf.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
fiscanf(FILE *fp, _CONST char *fmt, ...)
|
||||
#else
|
||||
fiscanf(FILE *fp, fmt, va_alist)
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = __svfiscanf_r (_REENT, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_fiscanf_r(struct _reent *ptr, FILE *fp, _CONST char *fmt, ...)
|
||||
#else
|
||||
_fiscanf_r(ptr, FILE *fp, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = __svfiscanf_r (ptr, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return (ret);
|
||||
}
|
||||
|
86
contrib/sdk/sources/newlib/stdio/flags.c
Normal file
86
contrib/sdk/sources/newlib/stdio/flags.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 1990 Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92 */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/*
|
||||
* Return the (stdio) flags for a given mode. Store the flags
|
||||
* to be passed to an open() syscall through *optr.
|
||||
* Return 0 on error.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__sflags, (ptr, mode, optr),
|
||||
struct _reent *ptr _AND
|
||||
register char *mode _AND
|
||||
int *optr)
|
||||
{
|
||||
register int ret, m, o;
|
||||
|
||||
switch (mode[0])
|
||||
{
|
||||
case 'r': /* open for reading */
|
||||
ret = __SRD;
|
||||
m = O_RDONLY;
|
||||
o = 0;
|
||||
break;
|
||||
|
||||
case 'w': /* open for writing */
|
||||
ret = __SWR;
|
||||
m = O_WRONLY;
|
||||
o = O_CREAT | O_TRUNC;
|
||||
break;
|
||||
|
||||
case 'a': /* open for appending */
|
||||
ret = __SWR | __SAPP;
|
||||
m = O_WRONLY;
|
||||
o = O_CREAT | O_APPEND;
|
||||
break;
|
||||
default: /* illegal mode */
|
||||
ptr->_errno = EINVAL;
|
||||
return (0);
|
||||
}
|
||||
if (mode[1] && (mode[1] == '+' || mode[2] == '+'))
|
||||
{
|
||||
ret = (ret & ~(__SRD | __SWR)) | __SRW;
|
||||
m = O_RDWR;
|
||||
}
|
||||
if (mode[1] && (mode[1] == 'b' || mode[2] == 'b'))
|
||||
{
|
||||
#ifdef O_BINARY
|
||||
m |= O_BINARY;
|
||||
#endif
|
||||
}
|
||||
#ifdef __CYGWIN__
|
||||
else if (mode[1] && (mode[1] == 't' || mode[2] == 't'))
|
||||
#else
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#ifdef O_TEXT
|
||||
m |= O_TEXT;
|
||||
#endif
|
||||
}
|
||||
*optr = m | o;
|
||||
return ret;
|
||||
}
|
32
contrib/sdk/sources/newlib/stdio/floatio.h
Normal file
32
contrib/sdk/sources/newlib/stdio/floatio.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* %W% (Berkeley) %G%
|
||||
*/
|
||||
|
||||
/*
|
||||
* Floating point scanf/printf (input/output) definitions.
|
||||
*/
|
||||
|
||||
#ifdef _NO_LONGDBL
|
||||
/* 11-bit exponent (VAX G floating point) is 308 decimal digits */
|
||||
#define MAXEXP 308
|
||||
#else /* !_NO_LONGDBL */
|
||||
/* 15-bit exponent (Intel extended floating point) is 4932 decimal digits */
|
||||
#define MAXEXP 4932
|
||||
#endif /* !_NO_LONGDBL */
|
||||
/* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
|
||||
#define MAXFRACT 39
|
184
contrib/sdk/sources/newlib/stdio/fopen.c
Normal file
184
contrib/sdk/sources/newlib/stdio/fopen.c
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fopen>>---open a file
|
||||
|
||||
INDEX
|
||||
fopen
|
||||
INDEX
|
||||
_fopen_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *fopen(const char *<[file]>, const char *<[mode]>);
|
||||
|
||||
FILE *_fopen_r(struct _reent *<[reent]>,
|
||||
const char *<[file]>, const char *<[mode]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *fopen(<[file]>, <[mode]>)
|
||||
char *<[file]>;
|
||||
char *<[mode]>;
|
||||
|
||||
FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
|
||||
struct _reent *<[reent]>;
|
||||
char *<[file]>;
|
||||
char *<[mode]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fopen>> initializes the data structures needed to read or write a
|
||||
file. Specify the file's name as the string at <[file]>, and the kind
|
||||
of access you need to the file with the string at <[mode]>.
|
||||
|
||||
The alternate function <<_fopen_r>> is a reentrant version.
|
||||
The extra argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
Three fundamental kinds of access are available: read, write, and append.
|
||||
<<*<[mode]>>> must begin with one of the three characters `<<r>>',
|
||||
`<<w>>', or `<<a>>', to select one of these:
|
||||
|
||||
o+
|
||||
o r
|
||||
Open the file for reading; the operation will fail if the file does
|
||||
not exist, or if the host system does not permit you to read it.
|
||||
|
||||
o w
|
||||
Open the file for writing @emph{from the beginning} of the file:
|
||||
effectively, this always creates a new file. If the file whose name you
|
||||
specified already existed, its old contents are discarded.
|
||||
|
||||
o a
|
||||
Open the file for appending data, that is writing from the end of
|
||||
file. When you open a file this way, all data always goes to the
|
||||
current end of file; you cannot change this using <<fseek>>.
|
||||
o-
|
||||
|
||||
Some host systems distinguish between ``binary'' and ``text'' files.
|
||||
Such systems may perform data transformations on data written to, or
|
||||
read from, files opened as ``text''.
|
||||
If your system is one of these, then you can append a `<<b>>' to any
|
||||
of the three modes above, to specify that you are opening the file as
|
||||
a binary file (the default is to open the file as a text file).
|
||||
|
||||
`<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
|
||||
`<<ab>>', ``append binary''.
|
||||
|
||||
To make C programs more portable, the `<<b>>' is accepted on all
|
||||
systems, whether or not it makes a difference.
|
||||
|
||||
Finally, you might need to both read and write from the same file.
|
||||
You can also append a `<<+>>' to any of the three modes, to permit
|
||||
this. (If you want to append both `<<b>>' and `<<+>>', you can do it
|
||||
in either order: for example, <<"rb+">> means the same thing as
|
||||
<<"r+b">> when used as a mode string.)
|
||||
|
||||
Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
|
||||
an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
|
||||
to create a new file (or begin by discarding all data from an old one)
|
||||
that permits reading and writing anywhere in it; and <<"a+">> (or
|
||||
<<"ab+">>) to permit reading anywhere in an existing file, but writing
|
||||
only at the end.
|
||||
|
||||
RETURNS
|
||||
<<fopen>> returns a file pointer which you can use for other file
|
||||
operations, unless the file you requested could not be opened; in that
|
||||
situation, the result is <<NULL>>. If the reason for failure was an
|
||||
invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
|
||||
|
||||
PORTABILITY
|
||||
<<fopen>> is required by ANSI C.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/lock.h>
|
||||
#ifdef __CYGWIN__
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
_DEFUN(_fopen_r, (ptr, file, mode),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *file _AND
|
||||
_CONST char *mode)
|
||||
{
|
||||
register FILE *fp;
|
||||
register int f;
|
||||
int flags, oflags;
|
||||
|
||||
if ((flags = __sflags (ptr, mode, &oflags)) == 0)
|
||||
return NULL;
|
||||
if ((fp = __sfp (ptr)) == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((f = _open_r (ptr, file, oflags, 0666)) < 0)
|
||||
{
|
||||
__sfp_lock_acquire ();
|
||||
fp->_flags = 0; /* release */
|
||||
#ifndef __SINGLE_THREAD__
|
||||
__lock_close_recursive (fp->_lock);
|
||||
#endif
|
||||
__sfp_lock_release ();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
fp->_file = f;
|
||||
fp->_flags = flags;
|
||||
fp->_cookie = (_PTR) fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
|
||||
if (fp->_flags & __SAPP)
|
||||
_fseek_r (ptr, fp, 0, SEEK_END);
|
||||
|
||||
#ifdef __SCLE
|
||||
if (__stextmode (fp->_file))
|
||||
fp->_flags |= __SCLE;
|
||||
#endif
|
||||
|
||||
_funlockfile (fp);
|
||||
return fp;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
FILE *
|
||||
_DEFUN(fopen, (file, mode),
|
||||
_CONST char *file _AND
|
||||
_CONST char *mode)
|
||||
{
|
||||
return _fopen_r (_REENT, file, mode);
|
||||
}
|
||||
|
||||
#endif
|
55
contrib/sdk/sources/newlib/stdio/fprintf.c
Normal file
55
contrib/sdk/sources/newlib/stdio/fprintf.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in sprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
int
|
||||
_DEFUN(_fprintf_r, (ptr, fp, fmt),
|
||||
struct _reent *ptr _AND
|
||||
FILE *fp _AND
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = _vfprintf_r (ptr, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fprintf, (fp, fmt),
|
||||
FILE *fp _AND
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
ret = _vfprintf_r (_REENT, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
109
contrib/sdk/sources/newlib/stdio/fputc.c
Normal file
109
contrib/sdk/sources/newlib/stdio/fputc.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fputc>>---write a character on a stream or file
|
||||
|
||||
INDEX
|
||||
fputc
|
||||
INDEX
|
||||
_fputc_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fputc(int <[ch]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
int _fputc_r(struct _rent *<[ptr]>, int <[ch]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fputc(<[ch]>, <[fp]>)
|
||||
int <[ch]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
int _fputc_r(<[ptr]>, <[ch]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
int <[ch]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fputc>> converts the argument <[ch]> from an <<int>> to an
|
||||
<<unsigned char>>, then writes it to the file or stream identified by
|
||||
<[fp]>.
|
||||
|
||||
If the file was opened with append mode (or if the stream cannot
|
||||
support positioning), then the new character goes at the end of the
|
||||
file or stream. Otherwise, the new character is written at the
|
||||
current value of the position indicator, and the position indicator
|
||||
oadvances by one.
|
||||
|
||||
For a macro version of this function, see <<putc>>.
|
||||
|
||||
The <<_fputc_r>> function is simply a reentrant version of <<fputc>>
|
||||
that takes an additional reentrant structure argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
If successful, <<fputc>> returns its argument <[ch]>. If an error
|
||||
intervenes, the result is <<EOF>>. You can use `<<ferror(<[fp]>)>>' to
|
||||
query for errors.
|
||||
|
||||
PORTABILITY
|
||||
<<fputc>> is required by ANSI C.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_fputc_r, (ptr, ch, file),
|
||||
struct _reent *ptr _AND
|
||||
int ch _AND
|
||||
FILE * file)
|
||||
{
|
||||
int result;
|
||||
CHECK_INIT(ptr, file);
|
||||
_flockfile (file);
|
||||
result = _putc_r (ptr, ch, file);
|
||||
_funlockfile (file);
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
int
|
||||
_DEFUN(fputc, (ch, file),
|
||||
int ch _AND
|
||||
FILE * file)
|
||||
{
|
||||
#if !defined(__OPTIMIZE_SIZE__) && !defined(PREFER_SIZE_OVER_SPEED)
|
||||
int result;
|
||||
CHECK_INIT(_REENT, file);
|
||||
_flockfile (file);
|
||||
result = _putc_r (_REENT, ch, file);
|
||||
_funlockfile (file);
|
||||
return result;
|
||||
#else
|
||||
return _fputc_r (_REENT, ch, file);
|
||||
#endif
|
||||
}
|
||||
#endif /* !_REENT_ONLY */
|
106
contrib/sdk/sources/newlib/stdio/fputs.c
Normal file
106
contrib/sdk/sources/newlib/stdio/fputs.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fputs>>---write a character string in a file or stream
|
||||
|
||||
INDEX
|
||||
fputs
|
||||
INDEX
|
||||
_fputs_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fputs(const char *<[s]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
int _fputs_r(struct _reent *<[ptr]>, const char *<[s]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fputs(<[s]>, <[fp]>)
|
||||
char *<[s]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
int _fputs_r(<[ptr]>, <[s]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[s]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fputs>> writes the string at <[s]> (but without the trailing null)
|
||||
to the file or stream identified by <[fp]>.
|
||||
|
||||
<<_fputs_r>> is simply the reentrant version of <<fputs>> that takes
|
||||
an additional reentrant struct pointer argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
If successful, the result is <<0>>; otherwise, the result is <<EOF>>.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fputs>>, but does not specify that the result on
|
||||
success must be <<0>>; any non-negative value is permitted.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "fvwrite.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Write the given string to the given file.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(_fputs_r, (ptr, s, fp),
|
||||
struct _reent * ptr _AND
|
||||
char _CONST * s _AND
|
||||
FILE * fp)
|
||||
{
|
||||
int result;
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
iov.iov_base = s;
|
||||
iov.iov_len = uio.uio_resid = strlen (s);
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
|
||||
CHECK_INIT(ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
ORIENT (fp, -1);
|
||||
result = __sfvwrite_r (ptr, fp, &uio);
|
||||
_funlockfile (fp);
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
int
|
||||
_DEFUN(fputs, (s, fp),
|
||||
char _CONST * s _AND
|
||||
FILE * fp)
|
||||
{
|
||||
return _fputs_r (_REENT, s, fp);
|
||||
}
|
||||
#endif /* !_REENT_ONLY */
|
177
contrib/sdk/sources/newlib/stdio/fputwc.c
Normal file
177
contrib/sdk/sources/newlib/stdio/fputwc.c
Normal file
@@ -0,0 +1,177 @@
|
||||
/*-
|
||||
* Copyright (c) 2002-2004 Tim J. Robbins.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fputwc>>, <<putwc>>---write a wide character on a stream or file
|
||||
|
||||
INDEX
|
||||
fputwc
|
||||
INDEX
|
||||
_fputwc_r
|
||||
INDEX
|
||||
putwc
|
||||
INDEX
|
||||
_putwc_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t fputwc(wchar_t <[wc]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t _fputwc_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t putwc(wchar_t <[wc]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t _putwc_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t fputwc(<[wc]>, <[fp]>)
|
||||
wchar_t <[wc]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t _fputwc_r(<[ptr]>, <[wc]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
wchar_t <[wc]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t putwc(<[wc]>, <[fp]>)
|
||||
wchar_t <[wc]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
wint_t _putwc_r(<[ptr]>, <[wc]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
wchar_t <[wc]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fputwc>> writes the wide character argument <[wc]> to the file or
|
||||
stream identified by <[fp]>.
|
||||
|
||||
If the file was opened with append mode (or if the stream cannot
|
||||
support positioning), then the new wide character goes at the end of the
|
||||
file or stream. Otherwise, the new wide character is written at the
|
||||
current value of the position indicator, and the position indicator
|
||||
oadvances by one.
|
||||
|
||||
The <<putwc>> function or macro functions identically to <<fputwc>>. It
|
||||
may be implemented as a macro, and may evaluate its argument more than
|
||||
once. There is no reason ever to use it.
|
||||
|
||||
The <<_fputwc_r>> and <<_putwc_r>> functions are simply reentrant versions
|
||||
of <<fputwc>> and <<putwc>> that take an additional reentrant structure
|
||||
argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
If successful, <<fputwc>> and <<putwc>> return their argument <[wc]>.
|
||||
If an error intervenes, the result is <<EOF>>. You can use
|
||||
`<<ferror(<[fp]>)>>' to query for errors.
|
||||
|
||||
PORTABILITY
|
||||
C99, POSIX.1-2001
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include "local.h"
|
||||
|
||||
static wint_t
|
||||
_DEFUN(__fputwc, (ptr, wc, fp),
|
||||
struct _reent *ptr _AND
|
||||
wchar_t wc _AND
|
||||
FILE *fp)
|
||||
{
|
||||
char buf[MB_LEN_MAX];
|
||||
size_t i, len;
|
||||
|
||||
if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX)
|
||||
{
|
||||
/*
|
||||
* Assume single-byte locale with no special encoding.
|
||||
* A more careful test would be to check
|
||||
* _CurrentRuneLocale->encoding.
|
||||
*/
|
||||
*buf = (unsigned char)wc;
|
||||
len = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((len = _wcrtomb_r (ptr, buf, wc, &fp->_mbstate)) == (size_t) -1)
|
||||
{
|
||||
fp->_flags |= __SERR;
|
||||
return WEOF;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
if (__sputc_r (ptr, (unsigned char) buf[i], fp) == EOF)
|
||||
return WEOF;
|
||||
|
||||
return (wint_t) wc;
|
||||
}
|
||||
|
||||
wint_t
|
||||
_DEFUN(_fputwc_r, (ptr, wc, fp),
|
||||
struct _reent *ptr _AND
|
||||
wchar_t wc _AND
|
||||
FILE *fp)
|
||||
{
|
||||
wint_t r;
|
||||
|
||||
_flockfile (fp);
|
||||
ORIENT(fp, 1);
|
||||
r = __fputwc(ptr, wc, fp);
|
||||
_funlockfile (fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
wint_t
|
||||
_DEFUN(fputwc, (wc, fp),
|
||||
wchar_t wc _AND
|
||||
FILE *fp)
|
||||
{
|
||||
CHECK_INIT(_REENT, fp);
|
||||
return _fputwc_r (_REENT, wc, fp);
|
||||
}
|
258
contrib/sdk/sources/newlib/stdio/fread.c
Normal file
258
contrib/sdk/sources/newlib/stdio/fread.c
Normal file
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fread>>---read array elements from a file
|
||||
|
||||
INDEX
|
||||
fread
|
||||
INDEX
|
||||
_fread_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
|
||||
FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
size_t _fread_r(struct _reent *<[ptr]>, void *<[buf]>,
|
||||
size_t <[size]>, size_t <[count]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>)
|
||||
char *<[buf]>;
|
||||
size_t <[size]>;
|
||||
size_t <[count]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
size_t _fread_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[buf]>;
|
||||
size_t <[size]>;
|
||||
size_t <[count]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fread>> attempts to copy, from the file or stream identified by
|
||||
<[fp]>, <[count]> elements (each of size <[size]>) into memory,
|
||||
starting at <[buf]>. <<fread>> may copy fewer elements than
|
||||
<[count]> if an error, or end of file, intervenes.
|
||||
|
||||
<<fread>> also advances the file position indicator (if any) for
|
||||
<[fp]> by the number of @emph{characters} actually read.
|
||||
|
||||
<<_fread_r>> is simply the reentrant version of <<fread>> that
|
||||
takes an additional reentrant structure pointer argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
The result of <<fread>> is the number of elements it succeeded in
|
||||
reading.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fread>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __SCLE
|
||||
static size_t
|
||||
_DEFUN(crlf_r, (ptr, fp, buf, count, eof),
|
||||
struct _reent * ptr _AND
|
||||
FILE * fp _AND
|
||||
char * buf _AND
|
||||
size_t count _AND
|
||||
int eof)
|
||||
{
|
||||
int r;
|
||||
char *s, *d, *e;
|
||||
|
||||
if (count == 0)
|
||||
return 0;
|
||||
|
||||
e = buf + count;
|
||||
for (s=d=buf; s<e-1; s++)
|
||||
{
|
||||
if (*s == '\r' && s[1] == '\n')
|
||||
s++;
|
||||
*d++ = *s;
|
||||
}
|
||||
if (s < e)
|
||||
{
|
||||
if (*s == '\r')
|
||||
{
|
||||
int c = __sgetc_raw_r (ptr, fp);
|
||||
if (c == '\n')
|
||||
*s = '\n';
|
||||
else
|
||||
ungetc (c, fp);
|
||||
}
|
||||
*d++ = *s++;
|
||||
}
|
||||
|
||||
|
||||
while (d < e)
|
||||
{
|
||||
r = _getc_r (ptr, fp);
|
||||
if (r == EOF)
|
||||
return count - (e-d);
|
||||
*d++ = r;
|
||||
}
|
||||
|
||||
return count;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
size_t
|
||||
_DEFUN(_fread_r, (ptr, buf, size, count, fp),
|
||||
struct _reent * ptr _AND
|
||||
_PTR buf _AND
|
||||
size_t size _AND
|
||||
size_t count _AND
|
||||
FILE * fp)
|
||||
{
|
||||
register size_t resid;
|
||||
register char *p;
|
||||
register int r;
|
||||
size_t total;
|
||||
|
||||
if ((resid = count * size) == 0)
|
||||
return 0;
|
||||
|
||||
CHECK_INIT(ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
ORIENT (fp, -1);
|
||||
if (fp->_r < 0)
|
||||
fp->_r = 0;
|
||||
total = resid;
|
||||
p = buf;
|
||||
|
||||
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
|
||||
|
||||
/* Optimize unbuffered I/O. */
|
||||
if (fp->_flags & __SNBF)
|
||||
{
|
||||
/* First copy any available characters from ungetc buffer. */
|
||||
int copy_size = resid > fp->_r ? fp->_r : resid;
|
||||
_CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, (size_t) copy_size);
|
||||
fp->_p += copy_size;
|
||||
fp->_r -= copy_size;
|
||||
p += copy_size;
|
||||
resid -= copy_size;
|
||||
|
||||
/* If still more data needed, free any allocated ungetc buffer. */
|
||||
if (HASUB (fp) && resid > 0)
|
||||
FREEUB (ptr, fp);
|
||||
|
||||
/* Finally read directly into user's buffer if needed. */
|
||||
while (resid > 0)
|
||||
{
|
||||
int rc = 0;
|
||||
/* save fp buffering state */
|
||||
void *old_base = fp->_bf._base;
|
||||
void * old_p = fp->_p;
|
||||
int old_size = fp->_bf._size;
|
||||
/* allow __refill to use user's buffer */
|
||||
fp->_bf._base = (unsigned char *) p;
|
||||
fp->_bf._size = resid;
|
||||
fp->_p = (unsigned char *) p;
|
||||
rc = __srefill_r (ptr, fp);
|
||||
/* restore fp buffering back to original state */
|
||||
fp->_bf._base = old_base;
|
||||
fp->_bf._size = old_size;
|
||||
fp->_p = old_p;
|
||||
resid -= fp->_r;
|
||||
p += fp->_r;
|
||||
fp->_r = 0;
|
||||
if (rc)
|
||||
{
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return crlf_r (ptr, fp, buf, total-resid, 1) / size;
|
||||
}
|
||||
#endif
|
||||
_funlockfile (fp);
|
||||
return (total - resid) / size;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
|
||||
{
|
||||
while (resid > (r = fp->_r))
|
||||
{
|
||||
_CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, (size_t) r);
|
||||
fp->_p += r;
|
||||
/* fp->_r = 0 ... done in __srefill */
|
||||
p += r;
|
||||
resid -= r;
|
||||
if (__srefill_r (ptr, fp))
|
||||
{
|
||||
/* no more input: return partial result */
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return crlf_r (ptr, fp, buf, total-resid, 1) / size;
|
||||
}
|
||||
#endif
|
||||
_funlockfile (fp);
|
||||
return (total - resid) / size;
|
||||
}
|
||||
}
|
||||
_CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, resid);
|
||||
fp->_r -= resid;
|
||||
fp->_p += resid;
|
||||
}
|
||||
|
||||
/* Perform any CR/LF clean-up if necessary. */
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return crlf_r(ptr, fp, buf, total, 0) / size;
|
||||
}
|
||||
#endif
|
||||
_funlockfile (fp);
|
||||
return count;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
size_t
|
||||
_DEFUN(fread, (buf, size, count, fp),
|
||||
_PTR buf _AND
|
||||
size_t size _AND
|
||||
size_t count _AND
|
||||
FILE * fp)
|
||||
{
|
||||
return _fread_r (_REENT, buf, size, count, fp);
|
||||
}
|
||||
#endif
|
247
contrib/sdk/sources/newlib/stdio/freopen.c
Normal file
247
contrib/sdk/sources/newlib/stdio/freopen.c
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<freopen>>---open a file using an existing file descriptor
|
||||
|
||||
INDEX
|
||||
freopen
|
||||
INDEX
|
||||
_freopen_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *freopen(const char *<[file]>, const char *<[mode]>,
|
||||
FILE *<[fp]>);
|
||||
FILE *_freopen_r(struct _reent *<[ptr]>, const char *<[file]>,
|
||||
const char *<[mode]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *freopen(<[file]>, <[mode]>, <[fp]>)
|
||||
char *<[file]>;
|
||||
char *<[mode]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
FILE *_freopen_r(<[ptr]>, <[file]>, <[mode]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[file]>;
|
||||
char *<[mode]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use this variant of <<fopen>> if you wish to specify a particular file
|
||||
descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
|
||||
the file.
|
||||
|
||||
If <[fp]> was associated with another file or stream, <<freopen>>
|
||||
closes that other file or stream (but ignores any errors while closing
|
||||
it).
|
||||
|
||||
<[file]> and <[mode]> are used just as in <<fopen>>.
|
||||
|
||||
If <[file]> is <<NULL>>, the underlying stream is modified rather than
|
||||
closed. The file cannot be given a more permissive access mode (for
|
||||
example, a <[mode]> of "w" will fail on a read-only file descriptor),
|
||||
but can change status such as append or binary mode. If modification
|
||||
is not possible, failure occurs.
|
||||
|
||||
RETURNS
|
||||
If successful, the result is the same as the argument <[fp]>. If the
|
||||
file cannot be opened as specified, the result is <<NULL>>.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<freopen>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/lock.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Re-direct an existing, open (probably) file to some other file.
|
||||
*/
|
||||
|
||||
FILE *
|
||||
_DEFUN(_freopen_r, (ptr, file, mode, fp),
|
||||
struct _reent *ptr _AND
|
||||
const char *file _AND
|
||||
const char *mode _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
register int f;
|
||||
int flags, oflags;
|
||||
int e = 0;
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
if ((flags = __sflags (ptr, mode, &oflags)) == 0)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
_fclose_r (ptr, fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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,
|
||||
* so that, e.g., freopen("/dev/stdin", "r", stdin) works.
|
||||
* This is unnecessary if it was not a Unix file.
|
||||
*/
|
||||
|
||||
if (fp->_flags == 0)
|
||||
fp->_flags = __SEOF; /* hold on to it */
|
||||
else
|
||||
{
|
||||
if (fp->_flags & __SWR)
|
||||
_fflush_r (ptr, fp);
|
||||
/*
|
||||
* If close is NULL, closing is a no-op, hence pointless.
|
||||
* If file is NULL, the file should not be closed.
|
||||
*/
|
||||
if (fp->_close != NULL && file != NULL)
|
||||
fp->_close (ptr, fp->_cookie);
|
||||
}
|
||||
|
||||
/*
|
||||
* Now get a new descriptor to refer to the new file, or reuse the
|
||||
* existing file descriptor if file is NULL.
|
||||
*/
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
f = _open_r (ptr, (char *) file, oflags, 0666);
|
||||
e = ptr->_errno;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef HAVE_FCNTL
|
||||
int oldflags;
|
||||
/*
|
||||
* Reuse the file descriptor, but only if the new access mode is
|
||||
* equal or less permissive than the old. F_SETFL correctly
|
||||
* ignores creation flags.
|
||||
*/
|
||||
f = fp->_file;
|
||||
if ((oldflags = _fcntl_r (ptr, f, F_GETFL, 0)) == -1
|
||||
|| ! ((oldflags & O_ACCMODE) == O_RDWR
|
||||
|| ((oldflags ^ oflags) & O_ACCMODE) == 0)
|
||||
|| _fcntl_r (ptr, f, F_SETFL, oflags) == -1)
|
||||
f = -1;
|
||||
#else
|
||||
/* We cannot modify without fcntl support. */
|
||||
f = -1;
|
||||
#endif
|
||||
|
||||
#ifdef __SCLE
|
||||
/*
|
||||
* F_SETFL doesn't change textmode. Don't mess with modes of ttys.
|
||||
*/
|
||||
if (0 <= f && ! _isatty_r (ptr, f)
|
||||
&& setmode (f, oflags & (O_BINARY | O_TEXT)) == -1)
|
||||
f = -1;
|
||||
#endif
|
||||
|
||||
if (f < 0)
|
||||
{
|
||||
e = EBADF;
|
||||
if (fp->_close != NULL)
|
||||
fp->_close (ptr, fp->_cookie);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 (fp->_flags & __SMBF)
|
||||
_free_r (ptr, (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 (ptr, fp);
|
||||
fp->_ub._size = 0;
|
||||
if (HASLB (fp))
|
||||
FREELB (ptr, fp);
|
||||
fp->_lb._size = 0;
|
||||
fp->_flags & ~__SORD;
|
||||
fp->_flags2 = 0;
|
||||
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
|
||||
|
||||
if (f < 0)
|
||||
{ /* did not get it after all */
|
||||
__sfp_lock_acquire ();
|
||||
fp->_flags = 0; /* set it free */
|
||||
ptr->_errno = e; /* restore in case _close clobbered */
|
||||
_funlockfile (fp);
|
||||
#ifndef __SINGLE_THREAD__
|
||||
__lock_close_recursive (fp->_lock);
|
||||
#endif
|
||||
__sfp_lock_release ();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fp->_flags = flags;
|
||||
fp->_file = f;
|
||||
fp->_cookie = (_PTR) fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
|
||||
#ifdef __SCLE
|
||||
if (__stextmode (fp->_file))
|
||||
fp->_flags |= __SCLE;
|
||||
#endif
|
||||
|
||||
_funlockfile (fp);
|
||||
return fp;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
FILE *
|
||||
_DEFUN(freopen, (file, mode, fp),
|
||||
_CONST char *file _AND
|
||||
_CONST char *mode _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
return _freopen_r (_REENT, file, mode, fp);
|
||||
}
|
||||
|
||||
#endif /*!_REENT_ONLY */
|
78
contrib/sdk/sources/newlib/stdio/fscanf.c
Normal file
78
contrib/sdk/sources/newlib/stdio/fscanf.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
fscanf(FILE *fp, _CONST char *fmt, ...)
|
||||
#else
|
||||
fscanf(FILE *fp, fmt, va_alist)
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _vfscanf_r (_REENT, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_fscanf_r(struct _reent *ptr, FILE *fp, _CONST char *fmt, ...)
|
||||
#else
|
||||
_fscanf_r(ptr, FILE *fp, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _vfscanf_r (ptr, fp, fmt, ap);
|
||||
va_end (ap);
|
||||
return (ret);
|
||||
}
|
||||
|
397
contrib/sdk/sources/newlib/stdio/fseek.c
Normal file
397
contrib/sdk/sources/newlib/stdio/fseek.c
Normal file
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fseek>>, <<fseeko>>---set file position
|
||||
|
||||
INDEX
|
||||
fseek
|
||||
INDEX
|
||||
fseeko
|
||||
INDEX
|
||||
_fseek_r
|
||||
INDEX
|
||||
_fseeko_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fseek(FILE *<[fp]>, long <[offset]>, int <[whence]>)
|
||||
int fseeko(FILE *<[fp]>, off_t <[offset]>, int <[whence]>)
|
||||
int _fseek_r(struct _reent *<[ptr]>, FILE *<[fp]>,
|
||||
long <[offset]>, int <[whence]>)
|
||||
int _fseeko_r(struct _reent *<[ptr]>, FILE *<[fp]>,
|
||||
off_t <[offset]>, int <[whence]>)
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int fseek(<[fp]>, <[offset]>, <[whence]>)
|
||||
FILE *<[fp]>;
|
||||
long <[offset]>;
|
||||
int <[whence]>;
|
||||
|
||||
int fseeko(<[fp]>, <[offset]>, <[whence]>)
|
||||
FILE *<[fp]>;
|
||||
off_t <[offset]>;
|
||||
int <[whence]>;
|
||||
|
||||
int _fseek_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fp]>;
|
||||
long <[offset]>;
|
||||
int <[whence]>;
|
||||
|
||||
int _fseeko_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fp]>;
|
||||
off_t <[offset]>;
|
||||
int <[whence]>;
|
||||
|
||||
DESCRIPTION
|
||||
Objects of type <<FILE>> can have a ``position'' that records how much
|
||||
of the file your program has already read. Many of the <<stdio>> functions
|
||||
depend on this position, and many change it as a side effect.
|
||||
|
||||
You can use <<fseek>>/<<fseeko>> to set the position for the file identified by
|
||||
<[fp]>. The value of <[offset]> determines the new position, in one
|
||||
of three ways selected by the value of <[whence]> (defined as macros
|
||||
in `<<stdio.h>>'):
|
||||
|
||||
<<SEEK_SET>>---<[offset]> is the absolute file position (an offset
|
||||
from the beginning of the file) desired. <[offset]> must be positive.
|
||||
|
||||
<<SEEK_CUR>>---<[offset]> is relative to the current file position.
|
||||
<[offset]> can meaningfully be either positive or negative.
|
||||
|
||||
<<SEEK_END>>---<[offset]> is relative to the current end of file.
|
||||
<[offset]> can meaningfully be either positive (to increase the size
|
||||
of the file) or negative.
|
||||
|
||||
See <<ftell>>/<<ftello>> to determine the current file position.
|
||||
|
||||
RETURNS
|
||||
<<fseek>>/<<fseeko>> return <<0>> when successful. On failure, the
|
||||
result is <<EOF>>. The reason for failure is indicated in <<errno>>:
|
||||
either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
|
||||
repositioning) or <<EINVAL>> (invalid file position).
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fseek>>.
|
||||
|
||||
<<fseeko>> is defined by the Single Unix specification.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include "local.h"
|
||||
|
||||
#define POS_ERR (-(_fpos_t)1)
|
||||
|
||||
/*
|
||||
* Seek the given file to the given offset.
|
||||
* `Whence' must be one of the three SEEK_* macros.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(_fseek_r, (ptr, fp, offset, whence),
|
||||
struct _reent *ptr _AND
|
||||
register FILE *fp _AND
|
||||
long offset _AND
|
||||
int whence)
|
||||
{
|
||||
_fpos_t _EXFNPTR(seekfn, (struct _reent *, _PTR, _fpos_t, int));
|
||||
_fpos_t target;
|
||||
_fpos_t curoff = 0;
|
||||
size_t n;
|
||||
#ifdef __USE_INTERNAL_STAT64
|
||||
struct stat64 st;
|
||||
#else
|
||||
struct stat st;
|
||||
#endif
|
||||
int havepos;
|
||||
|
||||
/* Make sure stdio is set up. */
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
/* If we've been doing some writing, and we're in append mode
|
||||
then we don't really know where the filepos is. */
|
||||
|
||||
if (fp->_flags & __SAPP && fp->_flags & __SWR)
|
||||
{
|
||||
/* So flush the buffer and seek to the end. */
|
||||
_fflush_r (ptr, fp);
|
||||
}
|
||||
|
||||
/* Have to be able to seek. */
|
||||
|
||||
if ((seekfn = fp->_seek) == NULL)
|
||||
{
|
||||
ptr->_errno = ESPIPE; /* ??? */
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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).
|
||||
*/
|
||||
_fflush_r (ptr, fp); /* may adjust seek offset on append stream */
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else
|
||||
{
|
||||
curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
|
||||
if (curoff == -1L)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
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:
|
||||
havepos = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
ptr->_errno = EINVAL;
|
||||
_funlockfile (fp);
|
||||
return (EOF);
|
||||
}
|
||||
|
||||
/*
|
||||
* Can only optimise if:
|
||||
* reading (and not reading-and-writing);
|
||||
* not unbuffered; and
|
||||
* this is a `regular' Unix file (and hence seekfn==__sseek).
|
||||
* We must check __NBF first, because it is possible to have __NBF
|
||||
* and __SOPT both set.
|
||||
*/
|
||||
|
||||
if (fp->_bf._base == NULL)
|
||||
__smakebuf_r (ptr, fp);
|
||||
if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
|
||||
goto dumb;
|
||||
if ((fp->_flags & __SOPT) == 0)
|
||||
{
|
||||
if (seekfn != __sseek
|
||||
|| fp->_file < 0
|
||||
#ifdef __USE_INTERNAL_STAT64
|
||||
|| _fstat64_r (ptr, fp->_file, &st)
|
||||
#else
|
||||
|| _fstat_r (ptr, fp->_file, &st)
|
||||
#endif
|
||||
|| (st.st_mode & S_IFMT) != S_IFREG)
|
||||
{
|
||||
fp->_flags |= __SNPT;
|
||||
goto dumb;
|
||||
}
|
||||
#ifdef HAVE_BLKSIZE
|
||||
fp->_blksize = st.st_blksize;
|
||||
#else
|
||||
fp->_blksize = 1024;
|
||||
#endif
|
||||
fp->_flags |= __SOPT;
|
||||
}
|
||||
|
||||
/*
|
||||
* We are reading; we can try to optimise.
|
||||
* Figure out where we are going and where we are now.
|
||||
*/
|
||||
|
||||
if (whence == SEEK_SET)
|
||||
target = offset;
|
||||
else
|
||||
{
|
||||
#ifdef __USE_INTERNAL_STAT64
|
||||
if (_fstat64_r (ptr, fp->_file, &st))
|
||||
#else
|
||||
if (_fstat_r (ptr, fp->_file, &st))
|
||||
#endif
|
||||
goto dumb;
|
||||
target = st.st_size + offset;
|
||||
}
|
||||
if ((long)target != target)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
if (!havepos)
|
||||
{
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else
|
||||
{
|
||||
curoff = seekfn (ptr, fp->_cookie, 0L, SEEK_CUR);
|
||||
if (curoff == POS_ERR)
|
||||
goto dumb;
|
||||
}
|
||||
curoff -= fp->_r;
|
||||
if (HASUB (fp))
|
||||
curoff -= fp->_ur;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the number of bytes in the input buffer (pretending
|
||||
* that any ungetc() input has been discarded). Adjust current
|
||||
* offset backwards by this count so that it represents the
|
||||
* file offset for the first byte in the current input buffer.
|
||||
*/
|
||||
|
||||
if (HASUB (fp))
|
||||
{
|
||||
curoff += fp->_r; /* kill off ungetc */
|
||||
n = fp->_up - fp->_bf._base;
|
||||
curoff -= n;
|
||||
n += fp->_ur;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = fp->_p - fp->_bf._base;
|
||||
curoff -= n;
|
||||
n += fp->_r;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the target offset is within the current buffer,
|
||||
* simply adjust the pointers, clear EOF, undo ungetc(),
|
||||
* and return.
|
||||
*/
|
||||
|
||||
if (target >= curoff && target < curoff + n)
|
||||
{
|
||||
register int o = target - curoff;
|
||||
|
||||
fp->_p = fp->_bf._base + o;
|
||||
fp->_r = n - o;
|
||||
if (HASUB (fp))
|
||||
FREEUB (ptr, fp);
|
||||
fp->_flags &= ~__SEOF;
|
||||
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
|
||||
_funlockfile (fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The place we want to get to is not within the current buffer,
|
||||
* but we can still be kind to the kernel copyout mechanism.
|
||||
* By aligning the file offset to a block boundary, we can let
|
||||
* the kernel use the VM hardware to map pages instead of
|
||||
* copying bytes laboriously. Using a block boundary also
|
||||
* ensures that we only read one block, rather than two.
|
||||
*/
|
||||
|
||||
curoff = target & ~(fp->_blksize - 1);
|
||||
if (seekfn (ptr, fp->_cookie, curoff, SEEK_SET) == POS_ERR)
|
||||
goto dumb;
|
||||
fp->_r = 0;
|
||||
fp->_p = fp->_bf._base;
|
||||
if (HASUB (fp))
|
||||
FREEUB (ptr, fp);
|
||||
fp->_flags &= ~__SEOF;
|
||||
n = target - curoff;
|
||||
if (n)
|
||||
{
|
||||
if (__srefill_r (ptr, fp) || fp->_r < n)
|
||||
goto dumb;
|
||||
fp->_p += n;
|
||||
fp->_r -= n;
|
||||
}
|
||||
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
|
||||
_funlockfile (fp);
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* We get here if we cannot optimise the seek ... just
|
||||
* do it. Allow the seek function to change fp->_bf._base.
|
||||
*/
|
||||
|
||||
dumb:
|
||||
if (_fflush_r (ptr, fp)
|
||||
|| seekfn (ptr, fp->_cookie, offset, whence) == POS_ERR)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
/* success: clear EOF indicator and discard ungetc() data */
|
||||
if (HASUB (fp))
|
||||
FREEUB (ptr, fp);
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = 0;
|
||||
/* fp->_w = 0; *//* unnecessary (I think...) */
|
||||
fp->_flags &= ~__SEOF;
|
||||
/* Reset no-optimization flag after successful seek. The
|
||||
no-optimization flag may be set in the case of a read
|
||||
stream that is flushed which by POSIX/SUSv3 standards,
|
||||
means that a corresponding seek must not optimize. The
|
||||
optimization is then allowed if no subsequent flush
|
||||
is performed. */
|
||||
fp->_flags &= ~__SNPT;
|
||||
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
|
||||
_funlockfile (fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fseek, (fp, offset, whence),
|
||||
register FILE *fp _AND
|
||||
long offset _AND
|
||||
int whence)
|
||||
{
|
||||
return _fseek_r (_REENT, fp, offset, whence);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
44
contrib/sdk/sources/newlib/stdio/fseeko.c
Normal file
44
contrib/sdk/sources/newlib/stdio/fseeko.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2002, Red Hat Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
_DEFUN(_fseeko_r, (ptr, fp, offset, whence),
|
||||
struct _reent *ptr _AND
|
||||
register FILE *fp _AND
|
||||
_off_t offset _AND
|
||||
int whence)
|
||||
{
|
||||
return _fseek_r (ptr, fp, (long)offset, whence);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(fseeko, (fp, offset, whence),
|
||||
register FILE *fp _AND
|
||||
_off_t offset _AND
|
||||
int whence)
|
||||
{
|
||||
/* for now we simply cast since off_t should be long */
|
||||
return _fseek_r (_REENT, fp, (long)offset, whence);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
177
contrib/sdk/sources/newlib/stdio/ftell.c
Normal file
177
contrib/sdk/sources/newlib/stdio/ftell.c
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<ftell>>, <<ftello>>---return position in a stream or file
|
||||
|
||||
INDEX
|
||||
ftell
|
||||
INDEX
|
||||
ftello
|
||||
INDEX
|
||||
_ftell_r
|
||||
INDEX
|
||||
_ftello_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
long ftell(FILE *<[fp]>);
|
||||
off_t ftello(FILE *<[fp]>);
|
||||
long _ftell_r(struct _reent *<[ptr]>, FILE *<[fp]>);
|
||||
off_t _ftello_r(struct _reent *<[ptr]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
long ftell(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
off_t ftello(<[fp]>)
|
||||
FILE *<[fp]>;
|
||||
|
||||
long _ftell_r(<[ptr]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
off_t _ftello_r(<[ptr]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
Objects of type <<FILE>> can have a ``position'' that records how much
|
||||
of the file your program has already read. Many of the <<stdio>> functions
|
||||
depend on this position, and many change it as a side effect.
|
||||
|
||||
The result of <<ftell>>/<<ftello>> is the current position for a file
|
||||
identified by <[fp]>. If you record this result, you can later
|
||||
use it with <<fseek>>/<<fseeko>> to return the file to this
|
||||
position. The difference between <<ftell>> and <<ftello>> is that
|
||||
<<ftell>> returns <<long>> and <<ftello>> returns <<off_t>>.
|
||||
|
||||
In the current implementation, <<ftell>>/<<ftello>> simply uses a character
|
||||
count to represent the file position; this is the same number that
|
||||
would be recorded by <<fgetpos>>.
|
||||
|
||||
RETURNS
|
||||
<<ftell>>/<<ftello>> return the file position, if possible. If they cannot do
|
||||
this, they return <<-1L>>. Failure occurs on streams that do not support
|
||||
positioning; the global <<errno>> indicates this condition with the
|
||||
value <<ESPIPE>>.
|
||||
|
||||
PORTABILITY
|
||||
<<ftell>> is required by the ANSI C standard, but the meaning of its
|
||||
result (when successful) is not specified beyond requiring that it be
|
||||
acceptable as an argument to <<fseek>>. In particular, other
|
||||
conforming C implementations may return a different result from
|
||||
<<ftell>> than what <<fgetpos>> records.
|
||||
|
||||
<<ftello>> is defined by the Single Unix specification.
|
||||
|
||||
No supporting OS subroutines are required.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
* ftell: return current offset.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
long
|
||||
_DEFUN(_ftell_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
_fpos_t pos;
|
||||
|
||||
/* Ensure stdio is set up. */
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
if (fp->_seek == NULL)
|
||||
{
|
||||
ptr->_errno = ESPIPE;
|
||||
_funlockfile (fp);
|
||||
return -1L;
|
||||
}
|
||||
|
||||
/* Find offset of underlying I/O object, then adjust for buffered
|
||||
bytes. Flush a write stream, since the offset may be altered if
|
||||
the stream is appending. Do not flush a read stream, since we
|
||||
must not lose the ungetc buffer. */
|
||||
if (fp->_flags & __SWR)
|
||||
_fflush_r (ptr, fp);
|
||||
if (fp->_flags & __SOFF)
|
||||
pos = fp->_offset;
|
||||
else
|
||||
{
|
||||
pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
|
||||
if (pos == -1L)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
if (fp->_flags & __SRD)
|
||||
{
|
||||
/*
|
||||
* Reading. Any unread characters (including
|
||||
* those from ungetc) cause the position to be
|
||||
* smaller than that in the underlying object.
|
||||
*/
|
||||
pos -= fp->_r;
|
||||
if (HASUB (fp))
|
||||
pos -= fp->_ur;
|
||||
}
|
||||
else if ((fp->_flags & __SWR) && fp->_p != NULL)
|
||||
{
|
||||
/*
|
||||
* Writing. Any buffered characters cause the
|
||||
* position to be greater than that in the
|
||||
* underlying object.
|
||||
*/
|
||||
pos += fp->_p - fp->_bf._base;
|
||||
}
|
||||
|
||||
_funlockfile (fp);
|
||||
if ((long)pos != pos)
|
||||
{
|
||||
pos = -1;
|
||||
ptr->_errno = EOVERFLOW;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
long
|
||||
_DEFUN(ftell, (fp),
|
||||
register FILE * fp)
|
||||
{
|
||||
return _ftell_r (_REENT, fp);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
40
contrib/sdk/sources/newlib/stdio/ftello.c
Normal file
40
contrib/sdk/sources/newlib/stdio/ftello.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2002, Red Hat Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
|
||||
_off_t
|
||||
_DEFUN(_ftello_r, (ptr, fp),
|
||||
struct _reent * ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
/* for now we simply cast since off_t should be long */
|
||||
return (_off_t)_ftell_r (ptr, fp);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
_off_t
|
||||
_DEFUN(ftello, (fp),
|
||||
register FILE * fp)
|
||||
{
|
||||
return (_off_t)_ftell_r (_REENT, fp);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
267
contrib/sdk/sources/newlib/stdio/fvwrite.c
Normal file
267
contrib/sdk/sources/newlib/stdio/fvwrite.c
Normal file
@@ -0,0 +1,267 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define COPY(n) _CAST_VOID memmove ((_PTR) fp->_p, (_PTR) p, (size_t) (n))
|
||||
|
||||
#define GETIOV(extra_work) \
|
||||
while (len == 0) \
|
||||
{ \
|
||||
extra_work; \
|
||||
p = iov->iov_base; \
|
||||
len = iov->iov_len; \
|
||||
iov++; \
|
||||
}
|
||||
|
||||
/*
|
||||
* Write some memory regions. Return zero on success, EOF on error.
|
||||
*
|
||||
* This routine is large and unsightly, but most of the ugliness due
|
||||
* to the three different kinds of output buffering is handled here.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__sfvwrite_r, (ptr, fp, uio),
|
||||
struct _reent *ptr _AND
|
||||
register FILE *fp _AND
|
||||
register struct __suio *uio)
|
||||
{
|
||||
register size_t len;
|
||||
register _CONST char *p = NULL;
|
||||
register struct __siov *iov;
|
||||
register int w, s;
|
||||
char *nl;
|
||||
int nlknown, nldist;
|
||||
|
||||
if ((len = uio->uio_resid) == 0)
|
||||
return 0;
|
||||
|
||||
/* make sure we can write */
|
||||
if (cantwrite (ptr, fp))
|
||||
return EOF;
|
||||
|
||||
iov = uio->uio_iov;
|
||||
len = 0;
|
||||
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE) /* text mode */
|
||||
{
|
||||
do
|
||||
{
|
||||
GETIOV (;);
|
||||
while (len > 0)
|
||||
{
|
||||
if (putc (*p, fp) == EOF)
|
||||
return EOF;
|
||||
p++;
|
||||
len--;
|
||||
uio->uio_resid--;
|
||||
}
|
||||
}
|
||||
while (uio->uio_resid > 0);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (fp->_flags & __SNBF)
|
||||
{
|
||||
/*
|
||||
* Unbuffered: write up to BUFSIZ bytes at a time.
|
||||
*/
|
||||
do
|
||||
{
|
||||
GETIOV (;);
|
||||
w = fp->_write (ptr, fp->_cookie, p, MIN (len, BUFSIZ));
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
p += w;
|
||||
len -= w;
|
||||
}
|
||||
while ((uio->uio_resid -= w) != 0);
|
||||
}
|
||||
else if ((fp->_flags & __SLBF) == 0)
|
||||
{
|
||||
/*
|
||||
* Fully buffered: fill partially full buffer, if any,
|
||||
* and then flush. If there is no partial buffer, write
|
||||
* one _bf._size byte chunk directly (without copying).
|
||||
*
|
||||
* String output is a special case: write as many bytes
|
||||
* as fit, but pretend we wrote everything. This makes
|
||||
* snprintf() return the number of bytes needed, rather
|
||||
* than the number used, and avoids its write function
|
||||
* (so that the write function can be invalid). If
|
||||
* we are dealing with the asprintf routines, we will
|
||||
* dynamically increase the buffer size as needed.
|
||||
*/
|
||||
do
|
||||
{
|
||||
GETIOV (;);
|
||||
w = fp->_w;
|
||||
if (fp->_flags & __SSTR)
|
||||
{
|
||||
if (len >= w && fp->_flags & (__SMBF | __SOPT))
|
||||
{ /* must be asprintf family */
|
||||
unsigned char *str;
|
||||
int curpos = (fp->_p - fp->_bf._base);
|
||||
/* Choose a geometric growth factor to avoid
|
||||
quadratic realloc behavior, but use a rate less
|
||||
than (1+sqrt(5))/2 to accomodate malloc
|
||||
overhead. asprintf EXPECTS us to overallocate, so
|
||||
that it can add a trailing \0 without
|
||||
reallocating. The new allocation should thus be
|
||||
max(prev_size*1.5, curpos+len+1). */
|
||||
int newsize = fp->_bf._size * 3 / 2;
|
||||
if (newsize < curpos + len + 1)
|
||||
newsize = curpos + len + 1;
|
||||
if (fp->_flags & __SOPT)
|
||||
{
|
||||
/* asnprintf leaves original buffer alone. */
|
||||
str = (unsigned char *)_malloc_r (ptr, newsize);
|
||||
if (!str)
|
||||
{
|
||||
ptr->_errno = ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
memcpy (str, fp->_bf._base, curpos);
|
||||
fp->_flags = (fp->_flags & ~__SOPT) | __SMBF;
|
||||
}
|
||||
else
|
||||
{
|
||||
str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
|
||||
newsize);
|
||||
if (!str)
|
||||
{
|
||||
/* Free buffer which is no longer used. */
|
||||
_free_r (ptr, fp->_bf._base);
|
||||
/* Ensure correct errno, even if free changed it. */
|
||||
ptr->_errno = ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
fp->_bf._base = str;
|
||||
fp->_p = str + curpos;
|
||||
fp->_bf._size = newsize;
|
||||
w = len;
|
||||
fp->_w = newsize - curpos;
|
||||
}
|
||||
if (len < w)
|
||||
w = len;
|
||||
COPY (w); /* copy MIN(fp->_w,len), */
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
w = len; /* but pretend copied all */
|
||||
}
|
||||
else if (fp->_p > fp->_bf._base && len > w)
|
||||
{
|
||||
/* fill and flush */
|
||||
COPY (w);
|
||||
/* fp->_w -= w; *//* unneeded */
|
||||
fp->_p += w;
|
||||
if (_fflush_r (ptr, fp))
|
||||
goto err;
|
||||
}
|
||||
else if (len >= (w = fp->_bf._size))
|
||||
{
|
||||
/* write directly */
|
||||
w = fp->_write (ptr, fp->_cookie, p, w);
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* fill and done */
|
||||
w = len;
|
||||
COPY (w);
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
}
|
||||
p += w;
|
||||
len -= w;
|
||||
}
|
||||
while ((uio->uio_resid -= w) != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* Line buffered: like fully buffered, but we
|
||||
* must check for newlines. Compute the distance
|
||||
* to the first newline (including the newline),
|
||||
* or `infinity' if there is none, then pretend
|
||||
* that the amount to write is MIN(len,nldist).
|
||||
*/
|
||||
nlknown = 0;
|
||||
nldist = 0;
|
||||
do
|
||||
{
|
||||
GETIOV (nlknown = 0);
|
||||
if (!nlknown)
|
||||
{
|
||||
nl = memchr ((_PTR) p, '\n', len);
|
||||
nldist = nl ? nl + 1 - p : len + 1;
|
||||
nlknown = 1;
|
||||
}
|
||||
s = MIN (len, nldist);
|
||||
w = fp->_w + fp->_bf._size;
|
||||
if (fp->_p > fp->_bf._base && s > w)
|
||||
{
|
||||
COPY (w);
|
||||
/* fp->_w -= w; */
|
||||
fp->_p += w;
|
||||
if (_fflush_r (ptr, fp))
|
||||
goto err;
|
||||
}
|
||||
else if (s >= (w = fp->_bf._size))
|
||||
{
|
||||
w = fp->_write (ptr, fp->_cookie, p, w);
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
}
|
||||
else
|
||||
{
|
||||
w = s;
|
||||
COPY (w);
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
}
|
||||
if ((nldist -= w) == 0)
|
||||
{
|
||||
/* copied the newline: flush and forget */
|
||||
if (_fflush_r (ptr, fp))
|
||||
goto err;
|
||||
nlknown = 0;
|
||||
}
|
||||
p += w;
|
||||
len -= w;
|
||||
}
|
||||
while ((uio->uio_resid -= w) != 0);
|
||||
}
|
||||
return 0;
|
||||
|
||||
err:
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
36
contrib/sdk/sources/newlib/stdio/fvwrite.h
Normal file
36
contrib/sdk/sources/newlib/stdio/fvwrite.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* %W% (Berkeley) %G% */
|
||||
#include <_ansi.h>
|
||||
|
||||
/*
|
||||
* I/O descriptors for __sfvwrite_r().
|
||||
*/
|
||||
struct __siov {
|
||||
_CONST _PTR iov_base;
|
||||
size_t iov_len;
|
||||
};
|
||||
struct __suio {
|
||||
struct __siov *uio_iov;
|
||||
int uio_iovcnt;
|
||||
int uio_resid;
|
||||
};
|
||||
|
||||
|
||||
extern int _EXFUN(__sfvwrite_r,(struct _reent *, FILE *, struct __suio *));
|
||||
extern int _EXFUN(__swsetup_r,(struct _reent *, FILE *));
|
86
contrib/sdk/sources/newlib/stdio/fwalk.c
Normal file
86
contrib/sdk/sources/newlib/stdio/fwalk.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_fwalk, (ptr, function),
|
||||
struct _reent *ptr _AND
|
||||
register int (*function) (FILE *))
|
||||
{
|
||||
register FILE *fp;
|
||||
register int n, ret = 0;
|
||||
register struct _glue *g;
|
||||
|
||||
/*
|
||||
* It should be safe to walk the list without locking it;
|
||||
* new nodes are only added to the end and none are ever
|
||||
* removed.
|
||||
*
|
||||
* Avoid locking this list while walking it or else you will
|
||||
* introduce a potential deadlock in [at least] refill.c.
|
||||
*/
|
||||
for (g = &ptr->__sglue; g != NULL; g = g->_next)
|
||||
for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
|
||||
if (fp->_flags != 0)
|
||||
{
|
||||
if (fp->_flags != 0 && fp->_flags != 1 && fp->_file != -1)
|
||||
ret |= (*function) (fp);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Special version of __fwalk where the function pointer is a reentrant
|
||||
I/O function (e.g. _fclose_r). */
|
||||
int
|
||||
_DEFUN(_fwalk_reent, (ptr, reent_function),
|
||||
struct _reent *ptr _AND
|
||||
register int (*reent_function) (struct _reent *, FILE *))
|
||||
{
|
||||
register FILE *fp;
|
||||
register int n, ret = 0;
|
||||
register struct _glue *g;
|
||||
|
||||
/*
|
||||
* It should be safe to walk the list without locking it;
|
||||
* new nodes are only added to the end and none are ever
|
||||
* removed.
|
||||
*
|
||||
* Avoid locking this list while walking it or else you will
|
||||
* introduce a potential deadlock in [at least] refill.c.
|
||||
*/
|
||||
for (g = &ptr->__sglue; g != NULL; g = g->_next)
|
||||
for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
|
||||
if (fp->_flags != 0)
|
||||
{
|
||||
if (fp->_flags != 0 && fp->_flags != 1 && fp->_file != -1)
|
||||
ret |= (*reent_function) (ptr, fp);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
143
contrib/sdk/sources/newlib/stdio/fwrite.c
Normal file
143
contrib/sdk/sources/newlib/stdio/fwrite.c
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<fwrite>>---write array elements
|
||||
|
||||
INDEX
|
||||
fwrite
|
||||
INDEX
|
||||
_fwrite_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
size_t fwrite(const void *<[buf]>, size_t <[size]>,
|
||||
size_t <[count]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
size_t _fwrite_r(struct _reent *<[ptr]>, const void *<[buf]>, size_t <[size]>,
|
||||
size_t <[count]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>)
|
||||
char *<[buf]>;
|
||||
size_t <[size]>;
|
||||
size_t <[count]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
size_t _fwrite_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[buf]>;
|
||||
size_t <[size]>;
|
||||
size_t <[count]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<fwrite>> attempts to copy, starting from the memory location
|
||||
<[buf]>, <[count]> elements (each of size <[size]>) into the file or
|
||||
stream identified by <[fp]>. <<fwrite>> may copy fewer elements than
|
||||
<[count]> if an error intervenes.
|
||||
|
||||
<<fwrite>> also advances the file position indicator (if any) for
|
||||
<[fp]> by the number of @emph{characters} actually written.
|
||||
|
||||
<<_fwrite_r>> is simply the reentrant version of <<fwrite>> that
|
||||
takes an additional reentrant structure argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
If <<fwrite>> succeeds in writing all the elements you specify, the
|
||||
result is the same as the argument <[count]>. In any event, the
|
||||
result is the number of complete elements that <<fwrite>> copied to
|
||||
the file.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<fwrite>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#if 0
|
||||
#include <sys/stdc.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
#if 1
|
||||
#include "fvwrite.h"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Write `count' objects (each size `size') from memory to the given file.
|
||||
* Return the number of whole objects written.
|
||||
*/
|
||||
|
||||
size_t
|
||||
_DEFUN(_fwrite_r, (ptr, buf, size, count, fp),
|
||||
struct _reent * ptr _AND
|
||||
_CONST _PTR buf _AND
|
||||
size_t size _AND
|
||||
size_t count _AND
|
||||
FILE * fp)
|
||||
{
|
||||
size_t n;
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
iov.iov_base = buf;
|
||||
uio.uio_resid = iov.iov_len = n = count * size;
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
|
||||
/*
|
||||
* The usual case is success (__sfvwrite_r returns 0);
|
||||
* skip the divide if this happens, since divides are
|
||||
* generally slow and since this occurs whenever size==0.
|
||||
*/
|
||||
|
||||
CHECK_INIT(ptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
ORIENT (fp, -1);
|
||||
if (__sfvwrite_r (ptr, fp, &uio) == 0)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return count;
|
||||
}
|
||||
_funlockfile (fp);
|
||||
return (n - uio.uio_resid) / size;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
size_t
|
||||
_DEFUN(fwrite, (buf, size, count, fp),
|
||||
_CONST _PTR buf _AND
|
||||
size_t size _AND
|
||||
size_t count _AND
|
||||
FILE * fp)
|
||||
{
|
||||
return _fwrite_r (_REENT, buf, size, count, fp);
|
||||
}
|
||||
#endif
|
229
contrib/sdk/sources/newlib/stdio/local.h
Normal file
229
contrib/sdk/sources/newlib/stdio/local.h
Normal file
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* %W% (UofMD/Berkeley) %G%
|
||||
*/
|
||||
|
||||
/*
|
||||
* Information local to this implementation of stdio,
|
||||
* in particular, macros and private variables.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#ifdef __SCLE
|
||||
# include <io.h>
|
||||
#endif
|
||||
|
||||
|
||||
extern u_char *_EXFUN(__sccl, (char *, u_char *fmt));
|
||||
extern int _EXFUN(__svfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
|
||||
extern int _EXFUN(__ssvfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
|
||||
extern int _EXFUN(__svfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
|
||||
extern int _EXFUN(__ssvfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list));
|
||||
extern int _EXFUN(__svfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
|
||||
extern int _EXFUN(__ssvfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
|
||||
extern int _EXFUN(__svfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
|
||||
extern int _EXFUN(__ssvfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list));
|
||||
int _EXFUN(_svfprintf_r,(struct _reent *, FILE *, const char *,
|
||||
va_list)
|
||||
_ATTRIBUTE ((__format__ (__printf__, 3, 0))));
|
||||
int _EXFUN(_svfiprintf_r,(struct _reent *, FILE *, const char *,
|
||||
va_list)
|
||||
_ATTRIBUTE ((__format__ (__printf__, 3, 0))));
|
||||
int _EXFUN(_svfwprintf_r,(struct _reent *, FILE *, const wchar_t *,
|
||||
va_list));
|
||||
int _EXFUN(_svfiwprintf_r,(struct _reent *, FILE *, const wchar_t *,
|
||||
va_list));
|
||||
extern FILE *_EXFUN(__sfp,(struct _reent *));
|
||||
extern int _EXFUN(__sflags,(struct _reent *,_CONST char*, int*));
|
||||
extern int _EXFUN(__sflush_r,(struct _reent *,FILE *));
|
||||
extern int _EXFUN(__srefill_r,(struct _reent *,FILE *));
|
||||
extern _READ_WRITE_RETURN_TYPE _EXFUN(__sread,(struct _reent *, void *, char *,
|
||||
int));
|
||||
extern _READ_WRITE_RETURN_TYPE _EXFUN(__seofread,(struct _reent *, void *,
|
||||
char *, int));
|
||||
extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite,(struct _reent *, void *,
|
||||
const char *, int));
|
||||
extern _fpos_t _EXFUN(__sseek,(struct _reent *, void *, _fpos_t, int));
|
||||
extern int _EXFUN(__sclose,(struct _reent *, void *));
|
||||
extern int _EXFUN(__stextmode,(int));
|
||||
extern _VOID _EXFUN(__sinit,(struct _reent *));
|
||||
extern _VOID _EXFUN(_cleanup_r,(struct _reent *));
|
||||
extern _VOID _EXFUN(__smakebuf_r,(struct _reent *, FILE *));
|
||||
extern int _EXFUN(_fwalk,(struct _reent *, int (*)(FILE *)));
|
||||
extern int _EXFUN(_fwalk_reent,(struct _reent *, int (*)(struct _reent *, FILE *)));
|
||||
struct _glue * _EXFUN(__sfmoreglue,(struct _reent *,int n));
|
||||
extern int _EXFUN(__submore, (struct _reent *, FILE *));
|
||||
|
||||
#ifdef __LARGE64_FILES
|
||||
extern _fpos64_t _EXFUN(__sseek64,(struct _reent *, void *, _fpos64_t, int));
|
||||
extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite64,(struct _reent *, void *,
|
||||
const char *, int));
|
||||
#endif
|
||||
|
||||
/* Called by the main entry point fns to ensure stdio has been initialized. */
|
||||
|
||||
#ifdef _REENT_SMALL
|
||||
#define CHECK_INIT(ptr, fp) \
|
||||
do \
|
||||
{ \
|
||||
if ((ptr) && !(ptr)->__sdidinit) \
|
||||
__sinit (ptr); \
|
||||
if ((fp) == (FILE *)&__sf_fake_stdin) \
|
||||
(fp) = _stdin_r(ptr); \
|
||||
else if ((fp) == (FILE *)&__sf_fake_stdout) \
|
||||
(fp) = _stdout_r(ptr); \
|
||||
else if ((fp) == (FILE *)&__sf_fake_stderr) \
|
||||
(fp) = _stderr_r(ptr); \
|
||||
} \
|
||||
while (0)
|
||||
#else /* !_REENT_SMALL */
|
||||
#define CHECK_INIT(ptr, fp) \
|
||||
do \
|
||||
{ \
|
||||
if ((ptr) && !(ptr)->__sdidinit) \
|
||||
__sinit (ptr); \
|
||||
} \
|
||||
while (0)
|
||||
#endif /* !_REENT_SMALL */
|
||||
|
||||
#define CHECK_STD_INIT(ptr) \
|
||||
do \
|
||||
{ \
|
||||
if ((ptr) && !(ptr)->__sdidinit) \
|
||||
__sinit (ptr); \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/* Return true and set errno and stream error flag iff the given FILE
|
||||
cannot be written now. */
|
||||
|
||||
#define cantwrite(ptr, fp) \
|
||||
((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \
|
||||
__swsetup_r(ptr, fp))
|
||||
|
||||
/* Test whether the given stdio file has an active ungetc buffer;
|
||||
release such a buffer, without restoring ordinary unread data. */
|
||||
|
||||
#define HASUB(fp) ((fp)->_ub._base != NULL)
|
||||
#define FREEUB(ptr, fp) { \
|
||||
if ((fp)->_ub._base != (fp)->_ubuf) \
|
||||
_free_r(ptr, (char *)(fp)->_ub._base); \
|
||||
(fp)->_ub._base = NULL; \
|
||||
}
|
||||
|
||||
/* Test for an fgetline() buffer. */
|
||||
|
||||
#define HASLB(fp) ((fp)->_lb._base != NULL)
|
||||
#define FREELB(ptr, fp) { _free_r(ptr,(char *)(fp)->_lb._base); \
|
||||
(fp)->_lb._base = NULL; }
|
||||
|
||||
/*
|
||||
* Set the orientation for a stream. If o > 0, the stream has wide-
|
||||
* orientation. If o < 0, the stream has byte-orientation.
|
||||
*/
|
||||
#define ORIENT(fp,ori) \
|
||||
do \
|
||||
{ \
|
||||
if (!((fp)->_flags & __SORD)) \
|
||||
{ \
|
||||
(fp)->_flags |= __SORD; \
|
||||
if (ori > 0) \
|
||||
(fp)->_flags2 |= __SWID; \
|
||||
else \
|
||||
(fp)->_flags2 &= ~__SWID; \
|
||||
} \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/* WARNING: _dcvt is defined in the stdlib directory, not here! */
|
||||
|
||||
char *_EXFUN(_dcvt,(struct _reent *, char *, double, int, int, char, int));
|
||||
char *_EXFUN(_sicvt,(char *, short, char));
|
||||
char *_EXFUN(_icvt,(char *, int, char));
|
||||
char *_EXFUN(_licvt,(char *, long, char));
|
||||
#ifdef __GNUC__
|
||||
char *_EXFUN(_llicvt,(char *, long long, char));
|
||||
#endif
|
||||
|
||||
#define CVT_BUF_SIZE 128
|
||||
|
||||
#define NDYNAMIC 4 /* add four more whenever necessary */
|
||||
|
||||
#ifdef __SINGLE_THREAD__
|
||||
#define __sfp_lock_acquire()
|
||||
#define __sfp_lock_release()
|
||||
#define __sinit_lock_acquire()
|
||||
#define __sinit_lock_release()
|
||||
#else
|
||||
_VOID _EXFUN(__sfp_lock_acquire,(_VOID));
|
||||
_VOID _EXFUN(__sfp_lock_release,(_VOID));
|
||||
_VOID _EXFUN(__sinit_lock_acquire,(_VOID));
|
||||
_VOID _EXFUN(__sinit_lock_release,(_VOID));
|
||||
#endif
|
||||
|
||||
/* Types used in positional argument support in vfprinf/vfwprintf.
|
||||
The implementation is char/wchar_t dependent but the class and state
|
||||
tables are only defined once in vfprintf.c. */
|
||||
typedef enum {
|
||||
ZERO, /* '0' */
|
||||
DIGIT, /* '1-9' */
|
||||
DOLLAR, /* '$' */
|
||||
MODFR, /* spec modifier */
|
||||
SPEC, /* format specifier */
|
||||
DOT, /* '.' */
|
||||
STAR, /* '*' */
|
||||
FLAG, /* format flag */
|
||||
OTHER, /* all other chars */
|
||||
MAX_CH_CLASS /* place-holder */
|
||||
} __CH_CLASS;
|
||||
|
||||
typedef enum {
|
||||
START, /* start */
|
||||
SFLAG, /* seen a flag */
|
||||
WDIG, /* seen digits in width area */
|
||||
WIDTH, /* processed width */
|
||||
SMOD, /* seen spec modifier */
|
||||
SDOT, /* seen dot */
|
||||
VARW, /* have variable width specifier */
|
||||
VARP, /* have variable precision specifier */
|
||||
PREC, /* processed precision */
|
||||
VWDIG, /* have digits in variable width specification */
|
||||
VPDIG, /* have digits in variable precision specification */
|
||||
DONE, /* done */
|
||||
MAX_STATE, /* place-holder */
|
||||
} __STATE;
|
||||
|
||||
typedef enum {
|
||||
NOOP, /* do nothing */
|
||||
NUMBER, /* build a number from digits */
|
||||
SKIPNUM, /* skip over digits */
|
||||
GETMOD, /* get and process format modifier */
|
||||
GETARG, /* get and process argument */
|
||||
GETPW, /* get variable precision or width */
|
||||
GETPWB, /* get variable precision or width and pushback fmt char */
|
||||
GETPOS, /* get positional parameter value */
|
||||
PWPOS, /* get positional parameter value for variable width or precision */
|
||||
} __ACTION;
|
||||
|
||||
extern _CONST __CH_CLASS __chclass[256];
|
||||
extern _CONST __STATE __state_table[MAX_STATE][MAX_CH_CLASS];
|
||||
extern _CONST __ACTION __action_table[MAX_STATE][MAX_CH_CLASS];
|
113
contrib/sdk/sources/newlib/stdio/makebuf.c
Normal file
113
contrib/sdk/sources/newlib/stdio/makebuf.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "local.h"
|
||||
|
||||
#define _DEFAULT_ASPRINTF_BUFSIZE 64
|
||||
|
||||
/*
|
||||
* Allocate a file buffer, or switch to unbuffered I/O.
|
||||
* Per the ANSI C standard, ALL tty devices default to line buffered.
|
||||
*
|
||||
* As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
|
||||
* optimization) right after the _fstat() that finds the buffer size.
|
||||
*/
|
||||
|
||||
_VOID
|
||||
_DEFUN(__smakebuf_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
register size_t size, couldbetty;
|
||||
register _PTR p;
|
||||
#ifdef __USE_INTERNAL_STAT64
|
||||
struct stat64 st;
|
||||
#else
|
||||
struct stat st;
|
||||
#endif
|
||||
|
||||
if (fp->_flags & __SNBF)
|
||||
{
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
return;
|
||||
}
|
||||
#ifdef __USE_INTERNAL_STAT64
|
||||
if (fp->_file < 0 || _fstat64_r (ptr, fp->_file, &st) < 0)
|
||||
#else
|
||||
if (fp->_file < 0 || _fstat_r (ptr, fp->_file, &st) < 0)
|
||||
#endif
|
||||
{
|
||||
couldbetty = 0;
|
||||
/* Check if we are be called by asprintf family for initial buffer. */
|
||||
if (fp->_flags & __SMBF)
|
||||
size = _DEFAULT_ASPRINTF_BUFSIZE;
|
||||
else
|
||||
size = BUFSIZ;
|
||||
/* do not try to optimise fseek() */
|
||||
fp->_flags |= __SNPT;
|
||||
}
|
||||
else
|
||||
{
|
||||
couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
|
||||
#ifdef HAVE_BLKSIZE
|
||||
size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
|
||||
#else
|
||||
size = BUFSIZ;
|
||||
#endif
|
||||
/*
|
||||
* Optimize fseek() only if it is a regular file.
|
||||
* (The test for __sseek is mainly paranoia.)
|
||||
*/
|
||||
if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek)
|
||||
{
|
||||
fp->_flags |= __SOPT;
|
||||
#ifdef HAVE_BLKSIZE
|
||||
fp->_blksize = st.st_blksize;
|
||||
#else
|
||||
fp->_blksize = 1024;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
fp->_flags |= __SNPT;
|
||||
}
|
||||
if ((p = _malloc_r (ptr, size)) == NULL)
|
||||
{
|
||||
if (!(fp->_flags & __SSTR))
|
||||
{
|
||||
fp->_flags |= __SNBF;
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr->__cleanup = _cleanup_r;
|
||||
fp->_flags |= __SMBF;
|
||||
fp->_bf._base = fp->_p = (unsigned char *) p;
|
||||
fp->_bf._size = size;
|
||||
if (couldbetty && _isatty_r (ptr, fp->_file))
|
||||
fp->_flags |= __SLBF;
|
||||
}
|
||||
}
|
57
contrib/sdk/sources/newlib/stdio/printf.c
Normal file
57
contrib/sdk/sources/newlib/stdio/printf.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in sprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_printf_r, (ptr, fmt),
|
||||
struct _reent *ptr _AND
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
va_start (ap, fmt);
|
||||
ret = _vfprintf_r (ptr, _stdout_r (ptr), fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(printf, (fmt),
|
||||
const char *fmt _DOTS)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
struct _reent *ptr = _REENT;
|
||||
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
va_start (ap, fmt);
|
||||
ret = _vfprintf_r (ptr, _stdout_r (ptr), fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
124
contrib/sdk/sources/newlib/stdio/putc.c
Normal file
124
contrib/sdk/sources/newlib/stdio/putc.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<putc>>---write a character (macro)
|
||||
|
||||
INDEX
|
||||
putc
|
||||
INDEX
|
||||
_putc_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int putc(int <[ch]>, FILE *<[fp]>);
|
||||
|
||||
#include <stdio.h>
|
||||
int _putc_r(struct _reent *<[ptr]>, int <[ch]>, FILE *<[fp]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int putc(<[ch]>, <[fp]>)
|
||||
int <[ch]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
#include <stdio.h>
|
||||
int _putc_r(<[ptr]>, <[ch]>, <[fp]>)
|
||||
struct _reent *<[ptr]>;
|
||||
int <[ch]>;
|
||||
FILE *<[fp]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<putc>> is a macro, defined in <<stdio.h>>. <<putc>>
|
||||
writes the argument <[ch]> to the file or stream identified by
|
||||
<[fp]>, after converting it from an <<int>> to an <<unsigned char>>.
|
||||
|
||||
If the file was opened with append mode (or if the stream cannot
|
||||
support positioning), then the new character goes at the end of the
|
||||
file or stream. Otherwise, the new character is written at the
|
||||
current value of the position indicator, and the position indicator
|
||||
advances by one.
|
||||
|
||||
For a subroutine version of this macro, see <<fputc>>.
|
||||
|
||||
The <<_putc_r>> function is simply the reentrant version of
|
||||
<<putc>> that takes an additional reentrant structure argument: <[ptr]>.
|
||||
|
||||
RETURNS
|
||||
If successful, <<putc>> returns its argument <[ch]>. If an error
|
||||
intervenes, the result is <<EOF>>. You can use `<<ferror(<[fp]>)>>' to
|
||||
query for errors.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<putc>>; it suggests, but does not require, that
|
||||
<<putc>> be implemented as a macro. The standard explicitly permits
|
||||
macro implementations of <<putc>> to use the <[fp]> argument more than once;
|
||||
therefore, in a portable program, you should not use an expression
|
||||
with side effects as this argument.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putc.
|
||||
*/
|
||||
|
||||
#undef putc
|
||||
|
||||
int
|
||||
_DEFUN(_putc_r, (ptr, c, fp),
|
||||
struct _reent *ptr _AND
|
||||
int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
int result;
|
||||
CHECK_INIT (ptr, fp);
|
||||
_flockfile (fp);
|
||||
result = __sputc_r (ptr, c, fp);
|
||||
_funlockfile (fp);
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
int
|
||||
_DEFUN(putc, (c, fp),
|
||||
int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
|
||||
int result;
|
||||
CHECK_INIT (_REENT, fp);
|
||||
_flockfile (fp);
|
||||
result = __sputc_r (_REENT, c, fp);
|
||||
_funlockfile (fp);
|
||||
return result;
|
||||
#else
|
||||
return _putc_r (_REENT, c, fp);
|
||||
#endif
|
||||
}
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
97
contrib/sdk/sources/newlib/stdio/putchar.c
Normal file
97
contrib/sdk/sources/newlib/stdio/putchar.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<putchar>>---write a character (macro)
|
||||
|
||||
INDEX
|
||||
putchar
|
||||
INDEX
|
||||
_putchar_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int putchar(int <[ch]>);
|
||||
|
||||
int _putchar_r(struct _reent *<[reent]>, int <[ch]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int putchar(<[ch]>)
|
||||
int <[ch]>;
|
||||
|
||||
int _putchar_r(<[reent]>, <[ch]>)
|
||||
struct _reent *<[reent]>;
|
||||
int <[ch]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<putchar>> is a macro, defined in <<stdio.h>>. <<putchar>>
|
||||
writes its argument to the standard output stream,
|
||||
after converting it from an <<int>> to an <<unsigned char>>.
|
||||
|
||||
The alternate function <<_putchar_r>> is a reentrant version. The
|
||||
extra argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
If successful, <<putchar>> returns its argument <[ch]>. If an error
|
||||
intervenes, the result is <<EOF>>. You can use `<<ferror(stdin)>>' to
|
||||
query for errors.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<putchar>>; it suggests, but does not require, that
|
||||
<<putchar>> be implemented as a macro.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putchar.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
#undef putchar
|
||||
|
||||
int
|
||||
_DEFUN(_putchar_r, (ptr, c),
|
||||
struct _reent *ptr _AND
|
||||
int c)
|
||||
{
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
return _putc_r (ptr, c, _stdout_r (ptr));
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(putchar, (c),
|
||||
int c)
|
||||
{
|
||||
_REENT_SMALL_CHECK_INIT (_REENT);
|
||||
return _putc_r (_REENT, c, _stdout_r (_REENT));
|
||||
}
|
||||
|
||||
#endif
|
106
contrib/sdk/sources/newlib/stdio/puts.c
Normal file
106
contrib/sdk/sources/newlib/stdio/puts.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<puts>>---write a character string
|
||||
|
||||
INDEX
|
||||
puts
|
||||
INDEX
|
||||
_puts_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int puts(const char *<[s]>);
|
||||
|
||||
int _puts_r(struct _reent *<[reent]>, const char *<[s]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int puts(<[s]>)
|
||||
char *<[s]>;
|
||||
|
||||
int _puts_r(<[reent]>, <[s]>)
|
||||
struct _reent *<[reent]>;
|
||||
char *<[s]>;
|
||||
|
||||
DESCRIPTION
|
||||
<<puts>> writes the string at <[s]> (followed by a newline, instead of
|
||||
the trailing null) to the standard output stream.
|
||||
|
||||
The alternate function <<_puts_r>> is a reentrant version. The extra
|
||||
argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
If successful, the result is a nonnegative integer; otherwise, the
|
||||
result is <<EOF>>.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<puts>>, but does not specify that the result on
|
||||
success must be <<0>>; any non-negative value is permitted.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "fvwrite.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Write the given string to stdout, appending a newline.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(_puts_r, (ptr, s),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char * s)
|
||||
{
|
||||
size_t c = strlen (s);
|
||||
struct __suio uio;
|
||||
struct __siov iov[2];
|
||||
|
||||
iov[0].iov_base = s;
|
||||
iov[0].iov_len = c;
|
||||
iov[1].iov_base = "\n";
|
||||
iov[1].iov_len = 1;
|
||||
uio.uio_resid = c + 1;
|
||||
uio.uio_iov = &iov[0];
|
||||
uio.uio_iovcnt = 2;
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
ORIENT (stdout, -1);
|
||||
return (__sfvwrite_r (ptr, _stdout_r (ptr), &uio) ? EOF : '\n');
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(puts, (s),
|
||||
char _CONST * s)
|
||||
{
|
||||
return _puts_r (_REENT, s);
|
||||
}
|
||||
|
||||
#endif
|
138
contrib/sdk/sources/newlib/stdio/refill.c
Normal file
138
contrib/sdk/sources/newlib/stdio/refill.c
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
static int
|
||||
_DEFUN(lflush, (fp),
|
||||
FILE *fp)
|
||||
{
|
||||
if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR))
|
||||
return fflush (fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Refill a stdio buffer.
|
||||
* Return EOF on eof or error, 0 otherwise.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__srefill_r, (ptr, fp),
|
||||
struct _reent * ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
/* make sure stdio is set up */
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
ORIENT (fp, -1);
|
||||
|
||||
fp->_r = 0; /* largely a convenience for callers */
|
||||
|
||||
#ifndef __CYGWIN__
|
||||
/* SysV does not make this test; take it out for compatibility */
|
||||
if (fp->_flags & __SEOF)
|
||||
return EOF;
|
||||
#endif
|
||||
|
||||
/* if not already reading, have to be reading and writing */
|
||||
if ((fp->_flags & __SRD) == 0)
|
||||
{
|
||||
if ((fp->_flags & __SRW) == 0)
|
||||
{
|
||||
ptr->_errno = EBADF;
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
||||
/* switch to reading */
|
||||
if (fp->_flags & __SWR)
|
||||
{
|
||||
if (_fflush_r (ptr, fp))
|
||||
return EOF;
|
||||
fp->_flags &= ~__SWR;
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = 0;
|
||||
}
|
||||
fp->_flags |= __SRD;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* We were reading. If there is an ungetc buffer,
|
||||
* we must have been reading from that. Drop it,
|
||||
* restoring the previous buffer (if any). If there
|
||||
* is anything in that buffer, return.
|
||||
*/
|
||||
if (HASUB (fp))
|
||||
{
|
||||
FREEUB (ptr, fp);
|
||||
if ((fp->_r = fp->_ur) != 0)
|
||||
{
|
||||
fp->_p = fp->_up;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fp->_bf._base == NULL)
|
||||
__smakebuf_r (ptr, fp);
|
||||
|
||||
/*
|
||||
* Before reading from a line buffered or unbuffered file,
|
||||
* flush all line buffered output files, per the ANSI C
|
||||
* standard.
|
||||
*/
|
||||
if (fp->_flags & (__SLBF | __SNBF))
|
||||
{
|
||||
/* Ignore this file in _fwalk to avoid potential deadlock. */
|
||||
short orig_flags = fp->_flags;
|
||||
fp->_flags = 1;
|
||||
_CAST_VOID _fwalk (_GLOBAL_REENT, lflush);
|
||||
fp->_flags = orig_flags;
|
||||
|
||||
/* Now flush this file without locking it. */
|
||||
if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))
|
||||
__sflush_r (ptr, fp);
|
||||
}
|
||||
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = fp->_read (ptr, fp->_cookie, (char *) fp->_p, fp->_bf._size);
|
||||
#ifndef __CYGWIN__
|
||||
if (fp->_r <= 0)
|
||||
#else
|
||||
if (fp->_r > 0)
|
||||
fp->_flags &= ~__SEOF;
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if (fp->_r == 0)
|
||||
fp->_flags |= __SEOF;
|
||||
else
|
||||
{
|
||||
fp->_r = 0;
|
||||
fp->_flags |= __SERR;
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
return 0;
|
||||
}
|
91
contrib/sdk/sources/newlib/stdio/remove.c
Normal file
91
contrib/sdk/sources/newlib/stdio/remove.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<remove>>---delete a file's name
|
||||
|
||||
INDEX
|
||||
remove
|
||||
INDEX
|
||||
_remove_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int remove(char *<[filename]>);
|
||||
|
||||
int _remove_r(struct _reent *<[reent]>, char *<[filename]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int remove(<[filename]>)
|
||||
char *<[filename]>;
|
||||
|
||||
int _remove_r(<[reent]>, <[filename]>)
|
||||
struct _reent *<[reent]>;
|
||||
char *<[filename]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use <<remove>> to dissolve the association between a particular
|
||||
filename (the string at <[filename]>) and the file it represents.
|
||||
After calling <<remove>> with a particular filename, you will no
|
||||
longer be able to open the file by that name.
|
||||
|
||||
In this implementation, you may use <<remove>> on an open file without
|
||||
error; existing file descriptors for the file will continue to access
|
||||
the file's data until the program using them closes the file.
|
||||
|
||||
The alternate function <<_remove_r>> is a reentrant version. The
|
||||
extra argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
<<remove>> returns <<0>> if it succeeds, <<-1>> if it fails.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<remove>>, but only specifies that the result on
|
||||
failure be nonzero. The behavior of <<remove>> when you call it on an
|
||||
open file may vary among implementations.
|
||||
|
||||
Supporting OS subroutine required: <<unlink>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/kos_io.h>
|
||||
|
||||
|
||||
int
|
||||
_DEFUN(_remove_r, (ptr, filename),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *filename)
|
||||
{
|
||||
return delete_file(filename)==0 ? 0: -1;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(remove, (filename),
|
||||
_CONST char *filename)
|
||||
{
|
||||
|
||||
return delete_file(filename)==0 ? 0: -1;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
80
contrib/sdk/sources/newlib/stdio/rename.c
Normal file
80
contrib/sdk/sources/newlib/stdio/rename.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<rename>>---rename a file
|
||||
|
||||
INDEX
|
||||
rename
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int rename(const char *<[old]>, const char *<[new]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int rename(<[old]>, <[new]>)
|
||||
char *<[old]>;
|
||||
char *<[new]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use <<rename>> to establish a new name (the string at <[new]>) for a
|
||||
file now known by the string at <[old]>. After a successful
|
||||
<<rename>>, the file is no longer accessible by the string at <[old]>.
|
||||
|
||||
If <<rename>> fails, the file named <<*<[old]>>> is unaffected. The
|
||||
conditions for failure depend on the host operating system.
|
||||
|
||||
RETURNS
|
||||
The result is either <<0>> (when successful) or <<-1>> (when the file
|
||||
could not be renamed).
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<rename>>, but only specifies that the result on
|
||||
failure be nonzero. The effects of using the name of an existing file
|
||||
as <<*<[new]>>> may vary from one implementation to another.
|
||||
|
||||
Supporting OS subroutines required: <<link>>, <<unlink>>, or <<rename>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/unistd.h>
|
||||
|
||||
int
|
||||
_DEFUN (_rename_r, (ptr, old, new),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *old _AND
|
||||
_CONST char *new)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(rename, (old, new),
|
||||
_CONST char *old _AND
|
||||
_CONST char *new)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif
|
59
contrib/sdk/sources/newlib/stdio/rget.c
Normal file
59
contrib/sdk/sources/newlib/stdio/rget.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Handle getc() when the buffer ran out:
|
||||
* Refill, then return the first character
|
||||
* in the newly-filled buffer.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__srget_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
/* Ensure that any fake std stream is resolved before
|
||||
we call __srefill_r so we may access the true read buffer. */
|
||||
CHECK_INIT(ptr, fp);
|
||||
|
||||
if (__srefill_r (ptr, fp) == 0)
|
||||
{
|
||||
fp->_r--;
|
||||
return *fp->_p++;
|
||||
}
|
||||
return EOF;
|
||||
}
|
||||
|
||||
/* This function isn't any longer declared in stdio.h, but it's
|
||||
required for backward compatibility with applications built against
|
||||
earlier dynamically built newlib libraries. */
|
||||
int
|
||||
_DEFUN(__srget, (fp),
|
||||
register FILE *fp)
|
||||
{
|
||||
return __srget_r (_REENT, fp);
|
||||
}
|
127
contrib/sdk/sources/newlib/stdio/sccl.c
Normal file
127
contrib/sdk/sources/newlib/stdio/sccl.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/*-
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* Split from vfscanf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <newlib.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Fill in the given table from the scanset at the given format
|
||||
* (just after `['). Return a pointer to the character past the
|
||||
* closing `]'. The table has a 1 wherever characters should be
|
||||
* considered part of the scanset.
|
||||
*/
|
||||
|
||||
u_char *
|
||||
_DEFUN(__sccl, (tab, fmt),
|
||||
register char *tab _AND
|
||||
register u_char *fmt)
|
||||
{
|
||||
register int c, n, v;
|
||||
|
||||
/* first `clear' the whole table */
|
||||
c = *fmt++; /* first char hat => negated scanset */
|
||||
if (c == '^')
|
||||
{
|
||||
v = 1; /* default => accept */
|
||||
c = *fmt++; /* get new first char */
|
||||
}
|
||||
else
|
||||
v = 0; /* default => reject */
|
||||
/* should probably use memset here */
|
||||
for (n = 0; n < 256; n++)
|
||||
tab[n] = v;
|
||||
if (c == 0)
|
||||
return fmt - 1; /* format ended before closing ] */
|
||||
|
||||
/*
|
||||
* Now set the entries corresponding to the actual scanset to the
|
||||
* opposite of the above.
|
||||
*
|
||||
* The first character may be ']' (or '-') without being special; the
|
||||
* last character may be '-'.
|
||||
*/
|
||||
|
||||
v = 1 - v;
|
||||
for (;;)
|
||||
{
|
||||
tab[c] = v; /* take character c */
|
||||
doswitch:
|
||||
n = *fmt++; /* and examine the next */
|
||||
switch (n)
|
||||
{
|
||||
|
||||
case 0: /* format ended too soon */
|
||||
return fmt - 1;
|
||||
|
||||
case '-':
|
||||
/*
|
||||
* A scanset of the form [01+-] is defined as `the digit 0, the
|
||||
* digit 1, the character +, the character -', but the effect of a
|
||||
* scanset such as [a-zA-Z0-9] is implementation defined. The V7
|
||||
* Unix scanf treats `a-z' as `the letters a through z', but treats
|
||||
* `a-a' as `the letter a, the character -, and the letter a'.
|
||||
*
|
||||
* For compatibility, the `-' is not considerd to define a range if
|
||||
* the character following it is either a close bracket (required by
|
||||
* ANSI) or is not numerically greater than the character we just
|
||||
* stored in the table (c).
|
||||
*/
|
||||
n = *fmt;
|
||||
if (n == ']' || n < c)
|
||||
{
|
||||
c = '-';
|
||||
break; /* resume the for(;;) */
|
||||
}
|
||||
fmt++;
|
||||
do
|
||||
{ /* fill in the range */
|
||||
tab[++c] = v;
|
||||
}
|
||||
while (c < n);
|
||||
#if 1 /* XXX another disgusting compatibility hack */
|
||||
/*
|
||||
* Alas, the V7 Unix scanf also treats formats such
|
||||
* as [a-c-e] as `the letters a through e'. This too
|
||||
* is permitted by the standard....
|
||||
*/
|
||||
goto doswitch;
|
||||
#else
|
||||
c = *fmt++;
|
||||
if (c == 0)
|
||||
return fmt - 1;
|
||||
if (c == ']')
|
||||
return fmt;
|
||||
#endif
|
||||
|
||||
break;
|
||||
|
||||
|
||||
case ']': /* end of scanset */
|
||||
return fmt;
|
||||
|
||||
default: /* just another character */
|
||||
c = n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* NOTREACHED */
|
||||
}
|
198
contrib/sdk/sources/newlib/stdio/setvbuf.c
Normal file
198
contrib/sdk/sources/newlib/stdio/setvbuf.c
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<setvbuf>>---specify file or stream buffering
|
||||
|
||||
INDEX
|
||||
setvbuf
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int setvbuf(FILE *<[fp]>, char *<[buf]>,
|
||||
int <[mode]>, size_t <[size]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int setvbuf(<[fp]>, <[buf]>, <[mode]>, <[size]>)
|
||||
FILE *<[fp]>;
|
||||
char *<[buf]>;
|
||||
int <[mode]>;
|
||||
size_t <[size]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use <<setvbuf>> to specify what kind of buffering you want for the
|
||||
file or stream identified by <[fp]>, by using one of the following
|
||||
values (from <<stdio.h>>) as the <[mode]> argument:
|
||||
|
||||
o+
|
||||
o _IONBF
|
||||
Do not use a buffer: send output directly to the host system for the
|
||||
file or stream identified by <[fp]>.
|
||||
|
||||
o _IOFBF
|
||||
Use full output buffering: output will be passed on to the host system
|
||||
only when the buffer is full, or when an input operation intervenes.
|
||||
|
||||
o _IOLBF
|
||||
Use line buffering: pass on output to the host system at every
|
||||
newline, as well as when the buffer is full, or when an input
|
||||
operation intervenes.
|
||||
o-
|
||||
|
||||
Use the <[size]> argument to specify how large a buffer you wish. You
|
||||
can supply the buffer itself, if you wish, by passing a pointer to a
|
||||
suitable area of memory as <[buf]>. Otherwise, you may pass <<NULL>>
|
||||
as the <[buf]> argument, and <<setvbuf>> will allocate the buffer.
|
||||
|
||||
WARNINGS
|
||||
You may only use <<setvbuf>> before performing any file operation other
|
||||
than opening the file.
|
||||
|
||||
If you supply a non-null <[buf]>, you must ensure that the associated
|
||||
storage continues to be available until you close the stream
|
||||
identified by <[fp]>.
|
||||
|
||||
RETURNS
|
||||
A <<0>> result indicates success, <<EOF>> failure (invalid <[mode]> or
|
||||
<[size]> can cause failure).
|
||||
|
||||
PORTABILITY
|
||||
Both ANSI C and the System V Interface Definition (Issue 2) require
|
||||
<<setvbuf>>. However, they differ on the meaning of a <<NULL>> buffer
|
||||
pointer: the SVID issue 2 specification says that a <<NULL>> buffer
|
||||
pointer requests unbuffered output. For maximum portability, avoid
|
||||
<<NULL>> buffer pointers.
|
||||
|
||||
Both specifications describe the result on failure only as a
|
||||
nonzero value.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Set one of the three kinds of buffering, optionally including a buffer.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(setvbuf, (fp, buf, mode, size),
|
||||
register FILE * fp _AND
|
||||
char *buf _AND
|
||||
register int mode _AND
|
||||
register size_t size)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
CHECK_INIT (_REENT, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
/*
|
||||
* Verify arguments. The `int' limit on `size' is due to this
|
||||
* particular implementation.
|
||||
*/
|
||||
|
||||
if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int)(_POINTER_INT) size < 0)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return (EOF);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write current buffer, if any; drop read count, if any.
|
||||
* Make sure putc() will not think fp is line buffered.
|
||||
* Free old buffer if it was from malloc(). Clear line and
|
||||
* non buffer flags, and clear malloc flag.
|
||||
*/
|
||||
|
||||
_fflush_r (_REENT, fp);
|
||||
fp->_r = 0;
|
||||
fp->_lbfsize = 0;
|
||||
if (fp->_flags & __SMBF)
|
||||
_free_r (_REENT, (_PTR) fp->_bf._base);
|
||||
fp->_flags &= ~(__SLBF | __SNBF | __SMBF);
|
||||
|
||||
if (mode == _IONBF)
|
||||
goto nbf;
|
||||
|
||||
/*
|
||||
* Allocate buffer if needed. */
|
||||
if (buf == NULL)
|
||||
{
|
||||
/* we need this here because malloc() may return a pointer
|
||||
even if size == 0 */
|
||||
if (!size) size = BUFSIZ;
|
||||
if ((buf = malloc (size)) == NULL)
|
||||
{
|
||||
ret = EOF;
|
||||
/* Try another size... */
|
||||
buf = malloc (BUFSIZ);
|
||||
size = BUFSIZ;
|
||||
}
|
||||
if (buf == NULL)
|
||||
{
|
||||
/* Can't allocate it, let's try another approach */
|
||||
nbf:
|
||||
fp->_flags |= __SNBF;
|
||||
fp->_w = 0;
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
_funlockfile (fp);
|
||||
return (ret);
|
||||
}
|
||||
fp->_flags |= __SMBF;
|
||||
}
|
||||
/*
|
||||
* Now put back whichever flag is needed, and fix _lbfsize
|
||||
* if line buffered. Ensure output flush on exit if the
|
||||
* stream will be buffered at all.
|
||||
* If buf is NULL then make _lbfsize 0 to force the buffer
|
||||
* to be flushed and hence malloced on first use
|
||||
*/
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case _IOLBF:
|
||||
fp->_flags |= __SLBF;
|
||||
fp->_lbfsize = buf ? -size : 0;
|
||||
/* FALLTHROUGH */
|
||||
|
||||
case _IOFBF:
|
||||
/* no flag */
|
||||
_REENT->__cleanup = _cleanup_r;
|
||||
fp->_bf._base = fp->_p = (unsigned char *) buf;
|
||||
fp->_bf._size = size;
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Patch up write count if necessary.
|
||||
*/
|
||||
|
||||
if (fp->_flags & __SWR)
|
||||
fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size;
|
||||
|
||||
_funlockfile (fp);
|
||||
return 0;
|
||||
}
|
171
contrib/sdk/sources/newlib/stdio/siprintf.c
Normal file
171
contrib/sdk/sources/newlib/stdio/siprintf.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<siprintf>>, <<fiprintf>>, <<iprintf>>, <<sniprintf>>, <<asiprintf>>, <<asniprintf>>---format output (integer only)
|
||||
|
||||
INDEX
|
||||
fiprintf
|
||||
INDEX
|
||||
_fiprintf_r
|
||||
INDEX
|
||||
iprintf
|
||||
INDEX
|
||||
_iprintf_r
|
||||
INDEX
|
||||
siprintf
|
||||
INDEX
|
||||
_siprintf_r
|
||||
INDEX
|
||||
sniprintf
|
||||
INDEX
|
||||
_sniprintf_r
|
||||
INDEX
|
||||
asiprintf
|
||||
INDEX
|
||||
_asiprintf_r
|
||||
INDEX
|
||||
asniprintf
|
||||
INDEX
|
||||
_asniprintf_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
|
||||
int iprintf(const char *<[format]>, ...);
|
||||
int fiprintf(FILE *<[fd]>, const char *<[format]> , ...);
|
||||
int siprintf(char *<[str]>, const char *<[format]>, ...);
|
||||
int sniprintf(char *<[str]>, size_t <[size]>, const char *<[format]>,
|
||||
...);
|
||||
int asiprintf(char **<[strp]>, const char *<[format]>, ...);
|
||||
char *asniprintf(char *<[str]>, size_t *<[size]>,
|
||||
const char *<[format]>, ...);
|
||||
|
||||
int _iprintf_r(struct _reent *<[ptr]>, const char *<[format]>, ...);
|
||||
int _fiprintf_r(struct _reent *<[ptr]>, FILE *<[fd]>,
|
||||
const char *<[format]>, ...);
|
||||
int _siprintf_r(struct _reent *<[ptr]>, char *<[str]>,
|
||||
const char *<[format]>, ...);
|
||||
int _sniprintf_r(struct _reent *<[ptr]>, char *<[str]>, size_t <[size]>,
|
||||
const char *<[format]>, ...);
|
||||
int _asiprintf_r(struct _reent *<[ptr]>, char **<[strp]>,
|
||||
const char *<[format]>, ...);
|
||||
char *_asniprintf_r(struct _reent *<[ptr]>, char *<[str]>,
|
||||
size_t *<[size]>, const char *<[format]>, ...);
|
||||
|
||||
DESCRIPTION
|
||||
<<iprintf>>, <<fiprintf>>, <<siprintf>>, <<sniprintf>>,
|
||||
<<asiprintf>>, and <<asniprintf>> are the same as <<printf>>,
|
||||
<<fprintf>>, <<sprintf>>, <<snprintf>>, <<asprintf>>, and
|
||||
<<asnprintf>>, respectively, except that they restrict usage
|
||||
to non-floating-point format specifiers.
|
||||
|
||||
<<_iprintf_r>>, <<_fiprintf_r>>, <<_asiprintf_r>>,
|
||||
<<_siprintf_r>>, <<_sniprintf_r>>, <<_asniprintf_r>> are
|
||||
simply reentrant versions of the functions above.
|
||||
|
||||
RETURNS
|
||||
Similar to <<printf>>, <<fprintf>>, <<sprintf>>, <<snprintf>>, <<asprintf>>,
|
||||
and <<asnprintf>>.
|
||||
|
||||
PORTABILITY
|
||||
<<iprintf>>, <<fiprintf>>, <<siprintf>>, <<sniprintf>>, <<asiprintf>>,
|
||||
and <<asniprintf>> are newlib extensions.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(_siprintf_r, (ptr, str, fmt),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
_siprintf_r(ptr, str, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfiprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(siprintf, (str, fmt),
|
||||
char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
siprintf(str, fmt, va_alist)
|
||||
char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfiprintf_r (_REENT, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#endif
|
120
contrib/sdk/sources/newlib/stdio/sniprintf.c
Normal file
120
contrib/sdk/sources/newlib/stdio/sniprintf.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/* This code created by modifying snprintf.c so copyright inherited. */
|
||||
/* doc in siprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN (_sniprintf_r, (ptr, str, size, fmt),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
_sniprintf_r (ptr, str, size, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
char *str;
|
||||
size_t size;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
if (size > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return EOF;
|
||||
}
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfiprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
if (ret < EOF)
|
||||
ptr->_errno = EOVERFLOW;
|
||||
if (size > 0)
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN (sniprintf, (str, size, fmt),
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
sniprintf (str, size, fmt, va_alist)
|
||||
char *str;
|
||||
size_t size;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
struct _reent *ptr = _REENT;
|
||||
|
||||
if (size > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return EOF;
|
||||
}
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfiprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
if (ret < EOF)
|
||||
ptr->_errno = EOVERFLOW;
|
||||
if (size > 0)
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#endif
|
119
contrib/sdk/sources/newlib/stdio/snprintf.c
Normal file
119
contrib/sdk/sources/newlib/stdio/snprintf.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in sprintf.c */
|
||||
/* This code created by modifying sprintf.c so copyright inherited. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(_snprintf_r, (ptr, str, size, fmt),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
_snprintf_r(ptr, str, size, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
char *str;
|
||||
size_t size;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
if (size > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return EOF;
|
||||
}
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
if (ret < EOF)
|
||||
ptr->_errno = EOVERFLOW;
|
||||
if (size > 0)
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(snprintf, (str, size, fmt),
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
snprintf(str, size, fmt, va_alist)
|
||||
char *str;
|
||||
size_t size;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
struct _reent *ptr = _REENT;
|
||||
|
||||
if (size > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return EOF;
|
||||
}
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
if (ret < EOF)
|
||||
ptr->_errno = EOVERFLOW;
|
||||
if (size > 0)
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#endif
|
640
contrib/sdk/sources/newlib/stdio/sprintf.c
Normal file
640
contrib/sdk/sources/newlib/stdio/sprintf.c
Normal file
@@ -0,0 +1,640 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<sprintf>>, <<fprintf>>, <<printf>>, <<snprintf>>, <<asprintf>>, <<asnprintf>>---format output
|
||||
|
||||
INDEX
|
||||
fprintf
|
||||
INDEX
|
||||
_fprintf_r
|
||||
INDEX
|
||||
printf
|
||||
INDEX
|
||||
_printf_r
|
||||
INDEX
|
||||
asprintf
|
||||
INDEX
|
||||
_asprintf_r
|
||||
INDEX
|
||||
sprintf
|
||||
INDEX
|
||||
_sprintf_r
|
||||
INDEX
|
||||
snprintf
|
||||
INDEX
|
||||
_snprintf_r
|
||||
INDEX
|
||||
asnprintf
|
||||
INDEX
|
||||
_asnprintf_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
|
||||
int printf(const char *<[format]>, ...);
|
||||
int fprintf(FILE *<[fd]>, const char *<[format]>, ...);
|
||||
int sprintf(char *<[str]>, const char *<[format]>, ...);
|
||||
int snprintf(char *<[str]>, size_t <[size]>, const char *<[format]>,
|
||||
...);
|
||||
int asprintf(char **<[strp]>, const char *<[format]>, ...);
|
||||
char *asnprintf(char *<[str]>, size_t *<[size]>, const char *<[format]>,
|
||||
...);
|
||||
|
||||
int _printf_r(struct _reent *<[ptr]>, const char *<[format]>, ...);
|
||||
int _fprintf_r(struct _reent *<[ptr]>, FILE *<[fd]>,
|
||||
const char *<[format]>, ...);
|
||||
int _sprintf_r(struct _reent *<[ptr]>, char *<[str]>,
|
||||
const char *<[format]>, ...);
|
||||
int _snprintf_r(struct _reent *<[ptr]>, char *<[str]>, size_t <[size]>,
|
||||
const char *<[format]>, ...);
|
||||
int _asprintf_r(struct _reent *<[ptr]>, char **<[strp]>,
|
||||
const char *<[format]>, ...);
|
||||
char *_asnprintf_r(struct _reent *<[ptr]>, char *<[str]>,
|
||||
size_t *<[size]>, const char *<[format]>, ...);
|
||||
|
||||
DESCRIPTION
|
||||
<<printf>> accepts a series of arguments, applies to each a
|
||||
format specifier from <<*<[format]>>>, and writes the
|
||||
formatted data to <<stdout>>, without a terminating NUL
|
||||
character. The behavior of <<printf>> is undefined if there
|
||||
are not enough arguments for the format. <<printf>> returns
|
||||
when it reaches the end of the format string. If there are
|
||||
more arguments than the format requires, excess arguments are
|
||||
ignored.
|
||||
|
||||
<<fprintf>> is like <<printf>>, except that output is directed
|
||||
to the stream <[fd]> rather than <<stdout>>.
|
||||
|
||||
<<sprintf>> is like <<printf>>, except that output is directed
|
||||
to the buffer <[str]>, and a terminating NUL is output.
|
||||
Behavior is undefined if more output is generated than the
|
||||
buffer can hold.
|
||||
|
||||
<<snprintf>> is like <<sprintf>>, except that output is
|
||||
limited to at most <[size]> bytes, including the terminating
|
||||
<<NUL>>. As a special case, if <[size]> is 0, <[str]> can be
|
||||
NULL, and <<snprintf>> merely calculates how many bytes would
|
||||
be printed.
|
||||
|
||||
<<asprintf>> is like <<sprintf>>, except that the output is
|
||||
stored in a dynamically allocated buffer, <[pstr]>, which
|
||||
should be freed later with <<free>>.
|
||||
|
||||
<<asnprintf>> is like <<sprintf>>, except that the return type
|
||||
is either the original <[str]> if it was large enough, or a
|
||||
dynamically allocated string if the output exceeds *<[size]>;
|
||||
the length of the result is returned in *<[size]>. When
|
||||
dynamic allocation occurs, the contents of the original
|
||||
<[str]> may have been modified.
|
||||
|
||||
For <<sprintf>>, <<snprintf>>, and <<asnprintf>>, the behavior
|
||||
is undefined if the output <<*<[str]>>> overlaps with one of
|
||||
the arguments. Behavior is also undefined if the argument for
|
||||
<<%n>> within <<*<[format]>>> overlaps another argument.
|
||||
|
||||
<[format]> is a pointer to a character string containing two
|
||||
types of objects: ordinary characters (other than <<%>>),
|
||||
which are copied unchanged to the output, and conversion
|
||||
specifications, each of which is introduced by <<%>>. (To
|
||||
include <<%>> in the output, use <<%%>> in the format string.)
|
||||
A conversion specification has the following form:
|
||||
|
||||
. %[<[pos]>][<[flags]>][<[width]>][.<[prec]>][<[size]>]<[type]>
|
||||
|
||||
The fields of the conversion specification have the following
|
||||
meanings:
|
||||
|
||||
O+
|
||||
o <[pos]>
|
||||
|
||||
Conversions normally consume arguments in the order that they
|
||||
are presented. However, it is possible to consume arguments
|
||||
out of order, and reuse an argument for more than one
|
||||
conversion specification (although the behavior is undefined
|
||||
if the same argument is requested with different types), by
|
||||
specifying <[pos]>, which is a decimal integer followed by
|
||||
'$'. The integer must be between 1 and <NL_ARGMAX> from
|
||||
limits.h, and if argument <<%n$>> is requested, all earlier
|
||||
arguments must be requested somewhere within <[format]>. If
|
||||
positional parameters are used, then all conversion
|
||||
specifications except for <<%%>> must specify a position.
|
||||
This positional parameters method is a POSIX extension to the C
|
||||
standard definition for the functions.
|
||||
|
||||
o <[flags]>
|
||||
|
||||
<[flags]> is an optional sequence of characters which control
|
||||
output justification, numeric signs, decimal points, trailing
|
||||
zeros, and octal and hex prefixes. The flag characters are
|
||||
minus (<<->>), plus (<<+>>), space ( ), zero (<<0>>), sharp
|
||||
(<<#>>), and quote (<<'>>). They can appear in any
|
||||
combination, although not all flags can be used for all
|
||||
conversion specification types.
|
||||
|
||||
o+
|
||||
o '
|
||||
A POSIX extension to the C standard. However, this
|
||||
implementation presently treats it as a no-op, which
|
||||
is the default behavior for the C locale, anyway. (If
|
||||
it did what it is supposed to, when <[type]> were <<i>>,
|
||||
<<d>>, <<u>>, <<f>>, <<F>>, <<g>>, or <<G>>, the
|
||||
integer portion of the conversion would be formatted
|
||||
with thousands' grouping wide characters.)
|
||||
|
||||
o -
|
||||
The result of the conversion is left
|
||||
justified, and the right is padded with
|
||||
blanks. If you do not use this flag, the
|
||||
result is right justified, and padded on the
|
||||
left.
|
||||
|
||||
o +
|
||||
The result of a signed conversion (as
|
||||
determined by <[type]> of <<d>>, <<i>>, <<a>>,
|
||||
<<A>>, <<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or
|
||||
<<G>>) will always begin with a plus or minus
|
||||
sign. (If you do not use this flag, positive
|
||||
values do not begin with a plus sign.)
|
||||
|
||||
o " " (space)
|
||||
If the first character of a signed conversion
|
||||
specification is not a sign, or if a signed
|
||||
conversion results in no characters, the
|
||||
result will begin with a space. If the space
|
||||
( ) flag and the plus (<<+>>) flag both
|
||||
appear, the space flag is ignored.
|
||||
|
||||
o 0
|
||||
If the <[type]> character is <<d>>, <<i>>,
|
||||
<<o>>, <<u>>, <<x>>, <<X>>, <<a>>, <<A>>,
|
||||
<<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or <<G>>: leading
|
||||
zeros are used to pad the field width
|
||||
(following any indication of sign or base); no
|
||||
spaces are used for padding. If the zero
|
||||
(<<0>>) and minus (<<->>) flags both appear,
|
||||
the zero (<<0>>) flag will be ignored. For
|
||||
<<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>
|
||||
conversions, if a precision <[prec]> is
|
||||
specified, the zero (<<0>>) flag is ignored.
|
||||
|
||||
Note that <<0>> is interpreted as a flag, not
|
||||
as the beginning of a field width.
|
||||
|
||||
o #
|
||||
The result is to be converted to an
|
||||
alternative form, according to the <[type]>
|
||||
character:
|
||||
|
||||
o+
|
||||
o o
|
||||
Increases precision to force the first
|
||||
digit of the result to be a zero.
|
||||
|
||||
o x
|
||||
A non-zero result will have a <<0x>>
|
||||
prefix.
|
||||
|
||||
o X
|
||||
A non-zero result will have a <<0X>>
|
||||
prefix.
|
||||
|
||||
o a, A, e, E, f, or F
|
||||
The result will always contain a
|
||||
decimal point even if no digits follow
|
||||
the point. (Normally, a decimal point
|
||||
appears only if a digit follows it.)
|
||||
Trailing zeros are removed.
|
||||
|
||||
o g or G
|
||||
The result will always contain a
|
||||
decimal point even if no digits follow
|
||||
the point. Trailing zeros are not
|
||||
removed.
|
||||
|
||||
o all others
|
||||
Undefined.
|
||||
|
||||
o-
|
||||
o-
|
||||
|
||||
o <[width]>
|
||||
|
||||
<[width]> is an optional minimum field width. You can
|
||||
either specify it directly as a decimal integer, or
|
||||
indirectly by using instead an asterisk (<<*>>), in
|
||||
which case an <<int>> argument is used as the field
|
||||
width. If positional arguments are used, then the
|
||||
width must also be specified positionally as <<*m$>>,
|
||||
with m as a decimal integer. Negative field widths
|
||||
are treated as specifying the minus (<<->>) flag for
|
||||
left justfication, along with a positive field width.
|
||||
The resulting format may be wider than the specified
|
||||
width.
|
||||
|
||||
o <[prec]>
|
||||
|
||||
<[prec]> is an optional field; if present, it is
|
||||
introduced with `<<.>>' (a period). You can specify
|
||||
the precision either directly as a decimal integer or
|
||||
indirectly by using an asterisk (<<*>>), in which case
|
||||
an <<int>> argument is used as the precision. If
|
||||
positional arguments are used, then the precision must
|
||||
also be specified positionally as <<*m$>>, with m as a
|
||||
decimal integer. Supplying a negative precision is
|
||||
equivalent to omitting the precision. If only a
|
||||
period is specified the precision is zero. The effect
|
||||
depends on the conversion <[type]>.
|
||||
|
||||
o+
|
||||
o d, i, o, u, x, or X
|
||||
Minimum number of digits to appear. If no
|
||||
precision is given, defaults to 1.
|
||||
|
||||
o a or A
|
||||
Number of digits to appear after the decimal
|
||||
point. If no precision is given, the
|
||||
precision defaults to the minimum needed for
|
||||
an exact representation.
|
||||
|
||||
o e, E, f or F
|
||||
Number of digits to appear after the decimal
|
||||
point. If no precision is given, the
|
||||
precision defaults to 6.
|
||||
|
||||
o g or G
|
||||
Maximum number of significant digits. A
|
||||
precision of 0 is treated the same as a
|
||||
precision of 1. If no precision is given, the
|
||||
precision defaults to 6.
|
||||
|
||||
o s or S
|
||||
Maximum number of characters to print from the
|
||||
string. If no precision is given, the entire
|
||||
string is printed.
|
||||
|
||||
o all others
|
||||
undefined.
|
||||
|
||||
o-
|
||||
|
||||
o <[size]>
|
||||
|
||||
<[size]> is an optional modifier that changes the data
|
||||
type that the corresponding argument has. Behavior is
|
||||
unspecified if a size is given that does not match the
|
||||
<[type]>.
|
||||
|
||||
o+
|
||||
o hh
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument should be
|
||||
converted to a <<signed char>> or <<unsigned
|
||||
char>> before printing.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<signed char>>.
|
||||
|
||||
o h
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument should be
|
||||
converted to a <<short>> or <<unsigned short>>
|
||||
before printing.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<short>>.
|
||||
|
||||
o l
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument is a
|
||||
<<long>> or <<unsigned long>>.
|
||||
|
||||
With <<c>>, specifies that the argument has
|
||||
type <<wint_t>>.
|
||||
|
||||
With <<s>>, specifies that the argument is a
|
||||
pointer to <<wchar_t>>.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<long>>.
|
||||
|
||||
With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>,
|
||||
<<g>>, or <<G>>, has no effect (because of
|
||||
vararg promotion rules, there is no need to
|
||||
distinguish between <<float>> and <<double>>).
|
||||
|
||||
o ll
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument is a
|
||||
<<long long>> or <<unsigned long long>>.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<long long>>.
|
||||
|
||||
o j
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument is an
|
||||
<<intmax_t>> or <<uintmax_t>>.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to an <<intmax_t>>.
|
||||
|
||||
o z
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument is a <<size_t>>.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<size_t>>.
|
||||
|
||||
o t
|
||||
With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
|
||||
<<X>>, specifies that the argument is a
|
||||
<<ptrdiff_t>>.
|
||||
|
||||
With <<n>>, specifies that the argument is a
|
||||
pointer to a <<ptrdiff_t>>.
|
||||
|
||||
o L
|
||||
With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>,
|
||||
<<g>>, or <<G>>, specifies that the argument
|
||||
is a <<long double>>.
|
||||
|
||||
o-
|
||||
|
||||
o <[type]>
|
||||
|
||||
<[type]> specifies what kind of conversion <<printf>>
|
||||
performs. Here is a table of these:
|
||||
|
||||
o+
|
||||
o %
|
||||
Prints the percent character (<<%>>).
|
||||
|
||||
o c
|
||||
Prints <[arg]> as single character. If the
|
||||
<<l>> size specifier is in effect, a multibyte
|
||||
character is printed.
|
||||
|
||||
o C
|
||||
Short for <<%lc>>. A POSIX extension to the C standard.
|
||||
|
||||
o s
|
||||
Prints the elements of a pointer to <<char>>
|
||||
until the precision or a null character is
|
||||
reached. If the <<l>> size specifier is in
|
||||
effect, the pointer is to an array of
|
||||
<<wchar_t>>, and the string is converted to
|
||||
multibyte characters before printing.
|
||||
|
||||
o S
|
||||
Short for <<%ls>>. A POSIX extension to the C standard.
|
||||
|
||||
o d or i
|
||||
Prints a signed decimal integer; takes an
|
||||
<<int>>. Leading zeros are inserted as
|
||||
necessary to reach the precision. A value of 0 with
|
||||
a precision of 0 produces an empty string.
|
||||
|
||||
o D
|
||||
Newlib extension, short for <<%ld>>.
|
||||
|
||||
o o
|
||||
Prints an unsigned octal integer; takes an
|
||||
<<unsigned>>. Leading zeros are inserted as
|
||||
necessary to reach the precision. A value of 0 with
|
||||
a precision of 0 produces an empty string.
|
||||
|
||||
o O
|
||||
Newlib extension, short for <<%lo>>.
|
||||
|
||||
o u
|
||||
Prints an unsigned decimal integer; takes an
|
||||
<<unsigned>>. Leading zeros are inserted as
|
||||
necessary to reach the precision. A value of 0 with
|
||||
a precision of 0 produces an empty string.
|
||||
|
||||
o U
|
||||
Newlib extension, short for <<%lu>>.
|
||||
|
||||
o x
|
||||
Prints an unsigned hexadecimal integer (using
|
||||
<<abcdef>> as digits beyond <<9>>); takes an
|
||||
<<unsigned>>. Leading zeros are inserted as
|
||||
necessary to reach the precision. A value of 0 with
|
||||
a precision of 0 produces an empty string.
|
||||
|
||||
o X
|
||||
Like <<x>>, but uses <<ABCDEF>> as digits
|
||||
beyond <<9>>.
|
||||
|
||||
o f
|
||||
Prints a signed value of the form
|
||||
<<[-]9999.9999>>, with the precision
|
||||
determining how many digits follow the decimal
|
||||
point; takes a <<double>> (remember that
|
||||
<<float>> promotes to <<double>> as a vararg).
|
||||
The low order digit is rounded to even. If
|
||||
the precision results in at most DECIMAL_DIG
|
||||
digits, the result is rounded correctly; if
|
||||
more than DECIMAL_DIG digits are printed, the
|
||||
result is only guaranteed to round back to the
|
||||
original value.
|
||||
|
||||
If the value is infinite, the result is
|
||||
<<inf>>, and no zero padding is performed. If
|
||||
the value is not a number, the result is
|
||||
<<nan>>, and no zero padding is performed.
|
||||
|
||||
o F
|
||||
Like <<f>>, but uses <<INF>> and <<NAN>> for
|
||||
non-finite numbers.
|
||||
|
||||
o e
|
||||
Prints a signed value of the form
|
||||
<<[-]9.9999e[+|-]999>>; takes a <<double>>.
|
||||
The digit before the decimal point is non-zero
|
||||
if the value is non-zero. The precision
|
||||
determines how many digits appear between
|
||||
<<.>> and <<e>>, and the exponent always
|
||||
contains at least two digits. The value zero
|
||||
has an exponent of zero. If the value is not
|
||||
finite, it is printed like <<f>>.
|
||||
|
||||
o E
|
||||
Like <<e>>, but using <<E>> to introduce the
|
||||
exponent, and like <<F>> for non-finite
|
||||
values.
|
||||
|
||||
o g
|
||||
Prints a signed value in either <<f>> or <<e>>
|
||||
form, based on the given value and
|
||||
precision---an exponent less than -4 or
|
||||
greater than the precision selects the <<e>>
|
||||
form. Trailing zeros and the decimal point
|
||||
are printed only if necessary; takes a
|
||||
<<double>>.
|
||||
|
||||
o G
|
||||
Like <<g>>, except use <<F>> or <<E>> form.
|
||||
|
||||
o a
|
||||
Prints a signed value of the form
|
||||
<<[-]0x1.ffffp[+|-]9>>; takes a <<double>>.
|
||||
The letters <<abcdef>> are used for digits
|
||||
beyond <<9>>. The precision determines how
|
||||
many digits appear after the decimal point.
|
||||
The exponent contains at least one digit, and
|
||||
is a decimal value representing the power of
|
||||
2; a value of 0 has an exponent of 0.
|
||||
Non-finite values are printed like <<f>>.
|
||||
|
||||
o A
|
||||
Like <<a>>, except uses <<X>>, <<P>>, and
|
||||
<<ABCDEF>> instead of lower case.
|
||||
|
||||
o n
|
||||
Takes a pointer to <<int>>, and stores a count
|
||||
of the number of bytes written so far. No
|
||||
output is created.
|
||||
|
||||
o p
|
||||
Takes a pointer to <<void>>, and prints it in
|
||||
an implementation-defined format. This
|
||||
implementation is similar to <<%#tx>>), except
|
||||
that <<0x>> appears even for the NULL pointer.
|
||||
|
||||
o-
|
||||
O-
|
||||
|
||||
<<_printf_r>>, <<_fprintf_r>>, <<_asprintf_r>>,
|
||||
<<_sprintf_r>>, <<_snprintf_r>>, <<_asnprintf_r>> are simply
|
||||
reentrant versions of the functions above.
|
||||
|
||||
RETURNS
|
||||
On success, <<sprintf>> and <<asprintf>> return the number of bytes in
|
||||
the output string, except the concluding <<NUL>> is not counted.
|
||||
<<snprintf>> returns the number of bytes that would be in the output
|
||||
string, except the concluding <<NUL>> is not counted. <<printf>> and
|
||||
<<fprintf>> return the number of characters transmitted.
|
||||
<<asnprintf>> returns the original <[str]> if there was enough room,
|
||||
otherwise it returns an allocated string.
|
||||
|
||||
If an error occurs, the result of <<printf>>, <<fprintf>>,
|
||||
<<snprintf>>, and <<asprintf>> is a negative value, and the result of
|
||||
<<asnprintf>> is NULL. No error returns occur for <<sprintf>>. For
|
||||
<<printf>> and <<fprintf>>, <<errno>> may be set according to
|
||||
<<fputc>>. For <<asprintf>> and <<asnprintf>>, <<errno>> may be set
|
||||
to ENOMEM if allocation fails, and for <<snprintf>>, <<errno>> may be
|
||||
set to EOVERFLOW if <[size]> or the output length exceeds INT_MAX.
|
||||
|
||||
BUGS
|
||||
The ``''' (quote) flag does not work when locale's thousands_sep is not empty.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<printf>>, <<fprintf>>, <<sprintf>>, and
|
||||
<<snprintf>>. <<asprintf>> and <<asnprintf>> are newlib extensions.
|
||||
|
||||
The ANSI C standard specifies that implementations must support at
|
||||
least formatted output of up to 509 characters. This implementation
|
||||
has no inherent limit.
|
||||
|
||||
Depending on how newlib was configured, not all format specifiers are
|
||||
supported.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(_sprintf_r, (ptr, str, fmt),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
_sprintf_r(ptr, str, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
*f._p = '\0'; /* terminate the string */
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
#ifdef _HAVE_STDC
|
||||
_DEFUN(sprintf, (str, fmt),
|
||||
char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
sprintf(str, fmt, va_alist)
|
||||
char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = _svfprintf_r (_REENT, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
*f._p = '\0'; /* terminate the string */
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#endif
|
469
contrib/sdk/sources/newlib/stdio/sscanf.c
Normal file
469
contrib/sdk/sources/newlib/stdio/sscanf.c
Normal file
@@ -0,0 +1,469 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
/*
|
||||
FUNCTION
|
||||
<<sscanf>>, <<fscanf>>, <<scanf>>---scan and format input
|
||||
|
||||
INDEX
|
||||
scanf
|
||||
INDEX
|
||||
_scanf_r
|
||||
INDEX
|
||||
fscanf
|
||||
INDEX
|
||||
_fscanf_r
|
||||
INDEX
|
||||
sscanf
|
||||
INDEX
|
||||
_sscanf_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
|
||||
int scanf(const char *<[format]>, ...);
|
||||
int fscanf(FILE *<[fd]>, const char *<[format]>, ...);
|
||||
int sscanf(const char *<[str]>, const char *<[format]>, ...);
|
||||
|
||||
int _scanf_r(struct _reent *<[ptr]>, const char *<[format]>, ...);
|
||||
int _fscanf_r(struct _reent *<[ptr]>, FILE *<[fd]>,
|
||||
const char *<[format]>, ...);
|
||||
int _sscanf_r(struct _reent *<[ptr]>, const char *<[str]>,
|
||||
const char *<[format]>, ...);
|
||||
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
|
||||
int scanf(<[format]> [, <[arg]>, ...])
|
||||
char *<[format]>;
|
||||
|
||||
int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
|
||||
FILE *<[fd]>;
|
||||
char *<[format]>;
|
||||
|
||||
int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
|
||||
char *<[str]>;
|
||||
char *<[format]>;
|
||||
|
||||
int _scanf_r(<[ptr]>, <[format]> [, <[arg]>, ...])
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[format]>;
|
||||
|
||||
int _fscanf_r(<[ptr]>, <[fd]>, <[format]> [, <[arg]>, ...]);
|
||||
struct _reent *<[ptr]>;
|
||||
FILE *<[fd]>;
|
||||
char *<[format]>;
|
||||
|
||||
int _sscanf_r(<[ptr]>, <[str]>, <[format]> [, <[arg]>, ...]);
|
||||
struct _reent *<[ptr]>;
|
||||
char *<[str]>;
|
||||
char *<[format]>;
|
||||
|
||||
|
||||
DESCRIPTION
|
||||
<<scanf>> scans a series of input fields from standard input,
|
||||
one character at a time. Each field is interpreted according to
|
||||
a format specifier passed to <<scanf>> in the format string at
|
||||
<<*<[format]>>>. <<scanf>> stores the interpreted input from
|
||||
each field at the address passed to it as the corresponding argument
|
||||
following <[format]>. You must supply the same number of
|
||||
format specifiers and address arguments as there are input fields.
|
||||
|
||||
There must be sufficient address arguments for the given format
|
||||
specifiers; if not the results are unpredictable and likely
|
||||
disasterous. Excess address arguments are merely ignored.
|
||||
|
||||
<<scanf>> often produces unexpected results if the input diverges from
|
||||
an expected pattern. Since the combination of <<gets>> or <<fgets>>
|
||||
followed by <<sscanf>> is safe and easy, that is the preferred way
|
||||
to be certain that a program is synchronized with input at the end
|
||||
of a line.
|
||||
|
||||
<<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
|
||||
source of input: <<fscanf>> reads from a file, and <<sscanf>>
|
||||
from a string.
|
||||
|
||||
The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant
|
||||
versions of <<scanf>>, <<fscanf>>, and <<sscanf>> that take an additional
|
||||
first argument pointing to a reentrancy structure.
|
||||
|
||||
The string at <<*<[format]>>> is a character sequence composed
|
||||
of zero or more directives. Directives are composed of
|
||||
one or more whitespace characters, non-whitespace characters,
|
||||
and format specifications.
|
||||
|
||||
Whitespace characters are blank (<< >>), tab (<<\t>>), or
|
||||
newline (<<\n>>).
|
||||
When <<scanf>> encounters a whitespace character in the format string
|
||||
it will read (but not store) all consecutive whitespace characters
|
||||
up to the next non-whitespace character in the input.
|
||||
|
||||
Non-whitespace characters are all other ASCII characters except the
|
||||
percent sign (<<%>>). When <<scanf>> encounters a non-whitespace
|
||||
character in the format string it will read, but not store
|
||||
a matching non-whitespace character.
|
||||
|
||||
Format specifications tell <<scanf>> to read and convert characters
|
||||
from the input field into specific types of values, and store then
|
||||
in the locations specified by the address arguments.
|
||||
|
||||
Trailing whitespace is left unread unless explicitly
|
||||
matched in the format string.
|
||||
|
||||
The format specifiers must begin with a percent sign (<<%>>)
|
||||
and have the following form:
|
||||
|
||||
. %[*][<[width]>][<[size]>]<[type]>
|
||||
|
||||
Each format specification begins with the percent character (<<%>>).
|
||||
The other fields are:
|
||||
o+
|
||||
o *
|
||||
an optional marker; if present, it suppresses interpretation and
|
||||
assignment of this input field.
|
||||
|
||||
o <[width]>
|
||||
an optional maximum field width: a decimal integer,
|
||||
which controls the maximum number of characters that
|
||||
will be read before converting the current input field. If the
|
||||
input field has fewer than <[width]> characters, <<scanf>>
|
||||
reads all the characters in the field, and then
|
||||
proceeds with the next field and its format specification.
|
||||
|
||||
If a whitespace or a non-convertable character occurs
|
||||
before <[width]> character are read, the characters up
|
||||
to that character are read, converted, and stored.
|
||||
Then <<scanf>> proceeds to the next format specification.
|
||||
|
||||
o size
|
||||
<<h>>, <<j>>, <<l>>, <<L>>, <<t>>, and <<z>> are optional size
|
||||
characters which override the default way that <<scanf>>
|
||||
interprets the data type of the corresponding argument.
|
||||
|
||||
|
||||
.Modifier Type(s)
|
||||
. hh d, i, o, u, x, n convert input to char,
|
||||
. store in char object
|
||||
.
|
||||
. h d, i, o, u, x, n convert input to short,
|
||||
. store in short object
|
||||
.
|
||||
. h D, I, O, U, X no effect
|
||||
. e, f, c, s, p
|
||||
.
|
||||
. j d, i, o, u, x, n convert input to intmax_t,
|
||||
. store in intmax_t object
|
||||
.
|
||||
. j all others no effect
|
||||
.
|
||||
. l d, i, o, u, x, n convert input to long,
|
||||
. store in long object
|
||||
.
|
||||
. l e, f, g convert input to double
|
||||
. store in a double object
|
||||
.
|
||||
. l D, I, O, U, X no effect
|
||||
. c, s, p
|
||||
.
|
||||
. ll d, i, o, u, x, n convert to long long,
|
||||
. store in long long
|
||||
.
|
||||
. L d, i, o, u, x, n convert to long long,
|
||||
. store in long long
|
||||
.
|
||||
. L e, f, g, E, G convert to long double,
|
||||
. store in long double
|
||||
.
|
||||
. L all others no effect
|
||||
.
|
||||
. t d, i, o, u, x, n convert input to ptrdiff_t,
|
||||
. store in ptrdiff_t object
|
||||
.
|
||||
. t all others no effect
|
||||
.
|
||||
. z d, i, o, u, x, n convert input to size_t,
|
||||
. store in size_t object
|
||||
.
|
||||
. z all others no effect
|
||||
.
|
||||
|
||||
|
||||
o <[type]>
|
||||
|
||||
A character to specify what kind of conversion
|
||||
<<scanf>> performs. Here is a table of the conversion
|
||||
characters:
|
||||
|
||||
o+
|
||||
o %
|
||||
No conversion is done; the percent character (<<%>>) is stored.
|
||||
|
||||
o c
|
||||
Scans one character. Corresponding <[arg]>: <<(char *arg)>>.
|
||||
|
||||
o s
|
||||
Reads a character string into the array supplied.
|
||||
Corresponding <[arg]>: <<(char arg[])>>.
|
||||
|
||||
o [<[pattern]>]
|
||||
Reads a non-empty character string into memory
|
||||
starting at <[arg]>. This area must be large
|
||||
enough to accept the sequence and a
|
||||
terminating null character which will be added
|
||||
automatically. (<[pattern]> is discussed in the paragraph following
|
||||
this table). Corresponding <[arg]>: <<(char *arg)>>.
|
||||
|
||||
o d
|
||||
Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
|
||||
|
||||
o D
|
||||
Reads a decimal integer into the corresponding
|
||||
<[arg]>: <<(long *arg)>>.
|
||||
|
||||
o o
|
||||
Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
|
||||
|
||||
o O
|
||||
Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
|
||||
|
||||
o u
|
||||
Reads an unsigned decimal integer into the corresponding
|
||||
<[arg]>: <<(unsigned int *arg)>>.
|
||||
|
||||
|
||||
o U
|
||||
Reads an unsigned decimal integer into the corresponding <[arg]>:
|
||||
<<(unsigned long *arg)>>.
|
||||
|
||||
o x,X
|
||||
Read a hexadecimal integer into the corresponding <[arg]>:
|
||||
<<(int *arg)>>.
|
||||
|
||||
o e, f, g
|
||||
Read a floating-point number into the corresponding <[arg]>:
|
||||
<<(float *arg)>>.
|
||||
|
||||
o E, F, G
|
||||
Read a floating-point number into the corresponding <[arg]>:
|
||||
<<(double *arg)>>.
|
||||
|
||||
o i
|
||||
Reads a decimal, octal or hexadecimal integer into the
|
||||
corresponding <[arg]>: <<(int *arg)>>.
|
||||
|
||||
o I
|
||||
Reads a decimal, octal or hexadecimal integer into the
|
||||
corresponding <[arg]>: <<(long *arg)>>.
|
||||
|
||||
o n
|
||||
Stores the number of characters read in the corresponding
|
||||
<[arg]>: <<(int *arg)>>.
|
||||
|
||||
o p
|
||||
Stores a scanned pointer. ANSI C leaves the details
|
||||
to each implementation; this implementation treats
|
||||
<<%p>> exactly the same as <<%U>>. Corresponding
|
||||
<[arg]>: <<(void **arg)>>.
|
||||
o-
|
||||
|
||||
A <[pattern]> of characters surrounded by square brackets can be used
|
||||
instead of the <<s>> type character. <[pattern]> is a set of
|
||||
characters which define a search set of possible characters making up
|
||||
the <<scanf>> input field. If the first character in the brackets is a
|
||||
caret (<<^>>), the search set is inverted to include all ASCII characters
|
||||
except those between the brackets. There is also a range facility
|
||||
which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
|
||||
The hyphen must not be the first or last character in the set.
|
||||
The character prior to the hyphen must be lexically less than the
|
||||
character after it.
|
||||
|
||||
Here are some <[pattern]> examples:
|
||||
o+
|
||||
o %[abcd]
|
||||
matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
|
||||
|
||||
o %[^abcd]
|
||||
matches strings containing any characters except <<a>>, <<b>>,
|
||||
<<c>>, or <<d>>
|
||||
|
||||
o %[A-DW-Z]
|
||||
matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
|
||||
<<X>>, <<Y>>, <<Z>>
|
||||
|
||||
o %[z-a]
|
||||
matches the characters <<z>>, <<->>, and <<a>>
|
||||
o-
|
||||
|
||||
Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
|
||||
<<F>>, <<G>>) must correspond to the following general form:
|
||||
|
||||
. [+/-] ddddd[.]ddd [E|e[+|-]ddd]
|
||||
|
||||
where objects inclosed in square brackets are optional, and <<ddd>>
|
||||
represents decimal, octal, or hexadecimal digits.
|
||||
o-
|
||||
|
||||
RETURNS
|
||||
<<scanf>> returns the number of input fields successfully
|
||||
scanned, converted and stored; the return value does
|
||||
not include scanned fields which were not stored.
|
||||
|
||||
If <<scanf>> attempts to read at end-of-file, the return
|
||||
value is <<EOF>>.
|
||||
|
||||
If no fields were stored, the return value is <<0>>.
|
||||
|
||||
<<scanf>> might stop scanning a particular field before
|
||||
reaching the normal field end character, or may
|
||||
terminate entirely.
|
||||
|
||||
<<scanf>> stops scanning and storing the current field
|
||||
and moves to the next input field (if any)
|
||||
in any of the following situations:
|
||||
|
||||
O+
|
||||
o The assignment suppressing character (<<*>>) appears
|
||||
after the <<%>> in the format specification; the current
|
||||
input field is scanned but not stored.
|
||||
|
||||
o <[width]> characters have been read (<[width]> is a
|
||||
width specification, a positive decimal integer).
|
||||
|
||||
o The next character read cannot be converted
|
||||
under the the current format (for example,
|
||||
if a <<Z>> is read when the format is decimal).
|
||||
|
||||
o The next character in the input field does not appear
|
||||
in the search set (or does appear in the inverted search set).
|
||||
O-
|
||||
|
||||
When <<scanf>> stops scanning the current input field for one of
|
||||
these reasons, the next character is considered unread and
|
||||
used as the first character of the following input field, or the
|
||||
first character in a subsequent read operation on the input.
|
||||
|
||||
<<scanf>> will terminate under the following circumstances:
|
||||
|
||||
O+
|
||||
o The next character in the input field conflicts
|
||||
with a corresponding non-whitespace character in the
|
||||
format string.
|
||||
|
||||
o The next character in the input field is <<EOF>>.
|
||||
|
||||
o The format string has been exhausted.
|
||||
O-
|
||||
|
||||
When the format string contains a character sequence that is
|
||||
not part of a format specification, the same character
|
||||
sequence must appear in the input; <<scanf>> will
|
||||
scan but not store the matched characters. If a
|
||||
conflict occurs, the first conflicting character remains in the input
|
||||
as if it had never been read.
|
||||
|
||||
PORTABILITY
|
||||
<<scanf>> is ANSI C.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
||||
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
int
|
||||
_DEFUN(sscanf, (str, fmt),
|
||||
_CONST char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
int
|
||||
sscanf(str, fmt, va_alist)
|
||||
_CONST char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SRD | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._r = strlen (str);
|
||||
f._read = __seofread;
|
||||
f._ub._base = NULL;
|
||||
f._lb._base = NULL;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = __ssvfscanf_r (_REENT, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
#ifdef _HAVE_STDC
|
||||
int
|
||||
_DEFUN(_sscanf_r, (ptr, str, fmt),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *str _AND
|
||||
_CONST char *fmt _DOTS)
|
||||
#else
|
||||
int
|
||||
_sscanf_r(ptr, str, fmt, va_alist)
|
||||
struct _reent *ptr;
|
||||
_CONST char *str;
|
||||
_CONST char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SRD | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._r = strlen (str);
|
||||
f._read = __seofread;
|
||||
f._ub._base = NULL;
|
||||
f._lb._base = NULL;
|
||||
f._file = -1; /* No file. */
|
||||
#ifdef _HAVE_STDC
|
||||
va_start (ap, fmt);
|
||||
#else
|
||||
va_start (ap);
|
||||
#endif
|
||||
ret = __ssvfscanf_r (ptr, &f, fmt, ap);
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
150
contrib/sdk/sources/newlib/stdio/stdio.c
Normal file
150
contrib/sdk/sources/newlib/stdio/stdio.c
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Small standard I/O/seek/close functions.
|
||||
* These maintain the `known seek offset' for seek optimisation.
|
||||
*/
|
||||
|
||||
_READ_WRITE_RETURN_TYPE
|
||||
_DEFUN(__sread, (ptr, cookie, buf, n),
|
||||
struct _reent *ptr _AND
|
||||
void *cookie _AND
|
||||
char *buf _AND
|
||||
int n)
|
||||
{
|
||||
register FILE *fp = (FILE *) cookie;
|
||||
register int ret;
|
||||
|
||||
#ifdef __SCLE
|
||||
int oldmode = 0;
|
||||
if (fp->_flags & __SCLE)
|
||||
oldmode = setmode (fp->_file, O_BINARY);
|
||||
#endif
|
||||
|
||||
ret = _read_r (ptr, fp->_file, buf, n);
|
||||
|
||||
#ifdef __SCLE
|
||||
if (oldmode)
|
||||
setmode (fp->_file, oldmode);
|
||||
#endif
|
||||
|
||||
/* If the read succeeded, update the current offset. */
|
||||
|
||||
if (ret >= 0)
|
||||
fp->_offset += ret;
|
||||
else
|
||||
fp->_flags &= ~__SOFF; /* paranoia */
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Dummy function used in sscanf/swscanf. */
|
||||
_READ_WRITE_RETURN_TYPE
|
||||
_DEFUN(__seofread, (ptr, cookie, buf, len),
|
||||
struct _reent *_ptr _AND
|
||||
_PTR cookie _AND
|
||||
char *buf _AND
|
||||
int len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
_READ_WRITE_RETURN_TYPE
|
||||
_DEFUN(__swrite, (ptr, cookie, buf, n),
|
||||
struct _reent *ptr _AND
|
||||
void *cookie _AND
|
||||
char const *buf _AND
|
||||
int n)
|
||||
{
|
||||
register FILE *fp = (FILE *) cookie;
|
||||
int w;
|
||||
#ifdef __SCLE
|
||||
int oldmode=0;
|
||||
#endif
|
||||
|
||||
if (fp->_flags & __SAPP)
|
||||
_lseek_r (ptr, fp->_file, (_off_t) 0, SEEK_END);
|
||||
fp->_flags &= ~__SOFF; /* in case O_APPEND mode is set */
|
||||
|
||||
#ifdef __SCLE
|
||||
if (fp->_flags & __SCLE)
|
||||
oldmode = setmode (fp->_file, O_BINARY);
|
||||
#endif
|
||||
|
||||
w = _write_r (ptr, fp->_file, buf, n);
|
||||
|
||||
#ifdef __SCLE
|
||||
if (oldmode)
|
||||
setmode (fp->_file, oldmode);
|
||||
#endif
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
_fpos_t
|
||||
_DEFUN(__sseek, (ptr, cookie, offset, whence),
|
||||
struct _reent *ptr _AND
|
||||
void *cookie _AND
|
||||
_fpos_t offset _AND
|
||||
int whence)
|
||||
{
|
||||
register FILE *fp = (FILE *) cookie;
|
||||
register _off_t ret;
|
||||
|
||||
ret = _lseek_r (ptr, fp->_file, (_off_t) offset, whence);
|
||||
if (ret == -1L)
|
||||
fp->_flags &= ~__SOFF;
|
||||
else
|
||||
{
|
||||
fp->_flags |= __SOFF;
|
||||
fp->_offset = ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
_DEFUN(__sclose, (ptr, cookie),
|
||||
struct _reent *ptr _AND
|
||||
void *cookie)
|
||||
{
|
||||
FILE *fp = (FILE *) cookie;
|
||||
|
||||
return _close_r (ptr, fp->_file);
|
||||
}
|
||||
|
||||
#ifdef __SCLE
|
||||
int
|
||||
_DEFUN(__stextmode, (fd),
|
||||
int fd)
|
||||
{
|
||||
#ifdef __CYGWIN__
|
||||
extern int _cygwin_istext_for_stdio (int);
|
||||
return _cygwin_istext_for_stdio (fd);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
96
contrib/sdk/sources/newlib/stdio/tmpfile.c
Normal file
96
contrib/sdk/sources/newlib/stdio/tmpfile.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
FUNCTION
|
||||
<<tmpfile>>---create a temporary file
|
||||
|
||||
INDEX
|
||||
tmpfile
|
||||
INDEX
|
||||
_tmpfile_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *tmpfile(void);
|
||||
|
||||
FILE *_tmpfile_r(struct _reent *<[reent]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
FILE *tmpfile();
|
||||
|
||||
FILE *_tmpfile_r(<[reent]>)
|
||||
struct _reent *<[reent]>;
|
||||
|
||||
DESCRIPTION
|
||||
Create a temporary file (a file which will be deleted automatically),
|
||||
using a name generated by <<tmpnam>>. The temporary file is opened with
|
||||
the mode <<"wb+">>, permitting you to read and write anywhere in it
|
||||
as a binary file (without any data transformations the host system may
|
||||
perform for text files).
|
||||
|
||||
The alternate function <<_tmpfile_r>> is a reentrant version. The
|
||||
argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
<<tmpfile>> normally returns a pointer to the temporary file. If no
|
||||
temporary file could be created, the result is NULL, and <<errno>>
|
||||
records the reason for failure.
|
||||
|
||||
PORTABILITY
|
||||
Both ANSI C and the System V Interface Definition (Issue 2) require
|
||||
<<tmpfile>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
|
||||
<<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
|
||||
<<tmpfile>> also requires the global pointer <<environ>>.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifndef O_BINARY
|
||||
# define O_BINARY 0
|
||||
#endif
|
||||
|
||||
FILE *
|
||||
_DEFUN(_tmpfile_r, (ptr),
|
||||
struct _reent *ptr)
|
||||
{
|
||||
FILE *fp;
|
||||
int e;
|
||||
char *f;
|
||||
char buf[L_tmpnam];
|
||||
int fd;
|
||||
|
||||
do
|
||||
{
|
||||
if ((f = _tmpnam_r (ptr, buf)) == NULL)
|
||||
return NULL;
|
||||
fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
|
||||
S_IRUSR | S_IWUSR);
|
||||
}
|
||||
while (fd < 0 && ptr->_errno == EEXIST);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
fp = _fdopen_r (ptr, fd, "wb+");
|
||||
e = ptr->_errno;
|
||||
if (!fp)
|
||||
_close_r (ptr, fd);
|
||||
_CAST_VOID _remove_r (ptr, f);
|
||||
ptr->_errno = e;
|
||||
return fp;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
FILE *
|
||||
_DEFUN_VOID(tmpfile)
|
||||
{
|
||||
return _tmpfile_r (_REENT);
|
||||
}
|
||||
|
||||
#endif
|
209
contrib/sdk/sources/newlib/stdio/tmpnam.c
Normal file
209
contrib/sdk/sources/newlib/stdio/tmpnam.c
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* tmpname.c
|
||||
* Original Author: G. Haley
|
||||
*/
|
||||
/*
|
||||
FUNCTION
|
||||
<<tmpnam>>, <<tempnam>>---name for a temporary file
|
||||
|
||||
INDEX
|
||||
tmpnam
|
||||
INDEX
|
||||
tempnam
|
||||
INDEX
|
||||
_tmpnam_r
|
||||
INDEX
|
||||
_tempnam_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
char *tmpnam(char *<[s]>);
|
||||
char *tempnam(char *<[dir]>, char *<[pfx]>);
|
||||
char *_tmpnam_r(struct _reent *<[reent]>, char *<[s]>);
|
||||
char *_tempnam_r(struct _reent *<[reent]>, char *<[dir]>, char *<[pfx]>);
|
||||
|
||||
TRAD_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
char *tmpnam(<[s]>)
|
||||
char *<[s]>;
|
||||
|
||||
char *tempnam(<[dir]>, <[pfx]>)
|
||||
char *<[dir]>;
|
||||
char *<[pfx]>;
|
||||
|
||||
char *_tmpnam_r(<[reent]>, <[s]>)
|
||||
struct _reent *<[reent]>;
|
||||
char *<[s]>;
|
||||
|
||||
char *_tempnam_r(<[reent]>, <[dir]>, <[pfx]>)
|
||||
struct *<[reent]>;
|
||||
char *<[dir]>;
|
||||
char *<[pfx]>;
|
||||
|
||||
DESCRIPTION
|
||||
Use either of these functions to generate a name for a temporary file.
|
||||
The generated name is guaranteed to avoid collision with other files
|
||||
(for up to <<TMP_MAX>> calls of either function).
|
||||
|
||||
<<tmpnam>> generates file names with the value of <<P_tmpdir>>
|
||||
(defined in `<<stdio.h>>') as the leading directory component of the path.
|
||||
|
||||
You can use the <<tmpnam>> argument <[s]> to specify a suitable area
|
||||
of memory for the generated filename; otherwise, you can call
|
||||
<<tmpnam(NULL)>> to use an internal static buffer.
|
||||
|
||||
<<tempnam>> allows you more control over the generated filename: you
|
||||
can use the argument <[dir]> to specify the path to a directory for
|
||||
temporary files, and you can use the argument <[pfx]> to specify a
|
||||
prefix for the base filename.
|
||||
|
||||
If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
|
||||
environment variable <<TMPDIR>> instead; if there is no such value,
|
||||
<<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
|
||||
|
||||
If you don't need any particular prefix to the basename of temporary
|
||||
files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
|
||||
|
||||
<<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
|
||||
and <<tempnam>> respectively. The extra argument <[reent]> is a
|
||||
pointer to a reentrancy structure.
|
||||
|
||||
WARNINGS
|
||||
The generated filenames are suitable for temporary files, but do not
|
||||
in themselves make files temporary. Files with these names must still
|
||||
be explicitly removed when you no longer want them.
|
||||
|
||||
If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
|
||||
that it has room for at least <<L_tmpnam>> elements of type <<char>>.
|
||||
|
||||
RETURNS
|
||||
Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
|
||||
generated filename.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<tmpnam>>, but does not specify the use of
|
||||
<<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
|
||||
both <<tmpnam>> and <<tempnam>>.
|
||||
|
||||
Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
|
||||
<<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
||||
|
||||
The global pointer <<environ>> is also required.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <reent.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* Try to open the file specified, if it can't be opened then try
|
||||
another one. Return nonzero if successful, otherwise zero. */
|
||||
|
||||
static int
|
||||
_DEFUN(worker, (ptr, result, part1, part2, part3, part4),
|
||||
struct _reent *ptr _AND
|
||||
char *result _AND
|
||||
_CONST char *part1 _AND
|
||||
_CONST char *part2 _AND
|
||||
int part3 _AND
|
||||
int *part4)
|
||||
{
|
||||
/* Generate the filename and make sure that there isn't one called
|
||||
it already. */
|
||||
|
||||
while (1)
|
||||
{
|
||||
int t;
|
||||
_sprintf_r (ptr, result, "%s/%s%x.%x", part1, part2, part3, *part4);
|
||||
(*part4)++;
|
||||
t = _open_r (ptr, result, O_RDONLY, 0);
|
||||
if (t == -1)
|
||||
{
|
||||
if (ptr->_errno == ENOSYS)
|
||||
{
|
||||
result[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
_close_r (ptr, t);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *
|
||||
_DEFUN(_tmpnam_r, (p, s),
|
||||
struct _reent *p _AND
|
||||
char *s)
|
||||
{
|
||||
char *result;
|
||||
int pid;
|
||||
|
||||
if (s == NULL)
|
||||
{
|
||||
/* ANSI states we must use an internal static buffer if s is NULL */
|
||||
_REENT_CHECK_EMERGENCY(p);
|
||||
result = _REENT_EMERGENCY(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = s;
|
||||
}
|
||||
pid = _getpid_r (p);
|
||||
|
||||
if (worker (p, result, P_tmpdir, "t", pid, &p->_inc))
|
||||
{
|
||||
p->_inc++;
|
||||
return result;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
_DEFUN(_tempnam_r, (p, dir, pfx),
|
||||
struct _reent *p _AND
|
||||
_CONST char *dir _AND
|
||||
_CONST char *pfx)
|
||||
{
|
||||
char *filename;
|
||||
int length;
|
||||
_CONST char *prefix = (pfx) ? pfx : "";
|
||||
if (dir == NULL && (dir = getenv ("TMPDIR")) == NULL)
|
||||
dir = P_tmpdir;
|
||||
|
||||
/* two 8 digit numbers + . / */
|
||||
length = strlen (dir) + strlen (prefix) + (4 * sizeof (int)) + 2 + 1;
|
||||
|
||||
filename = _malloc_r (p, length);
|
||||
if (filename)
|
||||
{
|
||||
if (! worker (p, filename, dir, prefix,
|
||||
_getpid_r (p) ^ (int) (_POINTER_INT) p, &p->_inc))
|
||||
return NULL;
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
char *
|
||||
_DEFUN(tempnam, (dir, pfx),
|
||||
_CONST char *dir _AND
|
||||
_CONST char *pfx)
|
||||
{
|
||||
return _tempnam_r (_REENT, dir, pfx);
|
||||
}
|
||||
|
||||
char *
|
||||
_DEFUN(tmpnam, (s),
|
||||
char *s)
|
||||
{
|
||||
return _tmpnam_r (_REENT, s);
|
||||
}
|
||||
|
||||
#endif
|
217
contrib/sdk/sources/newlib/stdio/ungetc.c
Normal file
217
contrib/sdk/sources/newlib/stdio/ungetc.c
Normal file
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/*
|
||||
FUNCTION
|
||||
<<ungetc>>---push data back into a stream
|
||||
|
||||
INDEX
|
||||
ungetc
|
||||
INDEX
|
||||
_ungetc_r
|
||||
|
||||
ANSI_SYNOPSIS
|
||||
#include <stdio.h>
|
||||
int ungetc(int <[c]>, FILE *<[stream]>);
|
||||
|
||||
int _ungetc_r(struct _reent *<[reent]>, int <[c]>, FILE *<[stream]>);
|
||||
|
||||
DESCRIPTION
|
||||
<<ungetc>> is used to return bytes back to <[stream]> to be read again.
|
||||
If <[c]> is EOF, the stream is unchanged. Otherwise, the unsigned
|
||||
char <[c]> is put back on the stream, and subsequent reads will see
|
||||
the bytes pushed back in reverse order. Pushed byes are lost if the
|
||||
stream is repositioned, such as by <<fseek>>, <<fsetpos>>, or
|
||||
<<rewind>>.
|
||||
|
||||
The underlying file is not changed, but it is possible to push back
|
||||
something different than what was originally read. Ungetting a
|
||||
character will clear the end-of-stream marker, and decrement the file
|
||||
position indicator. Pushing back beyond the beginning of a file gives
|
||||
unspecified behavior.
|
||||
|
||||
The alternate function <<_ungetc_r>> is a reentrant version. The
|
||||
extra argument <[reent]> is a pointer to a reentrancy structure.
|
||||
|
||||
RETURNS
|
||||
The character pushed back, or <<EOF>> on error.
|
||||
|
||||
PORTABILITY
|
||||
ANSI C requires <<ungetc>>, but only requires a pushback buffer of one
|
||||
byte; although this implementation can handle multiple bytes, not all
|
||||
can. Pushing back a signed char is a common application bug.
|
||||
|
||||
Supporting OS subroutines required: <<sbrk>>.
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Expand the ungetc buffer `in place'. That is, adjust fp->_p when
|
||||
* the buffer moves, so that it points the same distance from the end,
|
||||
* and move the bytes in the buffer around as necessary so that they
|
||||
* are all at the end (stack-style).
|
||||
*/
|
||||
|
||||
/*static*/
|
||||
int
|
||||
_DEFUN(__submore, (rptr, fp),
|
||||
struct _reent *rptr _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
register int i;
|
||||
register unsigned char *p;
|
||||
|
||||
if (fp->_ub._base == fp->_ubuf)
|
||||
{
|
||||
/*
|
||||
* Get a new buffer (rather than expanding the old one).
|
||||
*/
|
||||
if ((p = (unsigned char *) _malloc_r (rptr, (size_t) BUFSIZ)) == NULL)
|
||||
return EOF;
|
||||
fp->_ub._base = p;
|
||||
fp->_ub._size = BUFSIZ;
|
||||
p += BUFSIZ - sizeof (fp->_ubuf);
|
||||
for (i = sizeof (fp->_ubuf); --i >= 0;)
|
||||
p[i] = fp->_ubuf[i];
|
||||
fp->_p = p;
|
||||
return 0;
|
||||
}
|
||||
i = fp->_ub._size;
|
||||
p = (unsigned char *) _realloc_r (rptr, (_PTR) (fp->_ub._base), i << 1);
|
||||
if (p == NULL)
|
||||
return EOF;
|
||||
_CAST_VOID memcpy ((_PTR) (p + i), (_PTR) p, (size_t) i);
|
||||
fp->_p = p + i;
|
||||
fp->_ub._base = p;
|
||||
fp->_ub._size = i << 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
_DEFUN(_ungetc_r, (rptr, c, fp),
|
||||
struct _reent *rptr _AND
|
||||
int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
if (c == EOF)
|
||||
return (EOF);
|
||||
|
||||
/* Ensure stdio has been initialized.
|
||||
??? Might be able to remove this as some other stdio routine should
|
||||
have already been called to get the char we are un-getting. */
|
||||
|
||||
CHECK_INIT (rptr, fp);
|
||||
|
||||
_flockfile (fp);
|
||||
|
||||
ORIENT (fp, -1);
|
||||
|
||||
/* After ungetc, we won't be at eof anymore */
|
||||
fp->_flags &= ~__SEOF;
|
||||
|
||||
if ((fp->_flags & __SRD) == 0)
|
||||
{
|
||||
/*
|
||||
* Not already reading: no good unless reading-and-writing.
|
||||
* Otherwise, flush any current write stuff.
|
||||
*/
|
||||
if ((fp->_flags & __SRW) == 0)
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
if (fp->_flags & __SWR)
|
||||
{
|
||||
if (_fflush_r (rptr, fp))
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
fp->_flags &= ~__SWR;
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = 0;
|
||||
}
|
||||
fp->_flags |= __SRD;
|
||||
}
|
||||
c = (unsigned char) c;
|
||||
|
||||
/*
|
||||
* If we are in the middle of ungetc'ing, just continue.
|
||||
* This may require expanding the current ungetc buffer.
|
||||
*/
|
||||
|
||||
if (HASUB (fp))
|
||||
{
|
||||
if (fp->_r >= fp->_ub._size && __submore (rptr, fp))
|
||||
{
|
||||
_funlockfile (fp);
|
||||
return EOF;
|
||||
}
|
||||
*--fp->_p = c;
|
||||
fp->_r++;
|
||||
_funlockfile (fp);
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we can handle this by simply backing up, do so,
|
||||
* but never replace the original character.
|
||||
* (This makes sscanf() work when scanning `const' data.)
|
||||
*/
|
||||
|
||||
if (fp->_bf._base != NULL && fp->_p > fp->_bf._base && fp->_p[-1] == c)
|
||||
{
|
||||
fp->_p--;
|
||||
fp->_r++;
|
||||
_funlockfile (fp);
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an ungetc buffer.
|
||||
* Initially, we will use the `reserve' buffer.
|
||||
*/
|
||||
|
||||
fp->_ur = fp->_r;
|
||||
fp->_up = fp->_p;
|
||||
fp->_ub._base = fp->_ubuf;
|
||||
fp->_ub._size = sizeof (fp->_ubuf);
|
||||
fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
|
||||
fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];
|
||||
fp->_r = 1;
|
||||
_funlockfile (fp);
|
||||
return c;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
int
|
||||
_DEFUN(ungetc, (c, fp),
|
||||
int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
return _ungetc_r (_REENT, c, fp);
|
||||
}
|
||||
#endif /* !_REENT_ONLY */
|
71
contrib/sdk/sources/newlib/stdio/vasniprintf.c
Normal file
71
contrib/sdk/sources/newlib/stdio/vasniprintf.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/* Copyright (C) 2007, 2008 Eric Blake
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
/* This code was derived from asprintf.c */
|
||||
/* doc in viprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
char *
|
||||
_DEFUN(_vasniprintf_r, (ptr, buf, lenp, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
char *buf _AND
|
||||
size_t *lenp _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
size_t len = *lenp;
|
||||
|
||||
if (buf && len)
|
||||
{
|
||||
/* mark an existing buffer, but allow allocation of larger string */
|
||||
f._flags = __SWR | __SSTR | __SOPT;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* mark a zero-length reallocatable buffer */
|
||||
f._flags = __SWR | __SSTR | __SMBF;
|
||||
len = 0;
|
||||
buf = NULL;
|
||||
}
|
||||
f._bf._base = f._p = (unsigned char *) buf;
|
||||
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
|
||||
FIXME - it would be nice to rewrite sys/reent.h to support size_t
|
||||
for _size. */
|
||||
if (len > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return NULL;
|
||||
}
|
||||
f._bf._size = f._w = len;
|
||||
f._file = -1; /* No file. */
|
||||
ret = _svfiprintf_r (ptr, &f, fmt, ap);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
*lenp = ret;
|
||||
*f._p = '\0';
|
||||
return (char *) f._bf._base;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
char *
|
||||
_DEFUN(vasniprintf, (buf, lenp, fmt, ap),
|
||||
char *buf _AND
|
||||
size_t *lenp _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vasniprintf_r (_REENT, buf, lenp, fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
71
contrib/sdk/sources/newlib/stdio/vasnprintf.c
Normal file
71
contrib/sdk/sources/newlib/stdio/vasnprintf.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/* Copyright (C) 2007 Eric Blake
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
/* This code was derived from asprintf.c */
|
||||
/* doc in vfprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
char *
|
||||
_DEFUN(_vasnprintf_r, (ptr, buf, lenp, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
char *buf _AND
|
||||
size_t *lenp _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
size_t len = *lenp;
|
||||
|
||||
if (buf && len)
|
||||
{
|
||||
/* mark an existing buffer, but allow allocation of larger string */
|
||||
f._flags = __SWR | __SSTR | __SOPT;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* mark a zero-length reallocatable buffer */
|
||||
f._flags = __SWR | __SSTR | __SMBF;
|
||||
len = 0;
|
||||
buf = NULL;
|
||||
}
|
||||
f._bf._base = f._p = (unsigned char *) buf;
|
||||
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
|
||||
FIXME - it would be nice to rewrite sys/reent.h to support size_t
|
||||
for _size. */
|
||||
if (len > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return NULL;
|
||||
}
|
||||
f._bf._size = f._w = len;
|
||||
f._file = -1; /* No file. */
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
*lenp = ret;
|
||||
*f._p = '\0';
|
||||
return (char *) f._bf._base;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
char *
|
||||
_DEFUN(vasnprintf, (buf, lenp, fmt, ap),
|
||||
char *buf _AND
|
||||
size_t *lenp _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vasnprintf_r (_REENT, buf, lenp, fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
47
contrib/sdk/sources/newlib/stdio/vdiprintf.c
Normal file
47
contrib/sdk/sources/newlib/stdio/vdiprintf.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright 2005, 2007 Shaun Jackman
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
/* doc in diprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_vdiprintf_r, (ptr, fd, format, ap),
|
||||
struct _reent *ptr _AND
|
||||
int fd _AND
|
||||
const char *format _AND
|
||||
va_list ap)
|
||||
{
|
||||
char *p;
|
||||
char buf[512];
|
||||
size_t n = sizeof buf;
|
||||
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
p = _vasniprintf_r (ptr, buf, &n, format, ap);
|
||||
if (!p)
|
||||
return -1;
|
||||
n = _write_r (ptr, fd, p, n);
|
||||
if (p != buf)
|
||||
_free_r (ptr, p);
|
||||
return n;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vdiprintf, (fd, format, ap),
|
||||
int fd _AND
|
||||
const char *format _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vdiprintf_r (_REENT, fd, format, ap);
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
47
contrib/sdk/sources/newlib/stdio/vdprintf.c
Normal file
47
contrib/sdk/sources/newlib/stdio/vdprintf.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright 2005, 2007 Shaun Jackman
|
||||
* Permission to use, copy, modify, and distribute this software
|
||||
* is freely granted, provided that this notice is preserved.
|
||||
*/
|
||||
/* doc in dprintf.c */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
_DEFUN(_vdprintf_r, (ptr, fd, format, ap),
|
||||
struct _reent *ptr _AND
|
||||
int fd _AND
|
||||
const char *format _AND
|
||||
va_list ap)
|
||||
{
|
||||
char *p;
|
||||
char buf[512];
|
||||
size_t n = sizeof buf;
|
||||
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
p = _vasnprintf_r (ptr, buf, &n, format, ap);
|
||||
if (!p)
|
||||
return -1;
|
||||
n = _write_r (ptr, fd, p, n);
|
||||
if (p != buf)
|
||||
_free_r (ptr, p);
|
||||
return n;
|
||||
}
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vdprintf, (fd, format, ap),
|
||||
int fd _AND
|
||||
const char *format _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vdprintf_r (_REENT, fd, format, ap);
|
||||
}
|
||||
|
||||
#endif /* ! _REENT_ONLY */
|
283
contrib/sdk/sources/newlib/stdio/vfieeefp.h
Normal file
283
contrib/sdk/sources/newlib/stdio/vfieeefp.h
Normal file
@@ -0,0 +1,283 @@
|
||||
/****************************************************************
|
||||
*
|
||||
* The author of this software is David M. Gay.
|
||||
*
|
||||
* Copyright (c) 1991 by AT&T.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose without fee is hereby granted, provided that this entire notice
|
||||
* is included in all copies of any software which is or includes a copy
|
||||
* or modification of this software and in all copies of the supporting
|
||||
* documentation for such software.
|
||||
*
|
||||
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
|
||||
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
|
||||
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
|
||||
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
|
||||
*
|
||||
***************************************************************/
|
||||
|
||||
/* Please send bug reports to
|
||||
David M. Gay
|
||||
AT&T Bell Laboratories, Room 2C-463
|
||||
600 Mountain Avenue
|
||||
Murray Hill, NJ 07974-2070
|
||||
U.S.A.
|
||||
dmg@research.att.com or research!dmg
|
||||
*/
|
||||
|
||||
/* This header file is a modification of mprec.h that only contains floating
|
||||
point union code. */
|
||||
|
||||
#include <newlib.h>
|
||||
#include <ieeefp.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <errno.h>
|
||||
#include <sys/config.h>
|
||||
|
||||
#ifdef __IEEE_LITTLE_ENDIAN
|
||||
#define IEEE_8087
|
||||
#endif
|
||||
|
||||
#ifdef __IEEE_BIG_ENDIAN
|
||||
#define IEEE_MC68k
|
||||
#endif
|
||||
|
||||
#ifdef __Z8000__
|
||||
#define Just_16
|
||||
#endif
|
||||
|
||||
#ifdef Unsigned_Shifts
|
||||
#define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000;
|
||||
#else
|
||||
#define Sign_Extend(a,b) /*no-op*/
|
||||
#endif
|
||||
|
||||
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
|
||||
Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
|
||||
#endif
|
||||
|
||||
#ifdef _WANT_IO_LONG_DOUBLE
|
||||
/* If we are going to examine or modify specific bits in a long double using
|
||||
the lword0 or lwordx macros, then we must wrap the long double inside
|
||||
a union. This is necessary to avoid undefined behavior according to
|
||||
the ANSI C spec. */
|
||||
|
||||
#ifdef IEEE_8087
|
||||
#if LDBL_MANT_DIG == 24
|
||||
struct ldieee
|
||||
{
|
||||
unsigned manh:23;
|
||||
unsigned exp:8;
|
||||
unsigned sign:1;
|
||||
};
|
||||
#elif LDBL_MANT_DIG == 53
|
||||
struct ldieee
|
||||
{
|
||||
unsigned manl:20;
|
||||
unsigned manh:32;
|
||||
unsigned exp:11;
|
||||
unsigned sign:1;
|
||||
};
|
||||
#elif LDBL_MANT_DIG == 64
|
||||
struct ldieee
|
||||
{
|
||||
unsigned manl:32;
|
||||
unsigned manh:32;
|
||||
unsigned exp:15;
|
||||
unsigned sign:1;
|
||||
};
|
||||
#elif LDBL_MANT_DIG > 64
|
||||
struct ldieee
|
||||
{
|
||||
unsigned manl3:16;
|
||||
unsigned manl2:32;
|
||||
unsigned manl:32;
|
||||
unsigned manh:32;
|
||||
unsigned exp:15;
|
||||
unsigned sign:1;
|
||||
};
|
||||
#endif /* LDBL_MANT_DIG */
|
||||
#else /* !IEEE_8087 */
|
||||
#if LDBL_MANT_DIG == 24
|
||||
struct ldieee
|
||||
{
|
||||
unsigned sign:1;
|
||||
unsigned exp:8;
|
||||
unsigned manh:23;
|
||||
};
|
||||
#elif LDBL_MANT_DIG == 53
|
||||
struct ldieee
|
||||
{
|
||||
unsigned sign:1;
|
||||
unsigned exp:11;
|
||||
unsigned manh:32;
|
||||
unsigned manl:20;
|
||||
};
|
||||
#elif LDBL_MANT_DIG == 64
|
||||
struct ldieee
|
||||
{
|
||||
unsigned sign:1;
|
||||
unsigned exp:15;
|
||||
unsigned manh:32;
|
||||
unsigned manl:32;
|
||||
};
|
||||
#elif LDBL_MANT_DIG > 64
|
||||
struct ldieee
|
||||
{
|
||||
unsigned sign:1;
|
||||
unsigned exp:15;
|
||||
unsigned manh:32;
|
||||
unsigned manl:32;
|
||||
unsigned manl2:32;
|
||||
unsigned manl3;16;
|
||||
};
|
||||
#endif /* LDBL_MANT_DIG */
|
||||
#endif /* !IEEE_8087 */
|
||||
#endif /* _WANT_IO_LONG_DOUBLE */
|
||||
|
||||
/* If we are going to examine or modify specific bits in a double using
|
||||
the word0 and/or word1 macros, then we must wrap the double inside
|
||||
a union. This is necessary to avoid undefined behavior according to
|
||||
the ANSI C spec. */
|
||||
union double_union
|
||||
{
|
||||
double d;
|
||||
__uint32_t i[2];
|
||||
};
|
||||
|
||||
#ifdef IEEE_8087
|
||||
#define word0(x) (x.i[1])
|
||||
#define word1(x) (x.i[0])
|
||||
#else
|
||||
#define word0(x) (x.i[0])
|
||||
#define word1(x) (x.i[1])
|
||||
#endif
|
||||
|
||||
/* #define P DBL_MANT_DIG */
|
||||
/* Ten_pmax = floor(P*log(2)/log(5)) */
|
||||
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
|
||||
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
|
||||
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
|
||||
|
||||
#if defined(IEEE_8087) + defined(IEEE_MC68k)
|
||||
#if defined (_DOUBLE_IS_32BITS)
|
||||
#define Exp_shift 23
|
||||
#define Exp_shift1 23
|
||||
#define Exp_msk1 ((__uint32_t)0x00800000L)
|
||||
#define Exp_msk11 ((__uint32_t)0x00800000L)
|
||||
#define Exp_mask ((__uint32_t)0x7f800000L)
|
||||
#define P 24
|
||||
#define Bias 127
|
||||
#define IEEE_Arith
|
||||
#define Emin (-126)
|
||||
#define Exp_1 ((__uint32_t)0x3f800000L)
|
||||
#define Exp_11 ((__uint32_t)0x3f800000L)
|
||||
#define Ebits 8
|
||||
#define Frac_mask ((__uint32_t)0x007fffffL)
|
||||
#define Frac_mask1 ((__uint32_t)0x007fffffL)
|
||||
#define Ten_pmax 10
|
||||
#define Sign_bit ((__uint32_t)0x80000000L)
|
||||
#define Ten_pmax 10
|
||||
#define Bletch 2
|
||||
#define Bndry_mask ((__uint32_t)0x007fffffL)
|
||||
#define Bndry_mask1 ((__uint32_t)0x007fffffL)
|
||||
#define LSB 1
|
||||
#define Sign_bit ((__uint32_t)0x80000000L)
|
||||
#define Log2P 1
|
||||
#define Tiny0 0
|
||||
#define Tiny1 1
|
||||
#define Quick_max 5
|
||||
#define Int_max 6
|
||||
#define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L))
|
||||
#undef word0
|
||||
#undef word1
|
||||
|
||||
#define word0(x) (x.i[0])
|
||||
#define word1(x) 0
|
||||
#else
|
||||
|
||||
#define Exp_shift 20
|
||||
#define Exp_shift1 20
|
||||
#define Exp_msk1 ((__uint32_t)0x100000L)
|
||||
#define Exp_msk11 ((__uint32_t)0x100000L)
|
||||
#define Exp_mask ((__uint32_t)0x7ff00000L)
|
||||
#define P 53
|
||||
#define Bias 1023
|
||||
#define IEEE_Arith
|
||||
#define Emin (-1022)
|
||||
#define Exp_1 ((__uint32_t)0x3ff00000L)
|
||||
#define Exp_11 ((__uint32_t)0x3ff00000L)
|
||||
#define Ebits 11
|
||||
#define Frac_mask ((__uint32_t)0xfffffL)
|
||||
#define Frac_mask1 ((__uint32_t)0xfffffL)
|
||||
#define Ten_pmax 22
|
||||
#define Bletch 0x10
|
||||
#define Bndry_mask ((__uint32_t)0xfffffL)
|
||||
#define Bndry_mask1 ((__uint32_t)0xfffffL)
|
||||
#define LSB 1
|
||||
#define Sign_bit ((__uint32_t)0x80000000L)
|
||||
#define Log2P 1
|
||||
#define Tiny0 0
|
||||
#define Tiny1 1
|
||||
#define Quick_max 14
|
||||
#define Int_max 14
|
||||
#define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */
|
||||
#endif
|
||||
|
||||
#else
|
||||
#undef Sudden_Underflow
|
||||
#define Sudden_Underflow
|
||||
#ifdef IBM
|
||||
#define Exp_shift 24
|
||||
#define Exp_shift1 24
|
||||
#define Exp_msk1 ((__uint32_t)0x1000000L)
|
||||
#define Exp_msk11 ((__uint32_t)0x1000000L)
|
||||
#define Exp_mask ((__uint32_t)0x7f000000L)
|
||||
#define P 14
|
||||
#define Bias 65
|
||||
#define Exp_1 ((__uint32_t)0x41000000L)
|
||||
#define Exp_11 ((__uint32_t)0x41000000L)
|
||||
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
|
||||
#define Frac_mask ((__uint32_t)0xffffffL)
|
||||
#define Frac_mask1 ((__uint32_t)0xffffffL)
|
||||
#define Bletch 4
|
||||
#define Ten_pmax 22
|
||||
#define Bndry_mask ((__uint32_t)0xefffffL)
|
||||
#define Bndry_mask1 ((__uint32_t)0xffffffL)
|
||||
#define LSB 1
|
||||
#define Sign_bit ((__uint32_t)0x80000000L)
|
||||
#define Log2P 4
|
||||
#define Tiny0 ((__uint32_t)0x100000L)
|
||||
#define Tiny1 0
|
||||
#define Quick_max 14
|
||||
#define Int_max 15
|
||||
#else /* VAX */
|
||||
#define Exp_shift 23
|
||||
#define Exp_shift1 7
|
||||
#define Exp_msk1 0x80
|
||||
#define Exp_msk11 ((__uint32_t)0x800000L)
|
||||
#define Exp_mask ((__uint32_t)0x7f80L)
|
||||
#define P 56
|
||||
#define Bias 129
|
||||
#define Exp_1 ((__uint32_t)0x40800000L)
|
||||
#define Exp_11 ((__uint32_t)0x4080L)
|
||||
#define Ebits 8
|
||||
#define Frac_mask ((__uint32_t)0x7fffffL)
|
||||
#define Frac_mask1 ((__uint32_t)0xffff007fL)
|
||||
#define Ten_pmax 24
|
||||
#define Bletch 2
|
||||
#define Bndry_mask ((__uint32_t)0xffff007fL)
|
||||
#define Bndry_mask1 ((__uint32_t)0xffff007fL)
|
||||
#define LSB ((__uint32_t)0x10000L)
|
||||
#define Sign_bit ((__uint32_t)0x8000L)
|
||||
#define Log2P 1
|
||||
#define Tiny0 0x80
|
||||
#define Tiny1 0
|
||||
#define Quick_max 15
|
||||
#define Int_max 15
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
2190
contrib/sdk/sources/newlib/stdio/vfprintf.c
Normal file
2190
contrib/sdk/sources/newlib/stdio/vfprintf.c
Normal file
File diff suppressed because it is too large
Load Diff
1620
contrib/sdk/sources/newlib/stdio/vfscanf.c
Normal file
1620
contrib/sdk/sources/newlib/stdio/vfscanf.c
Normal file
File diff suppressed because it is too large
Load Diff
52
contrib/sdk/sources/newlib/stdio/vscanf.c
Normal file
52
contrib/sdk/sources/newlib/stdio/vscanf.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Code created by modifying scanf.c which has following copyright.
|
||||
*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vscanf, (fmt, ap),
|
||||
_CONST char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
_REENT_SMALL_CHECK_INIT (_REENT);
|
||||
return __svfscanf_r (_REENT, _stdin_r (_REENT), fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
_DEFUN(_vscanf_r, (ptr, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
_REENT_SMALL_CHECK_INIT (ptr);
|
||||
return __svfscanf_r (ptr, _stdin_r (ptr), fmt, ap);
|
||||
}
|
||||
|
72
contrib/sdk/sources/newlib/stdio/vsnprintf.c
Normal file
72
contrib/sdk/sources/newlib/stdio/vsnprintf.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in vfprintf.c */
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vsnprintf, (str, size, fmt, ap),
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vsnprintf_r (_REENT, str, size, fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
_DEFUN(_vsnprintf_r, (ptr, str, size, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
size_t size _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
|
||||
if (size > INT_MAX)
|
||||
{
|
||||
ptr->_errno = EOVERFLOW;
|
||||
return EOF;
|
||||
}
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = (size > 0 ? size - 1 : 0);
|
||||
f._file = -1; /* No file. */
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
if (ret < EOF)
|
||||
ptr->_errno = EOVERFLOW;
|
||||
if (size > 0)
|
||||
*f._p = 0;
|
||||
return ret;
|
||||
}
|
61
contrib/sdk/sources/newlib/stdio/vsprintf.c
Normal file
61
contrib/sdk/sources/newlib/stdio/vsprintf.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* doc in vfprintf.c */
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "local.h"
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vsprintf, (str, fmt, ap),
|
||||
char *str _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vsprintf_r (_REENT, str, fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
_DEFUN(_vsprintf_r, (ptr, str, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
char *str _AND
|
||||
const char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
f._file = -1; /* No file. */
|
||||
ret = _svfprintf_r (ptr, &f, fmt, ap);
|
||||
*f._p = 0;
|
||||
return ret;
|
||||
}
|
65
contrib/sdk/sources/newlib/stdio/vsscanf.c
Normal file
65
contrib/sdk/sources/newlib/stdio/vsscanf.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Code created by modifying scanf.c which has following copyright.
|
||||
*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <reent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef _HAVE_STDC
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* vsscanf
|
||||
*/
|
||||
|
||||
#ifndef _REENT_ONLY
|
||||
|
||||
int
|
||||
_DEFUN(vsscanf, (str, fmt, ap),
|
||||
_CONST char *str _AND
|
||||
_CONST char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
return _vsscanf_r (_REENT, str, fmt, ap);
|
||||
}
|
||||
|
||||
#endif /* !_REENT_ONLY */
|
||||
|
||||
int
|
||||
_DEFUN(_vsscanf_r, (ptr, str, fmt, ap),
|
||||
struct _reent *ptr _AND
|
||||
_CONST char *str _AND
|
||||
_CONST char *fmt _AND
|
||||
va_list ap)
|
||||
{
|
||||
FILE f;
|
||||
|
||||
f._flags = __SRD | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *) str;
|
||||
f._bf._size = f._r = strlen (str);
|
||||
f._read = __seofread;
|
||||
f._ub._base = NULL;
|
||||
f._lb._base = NULL;
|
||||
f._file = -1; /* No file. */
|
||||
return __ssvfscanf_r (ptr, &f, fmt, ap);
|
||||
}
|
96
contrib/sdk/sources/newlib/stdio/wbuf.c
Normal file
96
contrib/sdk/sources/newlib/stdio/wbuf.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "%W% (Berkeley) %G%";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* Write the given character into the (probably full) buffer for
|
||||
* the given file. Flush the buffer out if it is or becomes full,
|
||||
* or if c=='\n' and the file is line buffered.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__swbuf_r, (ptr, c, fp),
|
||||
struct _reent *ptr _AND
|
||||
register int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
register int n;
|
||||
|
||||
/* Ensure stdio has been initialized. */
|
||||
|
||||
CHECK_INIT (ptr, fp);
|
||||
|
||||
/*
|
||||
* In case we cannot write, or longjmp takes us out early,
|
||||
* make sure _w is 0 (if fully- or un-buffered) or -_bf._size
|
||||
* (if line buffered) so that we will get called again.
|
||||
* If we did not do this, a sufficient number of putc()
|
||||
* calls might wrap _w from negative to positive.
|
||||
*/
|
||||
|
||||
fp->_w = fp->_lbfsize;
|
||||
if (cantwrite (ptr, fp))
|
||||
return EOF;
|
||||
c = (unsigned char) c;
|
||||
|
||||
ORIENT (fp, -1);
|
||||
|
||||
/*
|
||||
* If it is completely full, flush it out. Then, in any case,
|
||||
* stuff c into the buffer. If this causes the buffer to fill
|
||||
* completely, or if c is '\n' and the file is line buffered,
|
||||
* flush it (perhaps a second time). The second flush will always
|
||||
* happen on unbuffered streams, where _bf._size==1; fflush()
|
||||
* guarantees that putc() will always call wbuf() by setting _w
|
||||
* to 0, so we need not do anything else.
|
||||
*/
|
||||
|
||||
n = fp->_p - fp->_bf._base;
|
||||
if (n >= fp->_bf._size)
|
||||
{
|
||||
if (_fflush_r (ptr, fp))
|
||||
return EOF;
|
||||
n = 0;
|
||||
}
|
||||
fp->_w--;
|
||||
*fp->_p++ = c;
|
||||
if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
|
||||
if (_fflush_r (ptr, fp))
|
||||
return EOF;
|
||||
return c;
|
||||
}
|
||||
|
||||
/* This function isn't any longer declared in stdio.h, but it's
|
||||
required for backward compatibility with applications built against
|
||||
earlier dynamically built newlib libraries. */
|
||||
int
|
||||
_DEFUN(__swbuf, (c, fp),
|
||||
register int c _AND
|
||||
register FILE *fp)
|
||||
{
|
||||
return __swbuf_r (_REENT, c, fp);
|
||||
}
|
94
contrib/sdk/sources/newlib/stdio/wsetup.c
Normal file
94
contrib/sdk/sources/newlib/stdio/wsetup.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/* No user fns here. Pesch 15apr92. */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1990, 2007 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are permitted
|
||||
* provided that the above copyright notice and this paragraph are
|
||||
* duplicated in all such forms and that any documentation,
|
||||
* advertising materials, and other materials related to such
|
||||
* distribution and use acknowledge that the software was developed
|
||||
* by the University of California, Berkeley. The name of the
|
||||
* University may not be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*/
|
||||
|
||||
#include <_ansi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Various output routines call wsetup to be sure it is safe to write,
|
||||
* because either _flags does not include __SWR, or _buf is NULL.
|
||||
* _wsetup returns 0 if OK to write, nonzero and set errno otherwise.
|
||||
*/
|
||||
|
||||
int
|
||||
_DEFUN(__swsetup_r, (ptr, fp),
|
||||
struct _reent *ptr _AND
|
||||
register FILE * fp)
|
||||
{
|
||||
/* Make sure stdio is set up. */
|
||||
|
||||
CHECK_INIT (_REENT, fp);
|
||||
|
||||
/*
|
||||
* If we are not writing, we had better be reading and writing.
|
||||
*/
|
||||
|
||||
if ((fp->_flags & __SWR) == 0)
|
||||
{
|
||||
if ((fp->_flags & __SRW) == 0)
|
||||
{
|
||||
ptr->_errno = EBADF;
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
||||
if (fp->_flags & __SRD)
|
||||
{
|
||||
/* clobber any ungetc data */
|
||||
if (HASUB (fp))
|
||||
FREEUB (ptr, fp);
|
||||
fp->_flags &= ~(__SRD | __SEOF);
|
||||
fp->_r = 0;
|
||||
fp->_p = fp->_bf._base;
|
||||
}
|
||||
fp->_flags |= __SWR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make a buffer if necessary, then set _w.
|
||||
* A string I/O file should not explicitly allocate a buffer
|
||||
* unless asprintf is being used.
|
||||
*/
|
||||
if (fp->_bf._base == NULL
|
||||
&& (!(fp->_flags & __SSTR) || (fp->_flags & __SMBF)))
|
||||
__smakebuf_r (ptr, fp);
|
||||
|
||||
if (fp->_flags & __SLBF)
|
||||
{
|
||||
/*
|
||||
* It is line buffered, so make _lbfsize be -_bufsize
|
||||
* for the putc() macro. We will change _lbfsize back
|
||||
* to 0 whenever we turn off __SWR.
|
||||
*/
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = -fp->_bf._size;
|
||||
}
|
||||
else
|
||||
fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size;
|
||||
|
||||
if (!fp->_bf._base && (fp->_flags & __SMBF))
|
||||
{
|
||||
/* __smakebuf_r set errno, but not flag */
|
||||
fp->_flags |= __SERR;
|
||||
return EOF;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user