summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/common/bits/uClibc_stdio.h
blob: 83ca2fb0404a0b32b9ff7e6f8e4984d17b1b616e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*  Copyright (C) 2002     Manuel Novoa III
 *  Header for my stdio library for linux and (soon) elks.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!
 *
 *  This code is currently under development.  Also, I plan to port
 *  it to elks which is a 16-bit environment with a fairly limited
 *  compiler.  Therefore, please refrain from modifying this code
 *  and, instead, pass any bug-fixes, etc. to me.  Thanks.  Manuel
 *
 *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */

#ifndef _STDIO_H
#error Always include <stdio.h> rather than <bits/uClibc_stdio.h>
#endif

/**********************************************************************/
#ifdef __UCLIBC__

#ifdef __UCLIBC_HAS_THREADS__
#define __STDIO_THREADSAFE
#endif

#ifdef __UCLIBC_HAVE_LFS__
#define __STDIO_LARGE_FILES
#endif /* __UCLIBC_HAVE_LFS__ */

/* Make sure defines related to large files are consistent. */
#ifdef _LIBC

#ifdef __UCLIBC_HAVE_LFS__
#undef __USE_LARGEFILE
#undef __USE_LARGEFILE64
#undef __USE_FILE_OFFSET64
/* if we're actually building uClibc with large file support, only define... */
#define __USE_LARGEFILE64	1
#endif /* __UCLIBC_HAVE_LFS__ */

#else  /* not _LIBC */

#ifndef __UCLIBC_HAVE_LFS__
#if defined(__LARGEFILE64_SOURCE) || defined(__USE_LARGEFILE64) \
    || defined(__USE_FILE_OFFSET64)
#error Sorry... uClibc was built without large file support!
#endif
#endif /* __UCLIBC_HAVE_LFS__ */

#endif /* _LIBC */

#endif /* __UCLIBC__ */
/**********************************************************************/
/* These are the stdio configuration options.  Keep them here until
   uClibc's configuration process gets reworked. */

/*  #define __STDIO_WIDE */
#ifdef __STDIO_WIDE
typedef int __wchar_t;			/* TODO: temporary, as not currently uClibc */
#endif

#define __STDIO_BUFFERS
#define __STDIO_GETC_MACRO
#define __STDIO_PUTC_MACRO

/* For uClibc, these are currently handled above. */
/*  #define __STDIO_LARGE_FILES */
/*  #define __STDIO_THREADSAFE */

/* L mode extension for fopen. */
#define __STDIO_FOPEN_LARGEFILE_MODE

/* size of builtin buf -- only tested with 0 */
#define _STDIO_BUILTIN_BUF_SIZE		0

/* TODO - enable features based on __STDIO_GLIBC_FEATURES */

/*  #define __STDIO_GLIBC_FEATURES */
#define __STDIO_AUTO_RW_TRANSITION
#define __STDIO_FOPEN_EXCLUSIVE_MODE
#define __STDIO_PRINTF_M_SPEC
#define __STDIO_GLIBC_CUSTOM_STREAMS


/* ANSI/ISO mandate at least 256. */
#define _STDIO_BUFSIZ			256

/* Currently unimplemented/untested */
/* #define __STDIO_FLEXIBLE_SETVBUF */

/**********************************************************************/
/* TODO -- posix or gnu -- belongs in limits.h and >= 9 for sus */
/* NOTE: for us it is currently _always_ 9 */
/*#define NL_ARGMAX			9*/

/**********************************************************************/

/* These are consistency checks on the different options */

#ifndef __STDIO_BUFFERS
#undef __STDIO_GETC_MACRO
#undef __STDIO_PUTC_MACRO
#endif

#ifdef __BCC__
#undef __STDIO_LARGE_FILES
#endif

#ifndef __STDIO_LARGE_FILES
#undef __STDIO_FOPEN_LARGEFILE_MODE
#endif

/**********************************************************************/

#ifdef __STDIO_THREADSAFE
/* Need this for pthread_mutex_t. */
#include <bits/pthreadtypes.h>

#define __STDIO_THREADLOCK(STREAM) \
	if ((STREAM)->user_locking == 0) { \
		pthread_mutex_lock(&(STREAM)->lock); \
	}

#define __STDIO_THREADUNLOCK(STREAM) \
	if ((STREAM)->user_locking == 0) { \
		pthread_mutex_unlock(&(STREAM)->lock); \
	}

#define __STDIO_THREADTRYLOCK(STREAM) \
	if ((STREAM)->user_locking == 0) { \
		pthread_mutex_trylock(&(STREAM)->lock); \
	}

