Add definitions for LONG_BIT and WORD_BIT.
[dragonfly.git] / sbin / hammer / hammer.c
CommitLineData
0dfeb6c8
MD
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $DragonFly: src/sbin/hammer/hammer.c,v 1.1 2008/01/01 00:57:57 dillon Exp $
35 */
36
37#include <sys/types.h>
38#include <sys/diskslice.h>
39#include <sys/diskmbr.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <stdarg.h>
46#include <stddef.h>
47#include <unistd.h>
48#include <string.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <uuid.h>
52#include <err.h>
53#include <assert.h>
54#include <ctype.h>
55
56static void hammer_parsetime(u_int64_t *tidp, const char *timestr);
57static void usage(int exit_code);
58
59int
60main(int ac, char **av)
61{
62 u_int64_t tid;
63 int ch;
64
65 while ((ch = getopt(ac, av, "h")) != -1) {
66 switch(ch) {
67 case 'h':
68 usage(0);
69 /* not reached */
70 default:
71 usage(1);
72 /* not reached */
73 }
74 }
75 ac -= optind;
76 av += optind;
77 if (ac < 1) {
78 usage(1);
79 /* not reached */
80 }
81
82 if (strcmp(av[0], "now") == 0) {
83 hammer_parsetime(&tid, "0s");
84 printf("0x%08x\n", (int)(tid / 1000000000LL));
85 } else if (strcmp(av[0], "stamp") == 0) {
86 if (av[1] == NULL)
87 usage(1);
88 hammer_parsetime(&tid, av[1]);
89 printf("0x%08x\n", (int)(tid / 1000000000LL));
90 } else {
91 usage(1);
92 /* not reached */
93 }
94 return(0);
95}
96
97/*
98 * Parse a timestamp for the mount point
99 *
100 * yyyymmddhhmmss
101 * -N[s/h/d/m/y]
102 */
103static
104void
105hammer_parsetime(u_int64_t *tidp, const char *timestr)
106{
107 struct tm tm;
108 time_t t;
109 int32_t n;
110 char c;
111 double seconds = 0;
112
113 t = time(NULL);
114
115 if (*timestr == 0)
116 usage(1);
117
118 if (isalpha(timestr[strlen(timestr)-1])) {
119 if (sscanf(timestr, "%d%c", &n, &c) != 2)
120 usage(1);
121 switch(c) {
122 case 'Y':
123 n *= 365;
124 goto days;
125 case 'M':
126 n *= 30;
127 /* fall through */
128 case 'D':
129 days:
130 n *= 24;
131 /* fall through */
132 case 'h':
133 n *= 60;
134 /* fall through */
135 case 'm':
136 n *= 60;
137 /* fall through */
138 case 's':
139 t -= n;
140 break;
141 default:
142 usage(1);
143 }
144 } else {
145 localtime_r(&t, &tm);
146 seconds = (double)tm.tm_sec;
147 tm.tm_year -= 1900;
148 tm.tm_mon -= 1;
149 n = sscanf(timestr, "%4d%2d%2d:%2d%2d%lf",
150 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
151 &tm.tm_hour, &tm.tm_min, &seconds);
152 tm.tm_mon += 1;
153 tm.tm_year += 1900;
154 tm.tm_sec = (int)seconds;
155 t = mktime(&tm);
156 }
157 localtime_r(&t, &tm);
158 *tidp = (u_int64_t)t * 1000000000 +
159 (seconds - (int)seconds) * 1000000000;
160}
161
162static
163void
164usage(int exit_code)
165{
166 fprintf(stderr,
167 "hammer -h\n"
168 "hammer now\n"
169 "hammer stamp <time>\n"
170 );
171 fprintf(stderr, " time: +n[s/m/h/D/M/Y]\n"
172 " time: yyyymmdd[:hhmmss]\n");
173 exit(exit_code);
174}