summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2018-05-16 19:17:02 +0200
committerPaweł Redman <pawel.redman@gmail.com>2018-05-16 19:17:02 +0200
commitd60566c7e31293273b417679782f0614dd00d01a (patch)
treee08a88a98583296fed9e95dcca4c45859e8bf5d0
parentc9998b28fb18a7b3f5b8481058da1c224e4cc6e1 (diff)
Better option parsing.
-rw-r--r--turbowc.c50
1 files changed, 39 insertions, 11 deletions
diff --git a/turbowc.c b/turbowc.c
index 36bd6d4..c556ed5 100644
--- a/turbowc.c
+++ b/turbowc.c
@@ -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;
+}