package

simple and robust package manager
Log | Files | Refs | README

commit 46a6ecd8153192d1bd922a4ac06f41fc48e91c10
parent 97aa5841ff3d7d66e8adabd27464983089575df0
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 14 Jan 2019 02:40:10 +0100

define a sane compiled tree definition

For instance:

	/package
	└─ libgit
	   ├─ 1.2.0
	   │  ├─ @400000005c291df00eac3f9c
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c2d4f04015fc0d4
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c2fa9d31c80edac
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c316f560b3c0b94
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c316fe11596d90c
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ current
	   │  ├─ latest
	   │  └─ source.tar
	   ├─ 1.2.2
	   │  ├─ @400000005c291d4528f1395c
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c325a0914f7d604
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ @400000005c345a0914f7d604
	   │  │  ├─ build
	   │  │  ├─ log
	   │  │  └─ source
	   │  ├─ current
	   │  ├─ latest
	   │  └─ source.tar
	   ├─ current
	   └─ git
	      ├─ @400000005c31710828a547f4
	      │  ├─ build
	      │  ├─ log
	      │  └─ source
	      ├─ @400000005c31710828a548f5
	      │  ├─ build
	      │  ├─ log
	      │  └─ source
	      ├─ @400000005c32359328ea24dc
	      │  ├─ build
	      │  ├─ log
	      │  └─ source
	      ├─ current
	      ├─ latest
	      └─ source.tar

