summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-14 00:47:59 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-07-14 00:47:59 +0200
commit1dd42dd21458601970a2cbd09ac1a86d34511c0a (patch)
tree548bceafbdc57ff32b8112e492e1546104088ac1 /src
parent3e3d08a7d36b45b13bbec559780f8d421aadf8a7 (diff)
hoofd
Diffstat (limited to 'src')
-rw-r--r--src/hoofd.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/hoofd.c b/src/hoofd.c
new file mode 100644
index 0000000..adbd87a
--- /dev/null
+++ b/src/hoofd.c
@@ -0,0 +1,82 @@
+#include <getopt.h>
+#include <pwd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "util/versie.h"
+#include "util/error.h"
+#include "util/debug.h"
+#include "util/map.h"
+
+static void usage(FILE *f) {
+ fprintf(f,
+ "Gebruik: hoofd [-nchV] [BESTAND]...\n"
+ "\n"
+ "Toon de eerste 10 regels van elk BESTAND naar standaard uitvoer\n"
+ "\n"
+ " -n=AANTAL Aantal regels om weer te geven\n"
+ " -c=AANTAL Aantal karakters om weer te geven\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n");
+}
+
+// Returns pointer to argument array containing the file names
+static char** parse_options(int argc, char **argv, int *n, int *c) {
+ int opt;
+ while ((opt = getopt(argc, argv, "n:c:hV")) != -1) {
+ switch (opt) {
+ case 'c':
+ if (optarg[0] == '=') optarg++;
+ *c = atoi(optarg);
+ *n = -1;
+ break;
+
+ case 'n':
+ if (optarg[0] == '=') optarg++;
+ *c = -1;
+ *n = atoi(optarg);
+ break;
+
+ case 'h':
+ usage(stdout);
+ exit(0);
+
+ case 'V':
+ drukkedoos_print_versie(stdout, "hoofd");
+ exit(0);
+
+ case '?':
+ usage(stderr);
+ exit(1);
+ }
+ }
+
+ return argv + optind;
+}
+
+int entry_hoofd(int argc, char **argv) {
+ int n = 10;
+ int c = -1;
+ char **args = parse_options(argc, argv, &n, &c);
+
+ while (*args != NULL) {
+ struct map *map = open_map(*args);
+ if (map == NULL) {
+ fprintf(stderr, "hoofd: fout bij lezen bestand");
+ return 1;
+ }
+
+ size_t i;
+ for (i = 0; i < (size_t)map->sb.st_size && (n > 0 || n == -1) && (c > 0 || c == -1); i++) {
+ if (map->addr[i] == '\n') {
+ if (n != -1) n--;
+ }
+ if (c != -1) c--;
+ }
+ fwrite(map->addr, 1, i, stdout);
+
+ args++;
+ }
+
+ return 0;
+}