Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / iscsi / dist / src / lib / uuid.c
blob7c5178acd76f69a49c18492093ac86d24dbab186
1 /* $NetBSD: uuid.c,v 1.4 2009/06/23 05:11:47 agc Exp $ */
3 /*
4 * Copyright © 2006 Alistair Crooks. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "config.h"
32 #ifdef HAVE_INTTYPES_H
33 #include <inttypes.h>
34 #endif
36 #include <sys/types.h>
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
40 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include "storage.h"
52 #include "compat.h"
53 #include "defs.h"
55 /* just fill the struct with random values for now */
56 void
57 nbuuid_create(nbuuid_t *uuid, uint32_t *status)
59 uint64_t ether;
60 time_t t;
62 (void) time(&t);
63 ether = ((uint64_t)random() << 32) | random();
64 uuid->time_low = (int)t;
65 uuid->time_mid = (uint16_t)(random() & 0xffff);
66 uuid->time_hi_and_version = (uint16_t)(random() & 0xffff);
67 uuid->clock_seq_low = (uint8_t)(random() & 0xff);
68 uuid->clock_seq_hi_and_reserved = (uint8_t)(random() & 0xff);
69 (void) memcpy(&uuid->node, &ether, sizeof(uuid->node));
70 *status = 0;
73 /* convert the struct to a printable string */
74 void
75 nbuuid_to_string(nbuuid_t *uuid, char **str, uint32_t *status)
77 char s[64];
79 (void) snprintf(s, sizeof(s), "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
80 uuid->time_low,
81 uuid->time_mid,
82 uuid->time_hi_and_version,
83 uuid->clock_seq_hi_and_reserved,
84 uuid->clock_seq_low,
85 uuid->node[0],
86 uuid->node[1],
87 uuid->node[2],
88 uuid->node[3],
89 uuid->node[4],
90 uuid->node[5]);
91 *str = strdup(s);
92 *status = 0;