36 int16_t _flag = (int16_t)stream->
_flag;
40 char *_base = stream->
_base;
41 uint32_t byte_count = stream->
_ptr - _base;
45 if ((uint32_t)
CRT_write(stream->
_file, _base, byte_count) != byte_count)
52 int32_t flags_after_write = stream->
_flag;
57 stream->
_flag = flags_after_write;
63 char *buffer_start = stream->
_base;
66 stream->
_ptr = buffer_start;
82 char _flag = (char)stream->
_flag;
98 int32_t result =
_flush(stream);
127 return _openfile(filename, mode, sharing_flag, stream);
145 if (stream->
_cnt < 0)
153 int16_t initial_stream_flags = (int16_t)stream->
_flag;
156 return offset - stream->
_cnt;
159 char *buffer_start = stream->
_base;
160 int32_t result =
_ptr - buffer_start;
166 for (
char *newline_cursor = buffer_start; newline_cursor <
_ptr; newline_cursor = &newline_cursor[1])
168 if (*(uint8_t *)newline_cursor == 0xa)
184 return result + offset;
191 int32_t buffered =
_ptr - buffer_start +
_cnt;
194 return result + offset - buffered;
196 bool no_extra_text_byte;
205 if (buffered <= 0x200)
220 if (*(uint8_t *)
_base == 0xa)
227 if (!no_extra_text_byte)
230 return result + offset - buffered;
246 int32_t effective_origin = origin;
266 int32_t flags_after_flush = stream->
_flag;
271 stream->
_flag = flags_after_flush;
276 int32_t seek_succeeded =
CRT_lseek(stream->
_file, offset, effective_origin) != -1;
278 return seek_succeeded - 1;
285 uint32_t requested_bytes = size * count, bytes_remaining = requested_bytes;
286 unsigned char *output_cursor = buffer;
288 if (!requested_bytes)
293 while (bytes_remaining)
297 uint32_t chunk_bytes = bytes_remaining < (uint32_t)stream->
_cnt ? bytes_remaining : (uint32_t)stream->
_cnt;
300 bytes_remaining -= chunk_bytes;
301 output_cursor += chunk_bytes;
302 stream->
_cnt -= chunk_bytes;
303 stream->
_ptr += chunk_bytes;
305 else if (bytes_remaining >= buffer_size)
307 uint32_t chunk_bytes = buffer_size ? bytes_remaining - bytes_remaining % buffer_size : bytes_remaining;
308 int32_t bytes_read =
CRT_read(stream->
_file, output_cursor, chunk_bytes);
317 bytes_remaining -= bytes_read;
318 output_cursor += bytes_read;
324 if (next_character < 0)
327 *output_cursor++ = (
unsigned char)next_character;
333 return (requested_bytes - bytes_remaining) / size;
340 uint32_t requested_bytes = size * count;
341 uint32_t bytes_remaining = requested_bytes;
343 if (!requested_bytes)
361 if (buffered_flags &&
_cnt)
363 uint32_t buffer_copy_bytes = bytes_remaining;
365 if (bytes_remaining >= (uint32_t)
_cnt)
366 buffer_copy_bytes =
_cnt;
369 stream->
_cnt -= buffer_copy_bytes;
370 stream->
_ptr = &stream->
_ptr[buffer_copy_bytes];
371 bytes_remaining -= buffer_copy_bytes;
372 buffer += buffer_copy_bytes;
374 else if (bytes_remaining < block_size)
376 if (
CRT_flsbuf((int32_t)*(int8_t *)buffer, stream) == -1)
385 bytes_remaining -= 1;
396 flush_failed =
_flush(stream);
398 if (buffered_flags && flush_failed)
405 byte_count = !block_size ? bytes_remaining : bytes_remaining - bytes_remaining % block_size;
407 int32_t bytes_written =
CRT_write(stream->
_file, buffer, byte_count);
409 if (bytes_written != -1)
411 buffer += bytes_written;
412 bytes_remaining -= bytes_written;
415 if (bytes_written == -1 || (uint32_t)bytes_written < byte_count)
423 if (!bytes_remaining)
427 return (requested_bytes - bytes_remaining) / size;
436 if (!stream_capacity)
438 stream_capacity = 0x200;
441 else if (stream_capacity < 0x14)
443 stream_capacity = 0x14;
459 if (!fallback_stream_table)
466 uint32_t static_stream_index = 0;
473 static_stream_index += 1;
476 int32_t standard_fd = 0;
482 if (os_handle == INVALID_HANDLE_VALUE || !os_handle)
483 standard_stream->_file = -1;
485 standard_stream += 1;
523 active_stream->
_cnt = 0;
534 active_stream->
_ptr = active_stream->
_base;
538 int32_t write_mode_flags = active_stream->
_flag;
540 active_stream->
_cnt = 0;
542 int32_t bytes_written = 0;
546 active_stream->
_flag = write_mode_flags;
570 byte_count = active_stream->
_ptr -
_base;
587 bytes_written =
CRT_write(_file, _base, byte_count);
589 *(uint8_t *)active_stream->
_base = (uint8_t)ch;
592 if ((uint32_t)bytes_written == byte_count)
593 return (uint32_t)(uint8_t)ch;
607 int32_t standard_buffer_index;
610 standard_buffer_index = 0;
612 standard_buffer_index = 1;
633 stream->
_base = standard_buffer;
634 stream->
_ptr = standard_buffer;
635 stream->
_cnt = 0x1000;
668 stream->
_base = NULL;
676 char _flag = (char)stream->
_flag;
684 stream->
_base =
nullptr;
693 char const *mode_cursor = mode;
694 int32_t commit_mode_seen = 0;
695 int32_t access_hint_seen = 0;
696 char mode_character = *(uint8_t *)mode_cursor;
698 int32_t stream_flags;
700 if (mode_character ==
'a')
705 else if (mode_character ==
'r')
712 if (mode_character !=
'w')
719 int32_t continue_parsing = 1;
723 mode_character = mode_cursor[1];
724 mode_cursor = &mode_cursor[1];
729 if (!continue_parsing)
732 int32_t mode_character_extended = (int32_t)mode_character;
734 if (mode_character_extended >
'T')
736 if (mode_character_extended ==
'b')
745 else if (mode_character_extended ==
'c')
747 if (!commit_mode_seen)
749 commit_mode_seen = 1;
755 else if (mode_character_extended ==
'n')
757 if (!commit_mode_seen)
759 commit_mode_seen = 1;
772 else if (mode_character_extended ==
'T')
781 else if (mode_character_extended ==
'+')
791 else if (mode_character_extended ==
'D')
800 else if (mode_character_extended ==
'R')
802 if (!access_hint_seen)
804 access_hint_seen = 1;
810 else if (mode_character_extended ==
'S' && !access_hint_seen)
812 access_hint_seen = 1;
818 continue_parsing = 0;
823 if (file_descriptor < 0)
827 stream->
_flag = stream_flags;
830 stream->
_base = NULL;
832 stream->
_file = file_descriptor;
843 int32_t stream_index = 0;
845 if (stream_capacity > 0)
852 struct File_Handle *candidate_stream = *stream_slot;
854 if (!candidate_stream)
860 if (!allocated_stream)
863 result = allocated_stream;
866 result = stream_table[stream_index];
870 stream_slot = &stream_slot[1];
872 if (stream_index >= stream_capacity)
884 result->
_base = NULL;
922 stream->
_cnt = bytes_read;
924 if (!bytes_read || bytes_read == -1)
932 int32_t flags_after_read = stream->
_flag;
947 stream->
_flag = flags_after_read;
953 int16_t buffer_growth_flags = (int16_t)stream->
_flag;
959 char *_ptr = stream->
_ptr;
961 stream->
_cnt = bytes_read - 1;
963 uint32_t result = (uint32_t)*(uint8_t *)_ptr;
965 stream->
_ptr = &_ptr[1];
1010 stream->
_base = allocated_buffer;
1012 if (!allocated_buffer)
1034 int32_t stream_index = 0;
1035 int32_t flushed_stream_count = 0;
1036 int32_t flush_error = 0;
1050 if (flush_mode == 1)
1053 flushed_stream_count += 1;
1057 int32_t stream_flush_result =
CRT_fflush(stream);
1059 if (stream_flush_result == -1)
1060 flush_error |= stream_flush_result;
1069 if (flush_mode == 1)
1070 return flushed_stream_count;
#define CRT_STREAM_OWN_BUFFER
#define CRT_PERMISSION_WRITE
#define CRT_PERMISSION_GROUP_READ
#define CRT_PERMISSION_OTHER_READ
#define CRT_STREAM_STRING
#define CRT_STREAM_SET_BUFFER
@ CRT_ERROR_INVALID_ARGUMENT
#define CRT_OPEN_TEMPORARY
#define CRT_STREAM_COMMIT
#define CRT_OPEN_READ_WRITE
#define CRT_OPEN_WRITE_ONLY
#define CRT_OPEN_SHORT_LIVED
#define CRT_OPEN_SEQUENTIAL
#define CRT_OPEN_TRUNCATE
#define CRT_STREAM_USER_BUFFER
#define CRT_STREAM_TEMP_BUFFER
#define CRT_FD_CHUNK_INDEX_MASK
#define CRT_FD_CHUNK_SHIFT
#define CRT_STREAM_UNBUFFERED
#define CRT_STREAM_CTRL_Z
#define CRT_STREAM_UPDATE
#define CRT_PERMISSION_READ
int32_t CRT_fclose(struct File_Handle *stream)
int32_t CRT_close(int32_t file_no)
void * CRT_malloc(uint32_t size)
int32_t CRT_read(int32_t fd, void *buffer, uint32_t count)
void * CRT_calloc(uint32_t count, uint32_t element_size)
void __amsg_exit(int32_t runtime_error)
int32_t CRT_write(int32_t file_no, void const *buffer, uint32_t byte_count)
int32_t CRT_commit(int32_t file_no)
char * CRT_MoveBytesOverlapSafe(char *dest, char const *src, uint32_t count)
int32_t CRT_lseek(int32_t fd, int32_t offset, int32_t origin)
int32_t CRT_sopen(char const *filename, int32_t oflag, int32_t shflag,...)
int32_t CRT_isatty(int32_t fd)
#define g_fileDescriptorTable
#define g_crtExitProcessing
#define g_crtDefaultTextMode
#define g_crtStandardBuffers
#define g_pCRT_ErrorHandler
#define g_crtFileBufferSerial
#define g_fileHandleArray
#define g_crtStaticStreams
#define g_maxFileHandleCount
#define g_CRT_FileCharBuffer
int32_t CRT_fclose(struct File_Handle *stream)
void __freebuf(struct File_Handle *stream)
int32_t CRT_flsall(int32_t flush_mode)
void _ftbuf(int32_t clear_buffer, struct File_Handle *stream)
uint32_t CRT_fwrite(void const *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
int32_t _stbuf(struct File_Handle *stream)
struct File_Handle * _openfile(char const *filename, char const *mode, int32_t sharing_flag, struct File_Handle *stream)
struct File_Handle * CRT_fsopen(char const *filename, char const *mode, int32_t sharing_flag)
int32_t CRT_fseek(struct File_Handle *stream, int32_t offset, int32_t origin)
int32_t CRT_fflush(struct File_Handle *stream)
void _getbuf(struct File_Handle *stream)
int32_t CRT_flsbuf(int32_t ch, struct File_Handle *stream)
int32_t _flush(struct File_Handle *stream)
uint32_t CRT_fread(void *buffer, uint32_t size, uint32_t count, struct File_Handle *stream)
struct File_Handle * CRT_fopen(char const *filename, char const *mode)
int32_t CRT_filbuf(struct File_Handle *stream)
int32_t CRT_ftell(struct File_Handle *stream)
struct File_Handle * _getstream()