commit a9c9a198a6c519d689c7534ccf3d32746571841c
parent 5692fa2841fef9238096b7ba5996cd726c5084bc
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Wed, 24 Jul 2024 12:41:00 -0700
Fix bugs with new numsep
Obviously rewriting the thing was going to introduce bugs, and since
I never made tests I didn't catch them before commiting. Anyway
this fixes the issue with fully buffering input even when you are
writing line-by-line (such as at the terminal) by ditching stdio,
and fix the comma placement logic.
Diffstat:
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/local/src/src/singles/numsep.c b/local/src/src/singles/numsep.c
@@ -23,14 +23,15 @@
#include <ctype.h>
#include <stdio.h>
+#include <unistd.h>
-int numsep_stream(FILE *input, FILE *output);
+int numsep_stream(int input, int output);
size_t numsep_bufs(char *i, char *o, int *newword, size_t len);
int
main(void)
{
- if (numsep_stream(stdin, stdout) != 0) {
+ if (numsep_stream(0, 1) != 0) {
perror("Unable to seperate numbers");
return 1;
}
@@ -38,19 +39,19 @@ main(void)
}
int
-numsep_stream(FILE *input, FILE *output)
+numsep_stream(int input, int output)
{
char wb[4096], rb[(size_t)(sizeof(wb) * 0.75 + 1)];
int len;
int newword;
newword = 1;
- while ((len = fread(rb, 1, sizeof(rb), input)) > 0) {
+ while ((len = read(input, rb, sizeof(rb))) > 0) {
len = numsep_bufs(rb, wb, &newword, len);
- if (fwrite(wb, 1, len, output) != len)
+ if (write(output, wb, len) != len)
return -1;
}
- return ferror(input);
+ return len < 0;
}
size_t
@@ -74,8 +75,9 @@ numsep_bufs(char *ib, char *ob, int *newword, size_t len)
} else {
offset = numlen % 3;
for (i = 0; i < numlen; ++i) {
- if (i == offset || i + offset % 3 == 0)
+ if ((i > 0 && i == offset) || ((i > offset) && (i - offset) % 3 == 0)) {
ob[oi++] = ',';
+ }
ob[oi++] = ib[num + i];
}
}