3252 Need a proper flock() implementation
[unleashed.git] / usr / src / cmd / uuidgen / uuidgen.c
blobeb7794650d1ee3502c549a1f18feaed70e60b8fc
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <libgen.h>
19 #include <uuid/uuid.h>
20 #include <getopt.h>
21 #include <locale.h>
23 static char *progname;
24 static int rflag, tflag;
25 static char uu_string[UUID_PRINTABLE_STRING_LENGTH];
27 static void
28 usage(void)
30 (void) fprintf(stderr, gettext(
31 "Usage: %s [-r | -t] [-o filename]\n"), progname);
32 exit(1);
35 int
36 main(int argc, char *argv[])
38 FILE *out;
39 uuid_t uu = { 0 };
40 int c;
42 (void) setlocale(LC_ALL, "");
44 #if !defined(TEXT_DOMAIN)
45 #define TEXT_DOMAIN "SYS_TEST"
46 #endif
47 (void) textdomain(TEXT_DOMAIN);
49 progname = basename(argv[0]);
50 out = stdout;
51 while ((c = getopt(argc, argv, ":rto:")) != EOF) {
52 switch ((char)c) {
53 case 'r':
54 rflag++;
55 break;
56 case 't':
57 tflag++;
58 break;
59 case 'o':
60 if ((out = fopen(optarg, "w")) == NULL) {
61 (void) fprintf(stderr, gettext(
62 "%s: cannot open %s\n"),
63 progname, optarg);
64 return (1);
66 break;
67 case '?': /* fallthrough */
68 default:
69 usage();
73 if ((rflag && tflag) || optind != argc) {
74 usage();
77 if (rflag) {
78 /* DCE version 4 */
79 uuid_generate_random(uu);
80 } else if (tflag) {
81 /* DCE version 1 */
82 uuid_generate_time(uu);
83 } else {
84 uuid_generate(uu);
87 if (uuid_is_null(uu) != 0) {
88 (void) fprintf(stderr, gettext(
89 "%s: failed to "
90 "generate uuid\n"), progname);
91 exit(1);
94 uuid_unparse(uu, uu_string);
96 (void) fprintf(out, "%s\n", uu_string);
98 if (out != NULL && out != stdout)
99 (void) fclose(out);
101 return (0);