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