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

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

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 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
OLDNEW
1 #include "stdio_impl.h" 1 #include "stdio_impl.h"
2 #include <fcntl.h> 2 #include <fcntl.h>
3 3
4 /* The basic idea of this implementation is to open a new FILE, 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 5 * hack the necessary parts of the new FILE into the old one, then
6 * close the new FILE. */ 6 * close the new FILE. */
7 7
8 /* Locking IS necessary because another thread may provably hold the 8 /* Locking IS necessary because another thread may provably hold the
9 * lock, via flockfile or otherwise, when freopen is called, and in that 9 * lock, via flockfile or otherwise, when freopen is called, and in that
10 * case, freopen cannot act until the lock is released. */ 10 * case, freopen cannot act until the lock is released. */
11 11
12 int __dup3(int, int, int); 12 int __dup3(int, int, int);
13 13
14 FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *re strict f) 14 FILE* freopen(const char* restrict filename,
15 { 15 const char* restrict mode,
16 » int fl = __fmodeflags(mode); 16 FILE* restrict f) {
17 » FILE *f2; 17 int fl = __fmodeflags(mode);
18 FILE* f2;
18 19
19 » FLOCK(f); 20 FLOCK(f);
20 21
21 » fflush(f); 22 fflush(f);
22 23
23 » if (!filename) { 24 if (!filename) {
24 » » if (fl&O_CLOEXEC) 25 if (fl & O_CLOEXEC)
25 » » » __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC); 26 __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC);
26 » » fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC); 27 fl &= ~(O_CREAT | O_EXCL | O_CLOEXEC);
27 » » if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0) 28 if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0)
28 » » » goto fail; 29 goto fail;
29 » } else { 30 } else {
30 » » f2 = fopen(filename, mode); 31 f2 = fopen(filename, mode);
31 » » if (!f2) goto fail; 32 if (!f2)
32 » » if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */ 33 goto fail;
33 » » else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2; 34 if (f2->fd == f->fd)
35 f2->fd = -1; /* avoid closing in fclose */
36 else if (__dup3(f2->fd, f->fd, fl & O_CLOEXEC) < 0)
37 goto fail2;
34 38
35 » » f->flags = (f->flags & F_PERM) | f2->flags; 39 f->flags = (f->flags & F_PERM) | f2->flags;
36 » » f->read = f2->read; 40 f->read = f2->read;
37 » » f->write = f2->write; 41 f->write = f2->write;
38 » » f->seek = f2->seek; 42 f->seek = f2->seek;
39 » » f->close = f2->close; 43 f->close = f2->close;
40 44
41 » » fclose(f2); 45 fclose(f2);
42 » } 46 }
43 47
44 » FUNLOCK(f); 48 FUNLOCK(f);
45 » return f; 49 return f;
46 50
47 fail2: 51 fail2:
48 » fclose(f2); 52 fclose(f2);
49 fail: 53 fail:
50 » fclose(f); 54 fclose(f);
51 » return NULL; 55 return NULL;
52 } 56 }
53 57
54 LFS64(freopen); 58 LFS64(freopen);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698