Changeset bd5414e in mainline
- Timestamp:
- 2015-09-21T21:05:26Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c70703a
- Parents:
- bea710f
- Location:
- uspace/lib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
rbea710f rbd5414e 64 64 .buf_head = NULL, 65 65 .buf_tail = NULL, 66 .buf_state = _bs_empty 66 .buf_state = _bs_empty, 67 67 }; 68 68 … … 466 466 return 0; 467 467 468 bytes_left = size * nmemb; 469 total_read = 0; 470 dp = (uint8_t *) dest; 471 472 /* Bytes from ungetc() buffer */ 473 while (stream->ungetc_chars > 0 && bytes_left > 0) { 474 *dp++ = stream->ungetc_buf[--stream->ungetc_chars]; 475 ++total_read; 476 } 477 468 478 /* If not buffered stream, read in directly. */ 469 479 if (stream->btype == _IONBF) { 470 now = _fread(dest, size, nmemb, stream);471 return now;480 total_read += _fread(dest, 1, bytes_left, stream); 481 return total_read / size; 472 482 } 473 483 … … 481 491 return 0; /* Errno set by _fallocbuf(). */ 482 492 } 483 484 bytes_left = size * nmemb;485 total_read = 0;486 dp = (uint8_t *) dest;487 493 488 494 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) { … … 674 680 } 675 681 682 int ungetc(int c, FILE *stream) 683 { 684 if (c == EOF) 685 return EOF; 686 687 if (stream->ungetc_chars >= UNGETC_MAX) 688 return EOF; 689 690 stream->ungetc_buf[stream->ungetc_chars++] = 691 (uint8_t)c; 692 693 stream->eof = false; 694 return (uint8_t)c; 695 } 696 676 697 int fseek(FILE *stream, off64_t offset, int whence) 677 698 { … … 679 700 680 701 _fflushbuf(stream); 702 stream->ungetc_chars = 0; 681 703 682 704 rc = lseek(stream->fd, offset, whence); … … 693 715 { 694 716 _fflushbuf(stream); 695 return lseek(stream->fd, 0, SEEK_CUR) ;717 return lseek(stream->fd, 0, SEEK_CUR) - stream->ungetc_chars; 696 718 } 697 719 -
uspace/lib/c/generic/private/stdio.h
rbea710f rbd5414e 39 39 #include <stdio.h> 40 40 #include <async.h> 41 42 /** Maximum characters that can be pushed back by ungetc() */ 43 #define UNGETC_MAX 1 41 44 42 45 struct _IO_FILE { … … 82 85 /** Points to end of occupied space when in read mode. */ 83 86 uint8_t *buf_tail; 87 88 /** Pushed back characters */ 89 uint8_t ungetc_buf[UNGETC_MAX]; 90 91 /** Number of pushed back characters */ 92 int ungetc_chars; 84 93 }; 85 94 -
uspace/lib/c/include/stdio.h
rbea710f rbd5414e 109 109 extern int puts(const char *); 110 110 111 extern int ungetc(int, FILE *); 112 111 113 /* Formatted string output functions */ 112 114 extern int fprintf(FILE *, const char*, ...) -
uspace/lib/posix/source/stdio.c
rbea710f rbd5414e 118 118 int posix_ungetc(int c, FILE *stream) 119 119 { 120 uint8_t b = (uint8_t) c; 121 122 bool can_unget = 123 /* Provided character is legal. */ 124 c != EOF && 125 /* Stream is consistent. */ 126 !stream->error && 127 /* Stream is buffered. */ 128 stream->btype != _IONBF && 129 /* Last operation on the stream was a read operation. */ 130 stream->buf_state == _bs_read && 131 /* Stream buffer is already allocated (i.e. there was already carried 132 * out either write or read operation on the stream). This is probably 133 * redundant check but let's be safe. */ 134 stream->buf != NULL && 135 /* There is still space in the stream to retreat. POSIX demands the 136 * possibility to unget at least 1 character. It should be always 137 * possible, assuming the last operation on the stream read at least 1 138 * character, because the buffer is refilled in the lazily manner. */ 139 stream->buf_tail > stream->buf; 140 141 if (can_unget) { 142 --stream->buf_tail; 143 stream->buf_tail[0] = b; 144 stream->eof = false; 145 return (int) b; 146 } else { 147 return EOF; 148 } 120 return ungetc(c, stream); 149 121 } 150 122
Note:
See TracChangeset
for help on using the changeset viewer.