File: uuid_cli.c

package info (click to toggle)
ossp-uuid 1.6.2-1.5
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,376 kB
  • ctags: 770
  • sloc: sh: 12,148; ansic: 4,778; makefile: 377; perl: 357; cpp: 256; php: 95; xml: 35
file content (284 lines) | stat: -rw-r--r-- 10,864 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
**  OSSP uuid - Universally Unique Identifier
**  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
**  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
**
**  This file is part of OSSP uuid, a library for the generation
**  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  uuid_cli.c: command line tool
*/

/* own headers */
#include "uuid.h"
#include "uuid_ac.h"

/* system headers */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

/* error handler */
static void
error(int ec, const char *str, ...)
{
    va_list ap;

    va_start(ap, str);
    fprintf(stderr, "uuid:ERROR: ");
    vfprintf(stderr, str, ap);
    fprintf(stderr, "\n");
    va_end(ap);
    exit(ec);
}

/* usage handler */
static void
usage(const char *str, ...)
{
    va_list ap;

    va_start(ap, str);
    if (str != NULL) {
        fprintf(stderr, "uuid:ERROR: ");
        vfprintf(stderr, str, ap);
        fprintf(stderr, "\n");
    }
    fprintf(stderr, "usage: uuid [-v version] [-m] [-n count] [-1] [-F format] [-o filename] [namespace name]\n");
    fprintf(stderr, "usage: uuid -d [-F format] [-o filename] [uuid]\n");
    va_end(ap);
    exit(1);
}

