summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/hoofd.c42
1 files changed, 8 insertions, 34 deletions
diff --git a/src/hoofd.c b/src/hoofd.c
index c02d166..ffb5563 100644
--- a/src/hoofd.c
+++ b/src/hoofd.c
@@ -7,12 +7,12 @@
#include "util/debug.h"
#include "util/error.h"
-#include "util/loop_args.h"
+#include "util/loop_files.h"
#include "util/versie.h"
#include "io/read_file.h"
-int n, c;
+static int gn, gc;
static void usage(FILE *f) {
fprintf(f,
@@ -62,7 +62,9 @@ static char** parse_options(int argc, char **argv, int *n, int *c) {
// TODO: be smarter, hoofd doesn't have to read the whole file in memory (for
// unmappable files)
-static void process(struct filebuf *fb, int n, int c) {
+static int process(struct filebuf *fb, char*, bool) {
+ int n = gn, c = gc;
+
size_t i;
for (i = 0; i < fb->sz && (n > 0 || n == -1) && (c > 0 || c == -1); i++) {
if (fb->buf[i] == '\n') {
@@ -73,40 +75,12 @@ static void process(struct filebuf *fb, int n, int c) {
fwrite(fb->buf, 1, i, stdout);
free_filebuf(fb);
-}
-
-static int handle(char *arg, bool isstdin) {
- struct filebuf *fb = NULL;
-
- if (isstdin) {
- fb = stream_to_filebuf(stdin, 0);
- if (fb == NULL) goto err_stdin;
- } else {
- bool isdir = false;
- fb = file_to_filebuf(arg, 0, &isdir);
- if (isdir) goto err_isdir;
- else if (fb == NULL) goto err_file;
- }
- process(fb, n, c);
return 0;
-
-err_stdin:
- fprintf(stderr, "hoofd: fout bij lezen van standaard invoer\n");
- return 1;
-
-err_file:
- fprintf(stderr, "hoofd: fout bij lezen van bestand\n");
- return 1;
-
-err_isdir:
- fprintf(stderr, "hoofd: bestand '%s' is een mapje\n", arg);
- return -1;
}
int entry_hoofd(int argc, char **argv) {
- n = 10;
- c = -1;
- char **args = parse_options(argc, argv, &n, &c);
- return loop_args(args, handle);
+ gn = 10, gc = -1;
+ char **args = parse_options(argc, argv, &gn, &gc);
+ return loop_files(args, process);
}