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