diff options
author | Leah Neukirchen <leah@vuxu.org> | 2024-10-02 18:32:53 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2024-10-02 18:32:53 +0200 |
commit | 945bc437108bd0bec6ffbd4e62d2eb5a4e7ba941 (patch) | |
tree | 84522a2d1fba27b4c8e6901af434c12b80fcca51 /ulidgen.c | |
download | ulidgen-945bc437108bd0bec6ffbd4e62d2eb5a4e7ba941.tar.gz ulidgen-945bc437108bd0bec6ffbd4e62d2eb5a4e7ba941.tar.xz ulidgen-945bc437108bd0bec6ffbd4e62d2eb5a4e7ba941.zip |
initial import of ulidgen
Diffstat (limited to 'ulidgen.c')
-rw-r--r-- | ulidgen.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/ulidgen.c b/ulidgen.c new file mode 100644 index 0000000..099807f --- /dev/null +++ b/ulidgen.c @@ -0,0 +1,53 @@ +/* + * ulidgen - generate or tag lines with ULID + * (Universally Unique Lexicographically Sortable Identifier) + * + * Usage: ulidgen [-n N | -t] + * -n N generate N ULID (default: 1) + * -t print each line of standard input prefixed with an ULID + * + * To the extent possible under law, Leah Neukirchen <leah@vuxu.org> + * has waived all copyright and related or neighboring rights to this work. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "ulid.h" + +int +main(int argc, char *argv[]) +{ + char ulid[27] = { 0 }; + + int c; + long n = 1; + int tflag = 0; + while ((c = getopt(argc, argv, "n:t")) != -1) + switch (c) { + case 'n': n = atol(optarg); break; + case 't': tflag = 1; break; + } + + if (tflag) { + char *line = 0; + size_t linelen = 0; + + setvbuf(stdout, 0, _IOLBF, 0); + + while (getdelim(&line, &linelen, '\n', stdin) != -1) { + ulidgen_r(ulid); + printf("%s %s", ulid, line); + } + } else { + for (long i = 0; i < n; i++) { + ulidgen_r(ulid); + puts(ulid); + } + } + + fflush(0); + exit(!!ferror(stdout)); +} |