* @brief Writes data from a buffer to a file descriptor. * * This system call writes up to `nbyte` bytes of data from the buffer pointed * to by `buf` to the file descriptor specified by `fd`. * * @param fd The file descriptor to write to. This should be a valid file * descriptor obtained through system calls like `open()` or `socket()`. * @param buf A pointer to the buffer contai
| 492 | * @see sys_read(), write() |
| 493 | */ |
| 494 | ssize_t sys_write(int fd, const void *buf, size_t nbyte) |
| 495 | { |
| 496 | #ifdef ARCH_MM_MMU |
| 497 | void *kmem = RT_NULL; |
| 498 | ssize_t ret = -1; |
| 499 | |
| 500 | if (nbyte) |
| 501 | { |
| 502 | if (!lwp_user_accessable((void *)buf, nbyte)) |
| 503 | { |
| 504 | return -EFAULT; |
| 505 | } |
| 506 | |
| 507 | kmem = kmem_get(nbyte); |
| 508 | if (!kmem) |
| 509 | { |
| 510 | return -ENOMEM; |
| 511 | } |
| 512 | |
| 513 | lwp_get_from_user(kmem, (void *)buf, nbyte); |
| 514 | } |
| 515 | |
| 516 | ret = write(fd, kmem, nbyte); |
| 517 | if (ret < 0) |
| 518 | { |
| 519 | ret = GET_ERRNO(); |
| 520 | } |
| 521 | |
| 522 | kmem_put(kmem); |
| 523 | |
| 524 | return ret; |
| 525 | #else |
| 526 | if (!lwp_user_accessable((void *)buf, nbyte)) |
| 527 | { |
| 528 | return -EFAULT; |
| 529 | } |
| 530 | ssize_t ret = write(fd, buf, nbyte); |
| 531 | return (ret < 0 ? GET_ERRNO() : ret); |
| 532 | #endif |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * @brief Repositions the file offset of the open file descriptor. |
no test coverage detected