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