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

97 lines
2.3 KiB
C

#include "descriptor.h"
#include <errno.h>
#include <unistd.h>
static dr_slot *stream_slot(dr_bank *bank, int slot)
{
if (bank == NULL || slot < 0 || slot >= DR_SLOTS) {
errno = EINVAL;
return NULL;
}
if (bank->slots[slot].fd < 0) {
errno = EBADF;
return NULL;
}
return &bank->slots[slot];
}
static int stream_span(const void *bytes, size_t length)
{
if (length > DR_LIMIT || (length > 0 && bytes == NULL)) {
errno = EINVAL;
return 0;
}
return 1;
}
ssize_t dr_take(dr_bank *bank, int slot, void *bytes, size_t length)
{
if (!stream_span(bytes, length)) {
return -1;
}
dr_slot *entry = stream_slot(bank, slot);
if (entry == NULL) {
return -1;
}
if (lseek(entry->fd, (off_t)entry->read_bytes, SEEK_SET) < 0) {
return -1;
}
ssize_t result;
do {
result = read(entry->fd, bytes, length);
} while (result < 0 && errno == EINTR);
if (result >= 0) {
entry->read_bytes += (unsigned long)result;
}
return result;
}
ssize_t dr_emit(dr_bank *bank, int slot, const void *bytes, size_t length)
{
if (!stream_span(bytes, length)) {
return -1;
}
dr_slot *entry = stream_slot(bank, slot);
if (entry == NULL) {
return -1;
}
off_t position = lseek(entry->fd, 0, SEEK_CUR);
if (position < 0) {
return -1;
}
if (position > DR_LIMIT || (off_t)length > DR_LIMIT - position) {
errno = EFBIG;
return -1;
}
size_t offset = 0;
const unsigned char *source = bytes;
while (offset < length) {
ssize_t count = write(entry->fd, source + offset, length - offset);
if (count < 0 && errno == EINTR) {
continue;
}
if (count <= 0) {
if (count == 0) {
errno = EIO;
}
return -1;
}
offset += (size_t)count;
}
entry->written_bytes += (unsigned long)offset;
return (ssize_t)offset;
}
off_t dr_seek(dr_bank *bank, int slot, off_t position)
{
if (position < 0 || position > DR_LIMIT) {
errno = EINVAL;
return -1;
}
dr_slot *entry = stream_slot(bank, slot);
if (entry == NULL) {
return -1;
}
return lseek(entry->fd, position, SEEK_SET);
}