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