]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * gen_uuid.c --- generate a DCE-compatible uuid | |
3 | * | |
4 | * Copyright (C) 1999, Andreas Dilger and Theodore Ts'o | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <getopt.h> | |
15 | ||
16 | #include "uuid.h" | |
17 | #include "nls.h" | |
18 | #include "c.h" | |
19 | #include "closestream.h" | |
20 | ||
21 | static void __attribute__((__noreturn__)) usage(void) | |
22 | { | |
23 | FILE *out = stdout; | |
24 | fputs(USAGE_HEADER, out); | |
25 | fprintf(out, | |
26 | _(" %s [options]\n"), program_invocation_short_name); | |
27 | ||
28 | fputs(USAGE_SEPARATOR, out); | |
29 | fputs(_("Create a new UUID value.\n"), out); | |
30 | ||
31 | fputs(USAGE_OPTIONS, out); | |
32 | fputs(_(" -r, --random generate random-based uuid\n"), out); | |
33 | fputs(_(" -t, --time generate time-based uuid\n"), out); | |
34 | fputs(USAGE_SEPARATOR, out); | |
35 | fputs(USAGE_HELP, out); | |
36 | fputs(USAGE_VERSION, out); | |
37 | fprintf(out, USAGE_MAN_TAIL("uuidgen(1)")); | |
38 | exit(EXIT_SUCCESS); | |
39 | } | |
40 | ||
41 | int | |
42 | main (int argc, char *argv[]) | |
43 | { | |
44 | int c; | |
45 | int do_type = 0; | |
46 | char str[37]; | |
47 | uuid_t uu; | |
48 | ||
49 | static const struct option longopts[] = { | |
50 | {"random", no_argument, NULL, 'r'}, | |
51 | {"time", no_argument, NULL, 't'}, | |
52 | {"version", no_argument, NULL, 'V'}, | |
53 | {"help", no_argument, NULL, 'h'}, | |
54 | {NULL, 0, NULL, 0} | |
55 | }; | |
56 | ||
57 | setlocale(LC_ALL, ""); | |
58 | bindtextdomain(PACKAGE, LOCALEDIR); | |
59 | textdomain(PACKAGE); | |
60 | atexit(close_stdout); | |
61 | ||
62 | while ((c = getopt_long(argc, argv, "rtVh", longopts, NULL)) != -1) | |
63 | switch (c) { | |
64 | case 't': | |
65 | do_type = UUID_TYPE_DCE_TIME; | |
66 | break; | |
67 | case 'r': | |
68 | do_type = UUID_TYPE_DCE_RANDOM; | |
69 | break; | |
70 | case 'V': | |
71 | printf(UTIL_LINUX_VERSION); | |
72 | return EXIT_SUCCESS; | |
73 | case 'h': | |
74 | usage(); | |
75 | default: | |
76 | errtryhelp(EXIT_FAILURE); | |
77 | } | |
78 | ||
79 | switch (do_type) { | |
80 | case UUID_TYPE_DCE_TIME: | |
81 | uuid_generate_time(uu); | |
82 | break; | |
83 | case UUID_TYPE_DCE_RANDOM: | |
84 | uuid_generate_random(uu); | |
85 | break; | |
86 | default: | |
87 | uuid_generate(uu); | |
88 | break; | |
89 | } | |
90 | ||
91 | uuid_unparse(uu, str); | |
92 | ||
93 | printf("%s\n", str); | |
94 | ||
95 | return EXIT_SUCCESS; | |
96 | } |