Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ typedef wchar_t wint_t;
#define TRUE 1
#define FALSE 0

#define nullptr 0
#define null 0

#ifndef NULL
Expand Down
190 changes: 102 additions & 88 deletions src/PowerPC_EABI_Support/src/MSL_C/MSL_Common/FILE_POS.C
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@

int _ftell(FILE* file)
{
int charsInUndoBuffer = 0;
int position;
int charsInUndoBuffer = 0;
int position;

u8 tmp_kind = file->mMode.file_kind;
if (!(tmp_kind == __disk_file || tmp_kind == __console_file) || file->mState.error) {
errno = 0x28;
return -1;
}
u8 tmp_kind = file->mMode.file_kind;
if (!(tmp_kind == __disk_file || tmp_kind == __console_file) || file->mState.error)
{
errno = 0x28;
return -1;
}

if (file->mState.io_state == __neutral)
return (file->mPosition);
if (file->mState.io_state == __neutral)
return (file->mPosition);

position = file->mBufferPosition + (file->mBufferPtr - file->mBuffer);
position = file->mBufferPosition + (file->mBufferPtr - file->mBuffer);

if (file->mState.io_state >= __rereading) {
charsInUndoBuffer = file->mState.io_state - __rereading + 1;
position -= charsInUndoBuffer;
}
if (file->mState.io_state >= __rereading)
{
charsInUndoBuffer = file->mState.io_state - __rereading + 1;
position -= charsInUndoBuffer;
}

// got added in later it seems?
/*if (!file->mMode.binary_io) {
// got added in later it seems?
/*if (!file->mMode.binary_io) {
int n = file->mBufferPtr - file->mBuffer - charsInUndoBuffer;
u8* p = (u8*)file->mBuffer;

Expand All @@ -44,87 +46,99 @@ int _ftell(FILE* file)
position++;
}*/

return (position);
return (position);
}

int ftell(FILE* stream)
{
int retval;
int retval;

__begin_critical_region(stdin_access);
retval = (long)_ftell(stream);
__end_critical_region(stdin_access);
return retval;
__begin_critical_region(stdin_access);
retval = (long)_ftell(stream);
__end_critical_region(stdin_access);
return retval;
}

int _fseek(FILE *file, fpos_t offset, int whence)
int _fseek(FILE* file, fpos_t offset, int whence)
{
fpos_t pos;
__pos_proc func;

unsigned char fileKind = file->mMode.file_kind;
if (fileKind != 1 || file->mState.error != 0) {
errno = 0x28;
return -1;
}

if (file->mState.io_state == 1) {
if (__flush_buffer(file, nullptr) != 0) {
file->mState.error = 1;
file->mBufferLength = 0;
errno = 0x28;
return -1;
}
}

if (whence == SEEK_CUR) {
whence = SEEK_SET;

if ((pos = _ftell(file)) < 0)
pos = 0;

offset += pos;
}

if ((whence != SEEK_END) && (file->mMode.io_mode != 3) && (file->mState.io_state == 2 || file->mState.io_state == 3)) {
if ((offset >= file->mPosition) || !(offset >= file->mBufferPosition)) {
file->mState.io_state = 0;
} else {
file->mBufferPtr = file->mBuffer + (offset - file->mBufferPosition);
file->mBufferLength = file->mPosition - offset;
file->mState.io_state = 2;
}
} else {
file->mState.io_state = 0;
}

if (file->mState.io_state == 0) {

if ((func = file->positionFunc) != nullptr && func(file->mHandle, &offset, whence, file->ref_con) != 0)
{
file->mState.error = 1;
file->mBufferLength = 0;
errno = 0x28;
return -1;
}
else
{
file->mState.eof = 0;
file->mPosition = offset;
file->mBufferLength = 0;
}
}

return 0;
fpos_t pos;
__pos_proc func;

unsigned char fileKind = file->mMode.file_kind;
if (fileKind != 1 || file->mState.error != 0)
{
errno = 0x28;
return -1;
}

if (file->mState.io_state == 1)
{
if (__flush_buffer(file, NULL) != 0)
{
file->mState.error = 1;
file->mBufferLength = 0;
errno = 0x28;
return -1;
}
}

if (whence == SEEK_CUR)
{
whence = SEEK_SET;

if ((pos = _ftell(file)) < 0)
pos = 0;

offset += pos;
}

if ((whence != SEEK_END) && (file->mMode.io_mode != 3) &&
(file->mState.io_state == 2 || file->mState.io_state == 3))
{
if ((offset >= file->mPosition) || !(offset >= file->mBufferPosition))
{
file->mState.io_state = 0;
}
else
{
file->mBufferPtr = file->mBuffer + (offset - file->mBufferPosition);
file->mBufferLength = file->mPosition - offset;
file->mState.io_state = 2;
}
}
else
{
file->mState.io_state = 0;
}

if (file->mState.io_state == 0)
{
if ((func = file->positionFunc) != NULL &&
func(file->mHandle, &offset, whence, file->ref_con) != 0)
{
file->mState.error = 1;
file->mBufferLength = 0;
errno = 0x28;
return -1;
}
else
{
file->mState.eof = 0;
file->mPosition = offset;
file->mBufferLength = 0;
}
}

return 0;
}

