Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: fusl/src/stdio/freopen.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/stdio/fread.c ('k') | fusl/src/stdio/fscanf.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "stdio_impl.h"
2 #include <fcntl.h>
3
4 /* The basic idea of this implementation is to open a new FILE,
5 * hack the necessary parts of the new FILE into the old one, then
6 * close the new FILE. */
7
8 /* Locking IS necessary because another thread may provably hold the
9 * lock, via flockfile or otherwise, when freopen is called, and in that
10 * case, freopen cannot act until the lock is released. */
11
12 int __dup3(int, int, int);
13
14 FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *re strict f)
15 {
16 int fl = __fmodeflags(mode);
17 FILE *f2;
18
19 FLOCK(f);
20
21 fflush(f);
22
23 if (!filename) {
24 if (fl&O_CLOEXEC)
25 __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC);
26 fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
27 if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0)
28 goto fail;
29 } else {
30 f2 = fopen(filename, mode);
31 if (!f2) goto fail;
32 if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */
33 else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2;
34
35 f->flags = (f->flags & F_PERM) | f2->flags;
36 f->read = f2->read;
37 f->write = f2->write;
38 f->seek = f2->seek;
39 f->close = f2->close;
40
41 fclose(f2);
42 }
43
44 FUNLOCK(f);
45 return f;
46
47 fail2:
48 fclose(f2);
49 fail:
50 fclose(f);
51 return NULL;
52 }
53
54 LFS64(freopen);
OLDNEW
« no previous file with comments | « fusl/src/stdio/fread.c ('k') | fusl/src/stdio/fscanf.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698