]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/uuidgen.c
textual: use UTIL_LINUX_VERSION everywhere
[thirdparty/util-linux.git] / misc-utils / uuidgen.c
CommitLineData
0140c397
KZ
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
19extern int getopt(int argc, char * const argv[], const char *optstring);
20extern char *optarg;
21extern int optind;
22#endif
23
24#include "uuid.h"
25#include "nls.h"
0379a55d 26#include "c.h"
c05a80ca 27#include "closestream.h"
0140c397
KZ
28
29#define DO_TYPE_TIME 1
30#define DO_TYPE_RANDOM 2
31
0379a55d 32static void __attribute__ ((__noreturn__)) usage(FILE * out)
0140c397 33{
44697763
KZ
34 fputs(_("\nUsage:\n"), out);
35 fprintf(out,
36 _(" %s [options]\n"), program_invocation_short_name);
0379a55d 37
44697763
KZ
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);
0379a55d
SK
43
44 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
0140c397
KZ
45}
46
47int
48main (int argc, char *argv[])
49{
50 int c;
51 int do_type = 0;
52 char str[37];
53 uuid_t uu;
54
0379a55d 55 static const struct option longopts[] = {
71383195 56 {"random", no_argument, NULL, 'r'},
0379a55d
SK
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
0140c397
KZ
63 setlocale(LC_ALL, "");
64 bindtextdomain(PACKAGE, LOCALEDIR);
65 textdomain(PACKAGE);
c05a80ca 66 atexit(close_stdout);
0140c397 67
0379a55d 68 while ((c = getopt_long(argc, argv, "rtVh", longopts, NULL)) != -1)
0140c397
KZ
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;
0379a55d 76 case 'V':
e421313d 77 printf(UTIL_LINUX_VERSION);
0379a55d
SK
78 return EXIT_SUCCESS;
79 case 'h':
80 usage(stdout);
0140c397 81 default:
0379a55d 82 usage(stderr);
0140c397
KZ
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
0379a55d 101 return EXIT_SUCCESS;
0140c397 102}