#else  /* __STDIO_THREADSAFE */

#define __STDIO_THREADLOCK(STREAM)
#define __STDIO_THREADUNLOCK(STREAM)
#define __STDIO_THREADTRYLOCK(STREAM)

#endif /* __STDIO_THREADSAFE */

/* This file may eventually have two personalities:
   1) core stuff (similar to glibc's libio.h)
   2) extern inlines (for glibc's bits/stdio.h)
   Right now, only (1) is implemented. */

#define _STDIO_IOFBF 0	/* Fully buffered.  */
#define _STDIO_IOLBF 1	/* Line buffered.  */
#define _STDIO_IONBF 2	/* No buffering.  */

typedef struct {
	__off_t __pos;
/*    __mbstate_t __state; */
} __stdio_fpos_t;

typedef struct {
	__off64_t __pos;
/*    __mbstate_t __state; */
} __stdio_fpos64_t;


/**********************************************************************/
#ifdef __STDIO_LARGE_FILES
typedef __off64_t __offmax_t;	/* TODO -- rename this? */
#else
typedef __off_t __offmax_t;		/* TODO -- rename this? */
#endif

/**********************************************************************/

#ifdef __STDIO_GLIBC_CUSTOM_STREAMS

typedef __ssize_t __io_read_fn(void *cookie,
							   char *buf, size_t bufsize);
typedef __ssize_t __io_write_fn(void *cookie,
								const char *buf, size_t bufsize);
/* NOTE: GLIBC difference!!! -- fopencookie seek function
 * For glibc, the type of pos is always (__off64_t *) but in our case
 * it is type (__off_t *) when the lib is built without large file support.
 */
typedef int __io_seek_fn(void *cookie,
						 __offmax_t *pos, int whence);
typedef int __io_close_fn(void *cookie);

typedef struct {
	__io_read_fn *read;
	__io_write_fn *write;
	__io_seek_fn *seek;
	__io_close_fn *close;
} _IO_cookie_io_functions_t;

#if defined(_LIBC) || defined(_GNU_SOURCE)
typedef __io_read_fn cookie_read_function_t;
typedef __io_write_fn cookie_write_function_t;
typedef __io_seek_fn cookie_seek_function_t;
typedef __io_close_fn cookie_close_function_t;

typedef _IO_cookie_io_functions_t cookie_io_functions_t;
#endif /* _GNU_SOURCE */

#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */

/*
 * ungot scheme:
 * 0 0   none
 * 0 1   one user (unused ungot is 1) or one scanf (unused ungot is 0)
 * 1 0   must be scanf[0] and user[1]
 * 1 1   illegal -- could be used to signal safe for setbuf
 */

#ifdef __UCLIBC__
#define __stdio_file_struct _UC_FILE
#endif

struct __stdio_file_struct {
	unsigned short modeflags;
	/* There could be a hole here, but modeflags is used most.*/
#ifdef __STDIO_WIDE
	unsigned char ungot_width[2];
	__wchar_t ungot[2];
#else  /* __STDIO_WIDE */
	unsigned char ungot[2];
#endif /* __STDIO_WIDE */
	int filedes;
#if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
	struct __stdio_file_struct *nextopen;
#endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
#ifdef __STDIO_BUFFERS
	unsigned char *bufstart;	/* pointer to buffer */
	unsigned char *bufend;		/* pointer to 1 past end of buffer */
	unsigned char *bufwpos;		/* pointer to 1 past last buffered */
	unsigned char *bufrpos;		/* pointer to next readable buffered */
#ifdef __STDIO_GETC_MACRO
	unsigned char *bufgetc;		/* 1 past last readable by getc */
#endif /* __STDIO_GETC_MACRO */
#ifdef __STDIO_PUTC_MACRO
	unsigned char *bufputc;		/* 1 past last writeable by putc */
#endif /* __STDIO_PUTC_MACRO */
#endif /* __STDIO_BUFFERS */
#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
	void *cookie;
	_IO_cookie_io_functions_t gcs;
#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
#ifdef __STDIO_THREADSAFE
	int user_locking;
	pthread_mutex_t lock;
#endif
/* Everything after this is unimplemented... and may be trashed. */
#if __STDIO_BUILTIN_BUF_SIZE > 0
	unsigned char builtinbuf[__STDIO_BUILTIN_BUF_SIZE];
#endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
};


/***********************************************************************/

