shithub: hx

ref: 9937039372fdaea29cd4d562ebeac18527d52dd2
dir: /hx.c/

View raw version
#ifdef __linux__
#define _GNU_SOURCE
#else
#define _DEFAULT_SOURCE
#endif
#define _FILE_OFFSET_BITS 64
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>

enum
{
	Bufsz = 4*1024*1024,
	Linesz = 87,
};

static unsigned char *buf;
static const char b2h[] = (
	"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
	"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
	"404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"
	"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
	"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
	"a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
	"c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
	"e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
);

static int
hx(int fd)
{
	size_t addr;
	ssize_t sz, i;
	char s[65536], c;
	unsigned char line[16];
	fd_set set;
	struct timeval timeout;
	int wroff, off, eof, lineoff, r;

#ifdef __linux__
	readahead(fd, 0, Bufsz);
#endif

	lineoff = 0;
	off = 0;
	eof = 0;
	for (addr = 0; eof == 0 && (sz = read(fd, buf, Bufsz)) > 0;) {
		for (i = 0; i < sz; ) {
			if (lineoff == 0) {
				s[off++] = b2h[((addr>>55) & 0x1fe)];
				s[off++] = b2h[((addr>>55) & 0x1fe)+1];
				s[off++] = b2h[((addr>>47) & 0x1fe)];
				s[off++] = b2h[((addr>>47) & 0x1fe)+1];
				s[off++] = b2h[((addr>>39) & 0x1fe)];
				s[off++] = b2h[((addr>>39) & 0x1fe)+1];
				s[off++] = b2h[((addr>>31) & 0x1fe)];
				s[off++] = b2h[((addr>>31) & 0x1fe)+1];
				s[off++] = b2h[((addr>>23) & 0x1fe)];
				s[off++] = b2h[((addr>>23) & 0x1fe)+1];
				s[off++] = b2h[((addr>>15) & 0x1fe)];
				s[off++] = b2h[((addr>>15) & 0x1fe)+1];
				s[off++] = b2h[((addr>>7) & 0x1fe)];
				s[off++] = b2h[((addr>>7) & 0x1fe)+1];
				s[off++] = b2h[((addr<<1) & 0x1fe)];
				s[off++] = b2h[((addr<<1) & 0x1fe)+1];
				s[off++] = ' ';
				s[off++] = ' ';
			}

			for (; i < sz && lineoff < 16; i++, addr++) {
				if (lineoff < 16)
					line[lineoff++] = buf[i];
				s[off++] = b2h[(int)buf[i]*2+0];
				s[off++] = b2h[(int)buf[i]*2+1];
				s[off++] = ' ';
				if (lineoff == 8)
					s[off++] = ' ';
			}

			if (lineoff == 16) {
ascii:
				s[off++] = ' ';
				s[off++] = '|';
				for (lineoff = 0; lineoff < 16; lineoff++) {
					c = line[lineoff];
					if (c < 0x20 || c > 0x7e)
						c = '.';
					s[off++] = c;
				}
				s[off++] = '|';
				s[off++] = '\n';
				lineoff = 0;
			}

			if (off >= (int)sizeof(s)-Linesz) {
writes:
				for (wroff = 0; wroff < off; wroff += r) {
					if ((r = write(1, s+wroff, off-wroff)) < 0) {
						sz = -1;
						break;
					}
				}
				off = 0;
			}
		}
		if (sz == 0)
			continue;

#ifdef __linux__
		readahead(fd, addr, Bufsz);
#endif
		FD_ZERO(&set);
		FD_SET(fd, &set);
		timeout.tv_sec = 0;
		timeout.tv_usec = 1000;
		if (select(fd+1, &set, NULL, NULL, &timeout) == 0) {
			sz = 0;
			goto writes;
		}
	}

	if (sz >= 0) {
		if (eof == 0 && lineoff > 0 && lineoff < 16) {
			eof = 1;
			for (; lineoff < 16; lineoff++) {
				line[lineoff] = 0;
				s[off++] = ' ';
				s[off++] = ' ';
				s[off++] = ' ';
				if (lineoff == 8)
					s[off++] = ' ';
			}
			i = sz = 0;
			goto ascii;
		}
		for (wroff = 0; wroff < off; wroff += r) {
			if ((r = write(1, s+wroff, off-wroff)) < 0) {
				sz = -1;
				break;
			}
		}
	}

	return sz != 0;
}

int
main(int argc, char **argv)
{
	int i, fd, res;

	if ((buf = malloc(Bufsz)) == NULL) {
		perror("buf");
		return 1;
	}

	res = 0;
	for (i = 1; i < argc && res == 0; i++) {
		if ((fd = open(argv[i], O_RDONLY)) < 0) {
			perror(argv[i]);
			res = 1;
		} else {
			if (argc > 2)
				printf("hx %s\n", argv[i]);
			if ((res = hx(fd)) != 0)
				perror(argv[1]);
			close(fd);
		}
	}

	memset(buf, 0, Bufsz);
	free(buf);

	return res;
}