summaryrefslogtreecommitdiff
path: root/src/stdio/_stdio.h
blob: 8cbf453c0f0b4d849d1da037a1c19f96442132f6 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef ___STDIO_H__
#define ___STDIO_H__

#include <inttypes.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>

#include "_safety.h"

#ifdef _POSIX_C_SOURCE
#include <sys/types.h>
#endif

#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506L
#define flockfile(_file)		(void)(_file)
#define funlockfile(_file)		(void)(_file)
#define putc_unlocked(_c, _stream)	fputc(_c, _stream)
#define getc_unlocked(_stream)		fgetc(_stream)
#endif

#define f_is_open(s) (s && (s->bmode != 0))

#define ORIENT_WIDE (1)
#define ORIENT_BYTE (-1)

#define OP_NONE   (0)
#define OP_INPUT  (1)
#define OP_OUTPUT (2)

#ifdef NDEBUG
#define ASSERT_STREAM(__stream, __orientation, __operation)
#else
#define ASSERT_STREAM(__stream, __orientation, __operation) do { \
		ASSERT_NONNULL(__stream); \
		if ((__stream)->fd == -1) { \
			UNDEFINED("In call to %s: Stream is not open (use after close?)", __func__); \
		} \
		if (((__operation) == OP_INPUT) && !(__stream)->read) { \
			UNDEFINED("In call to %s: Attempted input operation on output stream", __func__); \
		} \
		if (((__operation) == OP_OUTPUT) && !(__stream)->write) { \
			UNDEFINED("In call to %s: Attempted output operation on input stream", __func__); \
		} \
		if ((__orientation) && (__stream)->orientation && ((__orientation) != (__stream)->orientation)) { \
			UNDEFINED("In call to %s(): Requested %s operation on %s oriented stream", __func__, (__orientation) > 0 ? "wide" : "byte", ((__stream)->orientation) > 0 ? "wide" : "byte"); \
		} \
	} while (0)

#define ASSERT_FPOS(__stream, __pos) do { \
	if (!(__pos->__impl >= __stream->fpos && __pos->__impl <= __stream->fpos + __stream->nfpos)) { \
		UNDEFINED("In call to %s(): %s must be previously set by calling fesetpos()", __func__, #__pos); \
	} \
} while (0)

#endif

struct __FILE {
	fpos_t pos;

	char *buf;		/* pointer to in-use buffer */
	char ibuf[BUFSIZ];	/* statically allocated buffer */
	size_t bsize;		/* how big is the buffer */
	size_t bpos;		/* current position of next character */
	enum {
		UNUSED = 0,
		FULL = _IOFBF,
		LINE = _IOLBF,
		NONE = _IONBF,
	} bmode;		/* buffer mode or stream available */

	int fd;			/* the backing file descriptor */

	int nlocks;		/* in multithreaded, used by flockfile() */
	int thread;		/* the owning thread if locked */

	int orientation:2;	/* 0 = undetermind, < 0 = byte, > 0 = wide */
	unsigned int operation:2;	/* TODO: previous operation, NONE, INPUT, OUTPUT (are there others?) */
	unsigned int eof:1;		/* eof indicator */
	unsigned int err:1;		/* error indicator */
	unsigned int text:1;		/* is this a text file? */
	unsigned int read:1;		/* open for reading? */
	unsigned int write:1;		/* open for writing? */

	#ifdef _POSIX_C_SOURCE
	pid_t pipe_pid;		/* if stream is a pipe, the child pid */
	#endif

	struct __fpos_t *fpos;
	size_t nfpos;
	long int *valid_ftell;
	size_t nvalid_ftell;
};

struct __fpos_t {
	size_t pos;
};

extern struct __stdio_h {
	struct __FILE FILES[FOPEN_MAX];
	char **formatted_pointers;
	size_t nformatted_pointers;
} __stdio_h;

/*
STDC(-1)
*/

#endif