1515
1616int _ftell (FILE * file )
1717{
18- int charsInUndoBuffer = 0 ;
19- int position ;
18+ int charsInUndoBuffer = 0 ;
19+ int position ;
2020
21- u8 tmp_kind = file -> mMode .file_kind ;
22- if (!(tmp_kind == __disk_file || tmp_kind == __console_file ) || file -> mState .error ) {
23- errno = 0x28 ;
24- return -1 ;
25- }
21+ u8 tmp_kind = file -> mMode .file_kind ;
22+ if (!(tmp_kind == __disk_file || tmp_kind == __console_file ) || file -> mState .error )
23+ {
24+ errno = 0x28 ;
25+ return -1 ;
26+ }
2627
27- if (file -> mState .io_state == __neutral )
28- return (file -> mPosition );
28+ if (file -> mState .io_state == __neutral )
29+ return (file -> mPosition );
2930
30- position = file -> mBufferPosition + (file -> mBufferPtr - file -> mBuffer );
31+ position = file -> mBufferPosition + (file -> mBufferPtr - file -> mBuffer );
3132
32- if (file -> mState .io_state >= __rereading ) {
33- charsInUndoBuffer = file -> mState .io_state - __rereading + 1 ;
34- position -= charsInUndoBuffer ;
35- }
33+ if (file -> mState .io_state >= __rereading )
34+ {
35+ charsInUndoBuffer = file -> mState .io_state - __rereading + 1 ;
36+ position -= charsInUndoBuffer ;
37+ }
3638
37- // got added in later it seems?
38- /*if (!file->mMode.binary_io) {
39+ // got added in later it seems?
40+ /*if (!file->mMode.binary_io) {
3941 int n = file->mBufferPtr - file->mBuffer - charsInUndoBuffer;
4042 u8* p = (u8*)file->mBuffer;
4143
@@ -44,87 +46,99 @@ int _ftell(FILE* file)
4446 position++;
4547 }*/
4648
47- return (position );
49+ return (position );
4850}
4951
5052int ftell (FILE * stream )
5153{
52- int retval ;
54+ int retval ;
5355
54- __begin_critical_region (stdin_access );
55- retval = (long )_ftell (stream );
56- __end_critical_region (stdin_access );
57- return retval ;
56+ __begin_critical_region (stdin_access );
57+ retval = (long )_ftell (stream );
58+ __end_critical_region (stdin_access );
59+ return retval ;
5860}
5961
60- int _fseek (FILE * file , fpos_t offset , int whence )
62+ int _fseek (FILE * file , fpos_t offset , int whence )
6163{
62- fpos_t pos ;
63- __pos_proc func ;
64-
65- unsigned char fileKind = file -> mMode .file_kind ;
66- if (fileKind != 1 || file -> mState .error != 0 ) {
67- errno = 0x28 ;
68- return -1 ;
69- }
70-
71- if (file -> mState .io_state == 1 ) {
72- if (__flush_buffer (file , nullptr ) != 0 ) {
73- file -> mState .error = 1 ;
74- file -> mBufferLength = 0 ;
75- errno = 0x28 ;
76- return -1 ;
77- }
78- }
79-
80- if (whence == SEEK_CUR ) {
81- whence = SEEK_SET ;
82-
83- if ((pos = _ftell (file )) < 0 )
84- pos = 0 ;
85-
86- offset += pos ;
87- }
88-
89- if ((whence != SEEK_END ) && (file -> mMode .io_mode != 3 ) && (file -> mState .io_state == 2 || file -> mState .io_state == 3 )) {
90- if ((offset >= file -> mPosition ) || !(offset >= file -> mBufferPosition )) {
91- file -> mState .io_state = 0 ;
92- } else {
93- file -> mBufferPtr = file -> mBuffer + (offset - file -> mBufferPosition );
94- file -> mBufferLength = file -> mPosition - offset ;
95- file -> mState .io_state = 2 ;
96- }
97- } else {
98- file -> mState .io_state = 0 ;
99- }
100-
101- if (file -> mState .io_state == 0 ) {
102-
103- if ((func = file -> positionFunc ) != nullptr && func (file -> mHandle , & offset , whence , file -> ref_con ) != 0 )
104- {
105- file -> mState .error = 1 ;
106- file -> mBufferLength = 0 ;
107- errno = 0x28 ;
108- return -1 ;
109- }
110- else
111- {
112- file -> mState .eof = 0 ;
113- file -> mPosition = offset ;
114- file -> mBufferLength = 0 ;
115- }
116- }
117-
118- return 0 ;
64+ fpos_t pos ;
65+ __pos_proc func ;
66+
67+ unsigned char fileKind = file -> mMode .file_kind ;
68+ if (fileKind != 1 || file -> mState .error != 0 )
69+ {
70+ errno = 0x28 ;
71+ return -1 ;
72+ }
73+
74+ if (file -> mState .io_state == 1 )
75+ {
76+ if (__flush_buffer (file , NULL ) != 0 )
77+ {
78+ file -> mState .error = 1 ;
79+ file -> mBufferLength = 0 ;
80+ errno = 0x28 ;
81+ return -1 ;
82+ }
83+ }
84+
85+ if (whence == SEEK_CUR )
86+ {
87+ whence = SEEK_SET ;
88+
89+ if ((pos = _ftell (file )) < 0 )
90+ pos = 0 ;
91+
92+ offset += pos ;
93+ }
94+
95+ if ((whence != SEEK_END ) && (file -> mMode .io_mode != 3 ) &&
96+ (file -> mState .io_state == 2 || file -> mState .io_state == 3 ))
97+ {
98+ if ((offset >= file -> mPosition ) || !(offset >= file -> mBufferPosition ))
99+ {
100+ file -> mState .io_state = 0 ;
101+ }
102+ else
103+ {
104+ file -> mBufferPtr = file -> mBuffer + (offset - file -> mBufferPosition );
105+ file -> mBufferLength = file -> mPosition - offset ;
106+ file -> mState .io_state = 2 ;
107+ }
108+ }
109+ else
110+ {
111+ file -> mState .io_state = 0 ;
112+ }
113+
114+ if (file -> mState .io_state == 0 )
115+ {
116+ if ((func = file -> positionFunc ) != NULL &&
117+ func (file -> mHandle , & offset , whence , file -> ref_con ) != 0 )
118+ {
119+ file -> mState .error = 1 ;
120+ file -> mBufferLength = 0 ;
121+ errno = 0x28 ;
122+ return -1 ;
123+ }
124+ else
125+ {
126+ file -> mState .eof = 0 ;
127+ file -> mPosition = offset ;
128+ file -> mBufferLength = 0 ;
129+ }
130+ }
131+
132+ return 0 ;
119133}
120134
121- int fseek (FILE * stream , fpos_t offset , int whence )
135+ int fseek (FILE * stream , fpos_t offset , int whence )
122136{
123- fpos_t start ;
124- int code ;
125- start = offset ;
126- __begin_critical_region (stdin_access );
127- code = _fseek (stream , start , whence ); // 0 if successful, -1 if error
128- __end_critical_region (stdin_access );
129- return code ;
137+ fpos_t start ;
138+ int code ;
139+ start = offset ;
140+ __begin_critical_region (stdin_access );
141+ code = _fseek (stream , start , whence ); // 0 if successful, -1 if error
142+ __end_critical_region (stdin_access );
143+ return code ;
130144}
0 commit comments