2 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/endian.h>
31 #include <sys/param.h>
40 #include "../../lib/libc/gen/utxdb.h"
53 fprintf(stderr, "usage: wtmpcvt input output\n");
58 outmp_to_futx(const struct outmp *ui, struct futx *uo)
61 memset(uo, 0, sizeof *uo);
62 #define COPY_STRING(field) do { \
63 strncpy(uo->fu_ ## field, ui->ut_ ## field, \
64 MIN(sizeof uo->fu_ ## field, sizeof ui->ut_ ## field)); \
66 #define COPY_LINE_TO_ID() do { \
67 strncpy(uo->fu_id, ui->ut_line, \
68 MIN(sizeof uo->fu_id, sizeof ui->ut_line)); \
70 #define MATCH(field, value) (strncmp(ui->ut_ ## field, (value), \
71 sizeof(ui->ut_ ## field)) == 0)
72 if (MATCH(user, "reboot") && MATCH(line, "~"))
73 uo->fu_type = BOOT_TIME;
74 else if (MATCH(user, "date") && MATCH(line, "|"))
75 uo->fu_type = OLD_TIME;
76 else if (MATCH(user, "date") && MATCH(line, "{"))
77 uo->fu_type = NEW_TIME;
78 else if (MATCH(user, "shutdown") && MATCH(line, "~"))
79 uo->fu_type = SHUTDOWN_TIME;
80 else if (MATCH(user, "") && MATCH(host, "") && !MATCH(line, "")) {
81 uo->fu_type = DEAD_PROCESS;
83 } else if (!MATCH(user, "") && !MATCH(line, "") && ui->ut_time != 0) {
84 uo->fu_type = USER_PROCESS;
94 #undef COPY_LINE_TO_ID
97 /* Timestamp conversion. XXX: Assumes host byte order! */
98 uo->fu_tv = htobe64((uint64_t)ui->ut_time * 1000000);
102 main(int argc, char *argv[])
114 in = fopen(argv[1], "r");
116 err(1, "%s", argv[1]);
117 out = fopen(argv[2], "w");
119 err(1, "%s", argv[2]);
121 /* Process entries. */
122 while (fread(&ui, sizeof ui, 1, in) == 1) {
123 outmp_to_futx(&ui, &uo);
124 if (uo.fu_type == EMPTY)
127 /* Write new entry to output file. */
128 for (l = sizeof uo; l > 0 &&
129 ((const char *)&uo)[l - 1] == '\0'; l--);
131 fwrite(&lo, sizeof lo, 1, out);
132 fwrite(&uo, l, 1, out);