stew

a monorepo of some sort
Log | Files | Refs

commit ce8510fcd42ff72dddc14bc3530fb7e2d7b27f02
parent b2abf1f22277fcbacb784c97e1b1e6e922ca8a35
Author: rpa <rpa@laika>
Date:   Fri, 10 Feb 2023 22:38:54 +0000

src/shuf: shuffle lines

Diffstat:
Asrc/mkfile | 5+++++
Asrc/shuf.c | 38++++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/src/mkfile b/src/mkfile @@ -0,0 +1,5 @@ +</$objtype/mkfile +TARG=shuf +HFILES= +OFILES= +</sys/src/cmd/mkmany diff --git a/src/shuf.c b/src/shuf.c @@ -0,0 +1,38 @@ +/* + Read lines and write them back in random order. +*/ + +#include <u.h> +#include <libc.h> +#include <bio.h> +typedef struct Line Line; +struct Line { + char *s; + Line *next; +}; +long i, r, n; +Line start, *lp, *lr; +char *s; +void +main(void) +{ + srand(truerand()); + Biobuf *bp = Bfdopen(0, OREAD); + lp = &start; + while ((s = Brdstr(bp, '\n', 0)) != nil) { + lp->next = malloc(sizeof(Line)); + lp = lp->next; + if (lp == nil) sysfatal("nil"); + *lp = (Line){s, start.next}; + n++; + } + for (i = n; i > 0; i--) { + r = lnrand(i); + for (r = lnrand(i); r > 0; r--) { + lp = lp->next; + if (lp == nil) sysfatal("nil"); + } + print("%s", lp->next->s); + lp->next = lp->next->next; + }; +}