History log of /freebsd-current/lib/libc/stdio/freopen.c
Revision Date Author Comments
# dc36d6f9 23-Nov-2023 Warner Losh <imp@FreeBSD.org>

lib: Remove ancient SCCS tags.

Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by: Netflix


# 559a218c 01-Nov-2023 Warner Losh <imp@FreeBSD.org>

libc: Purge unneeded cdefs.h

These sys/cdefs.h are not needed. Purge them. They are mostly left-over
from the $FreeBSD$ removal. A few in libc are still required for macros
that cdefs.h defines. Keep those.

Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D42385


# 1d386b48 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: one-line .c pattern

Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/


# 8a16b7a1 20-Nov-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

General further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.


# fda0a14f 29-Jun-2017 Konstantin Belousov <kib@FreeBSD.org>

Make stdio deferred cancel-safe.

If used with fopen(3)/fdopen(3)-ed FILEs, stdio accurately uses
non-cancellable internal versions of the functions, i.e. it seems to
be fine with regard to cancellation. But if the funopen(3) and
f{r,w}open(3) functions were used to open the FILE, and corresponding
user functions create cancellation points (they typically have no
other choice), then stdio code at least leaks FILE' lock.

The change installs cleanup handler which unlocks FILE. Some minimal
restructuring of the code was required to make it use common return
place to satisfy hand-rolled pthread_cleanup_pop() requirements.

Noted by: eugen
Reviewed by: eugen, vangyzen
Tested by: pho
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
Differential revision: https://reviews.freebsd.org/D11246


# cc4cacc7 30-Mar-2016 Pedro F. Giffuni <pfg@FreeBSD.org>

freopen(3): prevent uninitialized errno.

Revert completley r297408 (and r297407): this ends up touching errno,
which we are not allowed to do.

Pointed out by: ache, bde


# 6a2a9844 30-Mar-2016 Pedro F. Giffuni <pfg@FreeBSD.org>

freopen(3): prevent uninitialized errno.

Revert r297407 and redo it cleanly.

Pointed out by: Jukka A. Ukkonen
CID: 1018720
MFC after: 1 week


# 36226c47 29-Mar-2016 Pedro F. Giffuni <pfg@FreeBSD.org>

freopen(3): prevent uninitialized errno.

The case doesn't look very likely but clean the possibility nevertheless

CID: 1018720
MFC after: 1 week


# 8b63538d 28-Oct-2015 Andrey A. Chernov <ache@FreeBSD.org>

Add _flags2 per jhb@ suggestion since no room left in _flags.
Rewrite O_APPEND flag checking using new __S2OAP flag.

MFC after: 3 weeks


# b956b176 23-Oct-2015 Andrey A. Chernov <ache@FreeBSD.org>

Since no room left in the _flags, reuse __SALC for O_APPEND.
It helps to remove _fcntl() call from _ftello() and optimize seek position
calculation in _swrite().

MFC after: 3 weeks


# 7922e2bb 26-Jul-2014 Andrey A. Chernov <ache@FreeBSD.org>

In the "Too many open files" edge cases don't try to preserve old
number for non-std* descriptors, but close old file and retry.

Obtained from: inspired by Apple's change from pfg@
MFC after: 2 weeks


# 120d6dd5 20-Jul-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

Use a correct errno in freopen.

Use EBADF instead of EINVAL when working around incorrect O_ACCMODE.

Phabric: D442
Obtained from: Apple Inc. (Libc 997.90.3)
Reviewed by: jilles
MFC after: 1 week


# 93a65e1b 21-Apr-2014 Jilles Tjoelker <jilles@FreeBSD.org>

libc/stdio: Fail fdopen() on an execute-only fd.

An execute-only fd (opened with O_EXEC) allows neither read() nor write()
and is therefore incompatible with all stdio modes. Therefore, the [EINVAL]
error applies.

Also adjust the similar check in freopen() with a NULL path, even though
this checks an fd which is already from a FILE.


# 5a6307cf 23-Apr-2013 Ed Maste <emaste@FreeBSD.org>

Convert libc/stdio from K&R to ANSI C

And add '__restrict' where it appeared in the header prototypes


# 1d8053c5 23-Apr-2013 Ed Maste <emaste@FreeBSD.org>

Renumber clauses to reduce diffs to other versions

NetBSD, OpenBSD, and Android's Bionic all number the clauses 1 through 3,
so follow suit to make comparison easier.

Acked-by: imp@


# 5ba651f0 30-Nov-2012 Jilles Tjoelker <jilles@FreeBSD.org>

libc: Allow setting close-on-exec in fopen/freopen/fdopen.

This commit adds a new mode option 'e' that must follow any 'b', '+' and/or
'x' options. C11 is clear about the 'x' needing to follow 'b' and/or '+' and
that is what we implement; therefore, require a strict position for 'e' as
well.

