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