/programs/develop/libraries/newlib/stdio/fdopen.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/fgets.c |
---|
0,0 → 1,193 |
/* |
* 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); |
__sfp_lock_acquire (); |
_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); |
__sfp_lock_release (); |
return NULL; |
} |
*s = 0; |
_funlockfile (fp); |
__sfp_lock_release (); |
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); |
__sfp_lock_release (); |
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); |
__sfp_lock_release (); |
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); |
__sfp_lock_release (); |
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 */ |
/programs/develop/libraries/newlib/stdio/fiscanf.c |
---|
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); |
} |
/programs/develop/libraries/newlib/stdio/fread.c |
---|
0,0 → 1,265 |
/* |
* 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); |
__sfp_lock_acquire (); |
_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); |
__sfp_lock_release (); |
return crlf_r (ptr, fp, buf, total-resid, 1) / size; |
} |
#endif |
_funlockfile (fp); |
__sfp_lock_release (); |
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); |
__sfp_lock_release (); |
return crlf_r (ptr, fp, buf, total-resid, 1) / size; |
} |
#endif |
_funlockfile (fp); |
__sfp_lock_release (); |
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); |
__sfp_lock_release (); |
return crlf_r(ptr, fp, buf, total, 0) / size; |
} |
#endif |
_funlockfile (fp); |
__sfp_lock_release (); |
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 |
/programs/develop/libraries/newlib/stdio/freopen.c |
---|
0,0 → 1,250 |
/* |
* 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; |
__sfp_lock_acquire (); |
CHECK_INIT (ptr, fp); |
_flockfile (fp); |
if ((flags = __sflags (ptr, mode, &oflags)) == 0) |
{ |
_funlockfile (fp); |
_fclose_r (ptr, fp); |
__sfp_lock_release (); |
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 */ |
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); |
__sfp_lock_release (); |
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 */ |
/programs/develop/libraries/newlib/stdio/fscanf.c |
---|
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 = __svfscanf_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 = __svfscanf_r (ptr, fp, fmt, ap); |
va_end (ap); |
return (ret); |
} |
/programs/develop/libraries/newlib/stdio/fseeko.c |
---|
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 */ |
/programs/develop/libraries/newlib/stdio/ftell.c |
---|
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 */ |
/programs/develop/libraries/newlib/stdio/ftello.c |
---|
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 */ |
/programs/develop/libraries/newlib/stdio/putchar.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/remove.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/rename.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/rget.c |
---|
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); |
} |
/programs/develop/libraries/newlib/stdio/setvbuf.c |
---|
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; |
} |
/programs/develop/libraries/newlib/stdio/tmpfile.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/tmpnam.c |
---|
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 |
/programs/develop/libraries/newlib/stdio/vscanf.c |
---|
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); |
} |
/programs/develop/libraries/newlib/stdio/vsscanf.c |
---|
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); |
} |