Diffstat:
MMakefile | 30+++++++++++++++---------------
Mfmt.c | 3+--
Mlog.c | 103++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mlog.h | 133+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mmake-mk | 5+----
Mpack-add.c | 2--
Mpack-build.c | 284+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mpack.c | 322++++---------------------------------------------------------------------------
Mpack.h | 10+---------
Atai.c | 33+++++++++++++++++++++++++++++++++
Atai.h | 23+++++++++++++++++++++++
Auint64.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Auint64.h | 11+++++++++++
13 files changed, 561 insertions(+), 459 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,5 @@ bin = pack-build pack-dep pack-add pack-get pack-tree +obj = pack-build.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o tai.o .PHONY: all all: ${bin} @@ -13,21 +14,17 @@ install: all cp -r bin ${PREFIX} cp ${bin} ${PREFIX}/bin -pack-build-o = pack-build.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o -pack-build: ${pack-build-o} - ./make-bin $@ ${pack-build-o} +pack-build: ${obj} + ./make-bin $@ ${obj} -pack-dep-o = pack-dep.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o -pack-dep: ${pack-dep-o} - ./make-bin $@ ${pack-dep-o} +pack-dep: ${obj} + ./make-bin $@ ${obj} -pack-add-o = pack-add.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o -pack-add: ${pack-add-o} - ./make-bin $@ ${pack-add-o} +pack-add: ${obj} + ./make-bin $@ ${obj} -pack-get-o = pack-get.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o -pack-get: ${pack-get-o} - ./make-bin $@ ${pack-get-o} +pack-get: ${obj} + ./make-bin $@ ${obj} pack-tree-o = pack-tree.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o pack-tree: ${pack-tree-o} @@ -42,10 +39,13 @@ forkexec.o: forkexec.c open.h forkexec.h fs.o: fs.c fs.h mem.h open.h str.h stralloc.h log.o: log.c buffer.h log.h mem.o: mem.c mem.h -pack-add.o: pack-add.c buffer.h env.h forkexec.h fs.h genalloc.h log.h pack.h str.h fmt.h open.h +pack-add.o: pack-add.c buffer.h env.h fs.h genalloc.h log.h pack.h str.h fmt.h open.h pack-build.o: pack-build.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h str.h stralloc.h pack-dep.o: pack-dep.c buffer.h env.h fs.h genalloc.h log.h pack.h str.h -pack-get.o: pack-get.c buffer.h open.h pack.h stralloc.h -pack.o: pack.c buffer.h env.h fmt.h forkexec.h fs.h genalloc.h mem.h open.h pack.h stralloc.h +pack-get.o: pack-get.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h stralloc.h +pack-tree.o: pack-tree.c pack.h stralloc.h open.h buffer.h env.h fs.h log.h +pack.o: pack.c buffer.h env.h fmt.h forkexec.h fs.h genalloc.h mem.h open.h pack.h stralloc.h lock.h tai.h str.o: str.c str.h stralloc.o: stralloc.c mem.h fmt.h str.h stralloc.h +tai.o: tai.c tai.h +uint64.o: uint64.c uint64.h diff --git a/fmt.c b/fmt.c @@ -47,7 +47,6 @@ fmt_fill(char *dest, size_t size, char const *src, size_t fill) max = size < srclen ? size : srclen; while (n < max) dest[n++] = *src++; while (n < fill) dest[n++] = ' '; - dest[n] = '\0'; return n; } @@ -65,7 +64,7 @@ fmt_pad(char *dest, size_t size, char const *src, size_t pad) max = size < pad + srclen ? size : pad + srclen; while (n++ < pad - srclen) *dest++ = ' '; while (n++ < max) *dest++ = *src++; - dest[n] = '\0'; + return n; } diff --git a/log.c b/log.c @@ -2,6 +2,8 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <stdarg.h> + #include "buffer.h" #include "log.h" @@ -14,61 +16,96 @@ log_init(int level) } void -log_put(int level, char const *s0, - char const *s1, char const *s2, char const *s3, char const *s4, - char const *s5, char const *s6, char const *s7, char const *s8) +log_put(int level, char const *type, int argc, ...) { - if (level < log_level) return; - - if (s0) buffer_puts(buffer_2, s0); - if (s1) buffer_puts(buffer_2, s1); - if (s2) buffer_puts(buffer_2, s2); - if (s3) buffer_puts(buffer_2, s3); - if (s4) buffer_puts(buffer_2, s4); - if (s5) buffer_puts(buffer_2, s5); - if (s6) buffer_puts(buffer_2, s6); - if (s7) buffer_puts(buffer_2, s7); - if (s8) buffer_puts(buffer_2, s8); + va_list a; + if (level < log_level) return; + va_start(a, argc); + buffer_puts(buffer_2, type); + buffer_puts(buffer_2, ": "); + for (int x = 0; x < argc; x++) + { + char *s = va_arg(a, char *); + buffer_puts(buffer_2, s); + } buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); + va_end(a); } void -log_putsys(int level, char const *s0, - char const *s1, char const *s2, char const *s3, char const *s4, - char const *s5, char const *s6) +log_putsys(int level, char const *type, int argc, ...) { - log_put(level, s0, s1, s2, s3, s4, s5, s6, ": ", strerror(errno)); + va_list a; + + if (level < log_level) return; + va_start(a, argc); + buffer_puts(buffer_2, type); + buffer_puts(buffer_2, ": "); + for (int x = 0; x < argc; x++) + { + char *s = va_arg(a, char *); + buffer_puts(buffer_2, s); + } + buffer_puts(buffer_2, ": "); + buffer_puts(buffer_2, strerror(errno)); + buffer_puts(buffer_2, "\n"); + buffer_flush(buffer_2); + va_end(a); } void -log_usage(char const *prog, char const *usage) +log_die(int e, char const *type, int argc, ...) { - buffer_puts(buffer_2, "usage: "); - buffer_puts(buffer_2, prog); - buffer_puts(buffer_2, " "); - buffer_puts(buffer_2, usage); + va_list a; + + va_start(a, argc); + buffer_puts(buffer_2, type); + buffer_puts(buffer_2, ": "); + for (int x = 0; x < argc; x++) + { + char *s = va_arg(a, char *); + buffer_puts(buffer_2, s); + } buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); + va_end(a); - exit(1); + exit(e); } void -log_die(int e, char const *s0, - char const *s1, char const *s2, char const *s3, char const *s4, - char const *s5, char const *s6, char const *s7, char const *s8) +log_diesys(int e, char const *type, int argc, ...) { - log_put(5, s0, s1, s2, s3, s4, s5, s6, s7, s8); + va_list a; + + va_start(a, argc); + buffer_puts(buffer_2, type); + buffer_puts(buffer_2, ": "); + for (int x = 0; x < argc; x++) + { + char *s = va_arg(a, char *); + buffer_puts(buffer_2, s); + } + buffer_puts(buffer_2, ": "); + buffer_puts(buffer_2, strerror(errno)); + buffer_puts(buffer_2, "\n"); + buffer_flush(buffer_2); + va_end(a); + exit(e); } void -log_diesys(int e, char const *s0, - char const *s1, char const *s2, char const *s3, char const *s4, - char const *s5, char const *s6) +log_usage(char const *prog, char const *usage) { - log_putsys(5, s0, s1, s2, s3, s4, s5, s6); - exit(e); + buffer_puts(buffer_2, "usage: "); + buffer_puts(buffer_2, prog); + buffer_puts(buffer_2, " "); + buffer_puts(buffer_2, usage); + buffer_puts(buffer_2, "\n"); + buffer_flush(buffer_2); + + exit(1); } diff --git a/log.h b/log.h @@ -1,71 +1,90 @@ #ifndef STRERR_H #define STRERR_H -#define log_die1(e, s1) log_die(e, "fatal: ", s1,0,0,0,0,0,0,0) -#define log_die2(e, s1,s2) log_die(e, "fatal: ", s1,s2,0,0,0,0,0,0) -#define log_die3(e, s1,s2,s3) log_die(e, "fatal: ", s1,s2,s3,0,0,0,0,0) -#define log_die4(e, s1,s2,s3,s4) log_die(e, "fatal: ", s1,s2,s3,s4,0,0,0,0) -#define log_die5(e, s1,s2,s3,s4,s5) log_die(e, "fatal: ", s1,s2,s3,s4,s5,0,0,0) -#define log_die6(e, s1,s2,s3,s4,s5,s6) log_die(e, "fatal: ", s1,s2,s3,s4,s5,s6,0,0) -#define log_die7(e, s1,s2,s3,s4,s5,s6,s7) log_die(e, "fatal: ", s1,s2,s3,s4,s5,s6,s7,0) -#define log_diesys1(e, s1) log_diesys(e, "fatal: ", s1,0,0,0,0,0) -#define log_diesys2(e, s1,s2) log_diesys(e, "fatal: ", s1,s2,0,0,0,0) -#define log_diesys3(e, s1,s2,s3) log_diesys(e, "fatal: ", s1,s2,s3,0,0,0) -#define log_diesys4(e, s1,s2,s3,s4) log_diesys(e, "fatal: ", s1,s2,s3,s4,0,0) -#define log_diesys5(e, s1,s2,s3,s4,s5) log_diesys(e, "fatal: ", s1,s2,s3,s4,s5,0) -#define log_diesys6(e, s1,s2,s3,s4,s5,s6) log_diesys(e, "fatal: ", s1,s2,s3,s4,s5,s6) +#define log_die1(x, a) log_die(x, "fatal", 1, a) +#define log_die2(x, a,b) log_die(x, "fatal", 2, a,b) +#define log_die3(x, a,b,c) log_die(x, "fatal", 3, a,b,c) +#define log_die4(x, a,b,c,d) log_die(x, "fatal", 4, a,b,c,d) +#define log_die5(x, a,b,c,d,e) log_die(x, "fatal", 5, a,b,c,d,e) +#define log_die6(x, a,b,c,d,e,f) log_die(x, "fatal", 6, a,b,c,d,e,f) +#define log_die7(x, a,b,c,d,e,f,g) log_die(x, "fatal", 7, a,b,c,d,e,f,g) +#define log_die8(x, a,b,c,d,e,f,g,h) log_die(x, "fatal", 8, a,b,c,d,e,f,g,h) +#define log_die9(x, a,b,c,d,e,f,g,h,i) log_die(x, "fatal", 9, a,b,c,d,e,f,g,h,i) +#define log_diesys1(x, a) log_diesys(x, "fatal", 1, a) +#define log_diesys2(x, a,b) log_diesys(x, "fatal", 2, a,b) +#define log_diesys3(x, a,b,c) log_diesys(x, "fatal", 3, a,b,c) +#define log_diesys4(x, a,b,c,d) log_diesys(x, "fatal", 4, a,b,c,d) +#define log_diesys5(x, a,b,c,d,e) log_diesys(x, "fatal", 5, a,b,c,d,e) +#define log_diesys6(x, a,b,c,d,e,f) log_diesys(x, "fatal", 6, a,b,c,d,e,f) +#define log_diesys7(x, a,b,c,d,e,f,g) log_diesys(x, "fatal", 7, a,b,c,d,e,f,g) +#define log_diesys8(x, a,b,c,d,e,f,g,h) log_diesys(x, "fatal", 8, a,b,c,d,e,f,g,h) +#define log_diesys9(x, a,b,c,d,e,f,g,h,i) log_diesys(x, "fatal", 9, a,b,c,d,e,f,g,h,i) -#define log_err1(s1) log_put(2, "error: ", s1,0,0,0,0,0,0,0) -#define log_err2(s1,s2) log_put(2, "error: ", s1,s2,0,0,0,0,0,0) -#define log_err3(s1,s2,s3) log_put(2, "error: ", s1,s2,s3,0,0,0,0,0) -#define log_err4(s1,s2,s3,s4) log_put(2, "error: ", s1,s2,s3,s4,0,0,0,0) -#define log_err5(s1,s2,s3,s4,s5) log_put(2, "error: ", s1,s2,s3,s4,s5,0,0,0) -#define log_err6(s1,s2,s3,s4,s5,s6) log_put(2, "error: ", s1,s2,s3,s4,s5,s6,0,0) -#define log_err7(s1,s2,s3,s4,s5,s6,s7) log_put(2, "error: ", s1,s2,s3,s4,s5,s6,s7,0) -#define log_errsys1(s1) log_putsys(2, "error: ", s1,0,0,0,0,0) -#define log_errsys2(s1,s2) log_putsys(2, "error: ", s1,s2,0,0,0,0) -#define log_errsys3(s1,s2,s3) log_putsys(2, "error: ", s1,s2,s3,0,0,0) -#define log_errsys4(s1,s2,s3,s4) log_putsys(2, "error: ", s1,s2,s3,s4,0,0) -#define log_errsys5(s1,s2,s3,s4,s5) log_putsys(2, "error: ", s1,s2,s3,s4,s5,0) -#define log_errsys6(s1,s2,s3,s4,s5,s6) log_putsys(2, "error: ", s1,s2,s3,s4,s5,s6) +#define log_err1(a) log_put(2, "error", 1, a) +#define log_err2(a,b) log_put(2, "error", 2, a,b) +#define log_err3(a,b,c) log_put(2, "error", 3, a,b,c) +#define log_err4(a,b,c,d) log_put(2, "error", 4, a,b,c,d) +#define log_err5(a,b,c,d,e) log_put(2, "error", 5, a,b,c,d,e) +#define log_err6(a,b,c,d,e,f) log_put(2, "error", 6, a,b,c,d,e,f) +#define log_err7(a,b,c,d,e,f,g) log_put(2, "error", 7, a,b,c,d,e,f,g) +#define log_err8(a,b,c,d,e,f,g,h) log_put(2, "error", 8, a,b,c,d,e,f,g,h) +#define log_err9(a,b,c,d,e,f,g,h,i) log_put(2, "error", 9, a,b,c,d,e,f,g,h,i) +#define log_errsys1(a) log_putsys(2, "error", 1, a) +#define log_errsys2(a,b) log_putsys(2, "error", 2, a,b) +#define log_errsys3(a,b,c) log_putsys(2, "error", 3, a,b,c) +#define log_errsys4(a,b,c,d) log_putsys(2, "error", 4, a,b,c,d) +#define log_errsys5(a,b,c,d,e) log_putsys(2, "error", 5, a,b,c,d,e) +#define log_errsys6(a,b,c,d,e,f) log_putsys(2, "error", 6, a,b,c,d,e,f) +#define log_errsys7(a,b,c,d,e,f,g) log_putsys(2, "error", 7, a,b,c,d,e,f,g) +#define log_errsys8(a,b,c,d,e,f,g,h) log_putsys(2, "error", 8, a,b,c,d,e,f,g,h) +#define log_errsys9(a,b,c,d,e,f,g,h,i) log_putsys(2, "error", 9, a,b,c,d,e,f,g,h,i) -#define log_warn1(s1) log_put(3, "warning: ", s1,0,0,0,0,0,0,0) -#define log_warn2(s1,s2) log_put(3, "warning: ", s1,s2,0,0,0,0,0,0) -#define log_warn3(s1,s2,s3) log_put(3, "warning: ", s1,s2,s3,0,0,0,0,0) -#define log_warn4(s1,s2,s3,s4) log_put(3, "warning: ", s1,s2,s3,s4,0,0,0,0) -#define log_warn5(s1,s2,s3,s4,s5) log_put(3, "warning: ", s1,s2,s3,s4,s5,0,0,0) -#define log_warn6(s1,s2,s3,s4,s5,s6) log_put(3, "warning: ", s1,s2,s3,s4,s5,s6,0,0) -#define log_warn7(s1,s2,s3,s4,s5,s6,s7) log_put(3, "warning: ", s1,s2,s3,s4,s5,s6,s7,0) -#define log_warnsys1(s1) log_putsys(3, "warning: ", s1,0,0,0,0) -#define log_warnsys2(s1,s2) log_putsys(3, "warning: ", s1,s2,0,0,0) -#define log_warnsys3(s1,s2,s3) log_putsys(3, "warning: ", s1,s2,s3,0,0) -#define log_warnsys4(s1,s2,s3,s4) log_putsys(3, "warning: ", s1,s2,s3,s4,0) -#define log_warnsys5(s1,s2,s3,s4,s5) log_putsys(3, "warning: ", s1,s2,s3,s4,s5) -#define log_warnsys6(s1,s2,s3,s4,s5,s6) log_putsys(3, "warning: ", s1,s2,s3,s4,s5,s6) +#define log_warn1(a) log_put(3, "warning", 1, a) +#define log_warn2(a,b) log_put(3, "warning", 2, a,b) +#define log_warn3(a,b,c) log_put(3, "warning", 3, a,b,c) +#define log_warn4(a,b,c,d) log_put(3, "warning", 4, a,b,c,d) +#define log_warn5(a,b,c,d,e) log_put(3, "warning", 5, a,b,c,d,e) +#define log_warn6(a,b,c,d,e,f) log_put(3, "warning", 6, a,b,c,d,e,f) +#define log_warn7(a,b,c,d,e,f,g) log_put(3, "warning", 7, a,b,c,d,e,f,g) +#define log_warn8(a,b,c,d,e,f,g,h) log_put(3, "warning", 8, a,b,c,d,e,f,g,h) +#define log_warn9(a,b,c,d,e,f,g,h,i) log_put(3, "warning", 9, a,b,c,d,e,f,g,h,i) +#define log_warnsys1(a) log_putsys(3, "warning", 1, a) +#define log_warnsys2(a,b) log_putsys(3, "warning", 2, a,b) +#define log_warnsys3(a,b,c) log_putsys(3, "warning", 3, a,b,c) +#define log_warnsys4(a,b,c,d) log_putsys(3, "warning", 4, a,b,c,d) +#define log_warnsys5(a,b,c,d,e) log_putsys(3, "warning", 5, a,b,c,d,e) +#define log_warnsys6(a,b,c,d,e,f) log_putsys(3, "warning", 6, a,b,c,d,e,f) +#define log_warnsys7(a,b,c,d,e,f,g) log_putsys(3, "warning", 7, a,b,c,d,e,f,g) +#define log_warnsys8(a,b,c,d,e,f,g,h) log_putsys(3, "warning", 8, a,b,c,d,e,f,g,h) +#define log_warnsys9(a,b,c,d,e,f,g,h,i) log_putsys(3, "warning", 9, a,b,c,d,e,f,g,h,i) -#define log_info1(s1) log_put(4, "info: ", s1,0,0,0,0,0,0) -#define log_info2(s1,s2) log_put(4, "info: ", s1,s2,0,0,0,0,0,0) -#define log_info3(s1,s2,s3) log_put(4, "info: ", s1,s2,s3,0,0,0,0,0) -#define log_info4(s1,s2,s3,s4) log_put(4, "info: ", s1,s2,s3,s4,0,0,0,0) -#define log_info5(s1,s2,s3,s4,s5) log_put(4, "info: ", s1,s2,s3,s4,s5,0,0,0) -#define log_info6(s1,s2,s3,s4,s5,s6) log_put(4, "info: ", s1,s2,s3,s4,s5,s6,0,0) -#define log_info7(s1,s2,s3,s4,s5,s6,s7) log_put(4, "info: ", s1,s2,s3,s4,s5,s6,s7,0) +#define log_info1(a) log_put(4, "info", 1, a) +#define log_info2(a,b) log_put(4, "info", 2, a,b) +#define log_info3(a,b,c) log_put(4, "info", 3, a,b,c) +#define log_info4(a,b,c,d) log_put(4, "info", 4, a,b,c,d) +#define log_info5(a,b,c,d,e) log_put(4, "info", 5, a,b,c,d,e) +#define log_info6(a,b,c,d,e,f) log_put(4, "info", 6, a,b,c,d,e,f) +#define log_info7(a,b,c,d,e,f,g) log_put(4, "info", 7, a,b,c,d,e,f,g) +#define log_info8(a,b,c,d,e,f,g,h) log_put(4, "info", 8, a,b,c,d,e,f,g,h) +#define log_info9(a,b,c,d,e,f,g,h,i) log_put(4, "info", 9, a,b,c,d,e,f,g,h,i) -#define log_debug1(s1) log_put(5, "debug: ", s1,0,0,0,0,0,0,0) -#define log_debug2(s1,s2) log_put(5, "debug: ", s1,s2,0,0,0,0,0,0) -#define log_debug3(s1,s2,s3) log_put(5, "debug: ", s1,s2,s3,0,0,0,0,0) -#define log_debug4(s1,s2,s3,s4) log_put(5, "debug: ", s1,s2,s3,s4,0,0,0,0) -#define log_debug5(s1,s2,s3,s4,s5) log_put(5, "debug: ", s1,s2,s3,s4,s5,0,0,0) -#define log_debug6(s1,s2,s3,s4,s5,s6) log_put(5, "debug: ", s1,s2,s3,s4,s5,s6,0,0) -#define log_debug7(s1,s2,s3,s4,s5,s6,s7) log_put(5, "debug: ", s1,s2,s3,s4,s5,s6,s7,0) +#define log_debug1(a) log_put(5, "debug", 1, a) +#define log_debug2(a,b) log_put(5, "debug", 2, a,b) +#define log_debug3(a,b,c) log_put(5, "debug", 3, a,b,c) +#define log_debug4(a,b,c,d) log_put(5, "debug", 4, a,b,c,d) +#define log_debug5(a,b,c,d,e) log_put(5, "debug", 5, a,b,c,d,e) +#define log_debug6(a,b,c,d,e,f) log_put(5, "debug", 6, a,b,c,d,e,f) +#define log_debug7(a,b,c,d,e,f,g) log_put(5, "debug", 7, a,b,c,d,e,f,g) +#define log_debug8(a,b,c,d,e,f,g,h) log_put(5, "debug", 8, a,b,c,d,e,f,g,h) +#define log_debug9(a,b,c,d,e,f,g,h,i) log_put(5, "debug", 9, a,b,c,d,e,f,g,h,i) extern int log_level; -void log_die(int, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *); -void log_diesys(int, char const *, char const *, char const *, char const *, char const *, char const *, char const *); +void log_die(int, char const *, int, ...); +void log_diesys(int, char const *, int, ...); void log_init(int); -void log_put(int, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *); -void log_putsys(int, char const *, char const *, char const *, char const *, char const *, char const *, char const *); +void log_put(int, char const *, int, ...); +void log_putsys(int, char const *, int, ...); void log_usage(char const *, char const *); #endif diff --git a/make-mk b/make-mk @@ -4,8 +4,5 @@ for file in "$@" do printf '%-16s %-16s ' "${file%.c}.o:" "$file" | tr -d '\n' - sed -rn 's/^#include "([^"]+.h)".*$/\1/p' "$file" | - xargs | - fold -w 80 -s | - sed -r 's/$/ \\/; $ s/ \\//' + sed -rn 's/^#include "([^"]+.h)".*$/\1/p' "$file" | xargs done diff --git a/pack-add.c b/pack-add.c @@ -90,8 +90,6 @@ main(int argc, char **argv) if (pack_isdone(p, dir, "tar")) goto step_tar; step(p, "pack-get", log, tar); -step_tar: - step(p, "pack-tar", log, src); step_build: step(p, "pack-build", log, out); step_install: diff --git a/pack-build.c b/pack-build.c @@ -1,4 +1,5 @@ #include <sys/types.h> +#include <sys/stat.h> #include <ctype.h> #include <sys/dirent.h> #include <dirent.h> @@ -13,49 +14,274 @@ #include "pack.h" #include "str.h" #include "stralloc.h" +#include "tai.h" +#include "fmt.h" -int -main(int argc, char **argv) +static int +patch(pack const *p, char *def) { - (void)argc; - (void)argv; - char *def; - char *src; - char *out; - pack p; + char const *argv[] = { "patch", "-N", NULL }; + char const *d[2]; + stralloc path; size_t n; + size_t m; + DIR *dp = NULL; + struct dirent *de; + int i; + int fd = -1; + int ret = 0; - if (argc != 2) log_usage(*argv, "package[/version]"); + stralloc_init(&path); + if (!stralloc_cats(&path, def)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + n = path.n; - def = env_get("PACK_DEF"); if (!def) def = PACK_DEF; - src = env_get("PACK_SRC"); if (!src) src = PACK_SRC; - out = env_get("PACK_OUT"); if (!out) out = PACK_OUT; + d[0] = "default"; + d[1] = p->ver; + for (i = 0; i < 2; ++i) { + path.n = n; + if (!stralloc_cats(&path, d[i])) goto err; + if (!stralloc_cats(&path, "/patch/")) goto err; + m = path.n; + if (!stralloc_cat0(&path)) goto err; + dp = opendir(path.x); + if (dp == NULL) { + if (errno == ENOTDIR || errno == ENOENT) continue; + goto err; + } + while ((de = readdir(dp)) != NULL) { + if (str_equal(de->d_name, ".")) continue; + if (str_equal(de->d_name, "..")) continue; - if (!fs_isdir(def)) errno = ENOTDIR, log_diesys1(101, def); - if (!fs_isdir(src)) errno = ENOTDIR, log_diesys1(101, src); + path.n = m; + if (!stralloc_cats(&path, de->d_name)) goto err; + if (!stralloc_cat0(&path)) goto err; + if (!fs_redir_read(0, path.x)) goto err; + if (forkexec_wait(argv) != 0) goto err; + close(fd); + } + closedir(dp); + } - ++argv; + ret = 1; - n = pack_scan(&p, *argv); - if (!n || (*argv)[n] != '\0') - log_die2(1, "invalid package name format: ", *argv); +err: stralloc_free(&path); + close(fd); + if (ret == 0) closedir(dp); + return ret; +} - if (!pack_version(&p, def)) - log_diesys2(100, "could not read the version of ", p.name); +static int +build(pack const *p, char const *def) +{ + stralloc sa; + char const *argv[] = { NULL, NULL }; + size_t n; - if (!pack_env(&p, def, out)) - log_diesys2(100, "could not set environment variables for ", *argv); + stralloc_init(&sa); + if (!stralloc_cats(&sa, def)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + n = sa.n; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_cats(&sa, "/build")) goto err; + if (!stralloc_cat0(&sa)) goto err; - if (!pack_chdir(&p, src)) - log_diesys1(100, "could not change directory to the source directory"); + argv[0] = sa.x; + forkexec_wait_redir(argv, "../log"); + if (!(errno == ENOTDIR || errno == ENOENT)) goto err; - if (!pack_data(&p, def)) - log_diesys6(100, "copying ", p.name, "'s data from ", def, " to ", src); + sa.n = n; + if (!stralloc_cats(&sa, "default/build")) goto err; + if (!stralloc_cat0(&sa)) goto err; - if (!pack_patch(&p, def)) - log_diesys3(100, "patching ", p.name, "'s source"); + argv[0] = sa.x; + forkexec_wait_redir(argv, "../log"); + if (!(errno == ENOTDIR || errno == ENOENT)) goto err; + +err: stralloc_free(&sa); + return 0; +} + +static int +data(pack const *p, char const *def) +{ + stralloc sa = STRALLOC_ZERO; + size_t n; + int ret = 0; + + stralloc_init(&sa); + if (!stralloc_cats(&sa, def)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + n = sa.n; + if (!stralloc_cats(&sa, "default/data")) goto err; + if (!stralloc_cat0(&sa)) goto err; + + if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; - if (!pack_build(&p, def)) + sa.n = n; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_cats(&sa, "/data")) goto err; + if (!stralloc_cat0(&sa)) goto err; + + if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; + + ret = 1; + +err: stralloc_free(&sa); + return ret; +} + +static int +untar(void) +{ + char const *argv[] = { "tar", "-x", "-f", "../source.tar", NULL }; + return (forkexec_wait_redir(argv, "log") == 0); +} + +static int +pack_env_set(char const *name, char const *path) +{ + char buf[NAME_MAX]; + int len; + + for (len = 0; name[len] != '\0'; ++len) + buf[len] = isalnum(name[len]) ? name[len] : '_'; + buf[len] = '\0'; + return env_set(buf, path); +} + +static int +env(pack *p, char const *def, char const *pkg, char const *id) +{ + stralloc sa = STRALLOC_ZERO; + stralloc line = STRALLOC_ZERO; + buffer b; + char buf[1024]; + size_t r; + size_t n; + int fd = -1; + int ret = 0; + + stralloc_init(&sa); + if (!stralloc_cats(&sa, pkg)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, id)) goto err; + if (!stralloc_cats(&sa, "/build")) goto err; + if (!stralloc_cat0(&sa)) goto err; + if (!pack_env_set("PREFIX", sa.x)) goto err; + + errno = 0; + fd = pack_opendepfile(p, def); + if (fd == -1) { ret = (errno == ENOTDIR || errno == ENOENT); goto err; } + + stralloc_init(&line); + buffer_init(&b, &read, fd, buf, sizeof buf); + while (stralloc_zero(&line), (r = buffer_getline(&b, &line)) > 0) + { + pack new; + + line.n -= (line.x[line.n - 1] == '\n'); + if (!stralloc_cat0(&line)) goto err; + n = pack_scan(&new, line.x); if (n == 0 || line.x[n] != '\0') goto err; + if (!pack_version(&new, def)) goto err; + + stralloc_zero(&sa); + if (!stralloc_cats(&sa, pkg)) + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, new.name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, new.ver)) goto err; + if (!stralloc_cats(&sa, "/current/build")) goto err; + if (!stralloc_cat0(&sa)) goto err; + if (!pack_env_set(new.name, sa.x)) goto err; + } + ret = (r == 0); + +err: stralloc_free(&sa); + stralloc_free(&line); + if (fd >= 0) close(fd); + return ret; +} + +void +usage(char const *argv0, char const *param) +{ + log_err2("missing parameter: ", param); + log_usage(argv0, "-p pkgdir -d defdir package[/version]"); +} + +int +main(int argc, char **argv) +{ + pack p; + tai t; + char *def; + char *pkg; + char **av = argv; + char id[50 + PACK_FMT]; + + (void)argc; + + while (*++av) { + if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } + if (str_equal(*av, "-p")) { if (!(pkg = *++av)) break; continue; } + break; + } + + if (!def) usage(*argv, "-d"); + if (!pkg) usage(*argv, "-p"); + if (!*av) usage(*argv, "package[/version]"); + + if (!fs_isdir(def)) log_diesys1(101, def); + if (!fs_isdir(pkg)) log_diesys1(101, pkg); + + tai_now(&t); + id[0] = '@'; + id[fmt_ulong(id + 1, tai_approx(&t)) + 1] = '\0'; + + if (chdir(pkg) == -1) + log_diesys2(101, "chdir ", pkg); + if ((*av)[pack_scan(&p, *av)] != '\0') + log_diesys2(1, "invalid package format: ", *av); + if (mkdir(p.name, 075) == -1 && errno != EEXIST) + log_diesys4(101, "mkdir ", pkg, "/", p.name); + if (chdir(p.name) == -1) + log_diesys4(101, "chdir ", pkg, "/", p.name); + if (!pack_version(&p, def)) + log_diesys2(101, "pack_version ", p.name); + if (mkdir(p.ver, 075) == -1 && errno != EEXIST) + log_diesys6(101, "mkdir ", pkg, "/", p.name, "/", p.ver); + if (chdir(p.ver) == -1) + log_diesys6(101, "chdir ", pkg, "/", p.name, "/", p.ver); + if (mkdir(id, 075) == -1 && errno != EEXIST) + log_diesys8(101, "mkdir ", pkg, "/", p.name, "/", p.ver, "/", id); + if (chdir(id) == -1) + log_diesys8(101, "chdir ", pkg, "/", p.name, "/", p.ver, "/", id); + if (mkdir("build", 075) == -1 && errno != EEXIST) + log_diesys9(101, "mkdir ", pkg, "/", p.name, "/", p.ver, "/", id, "/build"); + if (!env(&p, def, pkg, id)) + log_diesys2(100, "could not set environment variables for ", *argv); + if (!untar()) + log_diesys9(101, "untar to ", pkg, "/", p.name, "/", p.ver, "/", id, "/source"); + if (mkdir("source", 075) == -1 && errno != EEXIST) + log_diesys9(101, "mkdir ", pkg, "/", p.name, "/", p.ver, "/", id, "/source"); + if (chdir("source") == -1) + log_diesys4(101, "could not change directory to ", pkg, "/", p.name); + if (!data(&p, def)) + log_diesys6(100, "copying ", p.name, "'s data from ", def, " to ", pkg); + if (!patch(&p, def)) + log_diesys3(100, "patching ", p.name, "'s source"); + if (!build(&p, def)) log_diesys2(100, "could not execute build script of ", p.name); return 0; diff --git a/pack.c b/pack.c @@ -14,6 +14,7 @@ #include "pack.h" #include "stralloc.h" #include "lock.h" +#include "tai.h" size_t pack_fmt(pack const *p, char *buf) @@ -131,29 +132,6 @@ err: } int -pack_remove(pack const *p, char const *dir) -{ - stralloc sa; - int ret = 0; - - stralloc_init(&sa); - if (!stralloc_cats(&sa, dir)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, p->name)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, p->ver)) goto err; - if (!stralloc_cat0(&sa)) goto err; - - errno = 0; - if (!fs_remove_r(sa.x) && errno != ENOTDIR && errno != ENOENT) goto err; - - ret = 1; -err: - stralloc_free(&sa); - return ret; -} - -int pack_hasdep(pack const *p, genalloc *packs) { size_t i; @@ -170,55 +148,25 @@ pack_hasdep(pack const *p, genalloc *packs) } int -pack_step(pack const *p, char const *cmd, char const *log, char const *dir, char const *sub) +pack_lock(pack *p, char const *pkg) { - char fmt[PACK_FMT]; - char const *argv[] = { NULL, NULL, NULL }; stralloc sa; - stralloc sa_tmp; - stralloc sa_log; - char pidfmt[20]; - pid_t pid; - int fd_lock = -1; - int ret = -1; - - /* lock this operation on this package */ - - stralloc_zero(&sa); - if (!pack_dir(p, &sa_lock, dir, "lock")) goto err; - if (!fs_mkdir_r(sa_lock.x)) goto err; - --sa_lock.n; - if (!stralloc_cats(&sa_lock, "/")) goto err; - if (!stralloc_cats(&sa_lock, sub)) goto err; - if (!stralloc_cat0(&sa_lock)) goto err; - - if ((fd = open_write(sa_lock.x)) == -1) goto err; - if (!lock_exclusive(fd)) { close(fd); return -1; } - - /* perform the step in a temporary directory */ - - if ((pid = getpid() == -1)) goto err; - stralloc_init(&sa_tmp); - if (!pack_dir(p, &sa_tmp, dir, "tmp")) goto err; - --sa_tmp.n; - if (!stralloc_cats(&sa_tmp, "/")) goto err; - if (!stralloc_catulong(&sa_tmp, pid)) goto err; - if (!stralloc_cat0(&sa_tmp)) goto err; - - if (!fs_mkdir_r(sa.x, 0640)) goto err; + int fd = -1; + int ret = 0; - stralloc_zero(&sa); - if (!pack_dir(p, &sa, dir, "log")) goto err; - if (!fs_mkdir_r(sa.x, 0640)) goto err; - --sa.n; + stralloc_init(&sa); + if (!stralloc_cats(&sa, pkg)) goto err; if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, cmd + (str_start(cmd, "pack-") ? 5 : 0))) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_cats(&sa, "/source.tar")) goto err; if (!stralloc_cat0(&sa)) goto err; - argv[0] = cmd; - pack_fmt(p, fmt); - argv[1] = fmt; - ret = forkexec_wait_redir(argv, sa.x); + if ((fd = open_trunc(sa.x)) == -1) goto err; + if (!lock_exclusive(fd)) { ret = -1; goto err; } + + ret = fd; err: stralloc_free(&sa); return ret; @@ -262,245 +210,3 @@ err: close(fd); return ret; } - -/* - * CWD must be set to the source directory - */ -int -pack_patch(pack const *p, char *def) -{ - char const *argv[] = { "patch", "-N", NULL }; - char const *d[2]; - stralloc path; - size_t n; - size_t m; - DIR *dp = NULL; - struct dirent *de; - int i; - int fd = -1; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, def)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - - d[0] = "default"; - d[1] = p->ver; - for (i = 0; i < 2; ++i) { - path.n = n; - if (!stralloc_cats(&path, d[i])) goto err; - if (!stralloc_cats(&path, "/patch/")) goto err; - m = path.n; - if (!stralloc_cat0(&path)) goto err; - dp = opendir(path.x); - if (dp == NULL) { - if (errno == ENOTDIR || errno == ENOENT) continue; - goto err; - } - while ((de = readdir(dp)) != NULL) { - if (str_equal(de->d_name, ".")) continue; - if (str_equal(de->d_name, "..")) continue; - - path.n = m; - if (!stralloc_cats(&path, de->d_name)) goto err; - if (!stralloc_cat0(&path)) goto err; - if (!fs_redir_read(0, path.x)) goto err; - if (forkexec_wait(argv) != 0) goto err; - close(fd); - } - closedir(dp); - } - - ret = 1; - -err: stralloc_free(&path); - close(fd); - if (ret == 0) closedir(dp); - return ret; -} - -int -pack_chdir(pack const *p, char const *src) -{ - stralloc path; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, src)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_cat0(&path)) goto err; - - if (chdir(path.x) == -1) goto err; - - ret = 1; - -err: stralloc_free(&path); - return ret; -} - -/* - * CWD must be set to the source directory - */ -int -pack_build(pack const *p, char const *def) -{ - stralloc path; - char *argv[] = { "build", NULL }; - size_t n; - - stralloc_init(&path); - if (!stralloc_cats(&path, def)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_cats(&path, "/build")) goto err; - if (!stralloc_cat0(&path)) goto err; - - execv(path.x, argv); - if (!(errno == ENOTDIR || errno == ENOENT)) goto err; - - path.n = n; - if (!stralloc_cats(&path, "default/build")) goto err; - if (!stralloc_cat0(&path)) goto err; - - execv(path.x, argv); - if (!(errno == ENOTDIR || errno == ENOENT)) goto err; - -err: stralloc_free(&path); - return 0; -} - -/* - * CWD must be set to the source directory - */ -int -pack_data(pack const *p, char const *def) -{ - stralloc sa = STRALLOC_ZERO; - size_t n; - int ret = 0; - - stralloc_init(&sa); - if (!stralloc_cats(&sa, def)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, p->name)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - n = sa.n; - if (!stralloc_cats(&sa, "default/data")) goto err; - if (!stralloc_cat0(&sa)) goto err; - - if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; - - sa.n = n; - if (!stralloc_cats(&sa, p->ver)) goto err; - if (!stralloc_cats(&sa, "/data")) goto err; - if (!stralloc_cat0(&sa)) goto err; - - if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; - - ret = 1; - -err: stralloc_free(&sa); - return ret; -} - -int -pack_envset(char const *name, char const *path) -{ - char buf[NAME_MAX]; - int len; - - for (len = 0; name[len] != '\0'; ++len) - buf[len] = isalnum(name[len]) ? name[len] : '_'; - buf[len] = '\0'; - return env_set(buf, path); -} - -int -pack_env(pack *p, const char *def, const char *out) -{ - stralloc path = STRALLOC_ZERO; - stralloc line = STRALLOC_ZERO; - buffer b; - char buf[1024]; - size_t r; - size_t n; - size_t m; - int fd = -1; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, out)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_cat0(&path)) goto err; - if (!pack_envset("PREFIX", path.x)) goto err; - - errno = 0; - fd = pack_opendepfile(p, def); - if (fd == -1) { ret = (errno == ENOTDIR || errno == ENOENT); goto err; } - - buffer_init(&b, &read, fd, buf, sizeof buf); - stralloc_init(&line); - while (stralloc_zero(&line), (r = buffer_getline(&b, &line)) > 0) { - pack new; - - if (!stralloc_cat0(&line)) goto err; - m = pack_scan(&new, line.x); if (m == 0) goto err; - if (line.x[m] != '\n' && line.x[m] != '\0') goto err; - if (!pack_version(&new, def)) goto err; - - path.n = n; - if (!stralloc_cats(&path, new.name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, new.ver)) goto err; - if (!stralloc_cat0(&path)) goto err; - if (!pack_envset(new.name, path.x)) goto err; - } - ret = (r == 0); - -err: stralloc_free(&path); - stralloc_free(&line); - if (fd >= 0) close(fd); - return ret; -} - -int -pack_dir(pack const *p, stralloc *sa, char const *dir, char const *sub) -{ - stralloc_zero(sa); - if (!stralloc_cats(sa, dir)) return 0; - if (!stralloc_cats(sa, "/")) return 0; - if (!stralloc_cats(sa, p->name)) return 0; - if (!stralloc_cats(sa, "/")) return 0; - if (!stralloc_cats(sa, p->ver)) return 0; - if (!stralloc_cats(sa, "/")) return 0; - if (!stralloc_cats(sa, sub)) return 0; - if (!stralloc_cat0(sa)) return 0; - return 1; -} - -int -pack_isdone(pack const *p, char *dir, char *sub) -{ - stralloc sa; - int ret = 0; - - stralloc_init(&sa); - if (!pack_dir(p, &sa, dir, sub)) goto err; - ret = fs_isdir(sa.x); -err: - stralloc_free(&sa); - return ret; -} diff --git a/pack.h b/pack.h @@ -17,18 +17,10 @@ typedef struct { char ver[NAME_MAX]; } pack; -int pack_build(pack const *, char const *); -int pack_chdir(pack const *, char const *); -int pack_data(pack const *, char const *); int pack_dep(pack const *, genalloc *, char const *); -int pack_env(pack *, const char *, const char *); -int pack_envset(char const *, char const *); int pack_hasdep(pack const *, genalloc *); -int pack_isdone(pack const *, char *); +int pack_lock(pack *, char const *); int pack_opendepfile(pack const *, char const *); -int pack_patch(pack const *, char *); -int pack_remove(pack const *, char const *); -int pack_step(pack const *, char const *, char const *, char const *); int pack_version(pack *, char const *); size_t pack_fmt(pack const *, char *); size_t pack_scan(pack *, char const *); diff --git a/tai.c b/tai.c @@ -0,0 +1,33 @@ +#include "tai.h" + +void +tai_pack(char *s, tai *t) +{ + uint64_t x; + + x = t->x; + s[7] = x & 255; x >>= 8; + s[6] = x & 255; x >>= 8; + s[5] = x & 255; x >>= 8; + s[4] = x & 255; x >>= 8; + s[3] = x & 255; x >>= 8; + s[2] = x & 255; x >>= 8; + s[1] = x & 255; x >>= 8; + s[0] = x; +} + +void +tai_unpack(char *s, tai *t) +{ + uint64_t x; + + x = (unsigned char) s[0]; + x <<= 8; x += (unsigned char) s[1]; + x <<= 8; x += (unsigned char) s[2]; + x <<= 8; x += (unsigned char) s[3]; + x <<= 8; x += (unsigned char) s[4]; + x <<= 8; x += (unsigned char) s[5]; + x <<= 8; x += (unsigned char) s[6]; + x <<= 8; x += (unsigned char) s[7]; + t->x = x; +} diff --git a/tai.h b/tai.h @@ -0,0 +1,23 @@ +#ifndef TAI_H +#define TAI_H + +#include <time.h> +#include "uint64.h" + +typedef struct { + uint64_t x; +} tai; + +#define TAI_PACK 8 + +#define tai_approx(t) ((t)->x) +#define tai_add(t, t1, t2) ((t)->x = (t1)->x + (t2)->x) +#define tai_sub(t, t1, t2) ((t)->x = (t1)->x - (t2)->x) +#define tai_less(t1, t2) ((t1)->x < (t2)->x) +#define tai_more(t1, t2) ((t1)->x > (t2)->x) +#define tai_now(t) ((t)->x = 0x400000000000000aULL + (uint64_t)time((time_t *)0)) + +void tai_pack(char *, tai *); +void tai_unpack(char *, tai *); + +#endif diff --git a/uint64.c b/uint64.c @@ -0,0 +1,61 @@ +#include "uint64.h" + +void +uint64_pack(char s[8], uint64_t u) +{ + s[7] = u & 255; + s[6] = u >> 8 & 255; + s[5] = u >> 16 & 255; + s[4] = u >> 24 & 255; + s[3] = u >> 32 & 255; + s[2] = u >> 40 & 255; + s[1] = u >> 48 & 255; + s[0] = u >> 56; +} + +void +uint64_pack_big(char s[8], uint64_t u) +{ + s[0] = u & 255; + s[1] = u >> 8 & 255; + s[2] = u >> 16 & 255; + s[3] = u >> 24 & 255; + s[4] = u >> 32 & 255; + s[5] = u >> 40 & 255; + s[6] = u >> 48 & 255; + s[7] = u >> 56; +} + +void +uint64_unpack(const char s[8], uint64_t *u) +{ + uint64_t x; + + x = (unsigned char)s[7]; + x += (uint64_t)(unsigned char)s[6] << 8; + x += (uint64_t)(unsigned char)s[5] << 16; + x += (uint64_t)(unsigned char)s[4] << 24; + x += (uint64_t)(unsigned char)s[3] << 32; + x += (uint64_t)(unsigned char)s[2] << 40; + x += (uint64_t)(unsigned char)s[1] << 48; + x += (uint64_t)(unsigned char)s[0] << 56; + + *u = x; +} + +void +uint64_unpack_big(const char s[8], uint64_t *u) +{ + uint64_t x; + + x = (unsigned char)s[0]; + x += (uint64_t)(unsigned char)s[1] << 8; + x += (uint64_t)(unsigned char)s[2] << 16; + x += (uint64_t)(unsigned char)s[3] << 24; + x += (uint64_t)(unsigned char)s[4] << 32; + x += (uint64_t)(unsigned char)s[5] << 40; + x += (uint64_t)(unsigned char)s[6] << 48; + x += (uint64_t)(unsigned char)s[7] << 56; + + *u = x; +} diff --git a/uint64.h b/uint64.h @@ -0,0 +1,11 @@ +#ifndef UINT64_H +#define UINT64_H + +#include <stdint.h> + +void uint64_pack(char s[8], uint64_t); +void uint64_pack_big(char s[8], uint64_t); +void uint64_unpack(const char s[8], uint64_t *); +void uint64_unpack_big(const char s[8], uint64_t *); + +#endif