]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/uuidgen.c
textual: use UTIL_LINUX_VERSION everywhere
[thirdparty/util-linux.git] / misc-utils / uuidgen.c
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 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #ifdef HAVE_GETOPT_H
17 #include <getopt.h>
18 #else
19 extern int getopt(int argc, char * const argv[], const char *optstring);
20 extern char *optarg;
21 extern int optind;
22 #endif
23
24 #include "uuid.h"
25 #include "nls.h"
26 #include "c.h"
27 #include "closestream.h"
28
29 #define DO_TYPE_TIME 1
30 #define DO_TYPE_RANDOM 2
31
32 static void __attribute__ ((__noreturn__)) usage(FILE * out)
33 {
34 fputs(_("\nUsage:\n"), out);
35 fprintf(out,
36 _(" %s [options]\n"), program_invocation_short_name);
37
38 fputs(_("\nOptions:\n"), out);
39 fputs(_(" -r, --random generate random-based uuid\n"
40 " -t, --time generate time-based uuid\n"
41 " -V, --version output version information and exit\n"
42 " -h, --help display this help and exit\n\n"), out);
43
44 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
45 }
46
47 int
48 main (int argc, char *argv[])
49 {
50 int c;
51 int do_type = 0;
52 char str[37];
53 uuid_t uu;
54
55 static const struct option longopts[] = {
56 {"random", no_argument, NULL, 'r'},
57 {"time", no_argument, NULL, 't'},
58 {"version", no_argument, NULL, 'V'},
59 {"help", no_argument, NULL, 'h'},
60 {NULL, 0, NULL, 0}
61 };
62
63 setlocale(LC_ALL, "");
64 bindtextdomain(PACKAGE, LOCALEDIR);
65 textdomain(PACKAGE);
66 atexit(close_stdout);
67
68 while ((c = getopt_long(argc, argv, "rtVh", longopts, NULL)) != -1)
69 switch (c) {
70 case 't':
71 do_type = DO_TYPE_TIME;
72 break;
73 case 'r':
74 do_type = DO_TYPE_RANDOM;
75 break;
76 case 'V':
77 printf(UTIL_LINUX_VERSION);
78 return EXIT_SUCCESS;
79 case 'h':
80 usage(stdout);
81 default:
82 usage(stderr);
83 }
84
85 switch (do_type) {
86 case DO_TYPE_TIME:
87 uuid_generate_time(uu);
88 break;
89 case DO_TYPE_RANDOM:
90 uuid_generate_random(uu);
91 break;
92 default:
93 uuid_generate(uu);
94 break;
95 }
96
97 uuid_unparse(uu, str);
98
99 printf("%s\n", str);
100
101 return EXIT_SUCCESS;
102 }