Changes in uspace/lib/c/generic/io/io.c [01cc7b4:ed88c8e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
r01cc7b4 red88c8e 53 53 static void _fflushbuf(FILE *stream); 54 54 55 static size_t stdio_kio_read(void *, size_t, size_t, FILE *);56 static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);57 static int stdio_kio_flush(FILE *);58 59 static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);60 static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);61 62 static int stdio_vfs_flush(FILE *);63 64 /** KIO stream ops */65 static __stream_ops_t stdio_kio_ops = {66 .read = stdio_kio_read,67 .write = stdio_kio_write,68 .flush = stdio_kio_flush69 };70 71 /** VFS stream ops */72 static __stream_ops_t stdio_vfs_ops = {73 .read = stdio_vfs_read,74 .write = stdio_vfs_write,75 .flush = stdio_vfs_flush76 };77 78 55 static FILE stdin_null = { 79 56 .fd = -1, … … 81 58 .error = true, 82 59 .eof = true, 83 .ops = &stdio_vfs_ops, 84 .arg = NULL, 60 .kio = false, 85 61 .sess = NULL, 86 62 .btype = _IONBF, … … 97 73 .error = false, 98 74 .eof = false, 99 .ops = &stdio_kio_ops, 100 .arg = NULL, 75 .kio = true, 101 76 .sess = NULL, 102 77 .btype = _IOLBF, … … 113 88 .error = false, 114 89 .eof = false, 115 .ops = &stdio_kio_ops, 116 .arg = NULL, 90 .kio = true, 117 91 .sess = NULL, 118 92 .btype = _IONBF, … … 354 328 stream->error = false; 355 329 stream->eof = false; 356 stream->ops = &stdio_vfs_ops; 357 stream->arg = NULL; 330 stream->kio = false; 358 331 stream->sess = NULL; 359 332 stream->need_sync = false; … … 379 352 stream->error = false; 380 353 stream->eof = false; 381 stream->ops = &stdio_vfs_ops; 382 stream->arg = NULL; 354 stream->kio = false; 383 355 stream->sess = NULL; 384 356 stream->need_sync = false; … … 463 435 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream) 464 436 { 465 return stream->ops->read(buf, size, nmemb, stream); 437 errno_t rc; 438 size_t nread; 439 440 if (size == 0 || nmemb == 0) 441 return 0; 442 443 rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread); 444 if (rc != EOK) { 445 errno = rc; 446 stream->error = true; 447 } else if (nread == 0) { 448 stream->eof = true; 449 } 450 451 return (nread / size); 466 452 } 467 453 … … 478 464 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) 479 465 { 466 errno_t rc; 480 467 size_t nwritten; 481 468 … … 483 470 return 0; 484 471 485 nwritten = stream->ops->write(buf, size, nmemb, stream); 472 if (stream->kio) { 473 rc = kio_write(buf, size * nmemb, &nwritten); 474 if (rc != EOK) { 475 stream->error = true; 476 nwritten = 0; 477 } 478 } else { 479 rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, 480 &nwritten); 481 if (rc != EOK) { 482 errno = rc; 483 stream->error = true; 484 } 485 } 486 486 487 487 if (nwritten > 0) … … 905 905 } 906 906 907 if (stream->need_sync) { 907 if (stream->kio) { 908 kio_update(); 909 return 0; 910 } 911 912 if ((stream->fd >= 0) && (stream->need_sync)) { 913 errno_t rc; 914 908 915 /** 909 916 * Better than syncing always, but probably still not the 910 917 * right thing to do. 911 918 */ 912 if (stream->ops->flush(stream) == EOF) 919 stream->need_sync = false; 920 rc = vfs_sync(stream->fd); 921 if (rc != EOK) { 922 errno = rc; 913 923 return EOF; 914 915 stream->need_sync = false; 924 } 925 926 return 0; 916 927 } 917 928 … … 937 948 int fileno(FILE *stream) 938 949 { 939 if (stream-> ops != &stdio_vfs_ops) {950 if (stream->kio) { 940 951 errno = EBADF; 941 952 return EOF; … … 967 978 } 968 979 969 /** Read from KIO stream. */970 static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)971 {972 stream->eof = true;973 return 0;974 }975 976 /** Write to KIO stream. */977 static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,978 FILE *stream)979 {980 errno_t rc;981 size_t nwritten;982 983 rc = kio_write(buf, size * nmemb, &nwritten);984 if (rc != EOK) {985 stream->error = true;986 nwritten = 0;987 }988 989 return nwritten / size;990 }991 992 /** Flush KIO stream. */993 static int stdio_kio_flush(FILE *stream)994 {995 kio_update();996 return 0;997 }998 999 /** Read from VFS stream. */1000 static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)1001 {1002 errno_t rc;1003 size_t nread;1004 1005 if (size == 0 || nmemb == 0)1006 return 0;1007 1008 rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);1009 if (rc != EOK) {1010 errno = rc;1011 stream->error = true;1012 } else if (nread == 0) {1013 stream->eof = true;1014 }1015 1016 return (nread / size);1017 }1018 1019 /** Write to VFS stream. */1020 static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,1021 FILE *stream)1022 {1023 errno_t rc;1024 size_t nwritten;1025 1026 rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);1027 if (rc != EOK) {1028 errno = rc;1029 stream->error = true;1030 }1031 1032 return nwritten / size;1033 }1034 1035 /** Flush VFS stream. */1036 static int stdio_vfs_flush(FILE *stream)1037 {1038 errno_t rc;1039 1040 rc = vfs_sync(stream->fd);1041 if (rc != EOK) {1042 errno = rc;1043 return EOF;1044 }1045 1046 return 0;1047 }1048 1049 980 /** @} 1050 981 */
Note:
See TracChangeset
for help on using the changeset viewer.