]> pd.if.org Git - uuid/blob - uuidgen.c
die if no namespace provided for v3 or v5 uuid
[uuid] / uuidgen.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "pduuid.h"
7 #include "warn.h"
8
9 void print_help() {
10         printf("uuidgen [-rthV]\n");
11 }
12 void print_version() {
13         printf("uuidgen version 1.0\n");
14 }
15
16 int main(int ac, char *av[]) {
17         pd_uuid_t       u; /* generated uuid */
18         pd_uuid_t       nsuuid; /* namespace uuid */
19         pd_uuid_t       *nsid = 0; /* pointer to namespace uuid */
20         char *ns = 0; /* literal ns id string */
21
22         struct pd_uuid_state    s;
23         char uuid[PD_UUID_STRLEN+1];
24         int q = 1;
25         int opt;
26         int mode = 4;
27         int i;
28
29         /*
30          * V = version
31          * h = help
32          * q = quantity
33          *
34          * t = time, v1
35          * T = time with random mac
36          * r = random, v4
37          * m = md5 ns, v3
38          * s = sha1 ns, v5
39          * v2 isn't used
40          * v = set version
41          *
42          * n = namespace
43          * D = DNS ns
44          * U = URL ns
45          * O = OID ns
46          * X = X.500 ns
47          */
48
49         while ((opt = getopt(ac, av, "Vhq:tTrmsv:n:DUOX")) != -1) {
50                 switch (opt) {
51                         case 'n':
52                                 ns = optarg;
53                                 if (mode != 3 && mode != 5) mode = 5;
54                                 break;
55                         case 'D':
56                                 nsid = &pd_uuid_ns_dns;
57                                 break;
58                         case 'U':
59                                 nsid = &pd_uuid_ns_url;
60                                 break;
61                         case 'O':
62                                 nsid = &pd_uuid_ns_oid;
63                                 break;
64                         case 'X':
65                                 nsid = &pd_uuid_ns_x500;
66                                 break;
67                         case 'r':
68                                 mode = 4;
69                                 break;
70                         case 't':
71                                 mode = 1;
72                                 break;
73                         case 'm':
74                                 mode = 3;
75                                 break;
76                         case 's':
77                                 mode = 5;
78                                 break;
79                         case 'T':
80                                 mode = -1;
81                                 break;
82                         case 'h':
83                                 print_help();
84                                 exit(EXIT_SUCCESS);
85                                 break;
86                         case 'v':
87                                 mode = atoi(optarg);
88                                 break;
89                         case 'V':
90                                 print_version();
91                                 exit(EXIT_SUCCESS);
92                                 break;
93                         case 'q':
94                                 q = atoi(optarg);
95                                 break;
96                         default:
97                                 warn("uuidgen: unknown option '%c'", opt);
98                                 print_help();
99                                 exit(EXIT_FAILURE);
100                 }
101         }
102
103         pd_uuid_init_state(&s);
104
105         if (mode == 3 || mode == 5) {
106                 /* TODO warn if q != 1 */
107                 int len;
108                 if (!nsid) {
109                         if (!ns) {
110                                 die("must specify namespace for version %d uuids", mode);
111                         }
112                         if (!strcmp(ns, "dns")) {
113                                 nsid = &pd_uuid_ns_dns;
114                         } else if (!strcmp(ns, "url")) {
115                                 nsid = &pd_uuid_ns_url;
116                         } else if (!strcmp(ns, "oid")) {
117                                 nsid = &pd_uuid_ns_oid;
118                         } else if (!strcmp(ns, "x500")) {
119                                 nsid = &pd_uuid_ns_x500;
120                         } else {
121                                 nsid = &nsuuid;
122                                 if (pd_uuid_set_string(nsid, ns) != 0) {
123                                         die("malformed ns uuid: %s\n", ns);
124                                 }
125                         }
126                 }
127                 for (i = optind; i < ac; i++) { 
128                         len = strlen(av[i]);
129                         if (mode == 3) {
130                                 pd_uuid_get_string(nsid, uuid);
131                                 pd_uuid_make_v3(0, &u, nsid, av[i], len);
132                         } else {
133                                 pd_uuid_make_v5(&s, &u, nsid, av[i], len);
134                         }
135                         pd_uuid_get_string(&u, uuid);
136                         printf("%s\n", uuid);
137                 }
138                 exit(EXIT_SUCCESS);
139         }
140
141         while (q--) {
142                 switch (mode) {
143                         case -1:
144                                 pd_uuid_make_v1mc(&s, &u);
145                                 break;
146                         case 1:
147                                 pd_uuid_make_v1(&s, &u);
148                                 break;
149                         case 4:
150                                 pd_uuid_make_v4(&s, &u);
151                                 break;
152                         default:
153                                 die("uuidgen: unknown uuid version '%d'\n", mode);
154                 }
155                 pd_uuid_get_string(&u, uuid);
156                 printf("%s\n", uuid);
157         }
158
159         return 0;
160
161 }