#define __MASK_UNGOT    	(0x0002|0x0001)
#define __MASK_UNGOT1    	0x0001
#define __MASK_UNGOT2    	0x0002
#define __FLAG_EOF			0x0004	/* EOF reached? */
#define __FLAG_ERROR		0x0008	/* stream in error state? */
#define __FLAG_WRITEONLY  	0x0010	/* unreadable */
#define __FLAG_READONLY  	0x0020	/* unwriteable */
#define __FLAG_FREEFILE		0x0040	/* free FILE struct after use */
#define __FLAG_NARROW       0x0080

#define __FLAG_FBF          0		/* convenience value */
#define __FLAG_LBF          0x0100
#define __FLAG_NBF          0x0200
#define __MASK_BUFMODE      0x0300

#define __FLAG_APPEND       0x0400
#define __FLAG_WIDE			0x0800

#define __FLAG_READING		0x1000
#define __FLAG_WRITING		0x2000

#define __FLAG_FREEBUF		0x4000	/* free buffer after use */
#define __FLAG_LARGEFILE    0x8000

/**********************************************************************/

#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
extern __ssize_t _cs_read(void *cookie, char *buf, size_t bufsize);
extern __ssize_t _cs_write(void *cookie, const char *buf, size_t bufsize);
extern int _cs_seek(void *cookie, __offmax_t *pos, int whence);
extern int _cs_close(void *cookie);
#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */

/**********************************************************************/

/* TODO -- thread safety issues */
#define __CLEARERR(stream) \
	((stream)->modeflags &= ~(__FLAG_EOF|__FLAG_ERROR), (void)0)
#define __FEOF(stream)		((stream)->modeflags & __FLAG_EOF)
#define __FERROR(stream)	((stream)->modeflags & __FLAG_ERROR)

#define __FEOF_OR_FERROR(stream) \
	((stream)->modeflags & (__FLAG_EOF|__FLAG_ERROR))


/* TODO: check this
 * If we want to implement the getc and putc macros, we need to take
 * into account wide streams.  So... would need two additional variables
 * if we have wide streams (bufread and bufwrite), and one otherwise
 * (bufwrite).  getc would be effective for FBF streams.  It isn't for
 * LBF streams because other LBF streams need to be flushed.  putc
 * thouch is only effective for FBF streams.  Of course, to support
 * threads, we have to use functions.
 */

#ifdef __STDIO_GETC_MACRO
#define __GETC(stream)		( ((stream)->bufrpos < (stream)->bufgetc) \
							? (*(stream)->bufrpos++) \
							: fgetc_unlocked(stream) )
#else  /* __STDIO_GETC_MACRO */
#define __GETC(stream)		fgetc_unlocked(stream)
#endif /* __STDIO_GETC_MACRO */

#ifdef __STDIO_PUTC_MACRO
#define __PUTC(c, stream)	( ((stream)->bufwpos < (stream)->bufputc) \
							? (*(stream)->bufwpos++) = (c) \
							: fputc_unlocked((c),(stream)) )
#else  /* __STDIO_PUTC_MACRO */
#define __PUTC(c, stream)	fputc_unlocked(c, stream);
#endif /* __STDIO_PUTC_MACRO */


#if 0
/* TODO: disabled for now */
/* Masking macros for the above _are_ allowed by the standard. */
#define clearerr(stream)	__CLEARERR(stream)
#define feof(stream)		__FEOF(stream)
#define ferror(stream)		__FERROR(stream)
#endif

#if 0
/* TODO -- what about custom streams!!! */
/* Only use the macro below if you know fp is a valid FILE for a valid fd. */
#define __fileno(fp)	((fp)->filedes)
#endif

/**********************************************************************
 * PROTOTYPES OF INTERNAL FUNCTIONS
 **********************************************************************/

extern FILE *_stdio_openlist;

#ifdef __STDIO_THREADSAFE
extern pthread_mutex_t _stdio_openlist_lock;
extern void __stdio_init_mutex(pthread_mutex_t *m);
#endif

extern int _stdio_adjpos(FILE * __restrict stream, __offmax_t * pos);
extern int _stdio_lseek(FILE *stream, __offmax_t *pos, int whence);
/* TODO: beware of signals with _stdio_fwrite!!!! */
extern size_t _stdio_fwrite(const unsigned char *buffer, size_t bytes,
							  FILE *stream);
extern size_t _stdio_fread(unsigned char *buffer, size_t bytes,
							 FILE *stream);

extern FILE *_stdio_fopen(const char * __restrict filename,
							const char * __restrict mode,
							FILE * __restrict stream, int filedes);

extern FILE *_stdio_fsfopen(const char * __restrict filename,
							const char * __restrict mode,
							register FILE * __restrict stream);

