Fix tfw_createfile utility

Was not omitting characters given by the --omit option.
This commit is contained in:
Andrew Bettison 2018-03-28 17:42:02 +10:30
parent 2e0cb4e6dc
commit 2ef315b692
2 changed files with 56 additions and 36 deletions

View File

@ -20,6 +20,18 @@
source "${0%/*}/../testframework.sh" source "${0%/*}/../testframework.sh"
PATH="$(abspath "${0%/*}/.."):$PATH"
test_tfw_createfile() {
tfw_log "PATH=$PATH"
executeOk tfw_createfile --size=500 --label=wah --omit=2Dq
tfw_cat --stdout
assert [ $(stat --format '%s' "$TFWSTDOUT") -eq 500 ]
assertStdoutGrep --matches=0 2
assertStdoutGrep --matches=0 D
assertStdoutGrep --matches=0 q
}
test_tfw_cmp_version() { test_tfw_cmp_version() {
execute --exit-status=1 tfw_cmp_version 1 2 execute --exit-status=1 tfw_cmp_version 1 2
execute --exit-status=2 tfw_cmp_version 1.0.1 1.0.0 execute --exit-status=2 tfw_cmp_version 1.0.1 1.0.0

View File

@ -56,7 +56,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "str.h" #include "str.h"
#include "numeric_str.h" #include "numeric_str.h"
static const char *argv0 = "test_createfile"; static const char *argv0 = "tfw_createfile";
static void fatal(const char *fmt, ...) static void fatal(const char *fmt, ...)
{ {
@ -73,10 +73,9 @@ static void fatal(const char *fmt, ...)
exit(1); exit(1);
} }
static inline char stripe(int i, const char *omit) static inline char stripe(int i)
{ {
char c = (i >= ' ' && i <= '~') ? i : '.'; return (i >= ' ' && i <= '~') ? i : '.';
return strchr(omit, c) ? '.' : c;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -85,44 +84,56 @@ int main(int argc, char **argv)
uint64_t size = 0; uint64_t size = 0;
const char *label = ""; const char *label = "";
const char *omit = ""; const char *omit = "";
int i; {
for (i = 1; i < argc; ++i) { int i;
const char *arg = argv[i]; for (i = 1; i < argc; ++i) {
if (str_startswith(arg, "--size=", &arg)) { const char *arg = argv[i];
if (!str_to_uint64_scaled(arg, 10, &size, NULL)) if (str_startswith(arg, "--size=", &arg)) {
fatal("illegal --size= argument: %s", arg); if (!str_to_uint64_scaled(arg, 10, &size, NULL))
fatal("illegal --size= argument: %s", arg);
}
else if (str_startswith(arg, "--label=", &arg))
label = arg;
else if (str_startswith(arg, "--omit=", &arg)) {
omit = arg;
if (strchr(omit, '.'))
fatal("illegal --omit= argument, must not contain '.': %s", arg);
}
else
fatal("unrecognised argument: %s", arg);
} }
else if (str_startswith(arg, "--label=", &arg))
label = arg;
else if (str_startswith(arg, "--omit=", &arg)) {
omit = arg;
if (strchr(omit, '.'))
fatal("illegal --omit= argument, must not contain '.': %s", arg);
}
else
fatal("unrecognised argument: %s", arg);
} }
uint64_t offset = 0; uint64_t offset = 0;
char buf[127]; char buf[129];
for (i = 0; i != sizeof buf; ++i) const size_t linesiz = sizeof buf - 2;
buf[i] = stripe(i, omit); {
size_t i;
for (i = 0; i != linesiz; ++i)
buf[i] = stripe(i);
}
const size_t labellen = strlen(label); const size_t labellen = strlen(label);
unsigned bouncemax = labellen < sizeof buf ? sizeof buf - labellen : sizeof buf; unsigned bouncemax = labellen < linesiz ? linesiz - labellen : linesiz;
unsigned bounce = 3; unsigned bounce = 3;
int bouncedelta = 1; int bouncedelta = 1;
while (!ferror(stdout) && offset < size) { while (!ferror(stdout) && offset < size) {
unsigned n = sprintf(buf, "%"PRId64, offset); unsigned n = sprintf(buf, "%"PRId64, offset);
buf[n] = stripe(n, omit); buf[n] = stripe(n);
size_t labelsiz = labellen; size_t labelsiz = labellen;
if (labelsiz && bounce < sizeof buf) { if (labelsiz && bounce < linesiz) {
if (labelsiz > sizeof buf - bounce) if (labelsiz > linesiz - bounce)
labelsiz = sizeof buf - bounce; labelsiz = linesiz - bounce;
memcpy(buf + bounce, label, labelsiz); memcpy(buf + bounce, label, labelsiz);
} }
unsigned remain = size - offset - 1; unsigned remain = size - offset - 1;
if (remain > sizeof buf) if (remain > linesiz)
remain = sizeof buf; remain = linesiz;
buf[remain++] = '\n';
buf[remain] = '\0';
{
char *p = buf;
while ((p = strpbrk(p, omit)))
*p++ = '.';
}
{ {
size_t off=0; size_t off=0;
while(off<remain){ while(off<remain){
@ -132,17 +143,14 @@ int main(int argc, char **argv)
off+=wrote; off+=wrote;
} }
} }
char eol = strchr(omit, '\n') ? '.' : '\n'; offset += remain;
if (fputc(eol, stdout) == EOF)
fatal("write error: %s [errno=%d]", strerror(errno), errno);
offset += remain + 1;
if (bounce <= n || bounce >= bouncemax) if (bounce <= n || bounce >= bouncemax)
bouncedelta *= -1; bouncedelta *= -1;
if (labelsiz) { if (labelsiz) {
if (bouncedelta > 0) if (bouncedelta > 0)
buf[bounce] = stripe(bounce, omit); buf[bounce] = stripe(bounce);
else else
buf[bounce + labelsiz - 1] = stripe(bounce + labelsiz - 1, omit); buf[bounce + labelsiz - 1] = stripe(bounce + labelsiz - 1);
} }
bounce += bouncedelta; bounce += bouncedelta;
} }