For freopen() with a non-NULL path argument and fopen(), the close-on-exec
flag is set iff the 'e' mode option is specified. For freopen() with a NULL
path argument and fdopen(), the close-on-exec flag is turned on if the 'e'
mode option is specified and remains unchanged otherwise.

Although the same behaviour for fopen() can be obtained by open(O_CLOEXEC)
and fdopen(), this needlessly complicates the calling code.

Apart from the ordering requirement, the new option matches glibc.

PR: kern/169320


# cc3d8572 09-Dec-2010 John Baldwin <jhb@FreeBSD.org>

When reopening a stream backed by an open file descriptor, do not close
the existing file descriptor. Instead, let dup2() atomically close the
old file descriptor when assigning the newly opened file to the same
descriptor. This closes a race in a multithreaded application where a
concurrent open() could allocate the existing file descriptor in between
the calls to close() and dup2().

PR: threads/79887
Submitted by: Dmitrij Tejblum tejblum of yandex-team.ru
Reviewed by: davidxu
MFC after: 1 week


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# fe0506d7 09-Mar-2010 Marcel Moolenaar <marcel@FreeBSD.org>

Create the altix project branch. The altix project will add support
for the SGI Altix 350 to FreeBSD/ia64. The hardware used for porting
is a two-module system, consisting of a base compute module and a
CPU expansion module. SGI's NUMAFlex architecture can be an excellent
platform to test CPU affinity and NUMA-aware features in FreeBSD.


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 1e98f887 17-Apr-2008 John Baldwin <jhb@FreeBSD.org>

Next stage of stdio cleanup: Retire __sFILEX and merge the fields back into
__sFILE. This was supposed to be done in 6.0. Some notes:
- Where possible I restored the various lines to their pre-__sFILEX state.
- Retire INITEXTRA() and just initialize the wchar bits (orientation and
mbstate) explicitly instead. The various places that used INITEXTRA
didn't need the locking fields or _up initialized. (Some places needed
_up to exist and not be off the end of a NULL or garbage pointer, but
they didn't require it to be initialized to a specific value.)
- For now, stdio.h "knows" that pthread_t is a 'struct pthread *' to
avoid namespace pollution of including all the pthread types in stdio.h.
Once we remove all the inlines and make __sFILE private it can go back
to using pthread_t, etc.
- This does not remove any of the inlines currently and does not change
any of the public ABI of 'FILE'.

MFC after: 1 month
Reviewed by: peter


# fc9ab4f6 27-Feb-2008 John Baldwin <jhb@FreeBSD.org>

Add <limits.h> for SHRT_MAX.

Pointy hat to: jhb


# c55d7e86 27-Feb-2008 John Baldwin <jhb@FreeBSD.org>

File descriptors are an int, but our stdio FILE object uses a short to hold
them. Thus, any fd whose value is greater than SHRT_MAX is handled
incorrectly (the short value is sign-extended when converted to an int).
An unpleasant side effect is that if fopen() opens a file and gets a
backing fd that is greater than SHRT_MAX, fclose() will fail and the file
descriptor will be leaked. Better handle this by fixing fopen(), fdopen(),
and freopen() to fail attempts to use a fd greater than SHRT_MAX with
EMFILE.

At some point in the future we should look at expanding the file descriptor
in FILE to an int, but that is a bit complicated due to ABI issues.

MFC after: 1 week
Discussed on: arch
Reviewed by: wollman


# c879ae35 08-Jan-2007 Warner Losh <imp@FreeBSD.org>

Per Regents of the University of Calfornia letter, remove advertising
clause.

# If I've done so improperly on a file, please let me know.


# b0bf75c8 16-Oct-2006 Andrey A. Chernov <ache@FreeBSD.org>

file == NULL:
Issue __sflush() before possible setting O_APPEND mode or ftruncate(),
write to wrong place may occurse oserwise.
Use simplified _sseek() to the start, if no O_APPEND is set, instead
of _fseeko() (_sseek() to the end, if O_APPEND, occurse later, as for
file != NULL).
Don't check seek error return, as original fopen() and freopen() never
does.

file != NULL:
Add missing _sseek() to the end.


# 5a8d08f5 16-Oct-2006 Andrey A. Chernov <ache@FreeBSD.org>

Honor errno obtained from __sflags
Move errno restoring after FUNLOCKFILE in one case


# 6f8d08b3 16-Oct-2006 Andrey A. Chernov <ache@FreeBSD.org>

Back out ftruncate error handling. There can some file tipes
f.e. sockets when ftruncate normally fail.


# b48be86c 15-Oct-2006 Andrey A. Chernov <ache@FreeBSD.org>

Do real seek
Catch ftruncate errors

PR: 104295
Submitted by: ru (seek)


