Changeset 7f9df7b9 in mainline for uspace/lib/posix/src/stdio.c
- Timestamp:
- 2018-01-22T22:42:57Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7a08c70
- Parents:
- e0f47f5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/stdio.c
re0f47f5 r7f9df7b9 34 34 */ 35 35 36 #define LIBPOSIX_INTERNAL37 #define __POSIX_DEF__(x) posix_##x38 39 36 #include "internal/common.h" 40 37 #include "posix/stdio.h" … … 55 52 #include "libc/adt/list.h" 56 53 57 /** Clears the stream's error and end-of-file indicators.58 *59 * @param stream Stream whose indicators shall be cleared.60 */61 void posix_clearerr(FILE *stream)62 {63 clearerr(stream);64 }65 66 54 /** 67 55 * Generate a pathname for the controlling terminal. … … 70 58 * @return Either s or static location filled with the requested pathname. 71 59 */ 72 char * posix_ctermid(char *s)60 char *ctermid(char *s) 73 61 { 74 62 /* Currently always returns an error value (empty string). */ … … 83 71 s[0] = '\0'; 84 72 return s; 85 }86 87 /**88 * Put a string on the stream.89 *90 * @param s String to be written.91 * @param stream Output stream.92 * @return Non-negative on success, EOF on failure.93 */94 int posix_fputs(const char *restrict s, FILE *restrict stream)95 {96 return fputs(s, stream);97 }98 99 /**100 * Push byte back into input stream.101 *102 * @param c Byte to be pushed back.103 * @param stream Stream to where the byte shall be pushed.104 * @return Provided byte on success or EOF if not possible.105 */106 int posix_ungetc(int c, FILE *stream)107 {108 return ungetc(c, stream);109 73 } 110 74 … … 122 86 * or -1 on error (set in errno). 123 87 */ 124 ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,88 ssize_t getdelim(char **restrict lineptr, size_t *restrict n, 125 89 int delimiter, FILE *restrict stream) 126 90 { … … 194 158 * or -1 on error (set in errno). 195 159 */ 196 ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,160 ssize_t getline(char **restrict lineptr, size_t *restrict n, 197 161 FILE *restrict stream) 198 162 { 199 return posix_getdelim(lineptr, n, '\n', stream); 200 } 201 202 /** 203 * Reopen a file stream. 204 * 205 * @param filename Pathname of a file to be reopened or NULL for changing 206 * the mode of the stream. 207 * @param mode Mode to be used for reopening the file or changing current 208 * mode of the stream. 209 * @param stream Current stream associated with the opened file. 210 * @return On success, either a stream of the reopened file or the provided 211 * stream with a changed mode. NULL otherwise. 212 */ 213 FILE *posix_freopen(const char *restrict filename, 214 const char *restrict mode, FILE *restrict stream) 215 { 216 return freopen(filename, mode, stream); 163 return getdelim(lineptr, n, '\n', stream); 217 164 } 218 165 … … 222 169 * @param s Error message. 223 170 */ 224 void p osix_perror(const char *s)171 void perror(const char *s) 225 172 { 226 173 if (s == NULL || s[0] == '\0') { 227 fprintf(stderr, "%s\n", posix_strerror(errno));174 fprintf(stderr, "%s\n", strerror(errno)); 228 175 } else { 229 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));176 fprintf(stderr, "%s: %s\n", s, strerror(errno)); 230 177 } 231 178 } … … 237 184 * @return Zero on success, non-zero (with errno set) on failure 238 185 */ 239 int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)186 int fsetpos(FILE *stream, const fpos_t *pos) 240 187 { 241 188 return fseek64(stream, pos->offset, SEEK_SET); … … 248 195 * @return Zero on success, non-zero (with errno set) on failure 249 196 */ 250 int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)197 int fgetpos(FILE *restrict stream, fpos_t *restrict pos) 251 198 { 252 199 off64_t ret = ftell64(stream); … … 267 214 * @return Zero on success, -1 otherwise. 268 215 */ 269 int posix_fseek(FILE *stream, long offset, int whence) 270 { 271 return fseek(stream, offset, whence); 272 } 273 274 /** 275 * Reposition a file-position indicator in a stream. 276 * 277 * @param stream Stream to seek in. 278 * @param offset Direction and amount of bytes to seek. 279 * @param whence From where to seek. 280 * @return Zero on success, -1 otherwise. 281 */ 282 int posix_fseeko(FILE *stream, posix_off_t offset, int whence) 216 int fseeko(FILE *stream, off_t offset, int whence) 283 217 { 284 218 return fseek64(stream, offset, whence); … … 291 225 * @return Current offset or -1 if not possible. 292 226 */ 293 long posix_ftell(FILE *stream) 294 { 295 return ftell(stream); 296 } 297 298 /** 299 * Discover current file offset in a stream. 300 * 301 * @param stream Stream for which the offset shall be retrieved. 302 * @return Current offset or -1 if not possible. 303 */ 304 posix_off_t posix_ftello(FILE *stream) 227 off_t ftello(FILE *stream) 305 228 { 306 229 return ftell64(stream); … … 308 231 309 232 /** 310 * Discard prefetched data or write unwritten data.311 *312 * @param stream Stream that shall be flushed.313 * @return Zero on success, EOF on failure.314 */315 int posix_fflush(FILE *stream)316 {317 return fflush(stream);318 }319 320 /**321 233 * Print formatted output to the opened file. 322 234 * … … 325 237 * @return Either the number of printed characters or negative value on error. 326 238 */ 327 int posix_dprintf(int fildes, const char *restrict format, ...)239 int dprintf(int fildes, const char *restrict format, ...) 328 240 { 329 241 va_list list; 330 242 va_start(list, format); 331 int result = posix_vdprintf(fildes, format, list);243 int result = vdprintf(fildes, format, list); 332 244 va_end(list); 333 245 return result; … … 392 304 * @return Either the number of printed characters or negative value on error. 393 305 */ 394 int posix_vdprintf(int fildes, const char *restrict format, va_list ap)306 int vdprintf(int fildes, const char *restrict format, va_list ap) 395 307 { 396 308 printf_spec_t spec = { … … 411 323 * negative value on error. 412 324 */ 413 int posix_sprintf(char *s, const char *restrict format, ...)325 int sprintf(char *s, const char *restrict format, ...) 414 326 { 415 327 va_list list; 416 328 va_start(list, format); 417 int result = posix_vsprintf(s, format, list);329 int result = vsprintf(s, format, list); 418 330 va_end(list); 419 331 return result; … … 429 341 * negative value on error. 430 342 */ 431 int posix_vsprintf(char *s, const char *restrict format, va_list ap)343 int vsprintf(char *s, const char *restrict format, va_list ap) 432 344 { 433 345 return vsnprintf(s, STR_NO_LIMIT, format, ap); … … 441 353 * @return The number of converted output items or EOF on failure. 442 354 */ 443 int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)355 int fscanf(FILE *restrict stream, const char *restrict format, ...) 444 356 { 445 357 va_list list; 446 358 va_start(list, format); 447 int result = posix_vfscanf(stream, format, list);359 int result = vfscanf(stream, format, list); 448 360 va_end(list); 449 361 return result; … … 456 368 * @return The number of converted output items or EOF on failure. 457 369 */ 458 int posix_scanf(const char *restrict format, ...)370 int scanf(const char *restrict format, ...) 459 371 { 460 372 va_list list; 461 373 va_start(list, format); 462 int result = posix_vscanf(format, list);374 int result = vscanf(format, list); 463 375 va_end(list); 464 376 return result; … … 472 384 * @return The number of converted output items or EOF on failure. 473 385 */ 474 int posix_vscanf(const char *restrict format, va_list arg)475 { 476 return posix_vfscanf(stdin, format, arg);386 int vscanf(const char *restrict format, va_list arg) 387 { 388 return vfscanf(stdin, format, arg); 477 389 } 478 390 … … 484 396 * @return The number of converted output items or EOF on failure. 485 397 */ 486 int posix_sscanf(const char *restrict s, const char *restrict format, ...)398 int sscanf(const char *restrict s, const char *restrict format, ...) 487 399 { 488 400 va_list list; 489 401 va_start(list, format); 490 int result = posix_vsscanf(s, format, list);402 int result = vsscanf(s, format, list); 491 403 va_end(list); 492 404 return result; … … 498 410 * @param file File stream to lock. 499 411 */ 500 void posix_flockfile(FILE *file)412 void flockfile(FILE *file) 501 413 { 502 414 /* dummy */ … … 509 421 * @return Zero for success and non-zero if the lock cannot be acquired. 510 422 */ 511 int posix_ftrylockfile(FILE *file)423 int ftrylockfile(FILE *file) 512 424 { 513 425 /* dummy */ … … 520 432 * @param file File stream to unlock. 521 433 */ 522 void posix_funlockfile(FILE *file)434 void funlockfile(FILE *file) 523 435 { 524 436 /* dummy */ … … 531 443 * @return Either read byte or EOF. 532 444 */ 533 int posix_getc_unlocked(FILE *stream)445 int getc_unlocked(FILE *stream) 534 446 { 535 447 return getc(stream); … … 541 453 * @return Either read byte or EOF. 542 454 */ 543 int posix_getchar_unlocked(void)455 int getchar_unlocked(void) 544 456 { 545 457 return getchar(); … … 553 465 * @return Either written byte or EOF. 554 466 */ 555 int p osix_putc_unlocked(int c, FILE *stream)467 int putc_unlocked(int c, FILE *stream) 556 468 { 557 469 return putc(c, stream); … … 564 476 * @return Either written byte or EOF. 565 477 */ 566 int p osix_putchar_unlocked(int c)478 int putchar_unlocked(int c) 567 479 { 568 480 return putchar(c); 569 }570 571 /**572 * Remove a file or directory.573 *574 * @param path Pathname of the file that shall be removed.575 * @return Zero on success, -1 (with errno set) otherwise.576 */577 int posix_remove(const char *path)578 {579 if (failed(vfs_unlink_path(path)))580 return -1;581 else582 return 0;583 }584 585 /**586 * Rename a file or directory.587 *588 * @param old Old pathname.589 * @param new New pathname.590 * @return Zero on success, -1 (with errno set) otherwise.591 */592 int posix_rename(const char *old, const char *new)593 {594 if (failed(vfs_rename_path(old, new)))595 return -1;596 else597 return 0;598 481 } 599 482 … … 604 487 * @return The value of s on success, NULL on failure. 605 488 */ 606 char * posix_tmpnam(char *s)607 { 608 assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));489 char *tmpnam(char *s) 490 { 491 assert(L_tmpnam >= strlen("/tmp/tnXXXXXX")); 609 492 610 493 static char buffer[L_tmpnam + 1]; … … 613 496 } 614 497 615 posix_strcpy(s, "/tmp/tnXXXXXX");616 posix_mktemp(s);498 strcpy(s, "/tmp/tnXXXXXX"); 499 mktemp(s); 617 500 618 501 if (*s == '\0') { … … 631 514 * @return Newly allocated unique path for temporary file. NULL on failure. 632 515 */ 633 char * posix_tempnam(const char *dir, const char *pfx)516 char *tempnam(const char *dir, const char *pfx) 634 517 { 635 518 /* Sequence number of the filename. */ 636 519 static int seq = 0; 637 520 638 size_t dir_len = posix_strlen(dir);521 size_t dir_len = strlen(dir); 639 522 if (dir[dir_len - 1] == '/') { 640 523 dir_len--; 641 524 } 642 525 643 size_t pfx_len = posix_strlen(pfx);526 size_t pfx_len = strlen(pfx); 644 527 if (pfx_len > 5) { 645 528 pfx_len = 5; … … 655 538 656 539 char *res_ptr = result; 657 posix_strncpy(res_ptr, dir, dir_len);540 strncpy(res_ptr, dir, dir_len); 658 541 res_ptr += dir_len; 659 posix_strncpy(res_ptr, pfx, pfx_len);542 strncpy(res_ptr, pfx, pfx_len); 660 543 res_ptr += pfx_len; 661 544 … … 666 549 errno = EOK; 667 550 /* Check if the file exists. */ 668 if ( posix_access(result, F_OK) == -1) {551 if (access(result, F_OK) == -1) { 669 552 if (errno == ENOENT) { 670 553 errno = orig_errno; … … 694 577 * @return Newly allocated unique path for temporary file. NULL on failure. 695 578 */ 696 FILE * posix_tmpfile(void)579 FILE *tmpfile(void) 697 580 { 698 581 char filename[] = "/tmp/tfXXXXXX"; 699 int fd = posix_mkstemp(filename);582 int fd = mkstemp(filename); 700 583 if (fd == -1) { 701 584 /* errno set by mkstemp(). */ … … 704 587 705 588 /* Unlink the created file, so that it's removed on close(). */ 706 posix_unlink(filename);589 unlink(filename); 707 590 return fdopen(fd, "w+"); 708 591 }
Note:
See TracChangeset
for help on using the changeset viewer.