amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

64 lines
1.7 KiB
C

#include "stream.h"
#include <errno.h>
typedef struct receive_result {
int status;
size_t bytes;
} receive_result;
static receive_result receive_segments(stream *value, size_t remaining)
{
receive_result outcome = { STREAM_OK, 0 };
unsigned char arrived[STREAM_CAPACITY];
size_t count;
ssize_t result;
int error;
if (remaining == 0) {
return outcome;
}
count = ring_space(&value->incoming);
if (count == 0) {
outcome.status = STREAM_FULL;
return outcome;
}
if (count > remaining) {
count = remaining;
}
do {
result = value->port.recv_bytes(value->port.context, arrived, count, 0);
error = errno;
} while (result < 0 && error == EINTR);
if (result > 0) {
size_t committed = (size_t)result;
ring_append(&value->incoming, arrived, committed);
value->received += committed;
outcome = receive_segments(value, remaining - committed);
outcome.bytes += committed;
} else if (result == 0) {
value->read_end = 1;
outcome.status = STREAM_END;
} else if (error == EAGAIN) {
outcome.status = STREAM_WAIT;
} else {
value->read_end = 1;
value->last_error = error;
outcome.status = STREAM_FAULT;
}
return outcome;
}
int stream_receive(stream *value, size_t quota, size_t *progress)
{
receive_result outcome;
*progress = 0;
if (quota > STREAM_QUOTA) {
return STREAM_INVALID;
}
if (!value->opened) {
return STREAM_CLOSED;
}
if (value->read_end) {
return STREAM_END;
}
outcome = receive_segments(value, quota);
*progress = outcome.bytes;
return outcome.status;
}