From 65c411dcb3fd33122cbc314ca5b2a94542bc1e87 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 22 Aug 2020 20:09:13 +0200 Subject: Added shuf with trand --- .gitmodules | 3 +++ Makefile | 10 +++++++++- src/shuf.c | 8 ++++++++ trand | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 trand diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e5ead97 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "trand"] + path = trand + url = https://github.com/SaltyMilk/trand diff --git a/Makefile b/Makefile index edb7787..450a5f4 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,8 @@ CCFLAGS = -Wall -Wextra -Wpedantic all: prebuild $(BINDIR)/basename $(BINDIR)/chown $(BINDIR)/cut $(BINDIR)/head \ $(BINDIR)/mkdir $(BINDIR)/mv $(BINDIR)/rm $(BINDIR)/seq \ - $(BINDIR)/shuf $(BINDIR)/tee $(BINDIR)/tr + $(BINDIR)/shuf $(BINDIR)/tee $(BINDIR)/tr \ + $(BINDIR)/shuf_trand prebuild: mkdir -vp $(BINDIR) @@ -44,6 +45,13 @@ $(BINDIR)/tee: $(SRCDIR)/tee.c $(BINDIR)/tr: $(SRCDIR)/tr.c $(CC) $(CCFLAGS) -o $@ $^ +#### shuf using trand +TRAND_PATH = trand + +$(BINDIR)/shuf_trand: $(SRCDIR)/shuf.c + make --no-print-directory -C $(TRAND_PATH) + $(CC) $(CCFLAGS) -o $@ $^ -DUSE_TRAND -I$(TRAND_PATH) -L$(TRAND_PATH) -ltrand -lpthread + clean: rm -vf $(BINDIR)/* diff --git a/src/shuf.c b/src/shuf.c index 5b4808a..e97f1f0 100644 --- a/src/shuf.c +++ b/src/shuf.c @@ -8,6 +8,10 @@ #include #include +#ifdef USE_TRAND +# include "trand.h" +#endif + #define NUMBER_LEN(x) strlen(#x); static char *g_name = "shuf"; @@ -192,7 +196,11 @@ int main(int argc, char **argv) } for (size_t i = lines.len - 1; i > 0; i--) { +#ifndef USE_TRAND size_t j = rand() % i; +#else + size_t j = trand() % i; +#endif char *tmp = lines.data[i]; lines.data[i] = lines.data[j]; lines.data[j] = tmp; diff --git a/trand b/trand new file mode 160000 index 0000000..583ad1c --- /dev/null +++ b/trand @@ -0,0 +1 @@ +Subproject commit 583ad1cbe5d1ad9c0a64bbf8180aec21a68463e0 -- cgit