source: branches/stable/mindi-busybox/e2fsprogs/uuid/uuid_time.c @ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 14 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 4.1 KB
Line 
1/*
2 * uuid_time.c --- Interpret the time field from a uuid.  This program
3 *  violates the UUID abstraction barrier by reaching into the guts
4 *  of a UUID and interpreting it.
5 *
6 * Copyright (C) 1998, 1999 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, and the entire permission notice in its entirety,
14 *    including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 *    products derived from this software without specific prior
20 *    written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
25 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
33 * DAMAGE.
34 * %End-Header%
35 */
36
37#include <stdio.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <sys/types.h>
41#include <time.h>
42
43#include "uuidP.h"
44
45time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
46{
47    struct uuid     uuid;
48    uint32_t            high;
49    struct timeval      tv;
50    unsigned long long  clock_reg;
51
52    uuid_unpack(uu, &uuid);
53
54    high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
55    clock_reg = uuid.time_low | ((unsigned long long) high << 32);
56
57    clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
58    tv.tv_sec = clock_reg / 10000000;
59    tv.tv_usec = (clock_reg % 10000000) / 10;
60
61    if (ret_tv)
62        *ret_tv = tv;
63
64    return tv.tv_sec;
65}
66
67int uuid_type(const uuid_t uu)
68{
69    struct uuid     uuid;
70
71    uuid_unpack(uu, &uuid);
72    return ((uuid.time_hi_and_version >> 12) & 0xF);
73}
74
75int uuid_variant(const uuid_t uu)
76{
77    struct uuid     uuid;
78    int         var;
79
80    uuid_unpack(uu, &uuid);
81    var = uuid.clock_seq;
82
83    if ((var & 0x8000) == 0)
84        return UUID_VARIANT_NCS;
85    if ((var & 0x4000) == 0)
86        return UUID_VARIANT_DCE;
87    if ((var & 0x2000) == 0)
88        return UUID_VARIANT_MICROSOFT;
89    return UUID_VARIANT_OTHER;
90}
91
92#ifdef DEBUG
93static const char *variant_string(int variant)
94{
95    switch (variant) {
96    case UUID_VARIANT_NCS:
97        return "NCS";
98    case UUID_VARIANT_DCE:
99        return "DCE";
100    case UUID_VARIANT_MICROSOFT:
101        return "Microsoft";
102    default:
103        return "Other";
104    }
105}
106
107
108int
109main(int argc, char **argv)
110{
111    uuid_t      buf;
112    time_t      time_reg;
113    struct timeval  tv;
114    int     type, variant;
115
116    if (argc != 2) {
117        fprintf(stderr, "Usage: %s uuid\n", argv[0]);
118        exit(1);
119    }
120    if (uuid_parse(argv[1], buf)) {
121        fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
122        exit(1);
123    }
124    variant = uuid_variant(buf);
125    type = uuid_type(buf);
126    time_reg = uuid_time(buf, &tv);
127
128    printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
129    if (variant != UUID_VARIANT_DCE) {
130        printf("Warning: This program only knows how to interpret "
131               "DCE UUIDs.\n\tThe rest of the output is likely "
132               "to be incorrect!!\n");
133    }
134    printf("UUID type is %d", type);
135    switch (type) {
136    case 1:
137        printf(" (time based)\n");
138        break;
139    case 2:
140        printf(" (DCE)\n");
141        break;
142    case 3:
143        printf(" (name-based)\n");
144        break;
145    case 4:
146        printf(" (random)\n");
147        break;
148    default:
149        printf("\n");
150    }
151    if (type != 1) {
152        printf("Warning: not a time-based UUID, so UUID time "
153               "decoding will likely not work!\n");
154    }
155    printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
156           ctime(&time_reg));
157
158    return 0;
159}
160#endif
Note: See TracBrowser for help on using the repository browser.