# 87275e43 22-May-2004 Tim J. Robbins <tjr@FreeBSD.org>

Associate a multibyte conversion state object with each stream. Reset it
to the initial state when a stream is opened or seeked upon. Use the
stream's conversion state object instead of a freshly-zeroed one in
fgetwc(), fputwc() and ungetwc().

This is only a performance improvement for now, but it would also be
required in order to support state-dependent encodings.


# 21a1863d 26-Jan-2003 Tim J. Robbins <tjr@FreeBSD.org>

Remove part of my stateful locale patch that slipped into the previous rev.


# c2974987 26-Jan-2003 Tim J. Robbins <tjr@FreeBSD.org>

Initial implementation of the C99 feature whereby calling freopen() with
a NULL filename argument allows a stream's mode to be changed. At the
moment it just recycles the old file descriptor instead of storing the
filename somewhere and using that to reopen the file, as the standard
seems to require. Strictly conforming C99 applications probably can't
tell the difference but POSIX ones can.

PR: 46791


# 276ba5b4 02-Jan-2003 Tim J. Robbins <tjr@FreeBSD.org>

Reset the stream orientation to 0 (unoriented) in freopen(), as required
by the C standard.


# 54e4e385 12-Oct-2002 Mike Barcroft <mike@FreeBSD.org>

Add restrict type-qualifier.


# 333fc21e 22-Mar-2002 David E. O'Brien <obrien@FreeBSD.org>

Fix the style of the SCM ID's.
I believe have made all of libc .c's as consistent as possible.


# 29ac6bd2 11-Feb-2001 Daniel Eischen <deischen@FreeBSD.org>

libc MT-safety, part 2.

Add a lock to FILE. flockfile and friends are now implemented
(for the most part) in libc. flockfile_debug is implemented in
libc_r; I suppose it's about time to kill it but will do it in
a future commit.

Fix a potential deadlock in _fwalk in a threaded environment.
A file flag (__SIGN) was added to stdio.h that, when set, tells
_fwalk to ignore it in its walk. This seemed to be needed in
refill.c because each file needs to be locked when flushing.

Add a stub for pthread_self in libc. This is needed by flockfile
which is allowed by POSIX to be recursive.

Make fgetpos() error return value (-1) match man page.

Remove recursive calls to locked functions (stdio); I think I've
got them all, but I may have missed a couple.

A few K&R -> ANSI conversions along with removal of a few instances
of "register".

$Id$ -> $FreeBSD$ in libc/stdio/rget.c

Not objected to: -arch, a few months ago


# d201fe46 24-Jan-2001 Daniel Eischen <deischen@FreeBSD.org>

Remove _THREAD_SAFE and make libc thread-safe by default by
adding (weak definitions to) stubs for some of the pthread
functions. If the threads library is linked in, the real
pthread functions will pulled in.

Use the following convention for system calls wrapped by the
threads library:
__sys_foo - actual system call
_foo - weak definition to __sys_foo
foo - weak definition to __sys_foo

Change all libc uses of system calls wrapped by the threads
library from foo to _foo. In order to define the prototypes
for _foo(), we introduce namespace.h and un-namespace.h
(suggested by bde). All files that need to reference these
system calls, should include namespace.h before any standard
includes, then include un-namespace.h after the standard
includes and before any local includes. <db.h> is an exception
and shouldn't be included in between namespace.h and
un-namespace.h namespace.h will define foo to _foo, and
un-namespace.h will undefine foo.

Try to eliminate some of the recursive calls to MT-safe
functions in libc/stdio in preparation for adding a mutex
to FILE. We have recursive mutexes, but would like to avoid
using them if possible.

Remove uneeded includes of <errno.h> from a few files.

Add $FreeBSD$ to a few files in order to pass commitprep.

Approved by: -arch


# 9233c4d9 27-Jan-2000 Jason Evans <jasone@FreeBSD.org>

Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo(). In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate. In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().

Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().

Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().

Make thread cancellation fully POSIX-compliant.

Suggested by: deischen


# 92927338 12-Jan-2000 Jason Evans <jasone@FreeBSD.org>

Add three-tier symbol naming in support of POSIX thread cancellation
points. For library functions, the pattern is __sleep() <--
_libc_sleep() <-- sleep(). The arrows represent weak aliases. For
system calls, the pattern is _read() <-- _libc_read() <-- read().


# 32fc781e 20-Nov-1999 Dmitrij Tejblum <dt@FreeBSD.org>

Add (FILE *) locking.


# 6c06b4e2 29-May-1995 Rodney W. Grimes <rgrimes@FreeBSD.org>

Remove trailing whitespace.


# 58f0484f 26-May-1994 Rodney W. Grimes <rgrimes@FreeBSD.org>

BSD 4.4 Lite Lib Sources