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>
95 lines
2.2 KiB
C
95 lines
2.2 KiB
C
#include "descriptor.h"
|
|
#include <errno.h>
|
|
#include <string.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;
|
|
}
|
|
ssize_t result;
|
|
do {
|
|
memset(bytes, 0, length); result = (ssize_t)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);
|
|
}
|