]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/uuidgen.c
merge coreutils' last GPL2 version of su.c
[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", required_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(_("%s from %s\n"),
78 program_invocation_short_name,
79 PACKAGE_STRING);
80 return EXIT_SUCCESS;
81 case 'h':
82 usage(stdout);
83 default:
84 usage(stderr);
85 }
86
87 switch (do_type) {
88 case DO_TYPE_TIME:
89 uuid_generate_time(uu);
90 break;
91 case DO_TYPE_RANDOM:
92 uuid_generate_random(uu);
93 break;
94 default:
95 uuid_generate(uu);
96 break;
97 }
98
99 uuid_unparse(uu, str);
100
101 printf("%s\n", str);
102
103 return EXIT_SUCCESS;
104 }