summaryrefslogtreecommitdiff
path: root/runtime/stdio.c
blob: 5a2f33e65b4b72818c9232ae6ba8e7a11ad32fdd (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
/* *********************************************************************/
/*                                                                     */
/*              The Compcert verified compiler                         */
/*                                                                     */
/*          Xavier Leroy, INRIA Paris-Rocquencourt                     */
/*                                                                     */
/*  Copyright Institut National de Recherche en Informatique et en     */
/*  Automatique.  All rights reserved.  This file is distributed       */
/*  under the terms of the GNU General Public License as published by  */
/*  the Free Software Foundation, either version 2 of the License, or  */
/*  (at your option) any later version.  This file is also distributed */
/*  under the terms of the INRIA Non-Commercial License Agreement.     */
/*                                                                     */
/* *********************************************************************/

#include <stdarg.h>
#include <stdlib.h>
#define _INSIDE_COMPCERT_COMPATIBILITY_LIBRARY
#include "stdio.h"

static compcert_FILE * compcert_alloc_file(FILE * f)
{
  struct compcert_FILE_ * r;
  r = malloc(sizeof(struct compcert_FILE_));
  if (r == NULL) return NULL;
  r->fstr = (void *) f;
  return r;
}

compcert_FILE * compcert_stdin;
compcert_FILE * compcert_stdout;
compcert_FILE * compcert_stderr;

static __attribute__((constructor)) void compcert_stdio_init(void)
{
  compcert_stdin = compcert_alloc_file(stdin);
  compcert_stdout = compcert_alloc_file(stdout);
  compcert_stderr = compcert_alloc_file(stderr);
}

void compcert_clearerr(compcert_FILE * f)
{
  clearerr((FILE *)(f->fstr));
}

int compcert_fclose(compcert_FILE * f)
{
  int errcode = fclose((FILE *)(f->fstr));
  free(f);
  return errcode;
}

int compcert_feof(compcert_FILE * f)
{
  return feof((FILE *)(f->fstr));
}

int compcert_ferror(compcert_FILE * f)
{
  return ferror((FILE *)(f->fstr));
}

int compcert_fflush(compcert_FILE * f)
{
  return fflush((FILE *)(f->fstr));
}

int compcert_fgetc(compcert_FILE * f)
{
  return fgetc((FILE *)(f->fstr));
}

char *compcert_fgets(char * s, int n, compcert_FILE * f)
{
  return fgets(s, n, (FILE *)(f->fstr));
}

compcert_FILE *compcert_fopen(const char * p, const char * m)
{
  FILE * f = fopen(p, m);
  if (f == NULL) return NULL;
  return compcert_alloc_file(f);
}

int compcert_fprintf(compcert_FILE * f, const char * s, ...)
{
  va_list ap;
  int retcode;
  va_start(ap, s);
  retcode = vfprintf((FILE *)(f->fstr), s, ap);
  va_end(ap);
  return retcode;
}

int compcert_fputc(int c, compcert_FILE * f)
{
  return fputc(c, (FILE *)(f->fstr));
}

int compcert_fputs(const char * s, compcert_FILE * f)
{
  return fputs(s, (FILE *)(f->fstr));
}

size_t compcert_fread(void * s, size_t p, size_t q, compcert_FILE * f)
{
  return fread(s, p, q, (FILE *)(f->fstr));
}

compcert_FILE *compcert_freopen(const char * s, const char * m,
                                compcert_FILE * f)
{
  FILE * nf = freopen(s, m, (FILE *)(f->fstr));
  if (nf == NULL) return NULL;
  f->fstr = nf;
  return f;
}

int compcert_fscanf(compcert_FILE * f, const char * s, ...)
{
  va_list ap;
  int retcode;
  va_start(ap, s);
  retcode = vfscanf((FILE *)(f->fstr), s, ap);
  va_end(ap);
  return retcode;
}

int compcert_fseek(compcert_FILE * f, long p, int q)
{
  return fseek((FILE *)(f->fstr), p, q);
}

long compcert_ftell(compcert_FILE *f)
{
  return ftell((FILE *)(f->fstr));
}

size_t compcert_fwrite(const void * b, size_t p, size_t q, compcert_FILE * f)
{
  return fwrite(b, p, q, (FILE *)(f->fstr));
}

int compcert_getc(compcert_FILE * f)
{
  return getc((FILE *)(f->fstr));
}

int compcert_putc(int c , compcert_FILE * f)
{
  return putc(c, (FILE *)(f->fstr));
}

void compcert_rewind(compcert_FILE * f)
{
  rewind((FILE *)(f->fstr));
}

int compcert_ungetc(int c, compcert_FILE * f)
{
  return ungetc(c, (FILE *)(f->fstr));
}