/* main procedure */
int main(int argc, char *argv[])
{
    char uuid_buf_bin[UUID_LEN_BIN];
    char uuid_buf_str[UUID_LEN_STR+1];
    char uuid_buf_siv[UUID_LEN_SIV+1];
    uuid_t *uuid;
    uuid_t *uuid_ns;
    uuid_rc_t rc;
    FILE *fp;
    char *p;
    int ch;
    int count;
    int i;
    int iterate;
    uuid_fmt_t fmt;
    int decode;
    void *vp;
    size_t n;
    unsigned int version;

    /* command line parsing */
    count = -1;         /* no count yet */
    fp = stdout;        /* default output file */
    iterate = 0;        /* not one at a time */
    fmt = UUID_FMT_STR; /* default is ASCII output */
    decode = 0;         /* default is to encode */
    version = UUID_MAKE_V1;
    while ((ch = getopt(argc, argv, "1n:rF:dmo:v:h")) != -1) {
        switch (ch) {
            case '1':
                iterate = 1;
                break;
            case 'n':
                if (count > 0)
                    usage("option 'n' specified multiple times");
                count = strtol(optarg, &p, 10);
                if (*p != '\0' || count < 1)
                    usage("invalid argument to option 'n'");
                break;
            case 'r':
                fmt = UUID_FMT_BIN;
                break;
            case 'F':
                if (strcasecmp(optarg, "bin") == 0)
                    fmt = UUID_FMT_BIN;
                else if (strcasecmp(optarg, "str") == 0)
                    fmt = UUID_FMT_STR;
                else if (strcasecmp(optarg, "siv") == 0)
                    fmt = UUID_FMT_SIV;
                else
                    error(1, "invalid format \"%s\" (has to be \"bin\", \"str\" or \"siv\")", optarg);
                break;
            case 'd':
                decode = 1;
                break;
            case 'o':
                if (fp != stdout)
                    error(1, "multiple output files are not allowed");
                if ((fp = fopen(optarg, "w")) == NULL)
                    error(1, "fopen: %s", strerror(errno));
                break;
            case 'm':
                version |= UUID_MAKE_MC;
                break;
            case 'v':
                i = strtol(optarg, &p, 10);
                if (*p != '\0')
                    usage("invalid argument to option 'v'");
                version &= ~(UUID_MAKE_V1|UUID_MAKE_V3|UUID_MAKE_V4|UUID_MAKE_V5);
                switch (i) {
                    case 1: version |= UUID_MAKE_V1; break;
                    case 3: version |= UUID_MAKE_V3; break;
                    case 4: version |= UUID_MAKE_V4; break;
                    case 5: version |= UUID_MAKE_V5; break;
                    default:
                        usage("invalid version on option 'v'");
                        break;
                }
                break;
            case 'h':
                usage(NULL);
                break;
            default:
                usage("invalid option '%c'", optopt);
        }
    }
    argv += optind;
    argc -= optind;
    if (count == -1)
        count = 1;

    if (decode) {
        /* decoding */
        if ((rc = uuid_create(&uuid)) != UUID_RC_OK)
            error(1, "uuid_create: %s", uuid_error(rc));
        if (argc != 1)
            usage("invalid number of arguments");
        if (strcmp(argv[0], "-") == 0) {
            if (fmt == UUID_FMT_BIN) {
                if (fread(uuid_buf_bin, UUID_LEN_BIN, 1, stdin) != 1)
                    error(1, "fread: failed to read %d (UUID_LEN_BIN) bytes from stdin", UUID_LEN_BIN);
                if ((rc = uuid_import(uuid, UUID_FMT_BIN, uuid_buf_bin, UUID_LEN_BIN)) != UUID_RC_OK)
                    error(1, "uuid_import: %s", uuid_error(rc));
            }
            else if (fmt == UUID_FMT_STR) {
                if (fread(uuid_buf_str, UUID_LEN_STR, 1, stdin) != 1)
                    error(1, "fread: failed to read %d (UUID_LEN_STR) bytes from stdin", UUID_LEN_STR);
                uuid_buf_str[UUID_LEN_STR] = '\0';
                if ((rc = uuid_import(uuid, UUID_FMT_STR, uuid_buf_str, UUID_LEN_STR)) != UUID_RC_OK)
                    error(1, "uuid_import: %s", uuid_error(rc));
            }
            else if (fmt == UUID_FMT_SIV) {
                if (fread(uuid_buf_siv, UUID_LEN_SIV, 1, stdin) != 1)
                    error(1, "fread: failed to read %d (UUID_LEN_SIV) bytes from stdin", UUID_LEN_SIV);
                uuid_buf_siv[UUID_LEN_SIV] = '\0';
                if ((rc = uuid_import(uuid, UUID_FMT_SIV, uuid_buf_siv, UUID_LEN_SIV)) != UUID_RC_OK)
                    error(1, "uuid_import: %s", uuid_error(rc));
            }
        }
        else {
            if (fmt == UUID_FMT_BIN) {
                error(1, "binary input mode only possible if reading from stdin");
            }
            else if (fmt == UUID_FMT_STR) {
                if ((rc = uuid_import(uuid, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK)
                    error(1, "uuid_import: %s", uuid_error(rc));
            }
            else if (fmt == UUID_FMT_SIV) {
                if ((rc = uuid_import(uuid, UUID_FMT_SIV, argv[0], strlen(argv[0]))) != UUID_RC_OK)
                    error(1, "uuid_import: %s", uuid_error(rc));
            }
        }
        vp = NULL;
        if ((rc = uuid_export(uuid, UUID_FMT_TXT, &vp, NULL)) != UUID_RC_OK)
            error(1, "uuid_export: %s", uuid_error(rc));
        fprintf(stdout, "%s", (char *)vp);
        free(vp);
        if ((rc = uuid_destroy(uuid)) != UUID_RC_OK)
            error(1, "uuid_destroy: %s", uuid_error(rc));
    }
    else {
        /* encoding */
        if (   (version & UUID_MAKE_V1 && argc != 0)
            || (version & UUID_MAKE_V3 && argc != 2)
            || (version & UUID_MAKE_V4 && argc != 0)
            || (version & UUID_MAKE_V5 && argc != 2))
            usage("invalid number of arguments");
        if ((rc = uuid_create(&uuid)) != UUID_RC_OK)
            error(1, "uuid_create: %s", uuid_error(rc));
        if (argc == 1) {
            /* load initial UUID for setting old generator state */
            if (strlen(argv[0]) != UUID_LEN_STR)
                error(1, "invalid length of UUID string representation");
            if ((rc = uuid_import(uuid, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK)
                error(1, "uuid_import: %s", uuid_error(rc));
        }
        for (i = 0; i < count; i++) {
            if (iterate) {
                if ((rc = uuid_load(uuid, "nil")) != UUID_RC_OK)
                    error(1, "uuid_load: %s", uuid_error(rc));
            }
            if (version & UUID_MAKE_V3 || version & UUID_MAKE_V5) {
                if ((rc = uuid_create(&uuid_ns)) != UUID_RC_OK)
                    error(1, "uuid_create: %s", uuid_error(rc));
                if ((rc = uuid_load(uuid_ns, argv[0])) != UUID_RC_OK) {
                    if ((rc = uuid_import(uuid_ns, UUID_FMT_STR, argv[0], strlen(argv[0]))) != UUID_RC_OK)
                        error(1, "uuid_import: %s", uuid_error(rc));
                }
                if ((rc = uuid_make(uuid, version, uuid_ns, argv[1])) != UUID_RC_OK)
                    error(1, "uuid_make: %s", uuid_error(rc));
                if ((rc = uuid_destroy(uuid_ns)) != UUID_RC_OK)
                    error(1, "uuid_destroy: %s", uuid_error(rc));
            }
            else {
                if ((rc = uuid_make(uuid, version)) != UUID_RC_OK)
                    error(1, "uuid_make: %s", uuid_error(rc));
            }
            if (fmt == UUID_FMT_BIN) {
                vp = NULL;
                if ((rc = uuid_export(uuid, UUID_FMT_BIN, &vp, &n)) != UUID_RC_OK)
                    error(1, "uuid_export: %s", uuid_error(rc));
                fwrite(vp, n, 1, fp);
                free(vp);
            }
            else if (fmt == UUID_FMT_STR) {
                vp = NULL;
                if ((rc = uuid_export(uuid, UUID_FMT_STR, &vp, &n)) != UUID_RC_OK)
                    error(1, "uuid_export: %s", uuid_error(rc));
                fprintf(fp, "%s\n", (char *)vp);
                free(vp);
            }
            else if (fmt == UUID_FMT_SIV) {
                vp = NULL;
                if ((rc = uuid_export(uuid, UUID_FMT_SIV, &vp, &n)) != UUID_RC_OK)
                    error(1, "uuid_export: %s", uuid_error(rc));
                fprintf(fp, "%s\n", (char *)vp);
                free(vp);
            }
        }
        if ((rc = uuid_destroy(uuid)) != UUID_RC_OK)
            error(1, "uuid_destroy: %s", uuid_error(rc));
    }

    /* close output channel */
    if (fp != stdout)
        fclose(fp);

    return 0;
}