diff options
-rw-r--r-- | turbowc.c | 50 |
1 files changed, 39 insertions, 11 deletions
@@ -1,6 +1,7 @@ #include <stdio.h> #include <stdint.h> #include <string.h> +#include <getopt.h> #include <emmintrin.h> #include <pthread.h> #include <unistd.h> @@ -102,24 +103,19 @@ static void *thread_f(void *arg) return NULL; } -int main(int argc, char **argv) +static int turbowc(const char *path, size_t num_threads) { - int ret = 1, fd; + int ret = 1, opt, fd; struct stat stat; void *map; - size_t i, num_threads = 4; + size_t i; pthread_t *threads; struct timespec t0, t1; double delta; - if (argc < 2) { - fprintf(stderr, "%s [file]\n", argv[0]); - return 1; - } - - fd = open(argv[1], O_RDONLY); + fd = open(path, O_RDONLY); if (fd == -1) { - perror("open"); + perror(path); goto error_open; } @@ -170,7 +166,7 @@ int main(int argc, char **argv) data_pool.total += count_newlines(data_pool.cursor, data_pool.left); clock_gettime(CLOCK_MONOTONIC, &t1); - printf("%zu %s\n", data_pool.total, argv[1]); + printf("%zu %s\n", data_pool.total, path); delta = (t1.tv_sec - t0.tv_sec) + ((double)t1.tv_nsec - t0.tv_nsec) * 1.0e-9; @@ -196,3 +192,35 @@ error_fstat: error_open: return ret; } + +int main(int argc, char **argv) +{ + int opt; + size_t num_threads = 1; + + while ((opt = getopt(argc, argv, "vt:")) != -1) { + switch (opt) { + case 'v': + printf("turbowc, with mmap, SSE2 and multithreading\n"); + break; + + case 't': + num_threads = strtoull(optarg, NULL, 10); + break; + + default: + print_usage: + fprintf(stderr, "usage: turbowc [-v] [-t THREADS] [FILE]\n"); + return 1; + } + } + + if (optind >= argc) + goto print_usage; + + for (; optind < argc; optind++ ) + if (turbowc(argv[optind], num_threads)) + return 1; + + return 0; +} |