Skip to content

Commit

Permalink
examples/pipe: fix write usage
Browse files Browse the repository at this point in the history
write returns in case some bytes were written but not everything can
fit.

This wasn't the case in NuttX but commit
d0680fd1bc51b7ead0b068fb24a31a144d22dc6c introduced this standard
behavior.
  • Loading branch information
Cynerd committed Nov 7, 2024
1 parent 40a5e47 commit 8a806e8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions examples/pipe/transfer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ static void *transfer_writer(pthread_addr_t pvarg)
char buffer[WRITE_SIZE];
int fd = (intptr_t)pvarg;
int ret;
int nbytes;
int i;

printf("transfer_writer: started\n");
Expand All @@ -149,18 +150,23 @@ static void *transfer_writer(pthread_addr_t pvarg)

for (i = 0; i < NWRITES; i++)
{
ret = write(fd, buffer, WRITE_SIZE);
if (ret < 0)
{
fprintf(stderr, \
"transfer_writer: write failed, errno=%d\n", errno);
return (void *)(uintptr_t)1;
}
else if (ret != WRITE_SIZE)
for (nbytes = 0; nbytes < WRITE_SIZE; )
{
fprintf(stderr, \
"transfer_writer: Unexpected write size=%d\n", ret);
return (void *)(uintptr_t)2;
ret = write(fd, buffer + nbytes, WRITE_SIZE - nbytes);
if (ret < 0)
{
fprintf(stderr, \
"transfer_writer: write failed, errno=%d\n", errno);
return (void *)(uintptr_t)1;
}
else if (ret == 0)
{
fprintf(stderr, \
"transfer_writer: Unexpected zero write size\n");
return (void *)(uintptr_t)2;
}

nbytes += ret;
}
}

Expand Down

0 comments on commit 8a806e8

Please sign in to comment.