1
- #include <stddef.h>
2
- #include <stdarg.h>
1
+ #ifdef __cplusplus
2
+ extern "C" {
3
+ #endif
3
4
4
5
#pragma once
5
6
7
+ #include <alloca.h>
8
+ #include <assert.h>
9
+ #include <stdarg.h>
10
+ #include <stddef.h>
11
+
6
12
typedef struct {} FILE ;
7
13
8
14
#define SEEK_SET 0
@@ -12,6 +18,8 @@ typedef struct {} FILE;
12
18
#define stdout ((FILE *)2)
13
19
#define stderr ((FILE *)3)
14
20
21
+ #define EOF -1
22
+
15
23
FILE * fopen (const char * , char * );
16
24
int fclose (FILE * );
17
25
int fseek (FILE * , long , int );
@@ -21,6 +29,10 @@ size_t fwrite(const void *, size_t, size_t, FILE *);
21
29
22
30
int vfprintf (FILE * , const char * , va_list );
23
31
int vsnprintf (char * , size_t , const char * , va_list );
32
+ int vsprintf (char * , const char * , va_list );
33
+
34
+ int putchar (int ch );
35
+ int getchar ();
24
36
25
37
static inline int snprintf (char * buf , size_t size , const char * fmt , ...) {
26
38
va_list args ;
@@ -30,6 +42,14 @@ static inline int snprintf(char *buf, size_t size, const char *fmt, ...) {
30
42
return result ;
31
43
}
32
44
45
+ static inline int sprintf (char * buf , const char * fmt , ...) {
46
+ va_list args ;
47
+ va_start (args , fmt );
48
+ int result = vsprintf (buf , fmt , args );
49
+ va_end (args );
50
+ return result ;
51
+ }
52
+
33
53
static inline int fprintf (FILE * f , const char * fmt , ...) {
34
54
va_list args ;
35
55
va_start (args , fmt );
@@ -45,3 +65,37 @@ static inline int printf(const char *fmt, ...) {
45
65
va_end (args );
46
66
return result ;
47
67
}
68
+
69
+ int __sscanf (const char * str , const char * format , void * ptrs );
70
+
71
+ static inline int vsscanf (const char * str , const char * format , va_list ap ) {
72
+ int count = 0 ;
73
+ for (int i = 0 ; format [i ]; i ++ ) {
74
+ if (format [i ] == '%' ) {
75
+ if (format [i + 1 ] == '%' ) {
76
+ i ++ ;
77
+ continue ;
78
+ }
79
+ count ++ ;
80
+ }
81
+ }
82
+
83
+ void * * ptrs = (void * * )(alloca (count * sizeof (void * )));
84
+ for (int i = 0 ; i < count ; i ++ ) {
85
+ ptrs [i ] = va_arg (ap , void * );
86
+ }
87
+
88
+ return __sscanf (str , format , ptrs );
89
+ }
90
+
91
+ static inline int sscanf (const char * str , const char * format , ...) {
92
+ va_list args ;
93
+ va_start (args , format );
94
+ int res = vsscanf (str , format , args );
95
+ va_end (args );
96
+ return res ;
97
+ }
98
+
99
+ #ifdef __cplusplus
100
+ }
101
+ #endif
0 commit comments