1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2005-2010 Jan Engelhardt
/*
* tailhex.c - tail follower with hexdump
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libHX/ctype_helper.h>
#include <libHX/init.h>
#include <libHX/option.h>
static struct {
long long start;
int approx, follow, bsize, quad;
} Opt = {
.start = 0,
.approx = 0,
.follow = 0,
.bsize = 16,
.quad = 0,
};
static void getopt_op_e(const struct HXoptcb *cbi)
{
Opt.start = strtoll(cbi->data, NULL, 0);
}
static const struct HXoption options_table[] = {
{.sh = 'B', .type = HXTYPE_INT, .ptr = &Opt.bsize,
.help = "Buffer and window width"},
{.sh = 'Q', .type = HXTYPE_NONE, .ptr = &Opt.quad,
.help = "Use 64-bit position numbers beginning from 0"},
{.sh = 'a', .type = HXTYPE_NONE, .ptr = &Opt.approx,
.help = "Approximate position start"},
{.sh = 'e', .type = HXTYPE_STRP, .cb = getopt_op_e,
.help = "Exact position start"},
{.sh = 'f', .type = HXTYPE_NONE, .ptr = &Opt.follow,
.help = "Output appended data as file grows", NULL},
HXOPT_AUTOHELP,
HXOPT_TABLEEND,
};
static inline char printable(char x)
{
return HX_isprint(x) ? x : '.';
}
static int main2(int argc, char **argv, struct HXopt6_result *argp)
{
unsigned int buf_offset = 0;
unsigned char *buf;
struct stat sb;
long long rax;
int ret, fd;
if (HX_getopt6(options_table, argc, argv, argp,
HXOPT_USAGEONERR | HXOPT_ITER_ARGS) != HXOPT_ERR_SUCCESS)
return EXIT_FAILURE;
if (argp->nargs != 1) {
fprintf(stderr, "Error: Exactly one filename needs to be provided\n");
return EXIT_FAILURE;
}
if ((buf = malloc(Opt.bsize)) == NULL) {
fprintf(stderr, "Could not allocate buffer of size %d\n", Opt.bsize);
return EXIT_FAILURE;
}
fd = open(argp->uarg[0], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Could not open %s: %s\n", argp->uarg[0], strerror(errno));
return EXIT_FAILURE;
}
fstat(fd, &sb);
if (!Opt.start && Opt.approx)
Opt.start = (sb.st_size >> 8) << 8;
if (Opt.start < 0)
rax = lseek(fd, Opt.start, SEEK_END);
else
rax = lseek(fd, Opt.start, SEEK_SET);
if (rax < 0)
perror("seek to %lld failed");
while (true) {
long long pos;
int i;
pos = lseek(fd, 0, SEEK_CUR);
ret = read(fd, buf + buf_offset, Opt.bsize - buf_offset);
if (ret == 0) /* eof */
break;
if (ret < 0) {
perror("read()");
exit(EXIT_FAILURE);
}
if (Opt.follow && ret < Opt.bsize) {
buf_offset = ret;
sched_yield();
continue;
}
buf_offset = 0;
if (Opt.quad || pos > (long long)0xFFFFFFFF)
printf("0x%016llx |", pos);
else
printf("0x%08llx |", pos);
for (i = 0; i < Opt.bsize; ++i) {
printf(" ");
if (i < ret) printf("%02x", (int)buf[i]);
else printf(" ");
if (++i < ret) printf("%02x", (int)buf[i]);
else printf(" ");
}
printf(" | ");
for (i = 0; i < Opt.bsize; ++i) {
if (i < ret) printf("%c", printable(buf[i]));
else printf(" ");
if (++i < ret) printf("%c", printable(buf[i]));
else printf(" ");
}
printf("\n");
}
return EXIT_SUCCESS;
}
int main(int argc, char **argv)
{
struct HXopt6_result argp = {};
int ret;
if ((ret = HX_init()) <= 0) {
fprintf(stderr, "HX_init: %s\n", strerror(-ret));
abort();
}
ret = main2(argc, argv, &argp);
HX_getopt6_clean(&argp);
HX_exit();
return ret;
}
|