summaryrefslogtreecommitdiff
path: root/src/MSL_C.PPCEABI.bare.H/ansi_files.c
blob: 14cd868477b75423e25658998b2952f63b9779b5 (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
#include "ansi_files.h"

static unsigned char stdin_buff[0x100];
static unsigned char stdout_buff[0x100];
static unsigned char stderr_buff[0x100];

extern int fclose(FILE*);
extern int __read_console(__file_handle file, unsigned char* buff, size_t* count,
                          __idle_proc idle_proc);
extern int __write_console(__file_handle file, unsigned char* buff, size_t* count,
                           __idle_proc idle_proc);
extern int __close_console(__file_handle file);

FILE __files[4] = {
    {0,
     {0, 1, 1, 2, 0},
     {0, 0, 0, 0},
     0,
     0,
     0,
     {0, 0},
     {0, 0},
     0,
     stdin_buff,
     sizeof(stdin_buff),
     stdin_buff,
     0,
     0,
     0,
     0,
     NULL,
     &__read_console,
     &__write_console,
     &__close_console,
     0,
     &__files[1]},
    {1,
     {0, 2, 1, 2, 0},
     {0, 0, 0, 0},
     0,
     0,
     0,
     {0, 0},
     {0, 0},
     0,
     stdout_buff,
     sizeof(stdout_buff),
     stdout_buff,
     0,
     0,
     0,
     0,
     NULL,
     &__read_console,
     &__write_console,
     &__close_console,
     0,
     &__files[2]},
    {2,
     {0, 2, 0, 2, 0},
     {0, 0, 0, 0},
     0,
     0,
     0,
     {0, 0},
     {0, 0},
     0,
     stderr_buff,
     sizeof(stderr_buff),
     stderr_buff,
     0,
     0,
     0,
     0,
     NULL,
     &__read_console,
     &__write_console,
     &__close_console,
     0,
     &__files[3]},
};

void __close_all() {
  FILE* p = &__files[0];
  FILE* plast;

  // __begin_critical_region(2);

  while (p != NULL) {
    if (p->mode.file_kind != __closed_file) {
      fclose(p);
    }

    plast = p;
    p = p->next_file_struct;
    if (plast->is_dynamically_allocated)
      free(plast);
    else {
      plast->mode.file_kind = __unavailable_file;
      if ((p != NULL) && p->is_dynamically_allocated)
        plast->next_file_struct = NULL;
    }
  }

  // __end_critical_region(2);
}

int __flush_all() {
  int retval = 0;
  FILE* __stream;
  __stream = &__files[0];
  while (__stream) {
    if ((__stream->mode.file_kind) && (fflush(__stream))) {
      retval = -1;
    }
    __stream = __stream->next_file_struct;
  };
  return retval;
}