extern void _stdio_init(void);
extern void _stdio_term(void);

#ifndef NDEBUG
extern void __stdio_validate_FILE(FILE *stream);
#else
#define __stdio_validate_FILE(stream)		((void)0)
#endif

/**********************************************************************
 * UTILITY functions
 **********************************************************************/
#ifdef _STDIO_UTILITY

#include <features.h>
#include <limits.h>
#include <stdint.h>

#if UINTMAX_MAX <= 4294967295UL
#define __UIM_BUFLEN			12 /* 10 digits + 1 nul + 1 sign */
#elif UINTMAX_MAX <= 18446744073709551615ULL
#define __UIM_BUFLEN			22 /* 20 digits + 1 nul + 1 sign */
#else
#error unknown number of digits for intmax_t!
#endif

#ifdef ULLONG_MAX				/* --------------- */
#if ULLONG_MAX <= 4294967295UL
#define __UIM_BUFLEN_LLONG		12 /* 10 digits + 1 nul + 1 sign */
#elif ULLONG_MAX <= 18446744073709551615ULL
#define __UIM_BUFLEN_LLONG		22 /* 20 digits + 1 nul + 1 sign */
#else
#error unknown number of digits for long long!
#endif
#endif /* ULLONG_MAX ----------------------------- */

#if ULONG_MAX <= 4294967295UL
#define __UIM_BUFLEN_LONG		12 /* 10 digits + 1 nul + 1 sign */
#elif ULONG_MAX <= 18446744073709551615ULL
#define __UIM_BUFLEN_LONG		22 /* 20 digits + 1 nul + 1 sign */
#else
#error unknown number of digits for long!
#endif

#if UINT_MAX <= 65536U
#define __UIM_BUFLEN_INT		7 /* 10 digits + 1 nul + 1 sign */
#elif UINT_MAX <= 4294967295UL
#define __UIM_BUFLEN_INT		12 /* 10 digits + 1 nul + 1 sign */
#else
#error unknown number of digits for int!
#endif

typedef enum {
	__UIM_DECIMAL = 0,
	__UIM_LOWER = 'a' - 10,
	__UIM_UPPER = 'A' - 10,
} __UIM_CASE;

/* Write a NULL-terminated list of "char *" args to file descriptor fd.
 * For an example of usage, see __assert.c.
 */
extern void _stdio_fdout(int fd, ...);

/* Convert the int val to a string in base abs(base).  val is treated as
 * an unsigned ??? int type if base > 0, and signed if base < 0.  This
 * is an internal function with _no_ error checking done unless assert()s
 * are enabled.
 *
 * Note: bufend is a pointer to the END of the buffer passed.
 * Call like this:
 *     char buf[SIZE], *p;
 *     p = _xltostr(buf + sizeof(buf) - 1, {unsigned int},  10, __UIM_DECIMAL)
 *     p = _xltostr(buf + sizeof(buf) - 1,          {int}, -10, __UIM_DECIMAL)
 *
 * WARNING: If base > 10, case _must_be_ either __UIM_LOWER or __UIM_UPPER
 *          for lower and upper case alphas respectively.
 * WARNING: If val is really a signed type, make sure base is negative!
 *          Otherwise, you could overflow your buffer.
 */
extern char *_uintmaxtostr(char * __restrict bufend, uintmax_t uval,
						   int base, __UIM_CASE alphacase);

/* TODO -- make this either a (possibly inline) function? */
#ifndef __BCC__
#define _int10tostr(bufend, intval) \
	_uintmaxtostr((bufend), (intval), -10, __UIM_DECIMAL)
#else  /* bcc doesn't do prototypes, we need to explicitly cast */
#define _int10tostr(bufend, intval) \
	_uintmaxtostr((bufend), (uintmax_t)(intval), -10, __UIM_DECIMAL)
#endif

#define __BUFLEN_INT10TOSTR		__UIM_BUFLEN_INT

#endif /* _STDIO_UTILITY */
/**********************************************************************/
/* uClibc translations */
/**********************************************************************/

/* TODO: note done above..  typedef struct __stdio_file_struct _UC_FILE; */
typedef __stdio_fpos_t		_UC_fpos_t;
typedef __stdio_fpos64_t	_UC_fpos64_t;

#define _UC_IOFBF		_STDIO_IOFBF /* Fully buffered.  */
#define _UC_IOLBF 		_STDIO_IOLBF /* Line buffered.  */
#define _UC_IONBF 		_STDIO_IONBF /* No buffering.  */

#define _UC_BUFSIZ		_STDIO_BUFSIZ