1 /* 2 * Copyright (c) 2012-2017 Roberto E. Vargas Caballero 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 /* 7 * Portions copyright (c) 2018, ARM Limited and Contributors. 8 * All rights reserved. 9 */ 10 11 #ifndef _STDIO_H 12 #define _STDIO_H 13 14 #include <stdio_.h> 15 16 #ifndef FOPEN_MAX 17 #define FOPEN_MAX 12 18 #endif 19 20 #ifndef NULL 21 #define NULL ((void *) 0) 22 #endif 23 24 #define EOF -1 25 #define SEEK_CUR 0 26 #define SEEK_END 1 27 #define SEEK_SET 2 28 29 30 #define _IOWRITE (1 << 0) 31 #define _IOREAD (1 << 1) 32 #define _IORW (1 << 2) 33 #define _IOEOF (1 << 3) 34 #define _IOERR (1 << 4) 35 #define _IOSTRG (1 << 5) 36 #define _IOTXT (1 << 6) 37 #define _IOFBF (1 << 7) 38 #define _IOLBF (1 << 8) 39 #define _IONBF (1 << 9) 40 #define _IOALLOC (1 <<10) 41 42 typedef struct { 43 int fd; /* file descriptor */ 44 unsigned char *buf; /* pointer to i/o buffer */ 45 unsigned char *rp; /* read pointer */ 46 unsigned char *wp; /* write pointer */ 47 unsigned char *lp; /* write pointer used when line-buffering */ 48 size_t len; /* actual length of buffer */ 49 unsigned short flags; 50 unsigned char unbuf[1]; /* tiny buffer for unbuffered io */ 51 } FILE; 52 53 extern FILE __iob[FOPEN_MAX]; 54 55 #define stdin (&__iob[0]) 56 #define stdout (&__iob[1]) 57 #define stderr (&__iob[2]) 58 59 extern int remove(const char *filename); 60 extern int rename(const char *old, const char *new); 61 extern FILE *tmpfile(void); 62 extern char *tmpnam(char *s); 63 extern int fclose(FILE *fp); 64 extern int fflush(FILE *fp); 65 extern FILE *fopen(const char * restrict fname, const char * restrict mode); 66 extern FILE *freopen(const char * restrict fname, const char * restrict mode, 67 FILE * restrict fp); 68 extern void setbuf(FILE * restrict fp, char * restrict buf); 69 extern int setvbuf(FILE * restrict fp, 70 char * restrict buf, int mode, size_t size); 71 extern int fprintf(FILE * restrict fp, const char * restrict fmt, ...); 72 extern int fscanf(FILE * restrict fp, const char * restrict fmt, ...); 73 extern int printf(const char * restrict fmt, ...); 74 extern int scanf(const char * restrict fmt, ...); 75 extern int snprintf(char * restrict s, 76 size_t n, const char * restrict fmt, ...); 77 extern int sprintf(char * restrict s, const char * restrict fmt, ...); 78 extern int sscanf(const char * restrict s, const char * restrict fmt, ...); 79 80 #ifdef _STDARG_H 81 extern int vfprintf(FILE * restrict fp, 82 const char * restrict fmt, va_list arg); 83 extern int vfscanf(FILE * restrict fp, 84 const char * restrict fmt, va_list arg); 85 extern int vprintf(const char * restrict fmt, va_list arg); 86 extern int vscanf(const char * restrict fmt, va_list arg); 87 extern int vsnprintf(char * restrict s, size_t n, const char * restrict fmt, 88 va_list arg); 89 extern int vsprintf(char * restrict s, 90 const char * restrict fmt, va_list arg); 91 extern int vsscanf(const char * restrict s, 92 const char * restrict fmt, va_list arg); 93 #endif 94 95 extern int fgetc(FILE *fp); 96 extern char *fgets(char * restrict s, int n, FILE * restrict fp); 97 extern int fputc(int c, FILE *fp); 98 extern int fputs(const char * restrict s, FILE * restrict fp); 99 extern int getc(FILE *fp); 100 extern int getchar(void); 101 extern char *gets(char *s); 102 extern int putc(int c, FILE *fp); 103 extern int putchar(int c); 104 extern int puts(const char *s); 105 extern int ungetc(int c, FILE *fp); 106 extern size_t fread(void * restrict ptr, size_t size, size_t nmemb, 107 FILE * restrict fp); 108 extern size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, 109 FILE * restrict fp); 110 extern int fgetpos(FILE * restrict fp, fpos_t * restrict pos); 111 extern int fseek(FILE *fp, long int offset, int whence); 112 extern int fsetpos(FILE *fp, const fpos_t *pos); 113 extern long int ftell(FILE *fp); 114 extern void rewind(FILE *fp); 115 extern void clearerr(FILE *fp); 116 extern int feof(FILE *fp); 117 extern int ferror(FILE *fp); 118 extern void perror(const char *s); 119 120 extern int __getc(FILE *fp); 121 extern int __putc(int, FILE *fp); 122 123 #ifdef __USE_MACROS 124 #ifdef __UNIX_FILES 125 #define getc(fp) ((fp)->rp >= (fp)->wp ? __getc(fp) : *(fp)->rp++) 126 #define putc(c, fp) ((fp)->wp >= (fp)->rp ? __putc(c,fp) : (*(fp)->wp++ = c)) 127 #endif 128 129 #define ferror(fp) ((fp)->flags & _IOERR) 130 #define feof(fp) ((fp)->flags & _IOEOF) 131 #define clearerr(fp) (void) ((fp)->flags &= ~(_IOERR|_IOEOF)) 132 #define getchar() getc(stdin) 133 #define putchar(c) putc((c), stdout) 134 #define setbuf(fp, b) (void) setvbuf(fp, b, b ? _IOFBF:_IONBF, BUFSIZ) 135 #endif 136 137 #endif 138