2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Diomidis Spinellis
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/types.h>
43 int readrec_forward(FILE *f, struct acctv2 *av2);
44 int readrec_backward(FILE *f, struct acctv2 *av2);
47 * Reverse offsetof: return the offset of field f
48 * from the end of the structure s.
50 #define roffsetof(s, f) (sizeof(s) - offsetof(s, f))
53 * Read exactly one record of size size from stream f into ptr.
54 * Failure to read the complete record is considered a file format error,
55 * and will set errno to EFTYPE.
56 * Return 0 on success, EOF on end of file or error.
59 fread_record(void *ptr, size_t size, FILE *f)
63 if ((rv = fread(ptr, 1, size, f)) == size)
65 else if (ferror(f) || rv == 0)
75 * Return the value of a comp_t field.
83 for (exp = v >> 13; exp; exp--)
85 return ((double)result / AHZV1);
89 * Read a v1 accounting record stored at the current
90 * position of stream f.
91 * Convert the data to the current record format.
92 * Return EOF on error or end-of-file.
95 readrec_v1(FILE *f, struct acctv2 *av2)
100 if ((rv = fread_record(&av1, sizeof(av1), f)) == EOF)
104 av2->ac_len = av2->ac_len2 = sizeof(*av2);
105 memcpy(av2->ac_comm, av1.ac_comm, AC_COMM_LEN);
106 av2->ac_utime = decode_comp(av1.ac_utime) * 1000000;
107 av2->ac_stime = decode_comp(av1.ac_stime) * 1000000;
108 av2->ac_etime = decode_comp(av1.ac_etime) * 1000000;
109 av2->ac_btime = av1.ac_btime;
110 av2->ac_uid = av1.ac_uid;
111 av2->ac_gid = av1.ac_gid;
112 av2->ac_mem = av1.ac_mem;
113 av2->ac_io = decode_comp(av1.ac_io);
114 av2->ac_tty = av1.ac_tty;
115 av2->ac_flagx = av1.ac_flag | ANVER;
120 * Read an v2 accounting record stored at the current
121 * position of stream f.
122 * Return EOF on error or end-of-file.
125 readrec_v2(FILE *f, struct acctv2 *av2)
127 return (fread_record(av2, sizeof(*av2), f));
131 * Read a new-style (post-v1) accounting record stored at
132 * the current position of stream f.
133 * Convert the data to the current record format.
134 * Return EOF on error or end-of-file.
137 readrec_vx(FILE *f, struct acctv2 *av2)
139 uint8_t magic, version;
141 if (fread_record(&magic, sizeof(magic), f) == EOF ||
142 fread_record(&version, sizeof(version), f) == EOF ||
143 ungetc(version, f) == EOF ||
144 ungetc(magic, f) == EOF)
148 return (readrec_v2(f, av2));
150 /* Add handling for more versions here. */
159 * Read an accounting record stored at the current
160 * position of stream f.
161 * Old-format records are converted to the current record
163 * Return the number of records read (1 or 0 at the end-of-file),
167 readrec_forward(FILE *f, struct acctv2 *av2)
171 if ((magic = getc(f)) == EOF)
172 return (ferror(f) ? EOF : 0);
173 if (ungetc(magic, f) == EOF)
176 /* Old record format. */
177 rv = readrec_v1(f, av2);
179 /* New record formats. */
180 rv = readrec_vx(f, av2);
181 return (rv == EOF ? EOF : 1);
185 * Read an accounting record ending at the current
186 * position of stream f.
187 * Old-format records are converted to the current record
189 * The file pointer is positioned at the beginning of the
191 * Return the number of records read (1 or 0 at the end-of-file),
195 readrec_backward(FILE *f, struct acctv2 *av2)
201 if ((pos = ftell(f)) == -1)
205 if (fseek(f, -roffsetof(struct acctv2, ac_trailer),
207 (c = getc(f)) == EOF)
210 /* New record formats. */
211 if (fseeko(f, pos - roffsetof(struct acctv2, ac_len2),
213 fread_record(&len, sizeof(len), f) == EOF ||
214 fseeko(f, pos - len, SEEK_SET) == EOF ||
215 readrec_vx(f, av2) == EOF ||
216 fseeko(f, pos - len, SEEK_SET) == EOF)
221 /* Old record format. */
222 if (fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF ||
223 readrec_v1(f, av2) == EOF ||
224 fseeko(f, pos - sizeof(struct acctv1), SEEK_SET) == EOF)