int fseek(FILE *stream, fpos_t offset, int whence)
int fseek(FILE* stream, fpos_t offset, int whence)
{
fpos_t start;
int code;
start = offset;
__begin_critical_region(stdin_access);
code = _fseek(stream, start, whence); // 0 if successful, -1 if error
__end_critical_region(stdin_access);
return code;
fpos_t start;
int code;
start = offset;
__begin_critical_region(stdin_access);
code = _fseek(stream, start, whence); // 0 if successful, -1 if error
__end_critical_region(stdin_access);
return code;
}
20 changes: 10 additions & 10 deletions src/PowerPC_EABI_Support/src/MSL_C/MSL_Common/ansi_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern int __close_console(u32);
// clang-format off
FILE __files[4] =
{
{ 0, // _00
{ 0, // _00
0, // _04, open_mode
1, // _04, io_mode
1, // _04, buffer_mode
Expand All @@ -31,22 +31,22 @@ FILE __files[4] =
0, // _10
0, // _12
0, // _14
0, // _18
0, // _18
stdin_buff, // _1C
sizeof(stdin_buff), // _20
stdin_buff, // _24
0, // _28
0, // _2C
0, // _30
0, // _34
nullptr, // _38
NULL, // _38
&__read_console, // _3C
&__write_console, // _40
&__close_console, // _44
0, // _48
&__files[1] // _4C
},
{ 1, // _00
{ 1, // _00
0, // _04, open_mode
2, // _04, io_mode
1, // _04, buffer_mode
Expand All @@ -63,22 +63,22 @@ FILE __files[4] =
0, // _10
0, // _12
0, // _14
0, // _18
0, // _18
stdout_buff, // _1C
sizeof(stdout_buff), // _20
stdout_buff, // _24
0, // _28
0, // _2C
0, // _30
0, // _34
nullptr, // _38
NULL, // _38
&__read_console, // _3C
&__write_console, // _40
&__close_console, // _44
0, // _48
&__files[2] // _4C
},
{ 2, // _00
{ 2, // _00
0, // _04, open_mode
2, // _04, io_mode
0, // _04, buffer_mode
Expand All @@ -95,15 +95,15 @@ FILE __files[4] =
0, // _10
0, // _12
0, // _14
0, // _18
0, // _18
stderr_buff, // _1C
sizeof(stderr_buff), // _20
stderr_buff, // _24
0, // _28
0, // _2C
0, // _30
0, // _34
nullptr, // _38
NULL, // _38
&__read_console, // _3C
&__write_console, // _40
&__close_console, // _44
Expand Down Expand Up @@ -135,7 +135,7 @@ void __close_all()
{
plast->mMode.file_kind = __unavailable_file;
if ((p != NULL) && p->mIsDynamicallyAllocated)
plast->mNextFile = nullptr;
plast->mNextFile = NULL;
}
}

Expand Down
Loading