uuidgen.c revision 97372
1/*
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $Id$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/bin/uuidgen/uuidgen.c 97372 2002-05-28 06:16:08Z marcel $");
31
32#include <sys/types.h>
33#include <sys/endian.h>
34#include <sys/uuid.h>
35
36#include <err.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40
41static void
42usage(void)
43{
44	(void)fprintf(stderr, "usage: uuidgen [-1] [-n count]\n");
45	exit(1);
46}
47
48static void
49uuid_print(uuid_t *uuid)
50{
51	printf("%08x-%04x-%04x-%02x%02x-", uuid->time_low, uuid->time_mid,
52	    uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
53	    uuid->clock_seq_low);
54	printf("%02x%02x%02x%02x%02x%02x\n", uuid->node[0], uuid->node[1],
55	    uuid->node[2], uuid->node[3], uuid->node[4], uuid->node[5]);
56}
57
58int
59main(int argc, char *argv[])
60{
61	uuid_t *store, *uuid;
62	char *p;
63	int ch, count, i, iterate;
64
65	count = -1;	/* no count yet */
66	iterate = 0;	/* not one at a time */
67	while ((ch = getopt(argc, argv, "1n:")) != -1)
68		switch (ch) {
69		case '1':
70			iterate = 1;
71			break;
72		case 'n':
73			if (count > 0)
74				usage();
75			count = strtol(optarg, &p, 10);
76			if (*p != 0 || count < 1)
77				usage();
78			break;
79		default:
80			usage();
81		}
82	argv += optind;
83	argc -= optind;
84
85	if (argc)
86		usage();
87
88	if (count == -1)
89		count = 1;
90
91	store = (uuid_t*)malloc(sizeof(uuid_t) * count);
92	if (store == NULL)
93		err(1, "malloc()");
94
95	if (!iterate) {
96		/* Get them all in a single batch */
97		if (uuidgen(store, count) != 0)
98			err(1, "uuidgen()");
99	} else {
100		uuid = store;
101		for (i = 0; i < count; i++) {
102			if (uuidgen(uuid++, 1) != 0)
103				err(1, "uuidgen()");
104		}
105	}
106
107	uuid = store;
108	while (count--)
109		uuid_print(uuid++);
110
111	free(store);
112	return (0);
113}
114