]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/uuidgen.c
Use --help suggestion on invalid option
[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>
0140c397 13#include <stdlib.h>
0140c397 14#include <getopt.h>
0140c397
KZ
15
16#include "uuid.h"
17#include "nls.h"
0379a55d 18#include "c.h"
c05a80ca 19#include "closestream.h"
0140c397 20
0379a55d 21static void __attribute__ ((__noreturn__)) usage(FILE * out)
0140c397 22{
db433bf7 23 fputs(USAGE_HEADER, out);
44697763
KZ
24 fprintf(out,
25 _(" %s [options]\n"), program_invocation_short_name);
0379a55d 26
451dbcfa
BS
27 fputs(USAGE_SEPARATOR, out);
28 fputs(_("Create a new UUID value.\n"), out);
29
db433bf7 30 fputs(USAGE_OPTIONS, out);
44697763
KZ
31 fputs(_(" -r, --random generate random-based uuid\n"
32 " -t, --time generate time-based uuid\n"
33 " -V, --version output version information and exit\n"
34 " -h, --help display this help and exit\n\n"), out);
0379a55d 35
a587cc55 36 fprintf(out, USAGE_MAN_TAIL("uuidgen(1)"));
0379a55d 37 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
0140c397
KZ
38}
39
40int
41main (int argc, char *argv[])
42{
43 int c;
44 int do_type = 0;
45 char str[37];
46 uuid_t uu;
47
0379a55d 48 static const struct option longopts[] = {
71383195 49 {"random", no_argument, NULL, 'r'},
0379a55d
SK
50 {"time", no_argument, NULL, 't'},
51 {"version", no_argument, NULL, 'V'},
52 {"help", no_argument, NULL, 'h'},
53 {NULL, 0, NULL, 0}
54 };
55
0140c397
KZ
56 setlocale(LC_ALL, "");
57 bindtextdomain(PACKAGE, LOCALEDIR);
58 textdomain(PACKAGE);
c05a80ca 59 atexit(close_stdout);
0140c397 60
0379a55d 61 while ((c = getopt_long(argc, argv, "rtVh", longopts, NULL)) != -1)
0140c397
KZ
62 switch (c) {
63 case 't':
6328799d 64 do_type = UUID_TYPE_DCE_TIME;
0140c397
KZ
65 break;
66 case 'r':
6328799d 67 do_type = UUID_TYPE_DCE_RANDOM;
0140c397 68 break;
0379a55d 69 case 'V':
e421313d 70 printf(UTIL_LINUX_VERSION);
0379a55d
SK
71 return EXIT_SUCCESS;
72 case 'h':
73 usage(stdout);
0140c397 74 default:
677ec86c 75 errtryhelp(EXIT_FAILURE);
0140c397
KZ
76 }
77
78 switch (do_type) {
6328799d 79 case UUID_TYPE_DCE_TIME:
0140c397
KZ
80 uuid_generate_time(uu);
81 break;
6328799d 82 case UUID_TYPE_DCE_RANDOM:
0140c397
KZ
83 uuid_generate_random(uu);
84 break;
85 default:
86 uuid_generate(uu);
87 break;
88 }
89
90 uuid_unparse(uu, str);
91
92 printf("%s\n", str);
93
0379a55d 94 return EXIT_SUCCESS;
0140c397 95}