2024-01-15 13:32:33 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
2024-07-12 15:23:57 -06:00
|
|
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
2024-01-15 13:32:33 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
2024-07-03 14:22:23 -06:00
|
|
|
#include <assert.h> /* assert(3) */
|
2024-01-09 23:43:45 -07:00
|
|
|
#include <errno.h> /* errno */
|
|
|
|
#include <fcntl.h> /* open(2) */
|
|
|
|
#include <stdio.h> /* fprintf(3), stderr */
|
2024-07-03 13:50:24 -06:00
|
|
|
#include <stdlib.h> /* malloc(3), strtol(3), size_t */
|
2024-01-09 23:43:45 -07:00
|
|
|
#include <string.h> /* memcpy(3), memmove(3), memset(3) */
|
2024-07-13 16:56:56 -06:00
|
|
|
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
|
2024-01-09 23:43:45 -07:00
|
|
|
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
|
2024-01-15 13:34:30 -07:00
|
|
|
* optarg, optind, STDIN_FILENO, STDOUT_FILENO */
|
2024-07-12 15:23:57 -06:00
|
|
|
#include <sys/stat.h> /* S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR */
|
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
extern int errno;
|
|
|
|
|
2024-06-26 15:15:37 -06:00
|
|
|
char *program_name = "dj";
|
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
/* dj uses two structures that respectively correspond to the reading and
|
|
|
|
* writing ends of its jockeyed "pipe". User-configurable members are noted
|
|
|
|
* with their relevant options. */
|
2024-07-12 15:23:57 -06:00
|
|
|
struct Io {
|
2024-07-07 20:33:54 -06:00
|
|
|
char *buf; /* buffer */
|
|
|
|
char *fn; /* file name (-io) */
|
|
|
|
size_t bs; /* buffer size (-bB) */
|
2024-01-09 23:43:45 -07:00
|
|
|
size_t bufuse; /* buffer usage */
|
2024-07-07 20:33:54 -06:00
|
|
|
size_t bytes; /* bytes processed */
|
|
|
|
size_t prec; /* partial records processed */
|
|
|
|
size_t rec; /* records processed */
|
|
|
|
long seek; /* remaining bytes to seek/skip (-sS) */
|
2024-07-07 21:13:44 -06:00
|
|
|
int error; /* errno */
|
2024-07-07 20:33:54 -06:00
|
|
|
int fd; /* file descriptor */
|
|
|
|
int fl; /* file opening flags */
|
|
|
|
};
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-03 14:50:50 -06:00
|
|
|
/* To be assigned to main:fmt and used with printio(). */
|
2024-06-26 12:22:33 -06:00
|
|
|
static char *fmt_asv = "%d\037%d\036%d\037%d\035%d\036%d\034";
|
|
|
|
static char *fmt_human = "%d+%d > %d+%d; %d > %d\n";
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
static char *stdin_name = "<stdin>";
|
|
|
|
static char *stdout_name = "<stdout>";
|
2024-06-26 15:15:37 -06:00
|
|
|
|
2024-07-04 19:45:53 -06:00
|
|
|
static int creat_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
|
|
|
|
| S_IWOTH; /* Consistent with touch(1p). */
|
2024-06-26 12:22:33 -06:00
|
|
|
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
|
|
|
|
static int write_flags = O_WRONLY | O_CREAT;
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
2024-07-03 15:47:48 -06:00
|
|
|
/* Macro to check if fd is stdin or stdout */
|
|
|
|
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
static struct Io * Io_read(struct Io *io) {
|
2024-07-04 20:47:30 -06:00
|
|
|
int t;
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-04 19:36:32 -06:00
|
|
|
assert(io->bs > 0);
|
2024-07-04 20:47:30 -06:00
|
|
|
assert(io->bufuse < io->bs);
|
2024-07-04 19:36:32 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if ((t = read(io->fd, &(io->buf)[io->bufuse], io->bs - io->bufuse)) < 0) {
|
2024-07-07 21:13:44 -06:00
|
|
|
io->error = errno;
|
2024-07-05 08:02:09 -06:00
|
|
|
t = 0;
|
2024-07-07 20:33:54 -06:00
|
|
|
}
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-05 08:02:09 -06:00
|
|
|
io->bufuse += t;
|
|
|
|
io->bytes += t;
|
|
|
|
io->prec += (0 < io->bufuse && io->bufuse < io->bs);
|
|
|
|
io->rec += (io->bufuse == io->bs);
|
2024-07-04 19:21:40 -06:00
|
|
|
|
2024-07-05 08:02:09 -06:00
|
|
|
assert(io->bufuse <= io->bs);
|
2024-07-04 19:21:40 -06:00
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
static struct Io * Io_write(struct Io *io) {
|
2024-01-09 23:43:45 -07:00
|
|
|
int t;
|
|
|
|
|
2024-07-04 19:36:32 -06:00
|
|
|
assert(io->bufuse > 0);
|
|
|
|
assert(io->bufuse <= io->bs);
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if ((t = write(io->fd, io->buf, io->bufuse)) < 0) {
|
2024-07-07 21:13:44 -06:00
|
|
|
io->error = errno;
|
2024-07-07 18:14:48 -06:00
|
|
|
t = 0;
|
2024-07-12 16:39:11 -06:00
|
|
|
} else if (t > 0) {
|
2024-07-04 20:05:18 -06:00
|
|
|
memmove(io->buf, &(io->buf)[t], (io->bufuse -= t));
|
2024-07-12 16:39:11 -06:00
|
|
|
}
|
2024-07-04 19:36:32 -06:00
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
io->bytes += t;
|
2024-07-05 08:02:09 -06:00
|
|
|
io->prec += (t > 0 && io->bufuse > 0);
|
|
|
|
io->rec += (t > 0 && io->bufuse == 0);
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
static int oserr(char *e, int n) {
|
2024-07-07 21:13:44 -06:00
|
|
|
fprintf(stderr, "%s: %s: %s\n", program_name, e, strerror(n));
|
2024-01-09 23:43:45 -07:00
|
|
|
return EX_OSERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prints statistics regarding the use of dj, particularly partially and
|
2024-06-26 12:22:33 -06:00
|
|
|
* completely read and written records. */
|
2024-07-12 15:23:57 -06:00
|
|
|
static void fprintio(FILE *stream, char *fmt, struct Io io[2]) {
|
|
|
|
fprintf(
|
|
|
|
stream,
|
|
|
|
fmt,
|
|
|
|
io[0].rec,
|
|
|
|
io[0].prec,
|
|
|
|
io[1].rec,
|
|
|
|
io[1].prec,
|
|
|
|
io[0].bytes,
|
|
|
|
io[1].bytes
|
|
|
|
);
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parses the string s to an integer, returning either the integer or in the
|
|
|
|
* case of an error a negative integer. This is used for argument parsing
|
|
|
|
* (e.g. -B [int]) in dj and no negative integer would be valid anyway. */
|
2024-07-12 15:23:57 -06:00
|
|
|
static long parse(char *s){
|
2024-01-09 23:43:45 -07:00
|
|
|
long r;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
r = strtol(s, &s, 0);
|
2024-07-12 15:32:30 -06:00
|
|
|
return (*s == '\0' && errno == 0) ? r : -1; /* no chars left unparsed */
|
2024-01-09 23:43:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
static int usage(char *s){
|
|
|
|
fprintf(
|
|
|
|
stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n"
|
2024-06-29 05:28:23 -06:00
|
|
|
"\t[-i file] [-b block_size] [-s offset]\n"
|
2024-07-12 15:23:57 -06:00
|
|
|
"\t[-o file] [-B block_size] [-S offset]\n", program_name
|
|
|
|
);
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
int main(int argc, char *argv[]) {
|
2024-07-03 19:22:34 -06:00
|
|
|
int align; /* low 8b used, negative if no alignment is being done */
|
|
|
|
int count; /* 0 if dj(1) runs until no more reads are possible */
|
|
|
|
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
|
|
|
|
size_t i; /* side of io being modified */
|
2024-06-26 12:22:33 -06:00
|
|
|
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
|
2024-07-07 20:33:54 -06:00
|
|
|
struct Io io[2 /* { in, out } */];
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-06-26 12:22:33 -06:00
|
|
|
/* Set defaults. */
|
|
|
|
align = -1;
|
|
|
|
count = 0;
|
2024-07-03 14:50:50 -06:00
|
|
|
fmt = fmt_asv;
|
2024-06-26 12:22:33 -06:00
|
|
|
noerror = 0;
|
2024-07-12 15:23:57 -06:00
|
|
|
for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
|
2024-06-26 15:15:37 -06:00
|
|
|
io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */
|
2024-07-03 17:59:21 -06:00
|
|
|
io[i].bufuse = 0;
|
2024-06-26 15:15:37 -06:00
|
|
|
io[i].bytes = 0;
|
2024-07-03 14:22:23 -06:00
|
|
|
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
|
|
|
|
io[i].fn = i == 0 ? stdin_name : stdout_name;
|
|
|
|
io[i].fl = i == 0 ? read_flags : write_flags;
|
2024-07-07 20:33:54 -06:00
|
|
|
io[i].error = 0;
|
2024-06-26 15:15:37 -06:00
|
|
|
io[i].prec = 0;
|
|
|
|
io[i].rec = 0;
|
|
|
|
io[i].seek = 0;
|
|
|
|
}
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-13 16:56:56 -06:00
|
|
|
if (!(argc < 0)) { usage(program_name); }
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
program_name = argv[0];
|
|
|
|
while ((c = getopt(argc, argv, "a:b:B:c:i:hHns:S:o:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'i': case 'o': /* input, output */
|
|
|
|
i = (c == 'o');
|
2024-06-26 15:15:37 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
/* optarg == "-" (stdin/stdout) */
|
|
|
|
if (optarg[0] == '-' && optarg[1] == '\0') {
|
2024-07-03 14:22:23 -06:00
|
|
|
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
|
|
|
|
io[i].fn = i == 0 ? stdin_name : stdout_name;
|
2024-01-10 15:03:04 -07:00
|
|
|
break;
|
2024-07-12 15:23:57 -06:00
|
|
|
} else {
|
2024-07-03 19:22:34 -06:00
|
|
|
int fd;
|
|
|
|
|
2024-07-12 15:32:30 -06:00
|
|
|
if (
|
|
|
|
(fd = open(optarg, io[i].fl, creat_mode)) != -1
|
|
|
|
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)
|
|
|
|
) {
|
2024-07-03 19:22:34 -06:00
|
|
|
io[i].fd = fd;
|
|
|
|
io[i].fn = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
return oserr(optarg, errno); /* break; */
|
|
|
|
case 'n': noerror = 1; break; /* retry failed reads once */
|
|
|
|
case 'H': fmt = fmt_human; break; /* human-readable output */
|
|
|
|
case 'a': /* input buffer padding */
|
|
|
|
if (optarg[0] == '\0' || optarg[1] == '\0') {
|
2024-01-09 23:43:45 -07:00
|
|
|
align = optarg[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2024-07-12 15:23:57 -06:00
|
|
|
case 'c': /* number of reads */
|
|
|
|
case 'b': case 'B': /* input/output block size */
|
|
|
|
case 's': case 'S': /* (s)kip/(S)eek in input/output */
|
|
|
|
if (c == 'c' && (count = parse(optarg)) >= 0) { break; }
|
|
|
|
|
2024-07-03 14:50:50 -06:00
|
|
|
i = (c >= 'A' && c <= 'Z');
|
2024-07-12 15:23:57 -06:00
|
|
|
c |= 0b00100000; /* (ASCII) make lowercase */
|
|
|
|
|
2024-07-12 15:32:30 -06:00
|
|
|
if (
|
|
|
|
(c == 'b' && (io[i].bs = parse(optarg)) > 0)
|
|
|
|
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0)
|
|
|
|
) { break; }
|
2024-07-12 15:23:57 -06:00
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2024-06-26 15:15:37 -06:00
|
|
|
return usage(program_name);
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
2024-01-09 23:43:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-03 16:07:02 -06:00
|
|
|
assert(io->fd != STDIN_FILENO || io->fl == read_flags);
|
|
|
|
assert(io->fd != STDOUT_FILENO || io->fl == write_flags);
|
2024-07-03 13:50:24 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (argc > optind) { return usage(program_name); }
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
|
2024-07-03 16:07:02 -06:00
|
|
|
/* buffer allocation */
|
2024-07-12 15:23:57 -06:00
|
|
|
if ((io[i].buf = malloc(io[i].bs * (sizeof *(io[i].buf)))) == NULL) {
|
2024-07-12 15:32:30 -06:00
|
|
|
fprintf(
|
|
|
|
stderr, "%s: Failed to allocate %zd bytes\n",
|
|
|
|
program_name, io[i].bs
|
|
|
|
);
|
2024-01-09 23:43:45 -07:00
|
|
|
return EX_OSERR;
|
2024-07-03 14:22:23 -06:00
|
|
|
}
|
2024-07-12 15:23:57 -06:00
|
|
|
|
2024-07-03 16:07:02 -06:00
|
|
|
/* easy seeking */
|
2024-07-12 15:23:57 -06:00
|
|
|
if (!fdisstd(io[i].fd) && lseek(io[i].fd, io[i].seek, SEEK_SET) != -1) {
|
2024-07-03 16:07:02 -06:00
|
|
|
io[i].seek = 0;
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
2024-07-03 16:07:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* hard seeking */
|
2024-07-12 15:23:57 -06:00
|
|
|
if (io[1].seek > 0) {
|
2024-07-04 21:32:05 -06:00
|
|
|
size_t t;
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
do {
|
|
|
|
memset(
|
|
|
|
io[1].buf, '\0',
|
|
|
|
(t = io[1].bufuse = MIN(io[1].bs, io[1].seek))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) {
|
2024-07-04 21:32:05 -06:00
|
|
|
Io_write(&io[1]); /* second chance */
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
|
|
|
if (io[1].error != 0) { return oserr(io[1].fn, io[1].error); }
|
|
|
|
} while ((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t);
|
|
|
|
|
2024-07-03 16:07:02 -06:00
|
|
|
io[1].bufuse = 0;
|
2024-01-09 23:43:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (io[1].seek > 0) {
|
2024-07-04 21:32:05 -06:00
|
|
|
fprintio(stderr, fmt, io);
|
2024-07-07 21:13:44 -06:00
|
|
|
return oserr(io[1].fn, errno);
|
2024-07-04 21:32:05 -06:00
|
|
|
}
|
2024-07-03 16:07:02 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
do { /* while(count == 0 || --count > 0); */
|
2024-07-04 20:16:54 -06:00
|
|
|
assert(io[0].bufuse == 0);
|
|
|
|
|
2024-07-04 20:00:40 -06:00
|
|
|
{ /* read */
|
2024-07-08 22:48:16 -06:00
|
|
|
long skipping;
|
2024-07-04 20:00:40 -06:00
|
|
|
|
2024-07-05 08:02:09 -06:00
|
|
|
/* hack to intentionally get a partial read from Io_read */
|
2024-07-12 15:23:57 -06:00
|
|
|
if ((skipping = MIN(io[0].seek, io[0].bs)) > 0) {
|
2024-07-08 22:53:44 -06:00
|
|
|
io[0].bufuse = io[0].bs - (size_t)skipping;
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
2024-07-04 20:27:31 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
size_t t = io[0].bufuse;
|
|
|
|
if (Io_read(&io[0])->bufuse == t && !noerror && io[0].error == 0) {
|
2024-07-04 20:00:40 -06:00
|
|
|
Io_read(&io[0]); /* second chance */
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
|
|
|
|
2024-07-07 18:14:48 -06:00
|
|
|
assert(io[0].bufuse >= t);
|
2024-07-04 20:00:40 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (io[0].bufuse == t) /* that's all she wrote */ { break; }
|
|
|
|
|
|
|
|
if (/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs) {
|
2024-07-04 20:00:40 -06:00
|
|
|
fprintf(stderr, "%s: Partial read:\n\t", program_name);
|
|
|
|
fprintio(stderr, fmt, io);
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
if (!noerror) { count = 1; }
|
|
|
|
|
|
|
|
if (align >= 0) {
|
2024-07-04 20:00:40 -06:00
|
|
|
/* fill the rest of the ibuf with padding */
|
2024-07-12 15:23:57 -06:00
|
|
|
memset(
|
|
|
|
&(io[0].buf)[io[0].bufuse],
|
|
|
|
align,
|
|
|
|
io[0].bs - io[0].bufuse
|
|
|
|
);
|
|
|
|
|
2024-07-04 20:00:40 -06:00
|
|
|
io->bufuse = io->bs;
|
|
|
|
}
|
2024-07-03 19:06:59 -06:00
|
|
|
}
|
2024-07-04 20:27:31 -06:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (skipping > 0) {
|
2024-07-08 22:48:16 -06:00
|
|
|
io[0].seek -= skipping;
|
2024-07-04 20:27:31 -06:00
|
|
|
io[0].bufuse = 0;
|
|
|
|
count += (count != 0);
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-04 19:21:40 -06:00
|
|
|
}
|
2024-01-09 23:43:45 -07:00
|
|
|
|
|
|
|
/* write */
|
2024-07-12 15:23:57 -06:00
|
|
|
do { /* while(io[0].bufuse > 0); */
|
2024-06-26 15:28:02 -06:00
|
|
|
int t;
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (io[0].bs <= io[1].bs) {
|
2024-07-03 19:04:01 -06:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/* saturate obuf */
|
2024-07-12 15:23:57 -06:00
|
|
|
memcpy(
|
|
|
|
io[1].buf, io[0].buf,
|
|
|
|
(io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))
|
|
|
|
);
|
2024-07-03 19:04:01 -06:00
|
|
|
/* permute the copied units out of ibuf */
|
2024-07-04 20:05:18 -06:00
|
|
|
memmove(io[0].buf, &(io[0].buf)[n], (io[0].bufuse -= n));
|
2024-07-12 15:23:57 -06:00
|
|
|
} else /* if(io[0].bs < io[1].bs) */ {
|
2024-07-03 17:50:04 -06:00
|
|
|
int n;
|
|
|
|
|
2024-07-03 19:04:01 -06:00
|
|
|
/* drain what we can from ibuf */
|
2024-07-12 15:23:57 -06:00
|
|
|
memcpy(
|
|
|
|
&(io[1].buf)[io[1].bufuse], io[0].buf,
|
|
|
|
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))
|
|
|
|
);
|
|
|
|
|
2024-07-03 17:50:04 -06:00
|
|
|
io[1].bufuse += n;
|
2024-07-12 15:23:57 -06:00
|
|
|
|
2024-07-03 17:50:04 -06:00
|
|
|
/* permute out the copied units */
|
2024-07-04 20:16:54 -06:00
|
|
|
memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n);
|
2024-07-12 15:23:57 -06:00
|
|
|
|
2024-07-03 17:50:04 -06:00
|
|
|
io[0].bufuse -= n;
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) {
|
2024-07-03 19:04:01 -06:00
|
|
|
continue; /* obuf not saturated - we could write more */
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
2024-07-03 19:04:01 -06:00
|
|
|
}
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-06-26 15:28:02 -06:00
|
|
|
t = io[1].bufuse;
|
2024-07-12 15:23:57 -06:00
|
|
|
if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) {
|
2024-06-26 13:41:24 -06:00
|
|
|
Io_write(&io[1]); /* second chance */
|
2024-07-12 15:23:57 -06:00
|
|
|
}
|
|
|
|
|
2024-07-07 18:14:48 -06:00
|
|
|
assert(io[1].bufuse <= t);
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
if (io[1].bufuse == t) { /* no more love */
|
2024-01-09 23:43:45 -07:00
|
|
|
count = 1;
|
|
|
|
break;
|
2024-07-04 19:36:32 -06:00
|
|
|
}
|
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
if (0 < io[1].bufuse /* && io[1].bufuse < t */) {
|
2024-06-26 11:36:52 -06:00
|
|
|
fprintf(stderr, "%s: Partial write:\n\t", program_name);
|
2024-07-04 19:23:09 -06:00
|
|
|
fprintio(stderr, fmt, io);
|
2024-07-12 15:23:57 -06:00
|
|
|
|
|
|
|
if(!noerror) { count = 1; }
|
2024-07-04 19:36:32 -06:00
|
|
|
}
|
2024-07-12 15:23:57 -06:00
|
|
|
} while(io[0].bufuse > 0);
|
|
|
|
} while(count == 0 || --count > 0);
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-04 19:23:09 -06:00
|
|
|
fprintio(stderr, fmt, io);
|
2024-01-09 23:43:45 -07:00
|
|
|
|
2024-07-12 15:23:57 -06:00
|
|
|
for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
|
|
|
|
if (io[i].error) { return oserr(io[i].fn, io[i].error); }
|
|
|
|
}
|
2024-07-07 20:33:54 -06:00
|
|
|
|
2024-01-09 23:43:45 -07:00
|
|
|
return EX_OK;
|
|
|
|
}
|