197372Smarcel/*
297372Smarcel * Copyright (c) 2002 Marcel Moolenaar
397372Smarcel * All rights reserved.
497372Smarcel *
597372Smarcel * Redistribution and use in source and binary forms, with or without
697372Smarcel * modification, are permitted provided that the following conditions
797372Smarcel * are met:
897372Smarcel *
997372Smarcel * 1. Redistributions of source code must retain the above copyright
1097372Smarcel *    notice, this list of conditions and the following disclaimer.
1197372Smarcel * 2. Redistributions in binary form must reproduce the above copyright
1297372Smarcel *    notice, this list of conditions and the following disclaimer in the
1397372Smarcel *    documentation and/or other materials provided with the distribution.
1497372Smarcel *
1597372Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1697372Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1797372Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1897372Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1997372Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2097372Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2197372Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2297372Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2397372Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2497372Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2597372Smarcel *
2697372Smarcel */
2797372Smarcel
2897372Smarcel#include <sys/cdefs.h>
2997372Smarcel__FBSDID("$FreeBSD$");
3097372Smarcel
3197372Smarcel#include <err.h>
3297372Smarcel#include <stdio.h>
3397372Smarcel#include <stdlib.h>
3497372Smarcel#include <unistd.h>
35106283Smarcel#include <uuid.h>
3697372Smarcel
3797372Smarcelstatic void
3897372Smarcelusage(void)
3997372Smarcel{
40112252Smarcel	(void)fprintf(stderr, "usage: uuidgen [-1] [-n count] [-o filename]\n");
4197372Smarcel	exit(1);
4297372Smarcel}
4397372Smarcel
4497372Smarcelint
4597372Smarcelmain(int argc, char *argv[])
4697372Smarcel{
47112252Smarcel	FILE *fp;
4897372Smarcel	uuid_t *store, *uuid;
4997372Smarcel	char *p;
5097372Smarcel	int ch, count, i, iterate;
5197372Smarcel
5297372Smarcel	count = -1;	/* no count yet */
53112252Smarcel	fp = stdout;	/* default output file */
5497372Smarcel	iterate = 0;	/* not one at a time */
55112252Smarcel	while ((ch = getopt(argc, argv, "1n:o:")) != -1)
5697372Smarcel		switch (ch) {
5797372Smarcel		case '1':
5897372Smarcel			iterate = 1;
5997372Smarcel			break;
6097372Smarcel		case 'n':
6197372Smarcel			if (count > 0)
6297372Smarcel				usage();
6397372Smarcel			count = strtol(optarg, &p, 10);
6497372Smarcel			if (*p != 0 || count < 1)
6597372Smarcel				usage();
6697372Smarcel			break;
67112252Smarcel		case 'o':
68112252Smarcel			if (fp != stdout)
69112252Smarcel				errx(1, "multiple output files not allowed");
70112252Smarcel			fp = fopen(optarg, "w");
71112252Smarcel			if (fp == NULL)
72112252Smarcel				err(1, "fopen");
73112252Smarcel			break;
7497372Smarcel		default:
7597372Smarcel			usage();
7697372Smarcel		}
7797372Smarcel	argv += optind;
7897372Smarcel	argc -= optind;
7997372Smarcel
8097372Smarcel	if (argc)
8197372Smarcel		usage();
8297372Smarcel
8397372Smarcel	if (count == -1)
8497372Smarcel		count = 1;
8597372Smarcel
8697372Smarcel	store = (uuid_t*)malloc(sizeof(uuid_t) * count);
8797372Smarcel	if (store == NULL)
8897372Smarcel		err(1, "malloc()");
8997372Smarcel
9097372Smarcel	if (!iterate) {
9197372Smarcel		/* Get them all in a single batch */
9297372Smarcel		if (uuidgen(store, count) != 0)
9397372Smarcel			err(1, "uuidgen()");
9497372Smarcel	} else {
9597372Smarcel		uuid = store;
9697372Smarcel		for (i = 0; i < count; i++) {
9797372Smarcel			if (uuidgen(uuid++, 1) != 0)
9897372Smarcel				err(1, "uuidgen()");
9997372Smarcel		}
10097372Smarcel	}
10197372Smarcel
10297372Smarcel	uuid = store;
103106283Smarcel	while (count--) {
104106283Smarcel		uuid_to_string(uuid++, &p, NULL);
105112252Smarcel		fprintf(fp, "%s\n", p);
106106283Smarcel		free(p);
107106283Smarcel	}
10897372Smarcel
10997372Smarcel	free(store);
110112252Smarcel	if (fp != stdout)
111112252Smarcel		fclose(fp);
11297372Smarcel	return (0);
11397372Smarcel}
114