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.
   6  *
   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.
  10  */
  11 
  12 /*
  13  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  14  */
  15 
  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>
  22 
  23 static char *progname;
  24 static int rflag, tflag;
  25 static char uu_string[UUID_PRINTABLE_STRING_LENGTH];
  26 
  27 static void
  28 usage(void)
  29 {
  30         (void) fprintf(stderr, gettext(
  31             "Usage: %s [-r | -t] [-o filename]\n"), progname);
  32         exit(1);
  33 }
  34 
  35 int
  36 main(int argc, char *argv[])
  37 {
  38         FILE *out;
  39         uuid_t  uu = { 0 };
  40         int c;
  41 
  42         (void) setlocale(LC_ALL, "");
  43 
  44 #if !defined(TEXT_DOMAIN)
  45 #define TEXT_DOMAIN "SYS_TEST"
  46 #endif
  47         (void) textdomain(TEXT_DOMAIN);
  48 
  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);
  65                         }
  66                         break;
  67                 case '?': /* fallthrough */
  68                 default:
  69                         usage();
  70                 }
  71         }
  72 
  73         if ((rflag && tflag) || optind != argc) {
  74                 usage();
  75         }
  76 
  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);
  85         }
  86 
  87         if (uuid_is_null(uu) != 0) {
  88                 (void) fprintf(stderr, gettext(
  89                     "%s: failed to "
  90                     "generate uuid\n"), progname);
  91                 exit(1);
  92         }
  93 
  94         uuid_unparse(uu, uu_string);
  95 
  96         (void) fprintf(out, "%s\n", uu_string);
  97 
  98         if (out != NULL && out != stdout)
  99                 (void) fclose(out);
 100 
 101         return (0);
 102 }