xref: /netbsd-src/usr.bin/uuidgen/uuidgen.c (revision 6818646ac81b767d53e3d0745d2267de0bb68492)
1*6818646aSjoerg /*	$NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $	*/
21209149aSthorpej 
31209149aSthorpej /*-
41209149aSthorpej  * Copyright (c) 2004 The NetBSD Foundation, Inc.
51209149aSthorpej  * All rights reserved.
61209149aSthorpej  *
71209149aSthorpej  * This code is derived from software contributed to The NetBSD Foundation
81209149aSthorpej  * by Jason R. Thorpe.
91209149aSthorpej  *
101209149aSthorpej  * Redistribution and use in source and binary forms, with or without
111209149aSthorpej  * modification, are permitted provided that the following conditions
121209149aSthorpej  * are met:
131209149aSthorpej  * 1. Redistributions of source code must retain the above copyright
141209149aSthorpej  *    notice, this list of conditions and the following disclaimer.
151209149aSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
161209149aSthorpej  *    notice, this list of conditions and the following disclaimer in the
171209149aSthorpej  *    documentation and/or other materials provided with the distribution.
181209149aSthorpej  *
191209149aSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201209149aSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211209149aSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221209149aSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231209149aSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241209149aSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251209149aSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261209149aSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271209149aSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281209149aSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291209149aSthorpej  * POSSIBILITY OF SUCH DAMAGE.
301209149aSthorpej  */
311209149aSthorpej 
321209149aSthorpej /*
331209149aSthorpej  * Copyright (c) 2002 Marcel Moolenaar
341209149aSthorpej  * All rights reserved.
351209149aSthorpej  *
361209149aSthorpej  * Redistribution and use in source and binary forms, with or without
371209149aSthorpej  * modification, are permitted provided that the following conditions
381209149aSthorpej  * are met:
391209149aSthorpej  *
401209149aSthorpej  * 1. Redistributions of source code must retain the above copyright
411209149aSthorpej  *    notice, this list of conditions and the following disclaimer.
421209149aSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
431209149aSthorpej  *    notice, this list of conditions and the following disclaimer in the
441209149aSthorpej  *    documentation and/or other materials provided with the distribution.
451209149aSthorpej  *
461209149aSthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
471209149aSthorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
481209149aSthorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
491209149aSthorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
501209149aSthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
511209149aSthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
521209149aSthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
531209149aSthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
541209149aSthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
551209149aSthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
561209149aSthorpej  */
571209149aSthorpej 
581209149aSthorpej #include <sys/cdefs.h>
59*6818646aSjoerg __RCSID("$NetBSD: uuidgen.c,v 1.4 2011/09/16 15:39:30 joerg Exp $");
601209149aSthorpej 
611209149aSthorpej #include <err.h>
621209149aSthorpej #include <stdio.h>
631209149aSthorpej #include <stdlib.h>
641209149aSthorpej #include <unistd.h>
651209149aSthorpej #include <uuid.h>
661209149aSthorpej 
67*6818646aSjoerg __dead static void
usage(void)681209149aSthorpej usage(void)
691209149aSthorpej {
701209149aSthorpej 
71bf626dd2Swiz 	(void)fprintf(stderr, "usage: %s [-1s] [-n count] [-o filename]\n",
721209149aSthorpej 	    getprogname());
731209149aSthorpej 	exit(1);
741209149aSthorpej }
751209149aSthorpej 
761209149aSthorpej int
main(int argc,char * argv[])771209149aSthorpej main(int argc, char *argv[])
781209149aSthorpej {
791209149aSthorpej 	FILE *fp;
801209149aSthorpej 	uuid_t *store, *uuid;
811209149aSthorpej 	char *p;
821209149aSthorpej 	int ch, count, i, iterate, c_struct;
831209149aSthorpej 
841209149aSthorpej 	count = -1;	/* no count yet */
851209149aSthorpej 	fp = stdout;	/* default output file */
861209149aSthorpej 	iterate = 0;	/* not one at a time */
871209149aSthorpej 	c_struct = 0;	/* not as a C structure */
881209149aSthorpej 
891209149aSthorpej 	while ((ch = getopt(argc, argv, "1n:o:s")) != -1) {
901209149aSthorpej 		switch (ch) {
911209149aSthorpej 		case '1':
921209149aSthorpej 			iterate = 1;
931209149aSthorpej 			break;
941209149aSthorpej 		case 'n':
951209149aSthorpej 			if (count > 0)
961209149aSthorpej 				usage();
971209149aSthorpej 			count = strtol(optarg, &p, 10);
981209149aSthorpej 			if (*p != 0 || count < 1)
991209149aSthorpej 				usage();
1001209149aSthorpej 			break;
1011209149aSthorpej 		case 'o':
1021209149aSthorpej 			if (fp != stdout)
1031209149aSthorpej 				errx(1, "multiple output files not allowed");
1041209149aSthorpej 			fp = fopen(optarg, "w");
1051209149aSthorpej 			if (fp == NULL)
1061209149aSthorpej 				err(1, "fopen");
1071209149aSthorpej 			break;
1081209149aSthorpej 		case 's':
1091209149aSthorpej 			c_struct = 1;
1101209149aSthorpej 			break;
1111209149aSthorpej 		default:
1121209149aSthorpej 			usage();
1131209149aSthorpej 		}
1141209149aSthorpej 	}
1151209149aSthorpej 	argv += optind;
1161209149aSthorpej 	argc -= optind;
1171209149aSthorpej 
1181209149aSthorpej 	if (argc)
1191209149aSthorpej 		usage();
1201209149aSthorpej 
1211209149aSthorpej 	if (count == -1)
1221209149aSthorpej 		count = 1;
1231209149aSthorpej 
1241209149aSthorpej 	store = (uuid_t*)malloc(sizeof(uuid_t) * count);
1251209149aSthorpej 	if (store == NULL)
1261209149aSthorpej 		err(1, "malloc()");
1271209149aSthorpej 
1281209149aSthorpej 	if (!iterate) {
1291209149aSthorpej 		/* Get them all in a single batch */
1301209149aSthorpej 		if (uuidgen(store, count) != 0)
1311209149aSthorpej 			err(1, "uuidgen()");
1321209149aSthorpej 	} else {
1331209149aSthorpej 		uuid = store;
1341209149aSthorpej 		for (i = 0; i < count; i++) {
1351209149aSthorpej 			if (uuidgen(uuid++, 1) != 0)
1361209149aSthorpej 				err(1, "uuidgen()");
1371209149aSthorpej 		}
1381209149aSthorpej 	}
1391209149aSthorpej 
1401209149aSthorpej 	uuid = store;
1411209149aSthorpej 	while (count--) {
1421209149aSthorpej 		uuid_to_string(uuid++, &p, NULL);
1431209149aSthorpej 		if (c_struct) {
1441209149aSthorpej 			fprintf(fp, "= { /* %s */\n", p);	/* } */
1451209149aSthorpej 			/*
1461209149aSthorpej 			 * Chunk up the string for processing:
1471209149aSthorpej 			 *
1481209149aSthorpej 			 *	aaaaaaaa-bbbb-cccc-dddd-0123456789ab
1491209149aSthorpej 			 *
1501209149aSthorpej 			 * We output it like so:
1511209149aSthorpej 			 *
1521209149aSthorpej 			 * = { \/\* aaaaaaaa-bbbb-cccc-ddee-0123456789ab \*\/
1531209149aSthorpej 			 *	0xaaaaaaaa,
1541209149aSthorpej 			 *	0xbbbb,
1551209149aSthorpej 			 *	0xcccc,
1561209149aSthorpej 			 *	0xdd,
1571209149aSthorpej 			 *	0xee,
1581209149aSthorpej 			 *	{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab }
1591209149aSthorpej 			 * };
1601209149aSthorpej 			 */
1611209149aSthorpej 			p[8] = '\0';	/* aaaaaaaa */
1621209149aSthorpej 			p[13] = '\0';	/* bbbb */
1631209149aSthorpej 			p[18] = '\0';	/* cccc */
1641209149aSthorpej 			p[23] = '\0';	/* dddd */
1651209149aSthorpej 			fprintf(fp, "\t0x%s,\n", p);
1661209149aSthorpej 			fprintf(fp, "\t0x%s,\n", &p[9]);
1671209149aSthorpej 			fprintf(fp, "\t0x%s,\n", &p[14]);
1681209149aSthorpej 			fprintf(fp, "\t0x%c%c,\n", p[19], p[20]);
1691209149aSthorpej 			fprintf(fp, "\t0x%c%c,\n", p[21], p[22]);
1701209149aSthorpej 			fprintf(fp, "\t{ 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, "
1711209149aSthorpej 					"0x%c%c, 0x%c%c }\n",
1721209149aSthorpej 				p[24], p[25], p[26], p[27],
1731209149aSthorpej 				p[28], p[29], p[30], p[31],
1741209149aSthorpej 				p[32], p[33], p[34], p[35]);
1751209149aSthorpej 	/* { */		fprintf(fp, "};\n");
1761209149aSthorpej 		} else
1771209149aSthorpej 			fprintf(fp, "%s\n", p);
1781209149aSthorpej 		free(p);
1791209149aSthorpej 	}
1801209149aSthorpej 
1811209149aSthorpej 	free(store);
1821209149aSthorpej 	if (fp != stdout)
1831209149aSthorpej 		fclose(fp);
1841209149aSthorpej 	return (0);
1851209149aSthorpej }
186