#include #include #include #include #include #include "util/map.h" #include "util/versie.h" static void usage(FILE *f) { fprintf(f, "Gebruik: tak [-hV] \n" "\n" "Schakel bestanden aaneen naar standaard uitvoer omgekeerd.\n" "\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 opt; while ((opt = getopt(argc, argv, "hV")) != -1) { switch (opt) { case 'h': usage(stdout); exit(0); case 'V': drukkedoos_print_versie(stdout, "tak"); exit(0); case '?': usage(stderr); exit(1); } } return argv + optind; } struct state { char *buf; size_t sz; }; static void process(struct state state) { char *lstart, *lend; lend = &state.buf[state.sz - 1]; while (lend > state.buf) { if (*lend == '\n') { lend--; } lstart = lend; while (*lstart != '\n' && lstart != state.buf) { lstart--; } if (lstart != state.buf) { lstart++; } fwrite(lstart, 1, lend - lstart + 2, stdout); lend = lstart-1; } } static char *read_stdin(size_t *sz) { const size_t CHUNK_SIZE = 4096; size_t cap = CHUNK_SIZE; char *res = malloc(cap); while (!feof(stdin)) { if (cap-*sz < CHUNK_SIZE) { cap *= 2; res = realloc(res, cap); } const size_t n = fread(res+*sz, 1, CHUNK_SIZE, stdin); *sz += n; if (n != CHUNK_SIZE && !feof(stdin)) { fprintf(stderr, "tak: fout tijdens lezen van standaard invoer.\n"); exit(1); } } return res; } int entry_tak(int argc, char **argv) { char **args = parse_options(argc, argv); if (*args == NULL) { size_t sz = 0; char *data = read_stdin(&sz); struct state state = { .buf = data, .sz = sz }; process(state); free(data); } else { while (*args != NULL) { struct map *map = open_map(*args); if (map == NULL) { return 1; } struct state state = { .buf = map->addr, .sz = map->sb.st_size }; process(state); close_map(map); args++; } } return 0; }