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