mailing list of musl libc
 help / color / mirror / code / Atom feed
23c4ffdac7e474a3984786429d8ae7111d3790d9 blob 1722 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
#include "stdio_impl.h"
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "libc.h"

FILE *__fdopen(int fd, const char *mode)
{
	FILE *f;
	struct winsize wsz;

	/* Check for valid initial mode character */
	if (!strchr("rwa", *mode)) {
		errno = EINVAL;
		return 0;
	}

	/* Allocate FILE+buffer or fail */
	if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0;

	/* Zero-fill only the struct, not the buffer */
	memset(f, 0, sizeof *f);

	/* Impose mode restrictions */
	if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD;

	int fd_flag = __syscall(SYS_fcntl, fd, F_GETFL);

	if (fd_flag == -1) return 0;

	if (((fd_flag & O_ACCMODE) == O_RDONLY && !(f->flags & F_NORD)) ||
	    ((fd_flag & O_ACCMODE) == O_WRONLY && !(f->flags & F_NOWR))) {
		errno = EINVAL;
		return 0;
	}

	/* Apply close-on-exec flag */
	if (strchr(mode, 'e')) __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);

	/* Set append mode on fd if opened for append */
	if (*mode == 'a') {
		int flags = __syscall(SYS_fcntl, fd, F_GETFL);
		if (!(flags & O_APPEND))
			__syscall(SYS_fcntl, fd, F_SETFL, flags | O_APPEND);
		f->flags |= F_APP;
	}

	f->fd = fd;
	f->buf = (unsigned char *)f + sizeof *f + UNGET;
	f->buf_size = BUFSIZ;

	/* Activate line buffered mode for terminals */
	f->lbf = EOF;
	if (!(f->flags & F_NOWR) && !__syscall(SYS_ioctl, fd, TIOCGWINSZ, &wsz))
		f->lbf = '\n';

	/* Initialize op ptrs. No problem if some are unneeded. */
	f->read = __stdio_read;
	f->write = __stdio_write;
	f->seek = __stdio_seek;
	f->close = __stdio_close;

	if (!libc.threaded) f->lock = -1;

	/* Add new FILE to open file list */
	return __ofl_add(f);
}

weak_alias(__fdopen, fdopen);
debug log:

solving 23c4ffd ...
found 23c4ffd in https://inbox.vuxu.org/musl/20200219023729.37349-1-zhangtianci1@huawei.com/
found 116e78e in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 116e78e56d2a1b3907c66a643d932b1b3a5c8545	src/stdio/__fdopen.c

applying [1/1] https://inbox.vuxu.org/musl/20200219023729.37349-1-zhangtianci1@huawei.com/
diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
index 116e78e..23c4ffd 100644

Checking patch src/stdio/__fdopen.c...
Applied patch src/stdio/__fdopen.c cleanly.

index at:
100644 23c4ffdac7e474a3984786429d8ae7111d3790d9	src/stdio/__fdopen.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).