1 1.4 joerg /* $NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Jason R. Thorpe. 9 1.1 thorpej * 10 1.1 thorpej * Redistribution and use in source and binary forms, with or without 11 1.1 thorpej * modification, are permitted provided that the following conditions 12 1.1 thorpej * are met: 13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer. 15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.1 thorpej * documentation and/or other materials provided with the distribution. 18 1.1 thorpej * 19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.1 thorpej */ 31 1.1 thorpej 32 1.1 thorpej /* 33 1.1 thorpej * Copyright (c) 2002 Marcel Moolenaar 34 1.1 thorpej * All rights reserved. 35 1.1 thorpej * 36 1.1 thorpej * Redistribution and use in source and binary forms, with or without 37 1.1 thorpej * modification, are permitted provided that the following conditions 38 1.1 thorpej * are met: 39 1.1 thorpej * 40 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 41 1.1 thorpej * notice, this list of conditions and the following disclaimer. 42 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 43 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 44 1.1 thorpej * documentation and/or other materials provided with the distribution. 45 1.1 thorpej * 46 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 1.1 thorpej */ 57 1.1 thorpej 58 1.1 thorpej #include <sys/cdefs.h> 59 1.4 joerg __RCSID("$NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $"); 60 1.1 thorpej 61 1.1 thorpej #include <err.h> 62 1.1 thorpej #include <stdio.h> 63 1.1 thorpej #include <stdlib.h> 64 1.1 thorpej #include <unistd.h> 65 1.1 thorpej #include <uuid.h> 66 1.1 thorpej 67 1.4 joerg __dead static void 68 1.1 thorpej usage(void) 69 1.1 thorpej { 70 1.1 thorpej 71 1.2 wiz (void)fprintf(stderr, "usage: %s [-1s] [-n count] [-o filename]\n", 72 1.1 thorpej getprogname()); 73 1.1 thorpej exit(1); 74 1.1 thorpej } 75 1.1 thorpej 76 1.1 thorpej int 77 1.1 thorpej main(int argc, char *argv[]) 78 1.1 thorpej { 79 1.1 thorpej FILE *fp; 80 1.1 thorpej uuid_t *store, *uuid; 81 1.1 thorpej char *p; 82 1.1 thorpej int ch, count, i, iterate, c_struct; 83 1.1 thorpej 84 1.1 thorpej count = -1; /* no count yet */ 85 1.1 thorpej fp = stdout; /* default output file */ 86 1.1 thorpej iterate = 0; /* not one at a time */ 87 1.1 thorpej c_struct = 0; /* not as a C structure */ 88 1.1 thorpej 89 1.1 thorpej while ((ch = getopt(argc, argv, "1n:o:s")) != -1) { 90 1.1 thorpej switch (ch) { 91 1.1 thorpej case '1': 92 1.1 thorpej iterate = 1; 93 1.1 thorpej break; 94 1.1 thorpej case 'n': 95 1.1 thorpej if (count > 0) 96 1.1 thorpej usage(); 97 1.1 thorpej count = strtol(optarg, &p, 10); 98 1.1 thorpej if (*p != 0 || count < 1) 99 1.1 thorpej usage(); 100 1.1 thorpej break; 101 1.1 thorpej case 'o': 102 1.1 thorpej if (fp != stdout) 103 1.1 thorpej errx(1, "multiple output files not allowed"); 104 1.1 thorpej fp = fopen(optarg, "w"); 105 1.1 thorpej if (fp == NULL) 106 1.1 thorpej err(1, "fopen"); 107 1.1 thorpej break; 108 1.1 thorpej case 's': 109 1.1 thorpej c_struct = 1; 110 1.1 thorpej break; 111 1.1 thorpej default: 112 1.1 thorpej usage(); 113 1.1 thorpej } 114 1.1 thorpej } 115 1.1 thorpej argv += optind; 116 1.1 thorpej argc -= optind; 117 1.1 thorpej 118 1.1 thorpej if (argc) 119 1.1 thorpej usage(); 120 1.1 thorpej 121 1.1 thorpej if (count == -1) 122 1.1 thorpej count = 1; 123 1.1 thorpej 124 1.1 thorpej store = (uuid_t*)malloc(sizeof(uuid_t) * count); 125 1.1 thorpej if (store == NULL) 126 1.1 thorpej err(1, "malloc()"); 127 1.1 thorpej 128 1.1 thorpej if (!iterate) { 129 1.1 thorpej /* Get them all in a single batch */ 130 1.1 thorpej if (uuidgen(store, count) != 0) 131 1.1 thorpej err(1, "uuidgen()"); 132 1.1 thorpej } else { 133 1.1 thorpej uuid = store; 134 1.1 thorpej for (i = 0; i < count; i++) { 135 1.1 thorpej if (uuidgen(uuid++, 1) != 0) 136 1.1 thorpej err(1, "uuidgen()"); 137 1.1 thorpej } 138 1.1 thorpej } 139 1.1 thorpej 140 1.1 thorpej uuid = store; 141 1.1 thorpej while (count--) { 142 1.1 thorpej uuid_to_string(uuid++, &p, NULL); 143 1.1 thorpej if (c_struct) { 144 1.1 thorpej fprintf(fp, "= { /* %s */\n", p); /* } */ 145 1.1 thorpej /* 146 1.1 thorpej * Chunk up the string for processing: 147 1.1 thorpej * 148 1.1 thorpej * aaaaaaaa-bbbb-cccc-dddd-0123456789ab 149 1.1 thorpej * 150 1.1 thorpej * We output it like so: 151 1.1 thorpej * 152 1.1 thorpej * = { \/\* aaaaaaaa-bbbb-cccc-ddee-0123456789ab \*\/ 153 1.1 thorpej * 0xaaaaaaaa, 154 1.1 thorpej * 0xbbbb, 155 1.1 thorpej * 0xcccc, 156 1.1 thorpej * 0xdd, 157 1.1 thorpej * 0xee, 158 1.1 thorpej * { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } 159 1.1 thorpej * }; 160 1.1 thorpej */ 161 1.1 thorpej p[8] = '\0'; /* aaaaaaaa */ 162 1.1 thorpej p[13] = '\0'; /* bbbb */ 163 1.1 thorpej p[18] = '\0'; /* cccc */ 164 1.1 thorpej p[23] = '\0'; /* dddd */ 165 1.1 thorpej fprintf(fp, "\t0x%s,\n", p); 166 1.1 thorpej fprintf(fp, "\t0x%s,\n", &p[9]); 167 1.1 thorpej fprintf(fp, "\t0x%s,\n", &p[14]); 168 1.1 thorpej fprintf(fp, "\t0x%c%c,\n", p[19], p[20]); 169 1.1 thorpej fprintf(fp, "\t0x%c%c,\n", p[21], p[22]); 170 1.1 thorpej fprintf(fp, "\t{ 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, " 171 1.1 thorpej "0x%c%c, 0x%c%c }\n", 172 1.1 thorpej p[24], p[25], p[26], p[27], 173 1.1 thorpej p[28], p[29], p[30], p[31], 174 1.1 thorpej p[32], p[33], p[34], p[35]); 175 1.1 thorpej /* { */ fprintf(fp, "};\n"); 176 1.1 thorpej } else 177 1.1 thorpej fprintf(fp, "%s\n", p); 178 1.1 thorpej free(p); 179 1.1 thorpej } 180 1.1 thorpej 181 1.1 thorpej free(store); 182 1.1 thorpej if (fp != stdout) 183 1.1 thorpej fclose(fp); 184 1.1 thorpej return (0); 185 1.1 thorpej } 186