1 /* $NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 2002 Marcel Moolenaar
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
58 #include <sys/cdefs.h>
59 __RCSID("$NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $");
71 (void)fprintf(stderr
, "usage: %s [-1s] [-n count] [-o filename]\n",
77 main(int argc
, char *argv
[])
82 int ch
, count
, i
, iterate
, c_struct
;
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 */
89 while ((ch
= getopt(argc
, argv
, "1n:o:s")) != -1) {
97 count
= strtol(optarg
, &p
, 10);
98 if (*p
!= 0 || count
< 1)
103 errx(1, "multiple output files not allowed");
104 fp
= fopen(optarg
, "w");
124 store
= (uuid_t
*)malloc(sizeof(uuid_t
) * count
);
129 /* Get them all in a single batch */
130 if (uuidgen(store
, count
) != 0)
134 for (i
= 0; i
< count
; i
++) {
135 if (uuidgen(uuid
++, 1) != 0)
142 uuid_to_string(uuid
++, &p
, NULL
);
144 fprintf(fp
, "= { /* %s */\n", p
); /* } */
146 * Chunk up the string for processing:
148 * aaaaaaaa-bbbb-cccc-dddd-0123456789ab
150 * We output it like so:
152 * = { \/\* aaaaaaaa-bbbb-cccc-ddee-0123456789ab \*\/
158 * { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
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");
177 fprintf(fp
, "%s\n", p
);