Changeset 1978a5f in mainline for uspace/lib/posix/stdio.c
- Timestamp:
- 2011-07-01T23:39:27Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 557a747
- Parents:
- fb872c1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdio.c
rfb872c1 r1978a5f 38 38 #include <assert.h> 39 39 #include <errno.h> 40 #include <bool.h> 40 41 41 42 #include "internal/common.h" … … 44 45 #include "string.h" 45 46 #include "libc/str.h" 46 47 /* not the best of solutions, but freopen will eventually 47 #include "sys/types.h" 48 49 /* not the best of solutions, but freopen and ungetc will eventually 48 50 * need to be implemented in libc anyway 49 51 */ … … 52 54 /** Clears the stream's error and end-of-file indicators. 53 55 * 54 * @param stream 56 * @param stream Stream whose indicators shall be cleared. 55 57 */ 56 58 void posix_clearerr(FILE *stream) … … 61 63 62 64 /** 63 * 64 * @param s 65 * @return 65 * Generate a pathname for the controlling terminal. 66 * 67 * @param s Allocated buffer to which the pathname shall be put. 68 * @return Either s or static location filled with the requested pathname. 66 69 */ 67 70 char *posix_ctermid(char *s) … … 81 84 82 85 /** 83 * 84 * @param c 86 * Push byte back into input stream. 87 * 88 * @param c Byte to be pushed back. 89 * @param stream Stream to where the byte shall be pushed. 90 * @return Provided byte on succes or EOF if not possible. 91 */ 92 int posix_ungetc(int c, FILE *stream) 93 { 94 uint8_t b = (uint8_t) c; 95 96 bool can_unget = 97 /* Provided character is legal. */ 98 c != EOF && 99 /* Stream is consistent. */ 100 !stream->error && 101 /* Stream is buffered. */ 102 stream->btype != _IONBF && 103 /* Last operation on the stream was a read operation. */ 104 stream->buf_state == _bs_read && 105 /* Stream buffer is already allocated (i.e. there was already carried 106 * out either write or read operation on the stream). This is probably 107 * redundant check but let's be safe. */ 108 stream->buf != NULL && 109 /* There is still space in the stream to retreat. POSIX demands the 110 * possibility to unget at least 1 character. It should be always 111 * possible, assuming the last operation on the stream read at least 1 112 * character, because the buffer is refilled in the lazily manner. */ 113 stream->buf_tail > stream->buf; 114 115 if (can_unget) { 116 --stream->buf_tail; 117 stream->buf_tail[0] = b; 118 stream->eof = false; 119 return (int) b; 120 } else { 121 return EOF; 122 } 123 } 124 125 /** 126 * 127 * @param lineptr 128 * @param n 129 * @param delimiter 85 130 * @param stream 86 * @return 87 */ 88 int posix_ungetc(int c, FILE *stream) 131 * @return 132 */ 133 ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n, 134 int delimiter, FILE *restrict stream) 89 135 { 90 136 // TODO … … 92 138 } 93 139 94 ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n, 95 int delimiter, FILE *restrict stream) 140 /** 141 * 142 * @param lineptr 143 * @param n 144 * @param stream 145 * @return 146 */ 147 ssize_t posix_getline(char **restrict lineptr, size_t *restrict n, 148 FILE *restrict stream) 96 149 { 97 150 // TODO … … 99 152 } 100 153 101 ssize_t posix_getline(char **restrict lineptr, size_t *restrict n, 102 FILE *restrict stream) 103 { 104 // TODO 105 not_implemented(); 106 } 107 108 /** 109 * 110 * @param filename 111 * @param mode 112 * @param stream 113 * @return 154 /** 155 * Reopen a file stream. 156 * 157 * @param filename Pathname of a file to be reopened or NULL for changing 158 * the mode of the stream. 159 * @param mode Mode to be used for reopening the file or changing current 160 * mode of the stream. 161 * @param stream Current stream associated with the opened file. 162 * @return On success, either a stream of the reopened file or the provided 163 * stream with a changed mode. NULL otherwise. 114 164 */ 115 165 FILE *posix_freopen( … … 159 209 } 160 210 211 /** 212 * 213 * @param buf 214 * @param size 215 * @param mode 216 * @return 217 */ 161 218 FILE *posix_fmemopen(void *restrict buf, size_t size, 162 219 const char *restrict mode) … … 166 223 } 167 224 225 /** 226 * 227 * @param bufp 228 * @param sizep 229 * @return 230 */ 168 231 FILE *posix_open_memstream(char **bufp, size_t *sizep) 169 232 { … … 173 236 174 237 /** 175 * 176 * @param s 238 * Write error messages to standard error. 239 * 240 * @param s Error message. 177 241 */ 178 242 void posix_perror(const char *s) … … 217 281 218 282 /** 219 * 220 * @param stream 221 * @param offset 222 * @param whence 223 * @return 283 * Reposition a file-position indicator in a stream. 284 * 285 * @param stream Stream to seek in. 286 * @param offset Direction and amount of bytes to seek. 287 * @param whence From where to seek. 288 * @return Zero on success, -1 otherwise. 224 289 */ 225 290 int posix_fseek(FILE *stream, long offset, int whence) … … 229 294 230 295 /** 231 * 232 * @param stream 233 * @param offset 234 * @param whence 235 * @return 296 * Reposition a file-position indicator in a stream. 297 * 298 * @param stream Stream to seek in. 299 * @param offset Direction and amount of bytes to seek. 300 * @param whence From where to seek. 301 * @return Zero on success, -1 otherwise. 236 302 */ 237 303 int posix_fseeko(FILE *stream, posix_off_t offset, int whence) … … 241 307 242 308 /** 243 * 244 * @param stream 245 * @return 309 * Discover current file offset in a stream. 310 * 311 * @param stream Stream for which the offset shall be retrieved. 312 * @return Current offset or -1 if not possible. 246 313 */ 247 314 long posix_ftell(FILE *stream) … … 251 318 252 319 /** 253 * 254 * @param stream 255 * @return 320 * Discover current file offset in a stream. 321 * 322 * @param stream Stream for which the offset shall be retrieved. 323 * @return Current offset or -1 if not possible. 256 324 */ 257 325 posix_off_t posix_ftello(FILE *stream) … … 260 328 } 261 329 330 /** 331 * Print formatted output to the opened file. 332 * 333 * @param fildes File descriptor of the opened file. 334 * @param format Format description. 335 * @return Either the number of printed characters or negative value on error. 336 */ 262 337 int posix_dprintf(int fildes, const char *restrict format, ...) 263 338 { … … 269 344 } 270 345 346 /** 347 * Write ordinary string to the opened file. 348 * 349 * @param str String to be written. 350 * @param size Size of the string (in bytes).. 351 * @param fd File descriptor of the opened file. 352 * @return The number of written characters. 353 */ 271 354 static int _dprintf_str_write(const char *str, size_t size, void *fd) 272 355 { … … 275 358 } 276 359 360 /** 361 * Write wide string to the opened file. 362 * 363 * @param str String to be written. 364 * @param size Size of the string (in bytes). 365 * @param fd File descriptor of the opened file. 366 * @return The number of written characters. 367 */ 277 368 static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd) 278 369 { … … 299 390 } 300 391 392 /** 393 * Print formatted output to the opened file. 394 * 395 * @param fildes File descriptor of the opened file. 396 * @param format Format description. 397 * @param ap Print arguments. 398 * @return Either the number of printed characters or negative value on error. 399 */ 301 400 int posix_vdprintf(int fildes, const char *restrict format, va_list ap) 302 401 { … … 311 410 312 411 /** 313 * 314 * @param s 315 * @param format 316 * @param ... 317 * @return 412 * Print formatted output to the string. 413 * 414 * @param s Output string. 415 * @param format Format description. 416 * @return Either the number of printed characters (excluding null byte) or 417 * negative value on error. 318 418 */ 319 419 int posix_sprintf(char *s, const char *format, ...) … … 327 427 328 428 /** 329 * 330 * @param s 331 * @param format 332 * @param ap 333 * @return 429 * Print formatted output to the string. 430 * 431 * @param s Output string. 432 * @param format Format description. 433 * @param ap Print arguments. 434 * @return Either the number of printed characters (excluding null byte) or 435 * negative value on error. 334 436 */ 335 437 int posix_vsprintf(char *s, const char *format, va_list ap) … … 339 441 340 442 /** 341 * 342 * @param stream343 * @param format344 * @param ...345 * @return 443 * Convert formatted input from the stream. 444 * 445 * @param stream Input stream. 446 * @param format Format description. 447 * @return The number of converted output items or EOF on failure. 346 448 */ 347 449 int posix_fscanf(FILE *restrict stream, const char *restrict format, ...) … … 354 456 } 355 457 458 /** 459 * Convert formatted input from the stream. 460 * 461 * @param stream Input stream. 462 * @param format Format description. 463 * @param arg Output items. 464 * @return The number of converted output items or EOF on failure. 465 */ 356 466 int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg) 357 467 { … … 361 471 362 472 /** 363 * 364 * @param format365 * @param ...366 * @return 473 * Convert formatted input from the standard input. 474 * 475 * @param format Format description. 476 * @return The number of converted output items or EOF on failure. 367 477 */ 368 478 int posix_scanf(const char *restrict format, ...) … … 376 486 377 487 /** 378 * 379 * @param format 380 * @param arg 381 * @return 488 * Convert formatted input from the standard input. 489 * 490 * @param format Format description. 491 * @param arg Output items. 492 * @return The number of converted output items or EOF on failure. 382 493 */ 383 494 int posix_vscanf(const char *restrict format, va_list arg) … … 387 498 388 499 /** 389 * 390 * @param s391 * @param format392 * @param ...393 * @return 500 * Convert formatted input from the string. 501 * 502 * @param s Input string. 503 * @param format Format description. 504 * @return The number of converted output items or EOF on failure. 394 505 */ 395 506 int posix_sscanf(const char *s, const char *format, ...) … … 403 514 404 515 /** 405 * 406 * @param s 407 * @param format 408 * @param arg 409 * @return 516 * Convert formatted input from the string. 517 * 518 * @param s Input string. 519 * @param format Format description. 520 * @param arg Output items. 521 * @return The number of converted output items or EOF on failure. 410 522 */ 411 523 int posix_vsscanf( … … 416 528 } 417 529 530 /** 531 * Acquire file stream for the thread. 532 * 533 * @param file File stream to lock. 534 */ 418 535 void posix_flockfile(FILE *file) 419 536 { … … 421 538 } 422 539 540 /** 541 * Acquire file stream for the thread (non-blocking). 542 * 543 * @param file File stream to lock. 544 * @return Zero for success and non-zero if the lock cannot be acquired. 545 */ 423 546 int posix_ftrylockfile(FILE *file) 424 547 { … … 427 550 } 428 551 552 /** 553 * Relinquish the ownership of the locked file stream. 554 * 555 * @param file File stream to unlock. 556 */ 429 557 void posix_funlockfile(FILE *file) 430 558 { … … 432 560 } 433 561 562 /** 563 * Get a byte from a stream (thread-unsafe). 564 * 565 * @param stream Input file stream. 566 * @return Either read byte or EOF. 567 */ 434 568 int posix_getc_unlocked(FILE *stream) 435 569 { … … 437 571 } 438 572 573 /** 574 * Get a byte from the standard input stream (thread-unsafe). 575 * 576 * @return Either read byte or EOF. 577 */ 439 578 int posix_getchar_unlocked(void) 440 579 { … … 442 581 } 443 582 583 /** 584 * Put a byte on a stream (thread-unsafe). 585 * 586 * @param c Byte to output. 587 * @param stream Output file stream. 588 * @return Either written byte or EOF. 589 */ 444 590 int posix_putc_unlocked(int c, FILE *stream) 445 591 { … … 447 593 } 448 594 595 /** 596 * Put a byte on the standard output stream (thread-unsafe). 597 * 598 * @param c Byte to output. 599 * @return Either written byte or EOF. 600 */ 449 601 int posix_putchar_unlocked(int c) 450 602 { … … 453 605 454 606 /** 455 * 456 * @param path 457 * @return 607 * Remove a file. 608 * 609 * @param path Pathname of the file that shall be removed. 610 * @return Zero on success, -1 otherwise. 458 611 */ 459 612 int posix_remove(const char *path)
Note:
See TracChangeset
for help on using the changeset viewer.