ref: eaac0f22ca88dbe40afa83fbfaf36da0c6f7821b
parent: fbfd677a4f6cd2fac688dce5d39cc128be58524f
author: Sigrid Haflínudóttir <[email protected]>
date: Sat Dec 28 10:06:57 EST 2019
port to plan 9
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,2 @@
+*.out
+hx.[^c]
--- a/README.md
+++ b/README.md
@@ -12,9 +12,11 @@
Around 40 times faster, eh?
-# Building
+# Building/installing
-`gcc -Wall -Wextra -O2 -s -g0 hx.c -o hx`
+UNIX-like operating systems: `gcc -Wall -Wextra -O2 -s -g0 hx.c -o hx`
+
+Plan 9: `cd /tmp && hget https://github.com/ftrvxmtrx/hx/archive/master.tar.gz | tar xz && cd hx-master && mk install`
# Notes
--- a/hx.c
+++ b/hx.c
@@ -1,3 +1,4 @@
+#ifdef __unix__
#ifdef __linux__
#define _GNU_SOURCE
#else
@@ -9,6 +10,15 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#define print printf
+#define OREAD O_RDONLY
+#define nil NULL
+#define exits(s) do { return (s) != nil; } while (0)
+typedef size_t u64int;
+#else
+#include <u.h>
+#include <libc.h>
+#endif
enum
{
@@ -31,8 +41,8 @@
static int
hx(int fd)
{
- size_t addr;
- ssize_t sz, i;
+ u64int addr;
+ long sz, i;
char s[65536], c;
unsigned char line[16];
int wroff, off, eof, lineoff, r;
@@ -44,6 +54,7 @@
lineoff = 0;
off = 0;
eof = 0;
+ sz = 0;
for (addr = 0; eof == 0 && (sz = read(fd, buf, Bufsz)) > 0;) {
for (i = 0; i < sz; ) {
if (lineoff == 0) {
@@ -140,24 +151,28 @@
return sz != 0;
}
+#ifdef __unix__
int
+#else
+void
+#endif
main(int argc, char **argv)
{
int i, fd, res;
- if ((buf = malloc(Bufsz)) == NULL) {
+ if ((buf = malloc(Bufsz)) == nil) {
perror("buf");
- return 1;
+ exits("buf");
}
res = 0;
for (i = 1; i < argc && res == 0; i++) {
- if ((fd = open(argv[i], O_RDONLY)) < 0) {
+ if ((fd = open(argv[i], OREAD)) < 0) {
perror(argv[i]);
res = 1;
} else {
if (argc > 2)
- printf("hx %s\n", argv[i]);
+ print("hx %s\n", argv[i]);
if ((res = hx(fd)) != 0)
perror(argv[1]);
close(fd);
@@ -167,5 +182,5 @@
memset(buf, 0, Bufsz);
free(buf);
- return res;
+ exits(res == 0 ? nil : "error");
}
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,10 @@
+</$objtype/mkfile
+
+TARG=hx
+BIN=/$objtype/bin
+OFILES=\
+ hx.$O\
+
+default:V: all
+
+</sys/src/cmd/mkone