File: minuid.c

package info (click to toggle)
sch-rnd 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,696 kB
  • sloc: ansic: 119,120; awk: 1,502; makefile: 1,421; sh: 1,404; yacc: 905; lex: 172; xml: 160
file content (81 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
    libminuid - minimalistic globally unique IDs
    Copyright (C) 2022  Tibor 'Igor2' Palinkas

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 31 Milk Street, # 960789 Boston, MA 02196 USA

    Project page:  http://repo.hu/projects/libminuid
    Source code:   svn://repo.hu/libminuid/trunk
    Author:        emailto: libminuid [at] igor2.repo.hu
*/

#include <stdio.h>
#include "libminuid.h"

static int help(const char *prg)
{
	printf("minuid - command line utility for globally unique IDs\n");
	printf("Usage: %s cmd\n", prg);
	printf("cmd is one of:\n");
	printf("  help        this help screen\n");
	printf("  gen         generate UIDs on stdout\n");
	printf("\n");
	return 0;
}

static int gen(void)
{
	minuid_session_t sess;

	if (minuid_init(&sess) != 0) {
		fprintf(stderr, "minuid_init() failed\n");
		return 1;
	}

	for(;;) {
		minuid_bin_t bin;
		minuid_str_t txt;

		if (minuid_gen(&sess, bin) != 0) {
			fprintf(stderr, "minuid_gen() failed\n");
			return 1;
		}
		if (minuid_bin2str(txt, bin) != 0) {
			fprintf(stderr, "minuid_bin2str() failed\n");
			return 1;
		}
		printf("%s\n", txt);
	}

	return 0;
}

int main(int argc, char *argv[])
{
	char *cmd = argv[1];
	if (cmd == NULL) {
		fprintf(stderr, "need an argument; try --help\n");
		return 1;
	}

	while(*cmd == '-') cmd++;
	switch(*cmd) {
		case 'h': return help(argv[0]);
		case 'g': return gen();
	}

	fprintf(stderr, "Unknown argument: %s; try --help\n", argv[1]);
	return 1;
}