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

#pragma dont_inline on 
int fclose(FILE* file) {
  int flush_result, close_result;

  if (file == nullptr)
    return (-1);
  if (file->mode.file_kind == __closed_file)
    return (0);

  flush_result = fflush(file);

  close_result = (*file->close_proc)(file->handle);

  file->mode.file_kind = __closed_file;
  file->handle = NULL;

  if (file->state.free_buffer)
    free(file->buffer);
  return ((flush_result || close_result) ? -1 : 0);
}

int fflush(FILE* file) {
  int pos;

  if (file == nullptr) {
    return __flush_all();
  }

  if (file->state.error != 0 || file->mode.file_kind == __closed_file) {
    return -1;
  }

  if (file->mode.io_mode == 1) {
    return 0;
  }

  if (file->state.io_state >= 3) {
    file->state.io_state = 2;
  }

  if (file->state.io_state == 2) {
    file->buffer_len = 0;
  }

  if (file->state.io_state != 1) {
    file->state.io_state = 0;
    return 0;
  }

  if (file->mode.file_kind != __disk_file) {
    pos = 0;
  } else {
    pos = ftell(file);
  }

  if (__flush_buffer(file, 0) != 0) {
    file->state.error = 1;
    file->buffer_len = 0;
    return -1;
  }

  file->state.io_state = 0;
  file->position = pos;
  file->buffer_len = 0;
  return 0;
}
#pragma dont_inline reset