forked from Bananymous/banan-os
				
			LibC: Fix `FILE` buffering once again :D
This commit is contained in:
		
							parent
							
								
									b907263f35
								
							
						
					
					
						commit
						5d80c880c8
					
				|  | @ -82,12 +82,9 @@ static void init_closed_file(FILE* file) | ||||||
| 
 | 
 | ||||||
| static int drop_read_buffer(FILE* file) | static int drop_read_buffer(FILE* file) | ||||||
| { | { | ||||||
| 	if (file->buffer_rd_size == 0) | 	const off_t bytes_remaining = file->buffer_rd_size - file->buffer_idx; | ||||||
| 		return 0; | 	if (bytes_remaining > 0) | ||||||
| 	ASSERT(file->buffer_idx != 0); | 		if (syscall(SYS_SEEK, file->fd, -bytes_remaining, SEEK_CUR) == -1) | ||||||
| 	if (file->buffer_idx == 1) |  | ||||||
| 		return 0; |  | ||||||
| 	if (syscall(SYS_SEEK, file->fd, file->buffer_idx - 1, SEEK_CUR) == -1) |  | ||||||
| 			return EOF; | 			return EOF; | ||||||
| 	file->buffer_rd_size = 0; | 	file->buffer_rd_size = 0; | ||||||
| 	file->buffer_idx = 0; | 	file->buffer_idx = 0; | ||||||
|  | @ -452,8 +449,7 @@ int fseeko(FILE* file, off_t offset, int whence) | ||||||
| 	ScopeLock _(file); | 	ScopeLock _(file); | ||||||
| 	if (fflush(file) == EOF) | 	if (fflush(file) == EOF) | ||||||
| 		return -1; | 		return -1; | ||||||
| 	long ret = syscall(SYS_SEEK, file->fd, offset, whence); | 	if (syscall(SYS_SEEK, file->fd, offset, whence) == -1) | ||||||
| 	if (ret < 0) |  | ||||||
| 		return -1; | 		return -1; | ||||||
| 	file->eof = false; | 	file->eof = false; | ||||||
| 	return 0; | 	return 0; | ||||||
|  | @ -472,12 +468,12 @@ long ftell(FILE* file) | ||||||
| off_t ftello(FILE* file) | off_t ftello(FILE* file) | ||||||
| { | { | ||||||
| 	ScopeLock _(file); | 	ScopeLock _(file); | ||||||
| 	if (fflush(file) == EOF) | 	auto offset = syscall(SYS_TELL, file->fd); | ||||||
|  | 	if (offset == -1) | ||||||
| 		return -1; | 		return -1; | ||||||
| 	long ret = syscall(SYS_TELL, file->fd); | 	if (file->buffer_rd_size) | ||||||
| 	if (ret < 0) | 		offset -= file->buffer_rd_size - file->buffer_idx; | ||||||
| 		return -1; | 	return offset - file->unget_buf_idx; | ||||||
| 	return ret; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int ftrylockfile(FILE* fp) | int ftrylockfile(FILE* fp) | ||||||
|  | @ -541,10 +537,7 @@ int getc_unlocked(FILE* file) | ||||||
| 	if (file->unget_buf_idx) | 	if (file->unget_buf_idx) | ||||||
| 	{ | 	{ | ||||||
| 		file->unget_buf_idx--; | 		file->unget_buf_idx--; | ||||||
| 		unsigned char ch = file->unget_buffer[file->unget_buf_idx]; | 		return file->unget_buffer[file->unget_buf_idx]; | ||||||
| 		if (fseeko(file, 1, SEEK_CUR) == -1) |  | ||||||
| 			return EOF; |  | ||||||
| 		return ch; |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// read from unbuffered file
 | 	// read from unbuffered file
 | ||||||
|  | @ -582,8 +575,6 @@ int getc_unlocked(FILE* file) | ||||||
| 		((nread == 0) ? file->eof : file->error) = true; | 		((nread == 0) ? file->eof : file->error) = true; | ||||||
| 		return EOF; | 		return EOF; | ||||||
| 	} | 	} | ||||||
| 	if (fseeko(file, 1 - nread, SEEK_CUR) == -1) |  | ||||||
| 		return EOF; |  | ||||||
| 	file->buffer_rd_size = nread; | 	file->buffer_rd_size = nread; | ||||||
| 	file->buffer_idx = 1; | 	file->buffer_idx = 1; | ||||||
| 	return file->buffer[0]; | 	return file->buffer[0]; | ||||||
|  | @ -1020,9 +1011,6 @@ int ungetc_unlocked(int c, FILE* stream) | ||||||
| 		return EOF; | 		return EOF; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (fseeko(stream, -1, SEEK_CUR) == -1) |  | ||||||
| 		return EOF; |  | ||||||
| 
 |  | ||||||
| 	stream->unget_buffer[stream->unget_buf_idx] = c; | 	stream->unget_buffer[stream->unget_buf_idx] = c; | ||||||
| 	stream->unget_buf_idx++; | 	stream->unget_buf_idx++; | ||||||
| 	stream->eof = false; | 	stream->eof = false; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue