Changes in uspace/lib/c/generic/io/io.c [a35b458:1b20da0] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
ra35b458 r1b20da0 119 119 list_append(&stdin->link, &files); 120 120 } 121 121 122 122 int outfd = inbox_get("stdout"); 123 123 if (outfd >= 0) { … … 133 133 list_append(&stdout->link, &files); 134 134 } 135 135 136 136 int errfd = inbox_get("stderr"); 137 137 if (errfd >= 0) { … … 165 165 return false; 166 166 } 167 167 168 168 if ((*mp == 'b') || (*mp == 't')) 169 169 mp++; 170 170 171 171 bool plus; 172 172 if (*mp == '+') { … … 175 175 } else 176 176 plus = false; 177 177 178 178 if (*mp != 0) { 179 179 errno = EINVAL; … … 183 183 *create = false; 184 184 *truncate = false; 185 185 186 186 /* Parse first character of fmode and determine mode for vfs_open(). */ 187 187 switch (fmode[0]) { … … 209 209 return false; 210 210 } 211 211 212 212 return true; 213 213 } … … 241 241 { 242 242 /* FIXME: Use more complex rules for setting buffering options. */ 243 243 244 244 switch (stream->fd) { 245 245 case 1: … … 259 259 { 260 260 assert(stream->buf == NULL); 261 261 262 262 stream->buf = malloc(stream->buf_size); 263 263 if (stream->buf == NULL) { … … 265 265 return EOF; 266 266 } 267 267 268 268 stream->buf_head = stream->buf; 269 269 stream->buf_tail = stream->buf; … … 285 285 if (!parse_mode(fmode, &mode, &create, &truncate)) 286 286 return NULL; 287 287 288 288 /* Open file. */ 289 289 FILE *stream = malloc(sizeof(FILE)); … … 311 311 return NULL; 312 312 } 313 313 314 314 if (truncate) { 315 315 rc = vfs_resize(file, 0); … … 331 331 _setvbuf(stream); 332 332 stream->ungetc_chars = 0; 333 333 334 334 list_append(&stream->link, &files); 335 335 336 336 return stream; 337 337 } … … 345 345 return NULL; 346 346 } 347 347 348 348 stream->fd = fd; 349 349 stream->pos = 0; … … 355 355 _setvbuf(stream); 356 356 stream->ungetc_chars = 0; 357 357 358 358 list_append(&stream->link, &files); 359 359 360 360 return stream; 361 361 } … … 365 365 { 366 366 errno_t rc = 0; 367 367 368 368 fflush(stream); 369 369 370 370 if (stream->sess != NULL) 371 371 async_hangup(stream->sess); 372 372 373 373 if (stream->fd >= 0) 374 374 rc = vfs_put(stream->fd); 375 375 376 376 list_remove(&stream->link); 377 377 378 378 if (rc != EOK) { 379 379 errno = rc; 380 380 return EOF; 381 381 } 382 382 383 383 return 0; 384 384 } … … 387 387 { 388 388 int rc = _fclose_nofree(stream); 389 389 390 390 if ((stream != &stdin_null) 391 391 && (stream != &stdout_kio) 392 392 && (stream != &stderr_kio)) 393 393 free(stream); 394 394 395 395 return rc; 396 396 } … … 399 399 { 400 400 FILE *nstr; 401 401 402 402 if (path == NULL) { 403 403 /* Changing mode is not supported */ 404 404 return NULL; 405 405 } 406 406 407 407 (void) _fclose_nofree(stream); 408 408 nstr = fopen(path, mode); … … 411 411 return NULL; 412 412 } 413 413 414 414 list_remove(&nstr->link); 415 415 *stream = *nstr; 416 416 list_append(&stream->link, &files); 417 417 418 418 free(nstr); 419 419 420 420 return stream; 421 421 } … … 659 659 return 0; /* Errno set by _fallocbuf(). */ 660 660 } 661 661 662 662 data = (uint8_t *) buf; 663 663 bytes_left = size * nmemb; 664 664 total_written = 0; 665 665 need_flush = false; 666 666 667 667 while ((!stream->error) && (bytes_left > 0)) { 668 668 buf_free = stream->buf_size - (stream->buf_head - stream->buf); … … 671 671 else 672 672 now = bytes_left; 673 673 674 674 for (i = 0; i < now; i++) { 675 675 b = data[i]; 676 676 stream->buf_head[i] = b; 677 677 678 678 if ((b == '\n') && (stream->btype == _IOLBF)) 679 679 need_flush = true; 680 680 } 681 681 682 682 data += now; 683 683 stream->buf_head += now; … … 686 686 total_written += now; 687 687 stream->buf_state = _bs_write; 688 688 689 689 if (buf_free == 0) { 690 690 /* Only need to drain buffer. */ … … 697 697 if (need_flush) 698 698 fflush(stream); 699 699 700 700 return (total_written / size); 701 701 } … … 705 705 char buf[STR_BOUNDS(1)]; 706 706 size_t sz = 0; 707 707 708 708 if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) { 709 709 size_t wr = fwrite(buf, 1, sz, stream); 710 710 711 711 if (wr < sz) 712 712 return EOF; 713 713 714 714 return (int) c; 715 715 } 716 716 717 717 return EOF; 718 718 } … … 739 739 { 740 740 char c; 741 741 742 742 /* This could be made faster by only flushing when needed. */ 743 743 if (stdout) … … 745 745 if (stderr) 746 746 fflush(stderr); 747 747 748 748 if (fread(&c, sizeof(char), 1, stream) < sizeof(char)) 749 749 return EOF; 750 750 751 751 return (int) c; 752 752 } … … 841 841 if (stream->error) 842 842 return EOF; 843 843 844 844 _fflushbuf(stream); 845 845 if (stream->error) { … … 876 876 if (stream->error) 877 877 return EOF; 878 878 879 879 _fflushbuf(stream); 880 880 if (stream->error) { … … 882 882 return EOF; 883 883 } 884 884 885 885 if (stream->kio) { 886 886 kio_update(); 887 887 return 0; 888 888 } 889 889 890 890 if ((stream->fd >= 0) && (stream->need_sync)) { 891 891 errno_t rc; … … 904 904 return 0; 905 905 } 906 906 907 907 return 0; 908 908 } … … 930 930 return EOF; 931 931 } 932 932 933 933 return stream->fd; 934 934 } … … 939 939 if (stream->sess == NULL) 940 940 stream->sess = vfs_fd_session(stream->fd, iface); 941 941 942 942 return stream->sess; 943 943 } 944 944 945 945 return NULL; 946 946 } … … 952 952 return EOK; 953 953 } 954 954 955 955 return ENOENT; 956 956 }
Note:
See TracChangeset
for help on using the changeset viewer.