├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── blockfiles.c ├── blockfiles.h ├── blocksize-stats └── Makefile ├── ccan ├── README ├── ccan │ ├── alignof │ │ ├── LICENSE │ │ ├── _info │ │ ├── alignof.h │ │ └── test │ │ │ └── run.c │ ├── build_assert │ │ ├── LICENSE │ │ ├── _info │ │ ├── build_assert.h │ │ └── test │ │ │ ├── compile_fail-expr.c │ │ │ ├── compile_fail.c │ │ │ ├── compile_ok.c │ │ │ └── run-BUILD_ASSERT_OR_ZERO.c │ ├── cast │ │ ├── LICENSE │ │ ├── _info │ │ ├── cast.h │ │ └── test │ │ │ ├── compile_fail-cast_const.c │ │ │ ├── compile_fail-cast_const2.c │ │ │ ├── compile_fail-cast_const3.c │ │ │ ├── compile_fail-cast_signed-const.c │ │ │ ├── compile_fail-cast_signed-sizesame.c │ │ │ ├── compile_fail-cast_signed.c │ │ │ ├── compile_fail-cast_static-2.c │ │ │ ├── compile_fail-cast_static-3.c │ │ │ ├── compile_fail-cast_static.c │ │ │ ├── compile_ok-cast_void.c │ │ │ └── compile_ok-static.c │ ├── check_type │ │ ├── LICENSE │ │ ├── _info │ │ ├── check_type.h │ │ └── test │ │ │ ├── compile_fail-check_type.c │ │ │ ├── compile_fail-check_type_unsigned.c │ │ │ ├── compile_fail-check_types_match.c │ │ │ └── run.c │ ├── compiler │ │ ├── LICENSE │ │ ├── _info │ │ ├── compiler.h │ │ └── test │ │ │ ├── compile_fail-printf.c │ │ │ └── run-is_compile_constant.c │ ├── container_of │ │ ├── LICENSE │ │ ├── _info │ │ ├── container_of.h │ │ └── test │ │ │ ├── compile_fail-bad-type.c │ │ │ ├── compile_fail-types.c │ │ │ ├── compile_fail-var-types.c │ │ │ └── run.c │ ├── endian │ │ ├── LICENSE │ │ ├── _info │ │ ├── endian.h │ │ └── test │ │ │ ├── compile_ok-constant.c │ │ │ └── run.c │ ├── err │ │ ├── LICENSE │ │ ├── _info │ │ ├── err.c │ │ ├── err.h │ │ └── test │ │ │ └── run.c │ ├── hash │ │ ├── LICENSE │ │ ├── _info │ │ ├── hash.c │ │ ├── hash.h │ │ └── test │ │ │ ├── api-hash_stable.c │ │ │ └── run.c │ ├── htable │ │ ├── LICENSE │ │ ├── _info │ │ ├── htable.c │ │ ├── htable.h │ │ ├── htable_type.h │ │ ├── test │ │ │ ├── run-size.c │ │ │ ├── run-type.c │ │ │ ├── run-zero-hash-first-entry.c │ │ │ └── run.c │ │ └── tools │ │ │ ├── Makefile │ │ │ ├── hsearchspeed.c │ │ │ ├── speed.c │ │ │ └── stringspeed.c │ ├── likely │ │ ├── LICENSE │ │ ├── _info │ │ ├── likely.c │ │ ├── likely.h │ │ └── test │ │ │ ├── run-debug.c │ │ │ └── run.c │ ├── list │ │ ├── LICENSE │ │ ├── _info │ │ ├── list.c │ │ ├── list.h │ │ └── test │ │ │ ├── compile_ok-constant.c │ │ │ ├── helper.c │ │ │ ├── helper.h │ │ │ ├── run-CCAN_LIST_DEBUG.c │ │ │ ├── run-check-corrupt.c │ │ │ ├── run-check-nonconst.c │ │ │ ├── run-list_del_from-assert.c │ │ │ ├── run-list_prev-list_next.c │ │ │ ├── run-prepend_list.c │ │ │ ├── run-single-eval.c │ │ │ ├── run-with-debug.c │ │ │ └── run.c │ ├── membuf │ │ └── membuf.h │ ├── noerr │ │ ├── LICENSE │ │ ├── _info │ │ ├── noerr.c │ │ ├── noerr.h │ │ └── test │ │ │ └── run.c │ ├── opt │ │ ├── LICENSE │ │ ├── _info │ │ ├── helpers.c │ │ ├── opt.c │ │ ├── opt.h │ │ ├── parse.c │ │ ├── private.h │ │ ├── test │ │ │ ├── compile_ok-const-arg.c │ │ │ ├── run-add_desc.c │ │ │ ├── run-checkopt.c │ │ │ ├── run-consume_words.c │ │ │ ├── run-correct-reporting.c │ │ │ ├── run-early.c │ │ │ ├── run-helpers.c │ │ │ ├── run-iter.c │ │ │ ├── run-no-options.c │ │ │ ├── run-set_alloc.c │ │ │ ├── run-usage.c │ │ │ ├── run.c │ │ │ ├── utils.c │ │ │ └── utils.h │ │ └── usage.c │ ├── rbuf │ │ ├── LICENSE │ │ ├── _info │ │ ├── rbuf.c │ │ ├── rbuf.h │ │ └── test │ │ │ ├── run-all.c │ │ │ ├── run-open.c │ │ │ ├── run-partial-read.c │ │ │ ├── run-term-eof.c │ │ │ └── run.c │ ├── short_types │ │ ├── LICENSE │ │ ├── _info │ │ ├── short_types.h │ │ └── test │ │ │ ├── run-endian.c │ │ │ └── run.c │ ├── str │ │ ├── LICENSE │ │ ├── _info │ │ ├── debug.c │ │ ├── hex │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── hex.c │ │ │ ├── hex.h │ │ │ └── test │ │ │ │ └── run.c │ │ ├── str.c │ │ ├── str.h │ │ ├── str_debug.h │ │ └── test │ │ │ ├── compile_fail-STR_MAX_CHARS.c │ │ │ ├── compile_fail-isalnum.c │ │ │ ├── compile_fail-isalpha.c │ │ │ ├── compile_fail-isascii.c │ │ │ ├── compile_fail-isblank.c │ │ │ ├── compile_fail-iscntrl.c │ │ │ ├── compile_fail-isdigit.c │ │ │ ├── compile_fail-islower.c │ │ │ ├── compile_fail-isprint.c │ │ │ ├── compile_fail-ispunct.c │ │ │ ├── compile_fail-isspace.c │ │ │ ├── compile_fail-isupper.c │ │ │ ├── compile_fail-isxdigit.c │ │ │ ├── compile_fail-strchr.c │ │ │ ├── compile_fail-strrchr.c │ │ │ ├── compile_fail-strstr.c │ │ │ ├── debug.c │ │ │ ├── run-STR_MAX_CHARS.c │ │ │ └── run.c │ ├── take │ │ ├── LICENSE │ │ ├── _info │ │ ├── take.c │ │ ├── take.h │ │ └── test │ │ │ └── run.c │ ├── tal │ │ ├── LICENSE │ │ ├── _info │ │ ├── benchmark │ │ │ ├── Makefile │ │ │ ├── samba-allocs.c │ │ │ ├── speed.c │ │ │ └── talloc.dump │ │ ├── grab_file-Makefile │ │ ├── grab_file │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── grab_file.c │ │ │ ├── grab_file.h │ │ │ └── test │ │ │ │ └── run-grab.c │ │ ├── link-Makefile │ │ ├── link │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── link.c │ │ │ ├── link.h │ │ │ └── test │ │ │ │ └── run.c │ │ ├── path-Makefile │ │ ├── path │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── path.c │ │ │ ├── path.h │ │ │ ├── path │ │ │ │ ├── LICENSE │ │ │ │ ├── _info │ │ │ │ ├── path.c │ │ │ │ ├── path.h │ │ │ │ └── test │ │ │ │ │ ├── run-basename.c │ │ │ │ │ ├── run-canon.c │ │ │ │ │ ├── run-cwd.c │ │ │ │ │ ├── run-dirname.c │ │ │ │ │ ├── run-ext_off.c │ │ │ │ │ ├── run-is_abs.c │ │ │ │ │ ├── run-is_dir.c │ │ │ │ │ ├── run-is_file.c │ │ │ │ │ ├── run-join.c │ │ │ │ │ ├── run-pushd.c │ │ │ │ │ ├── run-readlink.c │ │ │ │ │ ├── run-rel.c │ │ │ │ │ ├── run-simplify.c │ │ │ │ │ └── run-split.c │ │ │ └── test │ │ │ │ ├── run-basename.c │ │ │ │ ├── run-canon.c │ │ │ │ ├── run-cwd.c │ │ │ │ ├── run-dirname.c │ │ │ │ ├── run-ext_off.c │ │ │ │ ├── run-is_abs.c │ │ │ │ ├── run-is_dir.c │ │ │ │ ├── run-is_file.c │ │ │ │ ├── run-join.c │ │ │ │ ├── run-pushd.c │ │ │ │ ├── run-readlink.c │ │ │ │ ├── run-rel.c │ │ │ │ ├── run-simplify.c │ │ │ │ └── run-split.c │ │ ├── stack-Makefile │ │ ├── stack │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── stack.c │ │ │ ├── stack.h │ │ │ └── test │ │ │ │ └── run-stack.c │ │ ├── str-Makefile │ │ ├── str │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── str.c │ │ │ ├── str.h │ │ │ ├── str │ │ │ │ ├── LICENSE │ │ │ │ ├── _info │ │ │ │ ├── str.c │ │ │ │ ├── str.h │ │ │ │ └── test │ │ │ │ │ ├── helper.h │ │ │ │ │ ├── run-string.c │ │ │ │ │ ├── run-strreg.c │ │ │ │ │ ├── run-take.c │ │ │ │ │ └── run.c │ │ │ └── test │ │ │ │ ├── helper.h │ │ │ │ ├── run-string.c │ │ │ │ ├── run-strreg.c │ │ │ │ ├── run-take.c │ │ │ │ └── run.c │ │ ├── tal.c │ │ ├── tal.h │ │ ├── talloc │ │ │ ├── LICENSE │ │ │ ├── _info │ │ │ ├── talloc.c │ │ │ ├── talloc.h │ │ │ ├── talloc │ │ │ │ ├── LICENSE │ │ │ │ ├── _info │ │ │ │ ├── talloc.c │ │ │ │ ├── talloc.h │ │ │ │ └── test │ │ │ │ │ ├── run-array.c │ │ │ │ │ ├── run-count.c │ │ │ │ │ ├── run-destructor.c │ │ │ │ │ ├── run-expand.c │ │ │ │ │ ├── run-free.c │ │ │ │ │ ├── run-named-debug.c │ │ │ │ │ ├── run-named-nolabels.c │ │ │ │ │ ├── run-named.c │ │ │ │ │ ├── run-overflow.c │ │ │ │ │ ├── run-steal.c │ │ │ │ │ ├── run-take.c │ │ │ │ │ └── run.c │ │ │ └── test │ │ │ │ ├── run-array.c │ │ │ │ ├── run-count.c │ │ │ │ ├── run-destructor.c │ │ │ │ ├── run-expand.c │ │ │ │ ├── run-free.c │ │ │ │ ├── run-named-debug.c │ │ │ │ ├── run-named-nolabels.c │ │ │ │ ├── run-named.c │ │ │ │ ├── run-overflow.c │ │ │ │ ├── run-steal.c │ │ │ │ ├── run-take.c │ │ │ │ └── run.c │ │ └── test │ │ │ ├── run-allocfail.c │ │ │ ├── run-array.c │ │ │ ├── run-count.c │ │ │ ├── run-destructor.c │ │ │ ├── run-expand.c │ │ │ ├── run-free.c │ │ │ ├── run-groups-grow.c │ │ │ ├── run-iter.c │ │ │ ├── run-named-debug.c │ │ │ ├── run-named-nolabels.c │ │ │ ├── run-named.c │ │ │ ├── run-notifier.c │ │ │ ├── run-overflow.c │ │ │ ├── run-resizez.c │ │ │ ├── run-steal.c │ │ │ ├── run-take.c │ │ │ ├── run-test-backend.c │ │ │ └── run.c │ ├── tcon │ │ └── tcon.h │ └── typesafe_cb │ │ ├── LICENSE │ │ ├── _info │ │ ├── test │ │ ├── compile_fail-cast_if_type-promotable.c │ │ ├── compile_fail-typesafe_cb-int.c │ │ ├── compile_fail-typesafe_cb.c │ │ ├── compile_fail-typesafe_cb_cast-multi.c │ │ ├── compile_fail-typesafe_cb_cast.c │ │ ├── compile_fail-typesafe_cb_postargs.c │ │ ├── compile_fail-typesafe_cb_preargs.c │ │ ├── compile_ok-typesafe_cb-NULL.c │ │ ├── compile_ok-typesafe_cb-undefined.c │ │ ├── compile_ok-typesafe_cb-vars.c │ │ ├── compile_ok-typesafe_cb_cast.c │ │ └── run.c │ │ └── typesafe_cb.h ├── licenses │ ├── BSD-MIT │ ├── CC0 │ ├── GPL-2 │ ├── LGPL-2.1 │ └── LGPL-3 └── tools │ └── configurator │ └── configurator.c ├── doc ├── bitcoin-iterate.1 └── bitcoin-iterate.1.txt ├── dump.c ├── dump.h ├── io.c ├── io.h ├── iterate.c ├── parse.c ├── parse.h ├── space.h └── types.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | ccan/config.h 3 | bitcoin-iterate 4 | ccan/tools/configurator/configurator 5 | ccan/tools/configurator/configurator.dSYM/* 6 | configurator.out.dSYM/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ccan/licenses/GPL-2 -------------------------------------------------------------------------------- /blockfiles.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | enum networks { 4 | MAIN, 5 | TESTNET3, 6 | REGTEST, 7 | SIGNET 8 | }; 9 | 10 | /* Return a tal_array of filenames. */ 11 | char **block_filenames(tal_t *ctx, const char *base, enum networks network); 12 | -------------------------------------------------------------------------------- /ccan/README: -------------------------------------------------------------------------------- 1 | CCAN imported from http://ccodearchive.net. 2 | 3 | CCAN version: b45a3266352be0da51c532c8dc462e67e5830877 4 | -------------------------------------------------------------------------------- /ccan/ccan/alignof/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/alignof/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * alignof - ALIGNOF() macro to determine alignment of a type. 7 | * 8 | * Many platforms have requirements that certain types must be aligned 9 | * to certain address boundaries, such as ints needing to be on 4-byte 10 | * boundaries. Attempting to access variables with incorrect 11 | * alignment may cause performance loss or even program failure (eg. a 12 | * bus signal). 13 | * 14 | * There are times which it's useful to be able to programatically 15 | * access these requirements, such as for dynamic allocators. 16 | * 17 | * Example: 18 | * #include 19 | * #include 20 | * #include 21 | * 22 | * // Output contains "ALIGNOF(char) == 1" 23 | * // Will also print out whether an onstack char array can hold a long. 24 | * int main(void) 25 | * { 26 | * char arr[sizeof(int)]; 27 | * 28 | * printf("ALIGNOF(char) == %zu\n", ALIGNOF(char)); 29 | * if ((unsigned long)arr % ALIGNOF(int)) { 30 | * printf("arr %p CANNOT hold an int\n", arr); 31 | * exit(1); 32 | * } else { 33 | * printf("arr %p CAN hold an int\n", arr); 34 | * exit(0); 35 | * } 36 | * } 37 | * 38 | * License: CC0 (Public domain) 39 | * Author: Rusty Russell 40 | */ 41 | int main(int argc, char *argv[]) 42 | { 43 | if (argc != 2) 44 | return 1; 45 | 46 | if (strcmp(argv[1], "depends") == 0) { 47 | return 0; 48 | } 49 | 50 | return 1; 51 | } 52 | -------------------------------------------------------------------------------- /ccan/ccan/alignof/alignof.h: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #ifndef CCAN_ALIGNOF_H 3 | #define CCAN_ALIGNOF_H 4 | #include "config.h" 5 | 6 | /** 7 | * ALIGNOF - get the alignment of a type 8 | * @t: the type to test 9 | * 10 | * This returns a safe alignment for the given type. 11 | */ 12 | #if HAVE_ALIGNOF 13 | /* A GCC extension. */ 14 | #define ALIGNOF(t) __alignof__(t) 15 | #else 16 | /* Alignment by measuring structure padding. */ 17 | #define ALIGNOF(t) ((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0) 18 | #endif 19 | 20 | #endif /* CCAN_ALIGNOF_H */ 21 | -------------------------------------------------------------------------------- /ccan/ccan/alignof/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* Alignment is remarkably difficult to test. The rules may be more 7 | * complex than ALIGNOF() can know: eg. on i386 __alignof__(double) == 8, but 8 | * __alignof__(struct containing double) == 4. 9 | * 10 | * Technically, we can only test that we give *at least* the alignment which 11 | * naturally occurs, and that accesses work. 12 | * 13 | * For the moment, we work around double. */ 14 | struct lots_of_types 15 | { 16 | char c; 17 | short s; 18 | char c2; 19 | int i; 20 | char c3; 21 | float f; 22 | char c4; 23 | double d; 24 | char c5; 25 | }; 26 | 27 | int main(void) 28 | { 29 | struct lots_of_types lots_of_types, *lp = malloc(sizeof(*lp)); 30 | char c; 31 | short s; 32 | char c2; 33 | int i; 34 | char c3; 35 | float f; 36 | char c4; 37 | double d; 38 | 39 | /* Make sure we use all the variables. */ 40 | c = c2 = c3 = c4 = 0; 41 | 42 | plan_tests(15); 43 | ok1((unsigned long)&c % ALIGNOF(char) == 0); 44 | ok1((unsigned long)&s % ALIGNOF(short) == 0); 45 | ok1((unsigned long)&i % ALIGNOF(int) == 0); 46 | ok1((unsigned long)&f % ALIGNOF(float) == 0); 47 | ok1((unsigned long)&d % ALIGNOF(double) == 0); 48 | 49 | ok1((unsigned long)&lots_of_types.c % ALIGNOF(char) == 0); 50 | ok1((unsigned long)&lots_of_types.s % ALIGNOF(short) == 0); 51 | ok1((unsigned long)&lots_of_types.i % ALIGNOF(int) == 0); 52 | ok1((unsigned long)&lots_of_types.f % ALIGNOF(float) == 0); 53 | ok1(offsetof(struct lots_of_types, d) % ALIGNOF(double) == 0); 54 | 55 | ok1((unsigned long)&lp->c % ALIGNOF(char) == 0); 56 | ok1((unsigned long)&lp->s % ALIGNOF(short) == 0); 57 | ok1((unsigned long)&lp->i % ALIGNOF(int) == 0); 58 | ok1((unsigned long)&lp->f % ALIGNOF(float) == 0); 59 | ok1((unsigned long)&lp->d % ALIGNOF(double) == 0); 60 | exit(exit_status()); 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/build_assert/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * build_assert - routines for build-time assertions 7 | * 8 | * This code provides routines which will cause compilation to fail should some 9 | * assertion be untrue: such failures are preferable to run-time assertions, 10 | * but much more limited since they can only depends on compile-time constants. 11 | * 12 | * These assertions are most useful when two parts of the code must be kept in 13 | * sync: it is better to avoid such cases if possible, but seconds best is to 14 | * detect invalid changes at build time. 15 | * 16 | * For example, a tricky piece of code might rely on a certain element being at 17 | * the start of the structure. To ensure that future changes don't break it, 18 | * you would catch such changes in your code like so: 19 | * 20 | * Example: 21 | * #include 22 | * #include 23 | * 24 | * struct foo { 25 | * char string[5]; 26 | * int x; 27 | * }; 28 | * 29 | * static char *foo_string(struct foo *foo) 30 | * { 31 | * // This trick requires that the string be first in the structure 32 | * BUILD_ASSERT(offsetof(struct foo, string) == 0); 33 | * return (char *)foo; 34 | * } 35 | * 36 | * License: CC0 (Public domain) 37 | * Author: Rusty Russell 38 | */ 39 | int main(int argc, char *argv[]) 40 | { 41 | if (argc != 2) 42 | return 1; 43 | 44 | if (strcmp(argv[1], "depends") == 0) 45 | /* Nothing. */ 46 | return 0; 47 | 48 | return 1; 49 | } 50 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/build_assert.h: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #ifndef CCAN_BUILD_ASSERT_H 3 | #define CCAN_BUILD_ASSERT_H 4 | 5 | /** 6 | * BUILD_ASSERT - assert a build-time dependency. 7 | * @cond: the compile-time condition which must be true. 8 | * 9 | * Your compile will fail if the condition isn't true, or can't be evaluated 10 | * by the compiler. This can only be used within a function. 11 | * 12 | * Example: 13 | * #include 14 | * ... 15 | * static char *foo_to_char(struct foo *foo) 16 | * { 17 | * // This code needs string to be at start of foo. 18 | * BUILD_ASSERT(offsetof(struct foo, string) == 0); 19 | * return (char *)foo; 20 | * } 21 | */ 22 | #define BUILD_ASSERT(cond) \ 23 | do { (void) sizeof(char [1 - 2*!(cond)]); } while(0) 24 | 25 | /** 26 | * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. 27 | * @cond: the compile-time condition which must be true. 28 | * 29 | * Your compile will fail if the condition isn't true, or can't be evaluated 30 | * by the compiler. This can be used in an expression: its value is "0". 31 | * 32 | * Example: 33 | * #define foo_to_char(foo) \ 34 | * ((char *)(foo) \ 35 | * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) 36 | */ 37 | #define BUILD_ASSERT_OR_ZERO(cond) \ 38 | (sizeof(char [1 - 2*!(cond)]) - 1) 39 | 40 | #endif /* CCAN_BUILD_ASSERT_H */ 41 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/test/compile_fail-expr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | #ifdef FAIL 6 | return BUILD_ASSERT_OR_ZERO(1 == 0); 7 | #else 8 | return 0; 9 | #endif 10 | } 11 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/test/compile_fail.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | #ifdef FAIL 6 | BUILD_ASSERT(1 == 0); 7 | #endif 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/test/compile_ok.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | BUILD_ASSERT(1 == 1); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /ccan/ccan/build_assert/test/run-BUILD_ASSERT_OR_ZERO.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | plan_tests(1); 7 | ok1(BUILD_ASSERT_OR_ZERO(1 == 1) == 0); 8 | return exit_status(); 9 | } 10 | -------------------------------------------------------------------------------- /ccan/ccan/cast/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/LGPL-2.1 -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_const.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Note: this *isn't* sizeof(char) on all platforms. */ 5 | struct char_struct { 6 | char c; 7 | }; 8 | 9 | int main(void) 10 | { 11 | char *uc; 12 | const 13 | #ifdef FAIL 14 | struct char_struct 15 | #else 16 | char 17 | #endif 18 | *p = NULL; 19 | 20 | uc = cast_const(char *, p); 21 | (void) uc; /* Suppress unused-but-set-variable warning. */ 22 | return 0; 23 | } 24 | 25 | #ifdef FAIL 26 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 27 | #error "Unfortunately we don't fail if cast_const can only use size" 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_const2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Note: this *isn't* sizeof(char) on all platforms. */ 5 | struct char_struct { 6 | char c; 7 | }; 8 | 9 | int main(void) 10 | { 11 | char **uc; 12 | const 13 | #ifdef FAIL 14 | struct char_struct 15 | #else 16 | char 17 | #endif 18 | **p = NULL; 19 | 20 | uc = cast_const2(char **, p); 21 | (void) uc; /* Suppress unused-but-set-variable warning. */ 22 | return 0; 23 | } 24 | 25 | #ifdef FAIL 26 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 27 | #error "Unfortunately we don't fail if cast_const can only use size" 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_const3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Note: this *isn't* sizeof(char) on all platforms. */ 5 | struct char_struct { 6 | char c; 7 | }; 8 | 9 | int main(void) 10 | { 11 | char ***uc; 12 | const 13 | #ifdef FAIL 14 | struct char_struct 15 | #else 16 | char 17 | #endif 18 | ***p = NULL; 19 | 20 | uc = cast_const3(char ***, p); 21 | (void) uc; /* Suppress unused-but-set-variable warning. */ 22 | return 0; 23 | } 24 | 25 | #ifdef FAIL 26 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 27 | #error "Unfortunately we don't fail if cast_const can only use size" 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_signed-const.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | unsigned char *uc; 7 | #ifdef FAIL 8 | const 9 | #endif 10 | char 11 | *p = NULL; 12 | 13 | uc = cast_signed(unsigned char *, p); 14 | (void) uc; /* Suppress unused-but-set-variable warning. */ 15 | return 0; 16 | } 17 | 18 | #ifdef FAIL 19 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 20 | #error "Unfortunately we don't fail if cast_const can only use size" 21 | #endif 22 | #endif 23 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_signed-sizesame.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Note: this *isn't* sizeof(char) on all platforms. */ 5 | struct char_struct { 6 | char c; 7 | }; 8 | 9 | int main(void) 10 | { 11 | unsigned char *uc; 12 | #ifdef FAIL 13 | struct char_struct 14 | #else 15 | char 16 | #endif 17 | *p = NULL; 18 | 19 | uc = cast_signed(unsigned char *, p); 20 | 21 | (void) uc; /* Suppress unused-but-set-variable warning. */ 22 | return 0; 23 | } 24 | 25 | #ifdef FAIL 26 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 27 | #error "Unfortunately we don't fail if cast_signed can only use size" 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_signed.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | unsigned char *uc; 7 | #ifdef FAIL 8 | int 9 | #else 10 | char 11 | #endif 12 | *p = NULL; 13 | 14 | uc = cast_signed(unsigned char *, p); 15 | (void) uc; /* Suppress unused-but-set-variable warning. */ 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_static-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | char *c; 7 | #ifdef FAIL 8 | long 9 | #else 10 | char 11 | #endif 12 | *p = 0; 13 | 14 | c = cast_static(char *, p); 15 | (void) c; /* Suppress unused-but-set-variable warning. */ 16 | return 0; 17 | } 18 | 19 | #ifdef FAIL 20 | #if !HAVE_COMPOUND_LITERALS 21 | #error "Unfortunately we don't fail if cast_static is a noop" 22 | #endif 23 | #endif 24 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_static-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | char *c; 7 | #ifdef FAIL 8 | const 9 | #endif 10 | char *p = 0; 11 | 12 | c = cast_static(char *, p); 13 | (void) c; /* Suppress unused-but-set-variable warning. */ 14 | return 0; 15 | } 16 | 17 | #ifdef FAIL 18 | #if !HAVE_COMPOUND_LITERALS 19 | #error "Unfortunately we don't fail if cast_static is a noop" 20 | #endif 21 | #endif 22 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_fail-cast_static.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | long c; 7 | #ifdef FAIL 8 | char * 9 | #else 10 | char 11 | #endif 12 | x = 0; 13 | 14 | c = cast_static(long, x); 15 | (void) c; /* Suppress unused-but-set-variable warning. */ 16 | return 0; 17 | } 18 | 19 | #ifdef FAIL 20 | #if !HAVE_COMPOUND_LITERALS 21 | #error "Unfortunately we don't fail if cast_static without compound literals" 22 | #endif 23 | #endif 24 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_ok-cast_void.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static void *remove_void(const void *p) 4 | { 5 | return cast_const(void *, p); 6 | } 7 | 8 | int main(void) 9 | { 10 | void *p = remove_void("foo"); 11 | return !p; 12 | } 13 | -------------------------------------------------------------------------------- /ccan/ccan/cast/test/compile_ok-static.c: -------------------------------------------------------------------------------- 1 | /* OpenIndiana's CC (aka suncc) has issues with constants: make sure 2 | * we are one! */ 3 | #include 4 | 5 | static char *p = cast_const(char *, (const char *)"hello"); 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | (void)argc; 10 | return p[0] == argv[0][0]; 11 | } 12 | -------------------------------------------------------------------------------- /ccan/ccan/check_type/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/check_type/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * check_type - routines for compile time type checking 7 | * 8 | * C has fairly weak typing: ints get automatically converted to longs, signed 9 | * to unsigned, etc. There are some cases where this is best avoided, and 10 | * these macros provide methods for evoking warnings (or build errors) when 11 | * a precise type isn't used. 12 | * 13 | * On compilers which don't support typeof() these routines are less effective, 14 | * since they have to use sizeof() which can only distiguish between types of 15 | * different size. 16 | * 17 | * License: CC0 (Public domain) 18 | * Author: Rusty Russell 19 | */ 20 | int main(int argc, char *argv[]) 21 | { 22 | if (argc != 2) 23 | return 1; 24 | 25 | if (strcmp(argv[1], "depends") == 0) { 26 | #if !HAVE_TYPEOF 27 | printf("ccan/build_assert\n"); 28 | #endif 29 | return 0; 30 | } 31 | 32 | return 1; 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/check_type/test/compile_fail-check_type.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | (void)argc; 6 | (void)argv; 7 | #ifdef FAIL 8 | check_type(argc, char); 9 | #endif 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /ccan/ccan/check_type/test/compile_fail-check_type_unsigned.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | (void)argc; 6 | (void)argv; 7 | #ifdef FAIL 8 | #if HAVE_TYPEOF 9 | check_type(argc, unsigned int); 10 | #else 11 | /* This doesn't work without typeof, so just fail */ 12 | #error "Fail without typeof" 13 | #endif 14 | #endif 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /ccan/ccan/check_type/test/compile_fail-check_types_match.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) 4 | { 5 | unsigned char x = argc; 6 | (void)argv; 7 | #ifdef FAIL 8 | check_types_match(argc, x); 9 | #endif 10 | return x; 11 | } 12 | -------------------------------------------------------------------------------- /ccan/ccan/check_type/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | int x = 0, y = 0; 7 | 8 | (void)argv; 9 | plan_tests(9); 10 | 11 | ok1(check_type(argc, int) == 0); 12 | ok1(check_type(&argc, int *) == 0); 13 | ok1(check_types_match(argc, argc) == 0); 14 | ok1(check_types_match(argc, x) == 0); 15 | ok1(check_types_match(&argc, &x) == 0); 16 | 17 | ok1(check_type(x++, int) == 0); 18 | ok(x == 0, "check_type does not evaluate expression"); 19 | ok1(check_types_match(x++, y++) == 0); 20 | ok(x == 0 && y == 0, "check_types_match does not evaluate expressions"); 21 | 22 | return exit_status(); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/compiler/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/compiler/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * compiler - macros for common compiler extensions 7 | * 8 | * Abstracts away some compiler hints. Currently these include: 9 | * - COLD 10 | * For functions not called in fast paths (aka. cold functions) 11 | * - PRINTF_FMT 12 | * For functions which take printf-style parameters. 13 | * - CONST_FUNCTION 14 | * For functions which return the same value for same parameters. 15 | * - NEEDED 16 | * For functions and variables which must be emitted even if unused. 17 | * - UNNEEDED 18 | * For functions and variables which need not be emitted if unused. 19 | * - UNUSED 20 | * For parameters which are not used. 21 | * - IS_COMPILE_CONSTANT() 22 | * For using different tradeoffs for compiletime vs runtime evaluation. 23 | * 24 | * License: CC0 (Public domain) 25 | * Author: Rusty Russell 26 | * 27 | * Example: 28 | * #include 29 | * #include 30 | * #include 31 | * 32 | * // Example of a (slow-path) logging function. 33 | * static int log_threshold = 2; 34 | * static void COLD PRINTF_FMT(2,3) 35 | * logger(int level, const char *fmt, ...) 36 | * { 37 | * va_list ap; 38 | * va_start(ap, fmt); 39 | * if (level >= log_threshold) 40 | * vfprintf(stderr, fmt, ap); 41 | * va_end(ap); 42 | * } 43 | * 44 | * int main(int argc, char *argv[] UNNEEDED) 45 | * { 46 | * if (argc != 1) { 47 | * logger(3, "Don't want %i arguments!\n", argc-1); 48 | * return 1; 49 | * } 50 | * return 0; 51 | * } 52 | */ 53 | int main(int argc, char *argv[]) 54 | { 55 | /* Expect exactly one argument */ 56 | if (argc != 2) 57 | return 1; 58 | 59 | if (strcmp(argv[1], "depends") == 0) { 60 | return 0; 61 | } 62 | 63 | return 1; 64 | } 65 | -------------------------------------------------------------------------------- /ccan/ccan/compiler/test/compile_fail-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static void PRINTF_FMT(2,3) my_printf(int x, const char *fmt, ...) 4 | { 5 | (void)x; 6 | (void)fmt; 7 | } 8 | 9 | int main(void) 10 | { 11 | unsigned int i = 0; 12 | 13 | my_printf(1, "Not a pointer " 14 | #ifdef FAIL 15 | "%p", 16 | #if !HAVE_ATTRIBUTE_PRINTF 17 | #error "Unfortunately we don't fail if !HAVE_ATTRIBUTE_PRINTF." 18 | #endif 19 | #else 20 | "%i", 21 | #endif 22 | i); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ccan/ccan/compiler/test/run-is_compile_constant.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | (void)argv; 8 | plan_tests(2); 9 | 10 | ok1(!IS_COMPILE_CONSTANT(argc)); 11 | #if HAVE_BUILTIN_CONSTANT_P 12 | ok1(IS_COMPILE_CONSTANT(7)); 13 | #else 14 | pass("If !HAVE_BUILTIN_CONSTANT_P, IS_COMPILE_CONSTANT always false"); 15 | #endif 16 | return exit_status(); 17 | } 18 | -------------------------------------------------------------------------------- /ccan/ccan/container_of/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/container_of/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * container_of - routine for upcasting 7 | * 8 | * It is often convenient to create code where the caller registers a pointer 9 | * to a generic structure and a callback. The callback might know that the 10 | * pointer points to within a larger structure, and container_of gives a 11 | * convenient and fairly type-safe way of returning to the enclosing structure. 12 | * 13 | * This idiom is an alternative to providing a void * pointer for every 14 | * callback. 15 | * 16 | * Example: 17 | * #include 18 | * #include 19 | * 20 | * struct timer { 21 | * void *members; 22 | * }; 23 | * 24 | * struct info { 25 | * int my_stuff; 26 | * struct timer timer; 27 | * }; 28 | * 29 | * static void my_timer_callback(struct timer *timer) 30 | * { 31 | * struct info *info = container_of(timer, struct info, timer); 32 | * printf("my_stuff is %u\n", info->my_stuff); 33 | * } 34 | * 35 | * static void register_timer(struct timer *timer) 36 | * { 37 | * (void)timer; 38 | * (void)my_timer_callback; 39 | * //... 40 | * } 41 | * 42 | * int main(void) 43 | * { 44 | * struct info info = { .my_stuff = 1 }; 45 | * 46 | * register_timer(&info.timer); 47 | * // ... 48 | * return 0; 49 | * } 50 | * 51 | * License: CC0 (Public domain) 52 | * Author: Rusty Russell 53 | */ 54 | int main(int argc, char *argv[]) 55 | { 56 | if (argc != 2) 57 | return 1; 58 | 59 | if (strcmp(argv[1], "depends") == 0) { 60 | printf("ccan/check_type\n"); 61 | return 0; 62 | } 63 | 64 | return 1; 65 | } 66 | -------------------------------------------------------------------------------- /ccan/ccan/container_of/test/compile_fail-bad-type.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int a; 6 | char b; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct foo foo = { .a = 1, .b = 2 }; 12 | int *intp = &foo.a; 13 | char *p; 14 | 15 | #ifdef FAIL 16 | /* p is a char *, but this gives a struct foo * */ 17 | p = container_of(intp, struct foo, a); 18 | #else 19 | p = (char *)intp; 20 | #endif 21 | return p == NULL; 22 | } 23 | -------------------------------------------------------------------------------- /ccan/ccan/container_of/test/compile_fail-types.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int a; 6 | char b; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct foo foo = { .a = 1, .b = 2 }, *foop; 12 | int *intp = &foo.a; 13 | 14 | #ifdef FAIL 15 | /* b is a char, but intp is an int * */ 16 | foop = container_of(intp, struct foo, b); 17 | #else 18 | foop = NULL; 19 | #endif 20 | (void) foop; /* Suppress unused-but-set-variable warning. */ 21 | return intp == NULL; 22 | } 23 | -------------------------------------------------------------------------------- /ccan/ccan/container_of/test/compile_fail-var-types.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int a; 6 | char b; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct foo foo = { .a = 1, .b = 2 }, *foop; 12 | int *intp = &foo.a; 13 | 14 | #ifdef FAIL 15 | /* b is a char, but intp is an int * */ 16 | foop = container_of_var(intp, foop, b); 17 | #if !HAVE_TYPEOF 18 | #error "Unfortunately we don't fail if we don't have typeof." 19 | #endif 20 | #else 21 | foop = NULL; 22 | #endif 23 | (void) foop; /* Suppress unused-but-set-variable warning. */ 24 | return intp == NULL; 25 | } 26 | -------------------------------------------------------------------------------- /ccan/ccan/container_of/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int a; 6 | char b; 7 | }; 8 | 9 | int main(void) 10 | { 11 | struct foo foo = { .a = 1, .b = 2 }; 12 | int *intp = &foo.a; 13 | char *charp = &foo.b; 14 | 15 | plan_tests(12); 16 | ok1(container_of(intp, struct foo, a) == &foo); 17 | ok1(container_of(charp, struct foo, b) == &foo); 18 | ok1(container_of_or_null(intp, struct foo, a) == &foo); 19 | ok1(container_of_or_null(charp, struct foo, b) == &foo); 20 | ok1(container_of_or_null((int *)NULL, struct foo, a) == NULL); 21 | ok1(container_of_or_null((char *)NULL, struct foo, b) == NULL); 22 | ok1(container_of_var(intp, &foo, a) == &foo); 23 | ok1(container_of_var(charp, &foo, b) == &foo); 24 | 25 | ok1(container_off(struct foo, a) == 0); 26 | ok1(container_off(struct foo, b) == offsetof(struct foo, b)); 27 | ok1(container_off_var(&foo, a) == 0); 28 | ok1(container_off_var(&foo, b) == offsetof(struct foo, b)); 29 | return exit_status(); 30 | } 31 | -------------------------------------------------------------------------------- /ccan/ccan/endian/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/endian/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * endian - endian conversion macros for simple types 7 | * 8 | * Portable protocols (such as on-disk formats, or network protocols) 9 | * are often defined to be a particular endian: little-endian (least 10 | * significant bytes first) or big-endian (most significant bytes 11 | * first). 12 | * 13 | * Similarly, some CPUs lay out values in memory in little-endian 14 | * order (most commonly, Intel's 8086 and derivatives), or big-endian 15 | * order (almost everyone else). 16 | * 17 | * This module provides conversion routines, inspired by the linux kernel. 18 | * It also provides leint32_t, beint32_t etc typedefs, which are annotated for 19 | * the sparse checker. 20 | * 21 | * Example: 22 | * #include 23 | * #include 24 | * #include 25 | * 26 | * // 27 | * int main(int argc, char *argv[]) 28 | * { 29 | * uint32_t value; 30 | * 31 | * if (argc != 2) 32 | * errx(1, "Usage: %s ", argv[0]); 33 | * 34 | * value = atoi(argv[1]); 35 | * printf("native: %08x\n", value); 36 | * printf("little-endian: %08x\n", cpu_to_le32(value)); 37 | * printf("big-endian: %08x\n", cpu_to_be32(value)); 38 | * printf("byte-reversed: %08x\n", bswap_32(value)); 39 | * exit(0); 40 | * } 41 | * 42 | * License: License: CC0 (Public domain) 43 | * Author: Rusty Russell 44 | */ 45 | int main(int argc, char *argv[]) 46 | { 47 | if (argc != 2) 48 | return 1; 49 | 50 | if (strcmp(argv[1], "depends") == 0) 51 | /* Nothing */ 52 | return 0; 53 | 54 | return 1; 55 | } 56 | -------------------------------------------------------------------------------- /ccan/ccan/endian/test/compile_ok-constant.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct foo { 4 | char one[BSWAP_16(0xFF00)]; 5 | char two[BSWAP_32(0xFF000000)]; 6 | char three[BSWAP_64(0xFF00000000000000ULL)]; 7 | }; 8 | 9 | int main(void) 10 | { 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /ccan/ccan/err/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/err/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * err - err(), errx(), warn() and warnx(), as per BSD's err.h. 7 | * 8 | * A few platforms don't provide err.h; for those, this provides replacements. 9 | * For most, it simple includes the system err.h. 10 | * 11 | * Unfortunately, you have to call err_set_progname() to tell the replacements 12 | * your program name, otherwise it prints "unknown program". 13 | * 14 | * Example: 15 | * #include 16 | * 17 | * int main(int argc, char *argv[]) 18 | * { 19 | * err_set_progname(argv[0]); 20 | * if (argc != 1) 21 | * errx(1, "Expect no arguments"); 22 | * exit(0); 23 | * } 24 | * 25 | * License: CC0 (Public domain) 26 | * Author: Rusty Russell 27 | */ 28 | int main(int argc, char *argv[]) 29 | { 30 | if (argc != 2) 31 | return 1; 32 | 33 | if (strcmp(argv[1], "depends") == 0) { 34 | #if !HAVE_ERR_H 35 | printf("ccan/compiler\n"); 36 | #endif 37 | return 0; 38 | } 39 | 40 | return 1; 41 | } 42 | -------------------------------------------------------------------------------- /ccan/ccan/err/err.c: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #include "err.h" 3 | 4 | #if !HAVE_ERR_H 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static const char *progname = "unknown program"; 12 | 13 | void err_set_progname(const char *name) 14 | { 15 | progname = name; 16 | } 17 | 18 | void NORETURN err(int eval, const char *fmt, ...) 19 | { 20 | int err_errno = errno; 21 | va_list ap; 22 | 23 | fprintf(stderr, "%s: ", progname); 24 | va_start(ap, fmt); 25 | vfprintf(stderr, fmt, ap); 26 | va_end(ap); 27 | fprintf(stderr, ": %s\n", strerror(err_errno)); 28 | exit(eval); 29 | } 30 | 31 | void NORETURN errx(int eval, const char *fmt, ...) 32 | { 33 | va_list ap; 34 | 35 | fprintf(stderr, "%s: ", progname); 36 | va_start(ap, fmt); 37 | vfprintf(stderr, fmt, ap); 38 | va_end(ap); 39 | fprintf(stderr, "\n"); 40 | exit(eval); 41 | } 42 | 43 | void warn(const char *fmt, ...) 44 | { 45 | int err_errno = errno; 46 | va_list ap; 47 | 48 | fprintf(stderr, "%s: ", progname); 49 | va_start(ap, fmt); 50 | vfprintf(stderr, fmt, ap); 51 | va_end(ap); 52 | fprintf(stderr, ": %s\n", strerror(err_errno)); 53 | } 54 | 55 | void warnx(const char *fmt, ...) 56 | { 57 | va_list ap; 58 | 59 | fprintf(stderr, "%s: ", progname); 60 | va_start(ap, fmt); 61 | vfprintf(stderr, fmt, ap); 62 | va_end(ap); 63 | fprintf(stderr, "\n"); 64 | } 65 | #endif 66 | -------------------------------------------------------------------------------- /ccan/ccan/hash/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/hash/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * hash - routines for hashing bytes 7 | * 8 | * When creating a hash table it's important to have a hash function 9 | * which mixes well and is fast. This package supplies such functions. 10 | * 11 | * The hash functions come in two flavors: the normal ones and the 12 | * stable ones. The normal ones can vary from machine-to-machine and 13 | * may change if we find better or faster hash algorithms in future. 14 | * The stable ones will always give the same results on any computer, 15 | * and on any version of this package. 16 | * 17 | * License: CC0 (Public domain) 18 | * Maintainer: Rusty Russell 19 | * Author: Bob Jenkins 20 | */ 21 | int main(int argc, char *argv[]) 22 | { 23 | if (argc != 2) 24 | return 1; 25 | 26 | if (strcmp(argv[1], "depends") == 0) { 27 | printf("ccan/build_assert\n"); 28 | return 0; 29 | } 30 | 31 | return 1; 32 | } 33 | -------------------------------------------------------------------------------- /ccan/ccan/htable/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/LGPL-2.1 -------------------------------------------------------------------------------- /ccan/ccan/htable/test/run-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define NUM_VALS 512 8 | 9 | /* We use the number divided by two as the hash (for lots of 10 | collisions). */ 11 | static size_t hash(const void *elem, void *unused UNNEEDED) 12 | { 13 | size_t h = *(uint64_t *)elem / 2; 14 | return h; 15 | } 16 | 17 | int main(void) 18 | { 19 | struct htable ht; 20 | uint64_t val[NUM_VALS]; 21 | unsigned int i; 22 | 23 | plan_tests((NUM_VALS) * 2); 24 | for (i = 0; i < NUM_VALS; i++) 25 | val[i] = i; 26 | 27 | htable_init(&ht, hash, NULL); 28 | for (i = 0; i < NUM_VALS; i++) { 29 | ok1(ht_max(&ht) >= i); 30 | ok1(ht_max(&ht) <= i * 2); 31 | htable_add(&ht, hash(&val[i], NULL), &val[i]); 32 | } 33 | htable_clear(&ht); 34 | 35 | return exit_status(); 36 | } 37 | -------------------------------------------------------------------------------- /ccan/ccan/htable/test/run-zero-hash-first-entry.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct data { 7 | size_t key; 8 | }; 9 | 10 | /* Hash is simply key itself. */ 11 | static size_t hash(const void *e, void *unused UNNEEDED) 12 | { 13 | struct data *d = (struct data *)e; 14 | 15 | return d->key; 16 | } 17 | 18 | static bool eq(const void *e, void *k) 19 | { 20 | struct data *d = (struct data *)e; 21 | size_t *key = (size_t *)k; 22 | 23 | return (d->key == *key); 24 | } 25 | 26 | int main(void) 27 | { 28 | struct htable table; 29 | struct data *d0, *d1; 30 | 31 | plan_tests(6); 32 | 33 | d1 = malloc(sizeof(struct data)); 34 | d1->key = 1; 35 | d0 = malloc(sizeof(struct data)); 36 | d0->key = 0; 37 | 38 | htable_init(&table, hash, NULL); 39 | 40 | htable_add(&table, d0->key, d0); 41 | htable_add(&table, d1->key, d1); 42 | 43 | ok1(table.elems == 2); 44 | ok1(htable_get(&table, 1, eq, &d1->key) == d1); 45 | ok1(htable_get(&table, 0, eq, &d0->key) == d0); 46 | htable_clear(&table); 47 | 48 | /* Now add in reverse order, should still be OK. */ 49 | htable_add(&table, d1->key, d1); 50 | htable_add(&table, d0->key, d0); 51 | 52 | ok1(table.elems == 2); 53 | ok1(htable_get(&table, 1, eq, &d1->key) == d1); 54 | ok1(htable_get(&table, 0, eq, &d0->key) == d0); 55 | htable_clear(&table); 56 | 57 | free(d0); 58 | free(d1); 59 | return exit_status(); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /ccan/ccan/htable/tools/Makefile: -------------------------------------------------------------------------------- 1 | CCANDIR=../../.. 2 | CFLAGS=-Wall -Werror -O3 -I$(CCANDIR) 3 | #CFLAGS=-Wall -Werror -g -I$(CCANDIR) 4 | 5 | CCAN_OBJS:=ccan-tal.o ccan-tal-str.o ccan-tal-grab_file.o ccan-take.o ccan-time.o ccan-str.o ccan-noerr.o ccan-list.o 6 | 7 | all: speed stringspeed hsearchspeed 8 | 9 | speed: speed.o hash.o $(CCAN_OBJS) 10 | 11 | speed.o: speed.c ../htable.h ../htable.c 12 | 13 | hash.o: ../../hash/hash.c 14 | $(CC) $(CFLAGS) -c -o $@ $< 15 | 16 | stringspeed: stringspeed.o hash.o $(CCAN_OBJS) 17 | 18 | stringspeed.o: speed.c ../htable.h ../htable.c 19 | 20 | hsearchspeed: hsearchspeed.o $(CCAN_OBJS) 21 | 22 | clean: 23 | rm -f stringspeed speed hsearchspeed *.o 24 | 25 | ccan-tal.o: $(CCANDIR)/ccan/tal/tal.c 26 | $(CC) $(CFLAGS) -c -o $@ $< 27 | ccan-tal-str.o: $(CCANDIR)/ccan/tal/str/str.c 28 | $(CC) $(CFLAGS) -c -o $@ $< 29 | ccan-take.o: $(CCANDIR)/ccan/take/take.c 30 | $(CC) $(CFLAGS) -c -o $@ $< 31 | ccan-tal-grab_file.o: $(CCANDIR)/ccan/tal/grab_file/grab_file.c 32 | $(CC) $(CFLAGS) -c -o $@ $< 33 | ccan-time.o: $(CCANDIR)/ccan/time/time.c 34 | $(CC) $(CFLAGS) -c -o $@ $< 35 | ccan-list.o: $(CCANDIR)/ccan/list/list.c 36 | $(CC) $(CFLAGS) -c -o $@ $< 37 | ccan-str.o: $(CCANDIR)/ccan/str/str.c 38 | $(CC) $(CFLAGS) -c -o $@ $< 39 | ccan-noerr.o: $(CCANDIR)/ccan/noerr/noerr.c 40 | $(CC) $(CFLAGS) -c -o $@ $< 41 | -------------------------------------------------------------------------------- /ccan/ccan/likely/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/likely/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * likely - macros for annotating likely/unlikely branches in the code 7 | * 8 | * Inspired by Andi Kleen's macros for the Linux Kernel, these macros 9 | * help you annotate rare paths in your code for the convenience of the 10 | * compiler and the reader. 11 | * 12 | * With CCAN_LIKELY_DEBUG defined, it provides statistics for each 13 | * likely()/unlikely() call (but note that this requires LGPL dependencies). 14 | * 15 | * License: CC0 (Public domain) 16 | * Author: Rusty Russell 17 | * 18 | * Example: 19 | * #include 20 | * #include 21 | * 22 | * int main(int argc, char *argv[]) 23 | * { 24 | * // This example is silly: the compiler knows exit() is unlikely. 25 | * if (unlikely(argc == 1)) { 26 | * fprintf(stderr, "Usage: %s ...\n", argv[0]); 27 | * return 1; 28 | * } 29 | * for (argc++; argv[argc]; argc++) 30 | * printf("%s\n", argv[argc]); 31 | * return 0; 32 | * } 33 | */ 34 | int main(int argc, char *argv[]) 35 | { 36 | /* Expect exactly one argument */ 37 | if (argc != 2) 38 | return 1; 39 | 40 | if (strcmp(argv[1], "depends") == 0) { 41 | #ifdef CCAN_LIKELY_DEBUG 42 | printf("ccan/str\n"); 43 | printf("ccan/htable\n"); 44 | printf("ccan/hash\n"); 45 | #endif 46 | return 0; 47 | } 48 | if (strcmp(argv[1], "testdepends") == 0) { 49 | #ifndef CCAN_LIKELY_DEBUG 50 | printf("ccan/str\n"); 51 | printf("ccan/htable\n"); 52 | printf("ccan/hash\n"); 53 | #endif 54 | return 0; 55 | } 56 | return 1; 57 | } 58 | -------------------------------------------------------------------------------- /ccan/ccan/likely/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static bool one_seems_likely(unsigned int val) 7 | { 8 | if (likely(val == 1)) 9 | return true; 10 | return false; 11 | } 12 | 13 | static bool one_seems_unlikely(unsigned int val) 14 | { 15 | if (unlikely(val == 1)) 16 | return true; 17 | return false; 18 | } 19 | 20 | int main(void) 21 | { 22 | plan_tests(4); 23 | 24 | /* Without debug, we can only check that it doesn't effect functions. */ 25 | ok1(one_seems_likely(1)); 26 | ok1(!one_seems_likely(2)); 27 | ok1(one_seems_unlikely(1)); 28 | ok1(!one_seems_unlikely(2)); 29 | exit(exit_status()); 30 | } 31 | -------------------------------------------------------------------------------- /ccan/ccan/list/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/list/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * list - double linked list routines 7 | * 8 | * The list header contains routines for manipulating double linked lists. 9 | * It defines two types: struct list_head used for anchoring lists, and 10 | * struct list_node which is usually embedded in the structure which is placed 11 | * in the list. 12 | * 13 | * Example: 14 | * #include 15 | * #include 16 | * #include 17 | * #include 18 | * 19 | * struct parent { 20 | * const char *name; 21 | * struct list_head children; 22 | * unsigned int num_children; 23 | * }; 24 | * 25 | * struct child { 26 | * const char *name; 27 | * struct list_node list; 28 | * }; 29 | * 30 | * int main(int argc, char *argv[]) 31 | * { 32 | * struct parent p; 33 | * struct child *c; 34 | * int i; 35 | * 36 | * if (argc < 2) 37 | * errx(1, "Usage: %s parent children...", argv[0]); 38 | * 39 | * p.name = argv[1]; 40 | * list_head_init(&p.children); 41 | * p.num_children = 0; 42 | * for (i = 2; i < argc; i++) { 43 | * c = malloc(sizeof(*c)); 44 | * c->name = argv[i]; 45 | * list_add(&p.children, &c->list); 46 | * p.num_children++; 47 | * } 48 | * 49 | * printf("%s has %u children:", p.name, p.num_children); 50 | * list_for_each(&p.children, c, list) 51 | * printf("%s ", c->name); 52 | * printf("\n"); 53 | * return 0; 54 | * } 55 | * 56 | * License: BSD-MIT 57 | * Author: Rusty Russell 58 | */ 59 | int main(int argc, char *argv[]) 60 | { 61 | if (argc != 2) 62 | return 1; 63 | 64 | if (strcmp(argv[1], "depends") == 0) { 65 | printf("ccan/str\n"); 66 | printf("ccan/container_of\n"); 67 | printf("ccan/check_type\n"); 68 | return 0; 69 | } 70 | 71 | return 1; 72 | } 73 | -------------------------------------------------------------------------------- /ccan/ccan/list/list.c: -------------------------------------------------------------------------------- 1 | /* Licensed under BSD-MIT - see LICENSE file for details */ 2 | #include 3 | #include 4 | #include "list.h" 5 | 6 | static void *corrupt(const char *abortstr, 7 | const struct list_node *head, 8 | const struct list_node *node, 9 | unsigned int count) 10 | { 11 | if (abortstr) { 12 | fprintf(stderr, 13 | "%s: prev corrupt in node %p (%u) of %p\n", 14 | abortstr, node, count, head); 15 | abort(); 16 | } 17 | return NULL; 18 | } 19 | 20 | struct list_node *list_check_node(const struct list_node *node, 21 | const char *abortstr) 22 | { 23 | const struct list_node *p, *n; 24 | int count = 0; 25 | 26 | for (p = node, n = node->next; n != node; p = n, n = n->next) { 27 | count++; 28 | if (n->prev != p) 29 | return corrupt(abortstr, node, n, count); 30 | } 31 | /* Check prev on head node. */ 32 | if (node->prev != p) 33 | return corrupt(abortstr, node, node, 0); 34 | 35 | return (struct list_node *)node; 36 | } 37 | 38 | struct list_head *list_check(const struct list_head *h, const char *abortstr) 39 | { 40 | if (!list_check_node(&h->n, abortstr)) 41 | return NULL; 42 | return (struct list_head *)h; 43 | } 44 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/compile_ok-constant.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct child { 8 | const char *name; 9 | struct list_node list; 10 | }; 11 | 12 | static bool children(const struct list_head *list) 13 | { 14 | return !list_empty(list); 15 | } 16 | 17 | static const struct child *first_child(const struct list_head *list) 18 | { 19 | return list_top(list, struct child, list); 20 | } 21 | 22 | static const struct child *last_child(const struct list_head *list) 23 | { 24 | return list_tail(list, struct child, list); 25 | } 26 | 27 | static void check_children(const struct list_head *list) 28 | { 29 | list_check(list, "bad child list"); 30 | } 31 | 32 | static void print_children(const struct list_head *list) 33 | { 34 | const struct child *c; 35 | list_for_each(list, c, list) 36 | printf("%s\n", c->name); 37 | } 38 | 39 | int main(void) 40 | { 41 | LIST_HEAD(h); 42 | 43 | children(&h); 44 | first_child(&h); 45 | last_child(&h); 46 | check_children(&h); 47 | print_children(&h); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/helper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include "helper.h" 7 | 8 | #define ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING \ 9 | (42) 10 | 11 | struct opaque { 12 | struct list_node list; 13 | size_t secret_offset; 14 | char secret_drawer[42]; 15 | }; 16 | 17 | static bool not_randomized = true; 18 | 19 | struct opaque *create_opaque_blob(void) 20 | { 21 | struct opaque *blob = calloc(1, sizeof(struct opaque)); 22 | 23 | if (not_randomized) { 24 | srandom((int)time(NULL)); 25 | not_randomized = false; 26 | } 27 | 28 | blob->secret_offset = random() % (sizeof(blob->secret_drawer)); 29 | blob->secret_drawer[blob->secret_offset] = 30 | ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING; 31 | 32 | return blob; 33 | } 34 | 35 | bool if_blobs_know_the_secret(struct opaque *blob) 36 | { 37 | bool answer = true; 38 | int i; 39 | for (i = 0; i < sizeof(blob->secret_drawer) / 40 | sizeof(blob->secret_drawer[0]); i++) 41 | if (i != blob->secret_offset) 42 | answer = answer && (blob->secret_drawer[i] == 0); 43 | else 44 | answer = answer && 45 | (blob->secret_drawer[blob->secret_offset] == 46 | ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING); 47 | 48 | return answer; 49 | } 50 | 51 | void destroy_opaque_blob(struct opaque *blob) 52 | { 53 | free(blob); 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/helper.h: -------------------------------------------------------------------------------- 1 | /* These are in a separate C file so we can test undefined structures. */ 2 | struct opaque; 3 | typedef struct opaque opaque_t; 4 | 5 | opaque_t *create_opaque_blob(void); 6 | bool if_blobs_know_the_secret(opaque_t *blob); 7 | void destroy_opaque_blob(opaque_t *blob); 8 | 9 | 10 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/run-CCAN_LIST_DEBUG.c: -------------------------------------------------------------------------------- 1 | /* Check that CCAN_LIST_DEBUG works */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /* We don't actually want it to exit... */ 10 | static jmp_buf aborted; 11 | #define abort() longjmp(aborted, 1) 12 | 13 | #define fprintf my_fprintf 14 | static char printf_buffer[1000]; 15 | 16 | static int my_fprintf(FILE *stream, const char *format, ...) 17 | { 18 | va_list ap; 19 | int ret; 20 | (void)stream; 21 | va_start(ap, format); 22 | ret = vsprintf(printf_buffer, format, ap); 23 | va_end(ap); 24 | return ret; 25 | } 26 | 27 | #define CCAN_LIST_DEBUG 1 28 | #include 29 | #include 30 | #include 31 | 32 | int main(void) 33 | { 34 | struct list_head list; 35 | struct list_node n1; 36 | char expect[100]; 37 | 38 | plan_tests(2); 39 | /* Empty list. */ 40 | list.n.next = &list.n; 41 | list.n.prev = &list.n; 42 | ok1(list_check(&list, NULL) == &list); 43 | 44 | /* Bad back ptr */ 45 | list.n.prev = &n1; 46 | 47 | /* Aborting version. */ 48 | sprintf(expect, "run-CCAN_LIST_DEBUG.c:51: prev corrupt in node %p (0) of %p\n", 49 | &list, &list); 50 | if (setjmp(aborted) == 0) { 51 | assert(list_empty(&list)); 52 | fail("list_empty on empty with bad back ptr didn't fail!"); 53 | } else { 54 | /* __FILE__ might give full path. */ 55 | int prep = strlen(printf_buffer) - strlen(expect); 56 | ok1(prep >= 0 && strcmp(printf_buffer + prep, expect) == 0); 57 | } 58 | 59 | return exit_status(); 60 | } 61 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/run-check-nonconst.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "helper.h" 5 | 6 | struct child { 7 | const char *name; 8 | struct list_node list; 9 | }; 10 | 11 | int main(void) 12 | { 13 | struct child c1, c2; 14 | struct list_head list = LIST_HEAD_INIT(list); 15 | 16 | plan_tests(1); 17 | 18 | list_add(&list, &c1.list); 19 | list_add_tail(list_check(&list, "Bad list!"), &c2.list); 20 | list_del_from(list_check(&list, "Bad list!"), 21 | list_check_node(&c2.list, "Bad node!")); 22 | list_del_from(list_check(&list, "Bad list!"), 23 | list_check_node(&c1.list, "Bad node!")); 24 | ok1(list_empty(list_check(&list, "Bad emptied list"))); 25 | 26 | return exit_status(); 27 | } 28 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/run-list_del_from-assert.c: -------------------------------------------------------------------------------- 1 | #define CCAN_LIST_DEBUG 1 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | struct list_head list1, list2; 13 | struct list_node n1, n2, n3; 14 | pid_t child; 15 | int status; 16 | 17 | plan_tests(1); 18 | list_head_init(&list1); 19 | list_head_init(&list2); 20 | list_add(&list1, &n1); 21 | list_add(&list2, &n2); 22 | list_add_tail(&list2, &n3); 23 | 24 | child = fork(); 25 | if (child) { 26 | wait(&status); 27 | } else { 28 | /* This should abort. */ 29 | list_del_from(&list1, &n3); 30 | exit(0); 31 | } 32 | 33 | ok1(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT); 34 | list_del_from(&list2, &n3); 35 | return exit_status(); 36 | } 37 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/run-list_prev-list_next.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "helper.h" 5 | 6 | struct parent { 7 | const char *name; 8 | unsigned int num_children; 9 | struct list_head children; 10 | }; 11 | 12 | struct child { 13 | const char *name; 14 | struct list_node list; 15 | }; 16 | 17 | int main(void) 18 | { 19 | struct parent parent; 20 | struct child c1, c2, c3; 21 | const struct parent *p; 22 | const struct child *c; 23 | 24 | plan_tests(20); 25 | parent.num_children = 0; 26 | list_head_init(&parent.children); 27 | 28 | c1.name = "c1"; 29 | list_add(&parent.children, &c1.list); 30 | 31 | ok1(list_next(&parent.children, &c1, list) == NULL); 32 | ok1(list_prev(&parent.children, &c1, list) == NULL); 33 | 34 | c2.name = "c2"; 35 | list_add_tail(&parent.children, &c2.list); 36 | 37 | ok1(list_next(&parent.children, &c1, list) == &c2); 38 | ok1(list_prev(&parent.children, &c1, list) == NULL); 39 | ok1(list_next(&parent.children, &c2, list) == NULL); 40 | ok1(list_prev(&parent.children, &c2, list) == &c1); 41 | 42 | c3.name = "c3"; 43 | list_add_tail(&parent.children, &c3.list); 44 | 45 | ok1(list_next(&parent.children, &c1, list) == &c2); 46 | ok1(list_prev(&parent.children, &c1, list) == NULL); 47 | ok1(list_next(&parent.children, &c2, list) == &c3); 48 | ok1(list_prev(&parent.children, &c2, list) == &c1); 49 | ok1(list_next(&parent.children, &c3, list) == NULL); 50 | ok1(list_prev(&parent.children, &c3, list) == &c2); 51 | 52 | /* Const variants */ 53 | p = &parent; 54 | c = &c2; 55 | ok1(list_next(&p->children, &c1, list) == &c2); 56 | ok1(list_prev(&p->children, &c1, list) == NULL); 57 | ok1(list_next(&p->children, c, list) == &c3); 58 | ok1(list_prev(&p->children, c, list) == &c1); 59 | ok1(list_next(&parent.children, c, list) == &c3); 60 | ok1(list_prev(&parent.children, c, list) == &c1); 61 | ok1(list_next(&p->children, &c3, list) == NULL); 62 | ok1(list_prev(&p->children, &c3, list) == &c2); 63 | 64 | return exit_status(); 65 | } 66 | -------------------------------------------------------------------------------- /ccan/ccan/list/test/run-with-debug.c: -------------------------------------------------------------------------------- 1 | /* Just like run.c, but with all debug checks enabled. */ 2 | #define CCAN_LIST_DEBUG 1 3 | #include 4 | -------------------------------------------------------------------------------- /ccan/ccan/noerr/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/noerr/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * noerr - routines for cleaning up without blatting errno 7 | * 8 | * It is a good idea to follow the standard C convention of setting errno in 9 | * your own helper functions. Unfortunately, care must be taken in the error 10 | * paths as most standard functions can (and do) overwrite errno, even if they 11 | * succeed. 12 | * 13 | * Example: 14 | * #include 15 | * #include 16 | * #include 17 | * #include 18 | * #include 19 | * #include 20 | * #include 21 | * 22 | * static bool write_string_to_file(const char *file, const char *string) 23 | * { 24 | * int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600); 25 | * if (fd < 0) 26 | * return false; 27 | * ret = write(fd, string, strlen(string)); 28 | * if (ret < 0) { 29 | * // Preserve errno from write above. 30 | * close_noerr(fd); 31 | * unlink_noerr(file); 32 | * return false; 33 | * } 34 | * if (close(fd) != 0) { 35 | * // Again, preserve errno. 36 | * unlink_noerr(file); 37 | * return false; 38 | * } 39 | * // A short write means out of space. 40 | * if (ret < (int)strlen(string)) { 41 | * unlink(file); 42 | * errno = ENOSPC; 43 | * return false; 44 | * } 45 | * return true; 46 | * } 47 | * 48 | * License: CC0 (Public domain) 49 | * Author: Rusty Russell 50 | */ 51 | int main(int argc, char *argv[]) 52 | { 53 | if (argc != 2) 54 | return 1; 55 | 56 | if (strcmp(argv[1], "depends") == 0) 57 | /* Nothing. */ 58 | return 0; 59 | 60 | return 1; 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/noerr/noerr.c: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #include "noerr.h" 3 | #include 4 | #include 5 | #include 6 | 7 | int close_noerr(int fd) 8 | { 9 | int saved_errno = errno, ret; 10 | 11 | if (close(fd) != 0) 12 | ret = errno; 13 | else 14 | ret = 0; 15 | 16 | errno = saved_errno; 17 | return ret; 18 | } 19 | 20 | int fclose_noerr(FILE *fp) 21 | { 22 | int saved_errno = errno, ret; 23 | 24 | if (fclose(fp) != 0) 25 | ret = errno; 26 | else 27 | ret = 0; 28 | 29 | errno = saved_errno; 30 | return ret; 31 | } 32 | 33 | int unlink_noerr(const char *pathname) 34 | { 35 | int saved_errno = errno, ret; 36 | 37 | if (unlink(pathname) != 0) 38 | ret = errno; 39 | else 40 | ret = 0; 41 | 42 | errno = saved_errno; 43 | return ret; 44 | } 45 | 46 | void free_noerr(void *p) 47 | { 48 | int saved_errno = errno; 49 | free(p); 50 | errno = saved_errno; 51 | } 52 | -------------------------------------------------------------------------------- /ccan/ccan/noerr/noerr.h: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #ifndef NOERR_H 3 | #define NOERR_H 4 | #include 5 | 6 | /** 7 | * close_noerr - close without stomping errno. 8 | * @fd: the file descriptor to close. 9 | * 10 | * errno is saved and restored across the call to close: if an error occurs, 11 | * the resulting (non-zero) errno is returned. 12 | */ 13 | int close_noerr(int fd); 14 | 15 | /** 16 | * fclose_noerr - close without stomping errno. 17 | * @fp: the FILE pointer. 18 | * 19 | * errno is saved and restored across the call to fclose: if an error occurs, 20 | * the resulting (non-zero) errno is returned. 21 | */ 22 | int fclose_noerr(FILE *fp); 23 | 24 | /** 25 | * unlink_noerr - unlink a file without stomping errno. 26 | * @pathname: the path to unlink. 27 | * 28 | * errno is saved and restored across the call to unlink: if an error occurs, 29 | * the resulting (non-zero) errno is returned. 30 | */ 31 | int unlink_noerr(const char *pathname); 32 | 33 | /** 34 | * free_noerr - free memory without stomping errno. 35 | * @p: the pointer to free. 36 | * 37 | * errno is saved and restored across the call to free: the standard leaves 38 | * that undefined. 39 | */ 40 | void free_noerr(void *p); 41 | #endif /* NOERR_H */ 42 | -------------------------------------------------------------------------------- /ccan/ccan/noerr/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | const char *name = "noerr.file"; 13 | int fd; 14 | FILE *fp; 15 | 16 | plan_tests(16); 17 | /* Should fail to unlink. */ 18 | ok1(unlink(name) != 0); 19 | ok1(errno == ENOENT); 20 | 21 | /* This one should not set errno. */ 22 | errno = 100; 23 | ok1(unlink_noerr(name) == ENOENT); 24 | ok1(errno == 100); 25 | 26 | /* Should fail to close. */ 27 | ok1(close(-1) != 0); 28 | ok1(errno == EBADF); 29 | 30 | /* This one should not set errno. */ 31 | errno = 100; 32 | ok1(close_noerr(-1) == EBADF); 33 | ok1(errno == 100); 34 | 35 | /* Test successful close/unlink doesn't hit errno either. */ 36 | fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600); 37 | assert(fd >= 0); 38 | 39 | errno = 100; 40 | ok1(close_noerr(fd) == 0); 41 | ok1(errno == 100); 42 | 43 | errno = 100; 44 | ok1(unlink_noerr(name) == 0); 45 | ok1(errno == 100); 46 | 47 | /* Test failing fclose */ 48 | fp = fopen(name, "wb"); 49 | assert(fp); 50 | close(fileno(fp)); 51 | ok1(fclose_noerr(fp) == EBADF); 52 | 53 | /* Test successful fclose */ 54 | fp = fopen(name, "wb"); 55 | assert(fp); 56 | 57 | errno = 100; 58 | ok1(fclose_noerr(fp) == 0); 59 | ok1(errno == 100); 60 | unlink(name); 61 | 62 | errno = 101; 63 | free_noerr(malloc(7)); 64 | ok1(errno == 101); 65 | 66 | return exit_status(); 67 | } 68 | -------------------------------------------------------------------------------- /ccan/ccan/opt/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/GPL-2 -------------------------------------------------------------------------------- /ccan/ccan/opt/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * opt - simple command line parsing 7 | * 8 | * Simple but powerful command line parsing. 9 | * 10 | * See Also: 11 | * ccan/autodata 12 | * 13 | * Example: 14 | * #include 15 | * #include 16 | * #include 17 | * 18 | * static bool someflag; 19 | * static int verbose; 20 | * static char *somestring; 21 | * 22 | * static struct opt_table opts[] = { 23 | * OPT_WITHOUT_ARG("--verbose|-v", opt_inc_intval, &verbose, 24 | * "Verbose mode (can be specified more than once)"), 25 | * OPT_WITHOUT_ARG("--someflag", opt_set_bool, &someflag, 26 | * "Set someflag"), 27 | * OPT_WITH_ARG("--somefile=", opt_set_charp, opt_show_charp, 28 | * &somestring, "Set somefile to "), 29 | * OPT_WITHOUT_ARG("--usage|--help|-h", opt_usage_and_exit, 30 | * "args...\nA silly test program.", 31 | * "Print this message."), 32 | * OPT_ENDTABLE 33 | * }; 34 | * 35 | * int main(int argc, char *argv[]) 36 | * { 37 | * int i; 38 | * 39 | * opt_register_table(opts, NULL); 40 | * // For fun, register an extra one. 41 | * opt_register_noarg("--no-someflag", opt_set_invbool, &someflag, 42 | * "Unset someflag"); 43 | * if (!opt_parse(&argc, argv, opt_log_stderr)) 44 | * exit(1); 45 | * 46 | * printf("someflag = %i, verbose = %i, somestring = %s\n", 47 | * someflag, verbose, somestring); 48 | * printf("%u args left over:", argc - 1); 49 | * for (i = 1; i < argc; i++) 50 | * printf(" %s", argv[i]); 51 | * printf("\n"); 52 | * return 0; 53 | * } 54 | * 55 | * License: GPL (v2 or any later version) 56 | * Author: Rusty Russell 57 | */ 58 | int main(int argc, char *argv[]) 59 | { 60 | if (argc != 2) 61 | return 1; 62 | 63 | if (strcmp(argv[1], "depends") == 0) { 64 | printf("ccan/cast\n"); 65 | printf("ccan/compiler\n"); 66 | printf("ccan/typesafe_cb\n"); 67 | return 0; 68 | } 69 | 70 | return 1; 71 | } 72 | -------------------------------------------------------------------------------- /ccan/ccan/opt/private.h: -------------------------------------------------------------------------------- 1 | /* Licensed under GPLv2+ - see LICENSE file for details */ 2 | #ifndef CCAN_OPT_PRIVATE_H 3 | #define CCAN_OPT_PRIVATE_H 4 | 5 | extern struct opt_table *opt_table; 6 | extern unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long; 7 | 8 | extern const char *opt_argv0; 9 | 10 | #define subtable_of(entry) ((const struct opt_table *)((entry)->names)) 11 | 12 | const char *first_sopt(unsigned *i); 13 | const char *next_sopt(const char *names, unsigned *i); 14 | const char *first_lopt(unsigned *i, unsigned *len); 15 | const char *next_lopt(const char *p, unsigned *i, unsigned *len); 16 | 17 | struct opt_alloc { 18 | void *(*alloc)(size_t size); 19 | void *(*realloc)(void *ptr, size_t size); 20 | void (*free)(void *ptr); 21 | }; 22 | extern struct opt_alloc opt_alloc; 23 | 24 | int parse_one(int *argc, char *argv[], enum opt_type is_early, unsigned *offset, 25 | void (*errlog)(const char *fmt, ...), bool unknown_ok); 26 | 27 | #endif /* CCAN_OPT_PRIVATE_H */ 28 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/compile_ok-const-arg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | opt_register_noarg("-v", opt_version_and_exit, 10 | (const char *)"1.2.3", 11 | (const char *)"Print version"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/run-consume_words.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* Test consume_words helper. */ 8 | int main(void) 9 | { 10 | size_t prefix, len; 11 | bool start = true; 12 | 13 | plan_tests(27); 14 | 15 | /* Every line over width. */ 16 | len = consume_words("hello world", 1, &prefix, &start); 17 | ok1(prefix == 0); 18 | ok1(!start); 19 | ok1(len == strlen("hello")); 20 | len = consume_words(" world", 1, &prefix, &start); 21 | ok1(prefix == 1); 22 | ok1(len == strlen("world")); 23 | ok1(!start); 24 | ok1(consume_words("", 1, &prefix, &start) == 0); 25 | 26 | /* Same with width where won't both fit. */ 27 | start = true; 28 | len = consume_words("hello world", 5, &prefix, &start); 29 | ok1(!start); 30 | ok1(prefix == 0); 31 | ok1(len == strlen("hello")); 32 | len = consume_words(" world", 5, &prefix, &start); 33 | ok1(!start); 34 | ok1(prefix == 1); 35 | ok1(len == strlen("world")); 36 | ok1(consume_words("", 5, &prefix, &start) == 0); 37 | 38 | start = true; 39 | len = consume_words("hello world", 11, &prefix, &start); 40 | ok1(!start); 41 | ok1(prefix == 0); 42 | ok1(len == strlen("hello world")); 43 | ok1(consume_words("", 11, &prefix, &start) == 0); 44 | 45 | /* Now try a literal, should not be broken */ 46 | start = true; 47 | len = consume_words(" hello world", 5, &prefix, &start); 48 | ok1(!start); 49 | ok1(prefix == 1); 50 | ok1(len == strlen("hello world")); 51 | 52 | /* A literal after an explicit \n also not broken */ 53 | start = true; 54 | len = consume_words("hi\n hello world", 5, &prefix, &start); 55 | ok1(start); 56 | ok1(prefix == 0); 57 | ok1(len == strlen("hi\n")); 58 | len = consume_words(" hello world", 5, &prefix, &start); 59 | ok1(!start); 60 | ok1(prefix == 1); 61 | ok1(len == strlen("hello world")); 62 | 63 | return exit_status(); 64 | } 65 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/run-correct-reporting.c: -------------------------------------------------------------------------------- 1 | /* Make sure when multiple equivalent options, correct one is used for errors */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "utils.h" 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | plan_tests(12); 14 | 15 | /* --aaa without args. */ 16 | opt_register_arg("-a|--aaa", test_arg, NULL, "aaa", ""); 17 | ok1(!parse_args(&argc, &argv, "--aaa", NULL)); 18 | ok1(strstr(err_output, ": --aaa: requires an argument")); 19 | free(err_output); 20 | err_output = NULL; 21 | ok1(!parse_args(&argc, &argv, "-a", NULL)); 22 | ok1(strstr(err_output, ": -a: requires an argument")); 23 | free(err_output); 24 | err_output = NULL; 25 | 26 | /* Multiple */ 27 | opt_register_arg("--bbb|-b|-c|--ccc", test_arg, NULL, "aaa", ""); 28 | ok1(!parse_args(&argc, &argv, "--bbb", NULL)); 29 | ok1(strstr(err_output, ": --bbb: requires an argument")); 30 | free(err_output); 31 | err_output = NULL; 32 | ok1(!parse_args(&argc, &argv, "-b", NULL)); 33 | ok1(strstr(err_output, ": -b: requires an argument")); 34 | free(err_output); 35 | err_output = NULL; 36 | ok1(!parse_args(&argc, &argv, "-c", NULL)); 37 | ok1(strstr(err_output, ": -c: requires an argument")); 38 | free(err_output); 39 | err_output = NULL; 40 | ok1(!parse_args(&argc, &argv, "--ccc", NULL)); 41 | ok1(strstr(err_output, ": --ccc: requires an argument")); 42 | free(err_output); 43 | err_output = NULL; 44 | 45 | /* parse_args allocates argv */ 46 | free(argv); 47 | return exit_status(); 48 | } 49 | 50 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/run-iter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "utils.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | /* Test iterators. */ 13 | int main(void) 14 | { 15 | unsigned j, i, len = 0; 16 | const char *p; 17 | 18 | plan_tests(37 * 2); 19 | for (j = 0; j < 2; j ++) { 20 | reset_options(); 21 | /* Giving subtable a title makes an extra entry! */ 22 | opt_register_table(subtables, j == 0 ? NULL : "subtable"); 23 | 24 | p = first_lopt(&i, &len); 25 | ok1(i == j + 0); 26 | ok1(len == 3); 27 | ok1(strncmp(p, "jjj", len) == 0); 28 | p = next_lopt(p, &i, &len); 29 | ok1(i == j + 0); 30 | ok1(len == 3); 31 | ok1(strncmp(p, "lll", len) == 0); 32 | p = next_lopt(p, &i, &len); 33 | ok1(i == j + 1); 34 | ok1(len == 3); 35 | ok1(strncmp(p, "mmm", len) == 0); 36 | p = next_lopt(p, &i, &len); 37 | ok1(i == j + 5); 38 | ok1(len == 3); 39 | ok1(strncmp(p, "ddd", len) == 0); 40 | p = next_lopt(p, &i, &len); 41 | ok1(i == j + 6); 42 | ok1(len == 3); 43 | ok1(strncmp(p, "eee", len) == 0); 44 | p = next_lopt(p, &i, &len); 45 | ok1(i == j + 7); 46 | ok1(len == 3); 47 | ok1(strncmp(p, "ggg", len) == 0); 48 | p = next_lopt(p, &i, &len); 49 | ok1(i == j + 8); 50 | ok1(len == 3); 51 | ok1(strncmp(p, "hhh", len) == 0); 52 | p = next_lopt(p, &i, &len); 53 | ok1(!p); 54 | 55 | p = first_sopt(&i); 56 | ok1(i == j + 0); 57 | ok1(*p == 'j'); 58 | p = next_sopt(p, &i); 59 | ok1(i == j + 0); 60 | ok1(*p == 'l'); 61 | p = next_sopt(p, &i); 62 | ok1(i == j + 1); 63 | ok1(*p == 'm'); 64 | p = next_sopt(p, &i); 65 | ok1(i == j + 2); 66 | ok1(*p == 'a'); 67 | p = next_sopt(p, &i); 68 | ok1(i == j + 3); 69 | ok1(*p == 'b'); 70 | p = next_sopt(p, &i); 71 | ok1(i == j + 7); 72 | ok1(*p == 'g'); 73 | p = next_sopt(p, &i); 74 | ok1(i == j + 8); 75 | ok1(*p == 'h'); 76 | p = next_sopt(p, &i); 77 | ok1(!p); 78 | } 79 | 80 | return exit_status(); 81 | } 82 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/run-no-options.c: -------------------------------------------------------------------------------- 1 | /* Make sure we still work with no options registered */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "utils.h" 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | const char *myname = argv[0]; 13 | 14 | plan_tests(7); 15 | 16 | /* Simple short arg.*/ 17 | ok1(!parse_args(&argc, &argv, "-a", NULL)); 18 | /* Simple long arg.*/ 19 | ok1(!parse_args(&argc, &argv, "--aaa", NULL)); 20 | 21 | /* Extra arguments preserved. */ 22 | ok1(parse_args(&argc, &argv, "extra", "args", NULL)); 23 | ok1(argc == 3); 24 | ok1(argv[0] == myname); 25 | ok1(strcmp(argv[1], "extra") == 0); 26 | ok1(strcmp(argv[2], "args") == 0); 27 | 28 | /* parse_args allocates argv */ 29 | free(argv); 30 | 31 | return exit_status(); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /ccan/ccan/opt/test/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef CCAN_OPT_TEST_UTILS_H 2 | #define CCAN_OPT_TEST_UTILS_H 3 | #include 4 | #include 5 | 6 | bool parse_args(int *argc, char ***argv, ...); 7 | bool parse_early_args(int *argc, char ***argv, ...); 8 | bool parse_early_args_incomplete(int *argc, char ***argv, ...); 9 | extern char *err_output; 10 | void save_err_output(const char *fmt, ...); 11 | void reset_options(void); 12 | 13 | extern unsigned int test_cb_called; 14 | char *test_noarg(void *arg); 15 | char *test_arg(const char *optarg, const char *arg); 16 | void show_arg(char buf[OPT_SHOW_LEN], const char *arg); 17 | 18 | extern struct opt_table short_table[]; 19 | extern struct opt_table long_table[]; 20 | extern struct opt_table long_and_short_table[]; 21 | extern struct opt_table subtables[]; 22 | #endif /* CCAN_OPT_TEST_UTILS_H */ 23 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/rbuf/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * rbuf - buffered I/O input primitive. 7 | * 8 | * This code is like stdio, only simpler and more transparent to the user. 9 | * 10 | * Author: Rusty Russell 11 | * License: BSD-MIT 12 | * 13 | * Example: 14 | * #include 15 | * #include 16 | * #include 17 | * #include 18 | * 19 | * // Dumb demo program to replace ' ' with '*'. 20 | * int main(int argc, char *argv[]) 21 | * { 22 | * struct rbuf in; 23 | * char *word; 24 | * 25 | * if (argv[1]) { 26 | * if (!rbuf_open(&in, argv[1], NULL, 0, membuf_realloc)) 27 | * err(1, "Failed opening %s", argv[1]); 28 | * } else 29 | * rbuf_init(&in, STDIN_FILENO, NULL, 0, membuf_realloc); 30 | * 31 | * while ((word = rbuf_read_str(&in, ' ')) != NULL) 32 | * printf("%s*", word); 33 | * 34 | * if (errno) 35 | * err(1, "Reading %s", argv[1] ? argv[1] : ""); 36 | * 37 | * // Free the buffer, just because we can. 38 | * free(rbuf_cleanup(&in)); 39 | * return 0; 40 | * } 41 | */ 42 | int main(int argc, char *argv[]) 43 | { 44 | /* Expect exactly one argument */ 45 | if (argc != 2) 46 | return 1; 47 | 48 | if (strcmp(argv[1], "depends") == 0) { 49 | printf("ccan/membuf\n"); 50 | return 0; 51 | } 52 | 53 | return 1; 54 | } 55 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/test/run-all.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Include the C files directly. */ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static bool test_realloc_fail; 11 | static void *test_realloc(struct membuf *mb, void *buf, size_t n) 12 | { 13 | if (test_realloc_fail) 14 | return NULL; 15 | return realloc(buf, n); 16 | } 17 | 18 | int main(void) 19 | { 20 | struct rbuf in; 21 | char buf[4096]; 22 | int i, size, fd = open("run-all-file", O_WRONLY|O_CREAT, 0600); 23 | 24 | /* This is how many tests you plan to run */ 25 | plan_tests(8); 26 | 27 | /* Make sure we're bigger than a single buffer! */ 28 | size = rbuf_good_size(fd)*2; 29 | for (i = 0; i * sizeof(buf) < size; i++) { 30 | memset(buf, 0x42 + i, sizeof(buf)); 31 | write(fd, buf, sizeof(buf)); 32 | } 33 | close(fd); 34 | 35 | ok1(rbuf_open(&in, "run-all-file", NULL, 0, test_realloc)); 36 | /* Can't fill if realloc fails. */ 37 | test_realloc_fail = true; 38 | ok1(!rbuf_fill(&in)); 39 | ok1(errno == ENOMEM); 40 | test_realloc_fail = false; 41 | ok1(rbuf_fill(&in)); 42 | /* But can't load in whole file. */ 43 | test_realloc_fail = true; 44 | ok1(!rbuf_fill_all(&in)); 45 | ok1(errno == ENOMEM); 46 | test_realloc_fail = false; 47 | ok1(rbuf_fill_all(&in)); 48 | ok1(rbuf_len(&in) == size); 49 | for (i = 0; i * sizeof(buf) < size; i++) { 50 | memset(buf, 0x42 + i, sizeof(buf)); 51 | if (memcmp(buf, rbuf_start(&in), sizeof(buf)) != 0) { 52 | fail("Bad buffer contents"); 53 | break; 54 | } 55 | rbuf_consume(&in, sizeof(buf)); 56 | } 57 | free(rbuf_cleanup(&in)); 58 | 59 | /* This exits depending on whether all tests passed */ 60 | return exit_status(); 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/test/run-open.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Include the C files directly. */ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int main(void) 11 | { 12 | struct rbuf in; 13 | 14 | /* This is how many tests you plan to run */ 15 | plan_tests(5); 16 | 17 | ok1(!rbuf_open(&in, "nonexistent-file", NULL, 0, NULL)); 18 | ok1(errno == ENOENT); 19 | ok1(rbuf_open(&in, "test/run-open.c", NULL, 0, NULL)); 20 | ok1(close(in.fd) == 0); 21 | /* If this fails to stat, it should fall back */ 22 | ok1(rbuf_good_size(in.fd) == 4096); 23 | 24 | return exit_status(); 25 | } 26 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/test/run-partial-read.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static ssize_t partial_read(int fd, void *buf, size_t count) 4 | { 5 | return read(fd, buf, 1); 6 | } 7 | static ssize_t full_read(int fd, void *buf, size_t count) 8 | { 9 | return read(fd, buf, count); 10 | } 11 | #define read partial_read 12 | 13 | #include 14 | /* Include the C files directly. */ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main(void) 23 | { 24 | struct rbuf in; 25 | char buf[4096]; 26 | char *lines[100], *p; 27 | int i, fd = open("test/run.c", O_RDONLY); 28 | 29 | /* This is how many tests you plan to run */ 30 | plan_tests(160); 31 | 32 | /* Grab ourselves for comparison. */ 33 | buf[full_read(fd, buf, sizeof(buf))] = '\0'; 34 | lseek(fd, 0, SEEK_SET); 35 | 36 | for (i = 0, p = buf; *p; i++) { 37 | lines[i] = p; 38 | p = strchr(p, '\n'); 39 | *p = '\0'; 40 | p++; 41 | } 42 | lines[i] = NULL; 43 | 44 | rbuf_init(&in, fd, malloc(31), 31, membuf_realloc); 45 | ok1(in.fd == fd); 46 | ok1(membuf_num_space(&in.m) == 31); 47 | p = rbuf_read_str(&in, '\n'); 48 | ok1(p); 49 | ok1(strcmp(p, lines[0]) == 0); 50 | 51 | p = rbuf_read_str(&in, '\n'); 52 | ok1(p); 53 | ok1(strcmp(p, lines[1]) == 0); 54 | 55 | for (i = 2; lines[i]; i++) { 56 | ok1(p = rbuf_read_str(&in, '\n')); 57 | ok1(strcmp(p, lines[i]) == 0); 58 | } 59 | 60 | p = rbuf_read_str(&in, '\n'); 61 | ok1(errno == 0); 62 | ok1(p == NULL); 63 | free(rbuf_cleanup(&in)); 64 | 65 | /* This exits depending on whether all tests passed */ 66 | return exit_status(); 67 | } 68 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/test/run-term-eof.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Include the C files directly. */ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static bool test_realloc_fail; 11 | static void *test_realloc(struct membuf *mb, void *buf, size_t n) 12 | { 13 | if (test_realloc_fail) 14 | return NULL; 15 | return realloc(buf, n); 16 | } 17 | 18 | int main(void) 19 | { 20 | struct rbuf in; 21 | char buf[4096], *p; 22 | int fd = open("test/run-term-eof.c", O_RDONLY), len; 23 | 24 | /* This is how many tests you plan to run */ 25 | plan_tests(10); 26 | 27 | /* Grab ourselves for comparison. */ 28 | len = read(fd, buf, sizeof(buf)); 29 | buf[len] = '\0'; 30 | lseek(fd, 0, SEEK_SET); 31 | 32 | /* We have exact-size buffer, which causes problems adding term. */ 33 | rbuf_init(&in, fd, malloc(len), len, test_realloc); 34 | test_realloc_fail = true; 35 | p = rbuf_read_str(&in, 64); /* At symbol does not appear. */ 36 | ok1(errno == ENOMEM); 37 | ok1(!p); 38 | /* This should succeed... */ 39 | test_realloc_fail = false; 40 | p = rbuf_read_str(&in, 64); 41 | ok1(p); 42 | ok1(strcmp(p, buf) == 0); 43 | ok1(rbuf_start(&in) == p + strlen(p)); 44 | free(rbuf_cleanup(&in)); 45 | 46 | /* Try again. */ 47 | lseek(fd, 0, SEEK_SET); 48 | rbuf_init(&in, fd, malloc(len), len, test_realloc); 49 | p = rbuf_read_str(&in, 64); 50 | ok1(p); 51 | ok1(strcmp(p, buf) == 0); 52 | ok1(rbuf_start(&in) == p + strlen(p)); 53 | free(rbuf_cleanup(&in)); 54 | 55 | /* Normal case, we get rbuf_start after nul */ 56 | lseek(fd, 0, SEEK_SET); 57 | rbuf_init(&in, fd, NULL, 0, test_realloc); 58 | p = rbuf_read_str(&in, '^'); 59 | ok1(p); 60 | ok1(rbuf_start(&in) == p + strlen(p) + 1); 61 | free(rbuf_cleanup(&in)); 62 | 63 | return exit_status(); 64 | } 65 | -------------------------------------------------------------------------------- /ccan/ccan/rbuf/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Include the C files directly. */ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static bool test_realloc_fail; 11 | static void *test_realloc(struct membuf *mb, void *buf, size_t n) 12 | { 13 | if (test_realloc_fail) 14 | return NULL; 15 | return realloc(buf, n); 16 | } 17 | 18 | int main(void) 19 | { 20 | struct rbuf in; 21 | char buf[4096]; 22 | char *lines[100], *p; 23 | int i, fd = open("test/run.c", O_RDONLY), len; 24 | 25 | /* This is how many tests you plan to run */ 26 | plan_tests(164); 27 | 28 | /* Grab ourselves for comparison. */ 29 | len = read(fd, buf, sizeof(buf)); 30 | buf[len] = '\0'; 31 | lseek(fd, 0, SEEK_SET); 32 | 33 | for (i = 0, p = buf; *p; i++) { 34 | lines[i] = p; 35 | p = strchr(p, '\n'); 36 | *p = '\0'; 37 | p++; 38 | } 39 | lines[i] = NULL; 40 | 41 | rbuf_init(&in, fd, malloc(31), 31, test_realloc); 42 | ok1(in.fd == fd); 43 | ok1(membuf_num_space(&in.m) == 31); 44 | test_realloc_fail = true; 45 | p = rbuf_read_str(&in, '\n'); 46 | ok1(p); 47 | ok1(strcmp(p, lines[0]) == 0); 48 | 49 | test_realloc_fail = false; 50 | p = rbuf_read_str(&in, '\n'); 51 | ok1(p); 52 | ok1(strcmp(p, lines[1]) == 0); 53 | 54 | for (i = 2; lines[i]; i++) { 55 | ok1(p = rbuf_read_str(&in, '\n')); 56 | ok1(strcmp(p, lines[i]) == 0); 57 | } 58 | 59 | p = rbuf_read_str(&in, '\n'); 60 | ok1(errno == 0); 61 | ok1(p == NULL); 62 | free(rbuf_cleanup(&in)); 63 | 64 | /* Another way of reading the entire (text) file. */ 65 | lseek(fd, 0, SEEK_SET); 66 | rbuf_init(&in, fd, NULL, 0, test_realloc); 67 | p = rbuf_read_str(&in, 0); 68 | ok1(p); 69 | ok1(strlen(p) == len); 70 | 71 | close(fd); 72 | p = rbuf_read_str(&in, 0); 73 | ok1(errno == EBADF); 74 | ok1(!p); 75 | free(rbuf_cleanup(&in)); 76 | 77 | return exit_status(); 78 | } 79 | -------------------------------------------------------------------------------- /ccan/ccan/short_types/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/short_types/short_types.h: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #ifndef CCAN_SHORT_TYPES_H 3 | #define CCAN_SHORT_TYPES_H 4 | #include 5 | 6 | /** 7 | * u64/s64/u32/s32/u16/s16/u8/s8 - short names for explicitly-sized types. 8 | */ 9 | typedef uint64_t u64; 10 | typedef int64_t s64; 11 | typedef uint32_t u32; 12 | typedef int32_t s32; 13 | typedef uint16_t u16; 14 | typedef int16_t s16; 15 | typedef uint8_t u8; 16 | typedef int8_t s8; 17 | 18 | /* Whichever they include first, they get these definitions. */ 19 | #ifdef CCAN_ENDIAN_H 20 | /** 21 | * be64/be32/be16 - 64/32/16 bit big-endian representation. 22 | */ 23 | typedef beint64_t be64; 24 | typedef beint32_t be32; 25 | typedef beint16_t be16; 26 | 27 | /** 28 | * le64/le32/le16 - 64/32/16 bit little-endian representation. 29 | */ 30 | typedef leint64_t le64; 31 | typedef leint32_t le32; 32 | typedef leint16_t le16; 33 | #endif 34 | 35 | #endif /* CCAN_SHORT_TYPES_H */ 36 | -------------------------------------------------------------------------------- /ccan/ccan/short_types/test/run-endian.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | plan_tests(6); 10 | 11 | ok1(sizeof(be64) == 8); 12 | ok1(sizeof(be32) == 4); 13 | ok1(sizeof(be16) == 2); 14 | 15 | ok1(sizeof(le64) == 8); 16 | ok1(sizeof(le32) == 4); 17 | ok1(sizeof(le16) == 2); 18 | 19 | return exit_status(); 20 | } 21 | -------------------------------------------------------------------------------- /ccan/ccan/short_types/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | plan_tests(16); 9 | 10 | ok1(sizeof(u64) == 8); 11 | ok1(sizeof(s64) == 8); 12 | ok1(sizeof(u32) == 4); 13 | ok1(sizeof(s32) == 4); 14 | ok1(sizeof(u16) == 2); 15 | ok1(sizeof(s16) == 2); 16 | ok1(sizeof(u8) == 1); 17 | ok1(sizeof(s8) == 1); 18 | 19 | /* Signedness tests. */ 20 | ok1((u64)-1 > 0); 21 | ok1((u32)-1 > 0); 22 | ok1((u16)-1 > 0); 23 | ok1((u8)-1 > 0); 24 | ok1((s64)-1 < 0); 25 | ok1((s32)-1 < 0); 26 | ok1((s16)-1 < 0); 27 | ok1((s8)-1 < 0); 28 | 29 | return exit_status(); 30 | } 31 | -------------------------------------------------------------------------------- /ccan/ccan/str/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/str/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * str - string helper routines 7 | * 8 | * This is a grab bag of functions for string operations, designed to enhance 9 | * the standard string.h. 10 | * 11 | * Note that if you define CCAN_STR_DEBUG, you will get extra compile 12 | * checks on common misuses of the following functions (they will now 13 | * be out-of-line, so there is a runtime penalty!). 14 | * 15 | * strstr, strchr, strrchr: 16 | * Return const char * if first argument is const (gcc only). 17 | * 18 | * isalnum, isalpha, isascii, isblank, iscntrl, isdigit, isgraph, 19 | * islower, isprint, ispunct, isspace, isupper, isxdigit: 20 | * Static and runtime check that input is EOF or an *unsigned* 21 | * char, as per C standard (really!). 22 | * 23 | * Example: 24 | * #include 25 | * #include 26 | * 27 | * int main(int argc, char *argv[]) 28 | * { 29 | * if (argc > 1 && streq(argv[1], "--verbose")) 30 | * printf("verbose set\n"); 31 | * if (argc > 1 && strstarts(argv[1], "--")) 32 | * printf("Some option set\n"); 33 | * if (argc > 1 && strends(argv[1], "cow-powers")) 34 | * printf("Magic option set\n"); 35 | * return 0; 36 | * } 37 | * 38 | * License: CC0 (Public domain) 39 | * Author: Rusty Russell 40 | */ 41 | int main(int argc, char *argv[]) 42 | { 43 | if (argc != 2) 44 | return 1; 45 | 46 | if (strcmp(argv[1], "depends") == 0) { 47 | printf("ccan/build_assert\n"); 48 | return 0; 49 | } 50 | 51 | return 1; 52 | } 53 | -------------------------------------------------------------------------------- /ccan/ccan/str/hex/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/str/hex/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * str/hex - hex-to-string conversions and vice-versa 7 | * 8 | * This code contains simple routines for hexadecimal strings. 9 | * 10 | * License: CC0 (Public domain) 11 | * Author: Rusty Russell 12 | * 13 | * Example: 14 | * int main(int argc, char *argv[]) 15 | * { 16 | * int i; 17 | * 18 | * for (i = 1; i < argc; i++) { 19 | * char str[hex_str_size(strlen(argv[i]))]; 20 | * 21 | * hex_encode(str, sizeof(str), argv[i], strlen(argv[i])); 22 | * printf("%s ", str); 23 | * } 24 | * printf("\n"); 25 | * return 0; 26 | * } 27 | */ 28 | int main(int argc, char *argv[]) 29 | { 30 | /* Expect exactly one argument */ 31 | if (argc != 2) 32 | return 1; 33 | 34 | if (strcmp(argv[1], "depends") == 0) { 35 | return 0; 36 | } 37 | 38 | return 1; 39 | } 40 | -------------------------------------------------------------------------------- /ccan/ccan/str/hex/hex.c: -------------------------------------------------------------------------------- 1 | /* CC0 license (public domain) - see LICENSE file for details */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | static bool char_to_hex(unsigned char *val, char c) 8 | { 9 | if (c >= '0' && c <= '9') { 10 | *val = c - '0'; 11 | return true; 12 | } 13 | if (c >= 'a' && c <= 'f') { 14 | *val = c - 'a' + 10; 15 | return true; 16 | } 17 | if (c >= 'A' && c <= 'F') { 18 | *val = c - 'A' + 10; 19 | return true; 20 | } 21 | return false; 22 | } 23 | 24 | bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize) 25 | { 26 | unsigned char v1, v2; 27 | unsigned char *p = buf; 28 | 29 | while (slen > 1) { 30 | if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1])) 31 | return false; 32 | if (!bufsize) 33 | return false; 34 | *(p++) = (v1 << 4) | v2; 35 | str += 2; 36 | slen -= 2; 37 | bufsize--; 38 | } 39 | return slen == 0 && bufsize == 0; 40 | } 41 | 42 | static char hexchar(unsigned int val) 43 | { 44 | if (val < 10) 45 | return '0' + val; 46 | if (val < 16) 47 | return 'a' + val - 10; 48 | abort(); 49 | } 50 | 51 | bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize) 52 | { 53 | size_t i; 54 | 55 | if (destsize < hex_str_size(bufsize)) 56 | return false; 57 | 58 | for (i = 0; i < bufsize; i++) { 59 | unsigned int c = ((const unsigned char *)buf)[i]; 60 | *(dest++) = hexchar(c >> 4); 61 | *(dest++) = hexchar(c & 0xF); 62 | } 63 | *dest = '\0'; 64 | 65 | return true; 66 | } 67 | -------------------------------------------------------------------------------- /ccan/ccan/str/hex/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Include the C files directly. */ 3 | #include 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | const char teststr[] = "0123456789abcdefABCDEF"; 10 | const char bad_teststr[] = "0123456789abcdefABCDEF1O"; 11 | const unsigned char testdata[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 12 | 0xcd, 0xef, 0xAB, 0xCD, 0xEF }; 13 | unsigned char data[11]; 14 | char str[23]; 15 | size_t i; 16 | 17 | plan_tests(10 + sizeof(str)); 18 | 19 | ok1(hex_str_size(sizeof(testdata)) == sizeof(teststr)); 20 | /* This gives right result with or without nul included */ 21 | ok1(hex_data_size(strlen(teststr)) == sizeof(testdata)); 22 | ok1(hex_data_size(sizeof(teststr)) == sizeof(testdata)); 23 | 24 | ok1(hex_decode(teststr, strlen(teststr), data, sizeof(data))); 25 | ok1(memcmp(data, testdata, sizeof(testdata)) == 0); 26 | ok1(hex_encode(testdata, sizeof(testdata), str, sizeof(str))); 27 | ok1(strcmp(str, "0123456789abcdefabcdef") == 0); 28 | 29 | /* Bad char */ 30 | ok1(!hex_decode(bad_teststr, strlen(bad_teststr), data, sizeof(data))); 31 | /* Bad hex string len */ 32 | ok1(!hex_decode(teststr, strlen(teststr) - 1, data, sizeof(data))); 33 | /* Bad buffer len */ 34 | ok1(!hex_decode(teststr, strlen(teststr), data, sizeof(data) - 1)); 35 | 36 | /* Bad deststring size. */ 37 | for (i = 1; i <= sizeof(str); i++) 38 | ok1(!hex_encode(testdata, sizeof(testdata), str, sizeof(str)-i)); 39 | 40 | /* This exits depending on whether all tests passed */ 41 | return exit_status(); 42 | } 43 | -------------------------------------------------------------------------------- /ccan/ccan/str/str.c: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #include 3 | 4 | size_t strcount(const char *haystack, const char *needle) 5 | { 6 | size_t i = 0, nlen = strlen(needle); 7 | 8 | while ((haystack = strstr(haystack, needle)) != NULL) { 9 | i++; 10 | haystack += nlen; 11 | } 12 | return i; 13 | } 14 | -------------------------------------------------------------------------------- /ccan/ccan/str/str_debug.h: -------------------------------------------------------------------------------- 1 | /* CC0 (Public domain) - see LICENSE file for details */ 2 | #ifndef CCAN_STR_DEBUG_H 3 | #define CCAN_STR_DEBUG_H 4 | 5 | /* #define CCAN_STR_DEBUG 1 */ 6 | 7 | #ifdef CCAN_STR_DEBUG 8 | /* Because we mug the real ones with macros, we need our own wrappers. */ 9 | int str_isalnum(int i); 10 | int str_isalpha(int i); 11 | int str_isascii(int i); 12 | #if HAVE_ISBLANK 13 | int str_isblank(int i); 14 | #endif 15 | int str_iscntrl(int i); 16 | int str_isdigit(int i); 17 | int str_isgraph(int i); 18 | int str_islower(int i); 19 | int str_isprint(int i); 20 | int str_ispunct(int i); 21 | int str_isspace(int i); 22 | int str_isupper(int i); 23 | int str_isxdigit(int i); 24 | 25 | char *str_strstr(const char *haystack, const char *needle); 26 | char *str_strchr(const char *s, int c); 27 | char *str_strrchr(const char *s, int c); 28 | #endif /* CCAN_STR_DEBUG */ 29 | 30 | #endif /* CCAN_STR_DEBUG_H */ 31 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-STR_MAX_CHARS.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct s { 4 | int val; 5 | }; 6 | 7 | int main(void) 8 | { 9 | struct s 10 | #ifdef FAIL 11 | #if !HAVE_TYPEOF 12 | #error We need typeof to check STR_MAX_CHARS. 13 | #endif 14 | #else 15 | /* A pointer is OK. */ 16 | * 17 | #endif 18 | val; 19 | char str[STR_MAX_CHARS(val)]; 20 | 21 | str[0] = '\0'; 22 | return str[0] ? 0 : 1; 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isalnum.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isalnum. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isalnum(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isalpha.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isalpha. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isalpha(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isascii.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isascii. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isascii(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isblank.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF || !HAVE_ISBLANK 9 | #error We need typeof to check isblank. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | #if HAVE_ISBLANK 23 | return isblank(c); 24 | #else 25 | return c; 26 | #endif 27 | } 28 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-iscntrl.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check iscntrl. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return iscntrl(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isdigit.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isdigit. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isdigit(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-islower.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check islower. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return islower(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isprint.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isprint. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isprint(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-ispunct.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check ispunct. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return ispunct(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isspace.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isspace. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isspace(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isupper.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isupper. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isupper(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-isxdigit.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | #ifdef FAIL 8 | #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF 9 | #error We need typeof to check isxdigit. 10 | #endif 11 | char 12 | #else 13 | unsigned char 14 | #endif 15 | c = argv[0][0]; 16 | 17 | #ifdef FAIL 18 | /* Fake fail on unsigned char platforms. */ 19 | BUILD_ASSERT((char)255 < 0); 20 | #endif 21 | 22 | return isxdigit(c); 23 | } 24 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-strchr.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(void) 5 | { 6 | #ifdef FAIL 7 | #if !HAVE_TYPEOF 8 | #error We need typeof to check strstr. 9 | #endif 10 | #else 11 | const 12 | #endif 13 | char *ret; 14 | const char *str = "hello"; 15 | 16 | ret = strchr(str, 'l'); 17 | return ret ? 0 : 1; 18 | } 19 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-strrchr.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(void) 5 | { 6 | #ifdef FAIL 7 | #if !HAVE_TYPEOF 8 | #error We need typeof to check strstr. 9 | #endif 10 | #else 11 | const 12 | #endif 13 | char *ret; 14 | const char *str = "hello"; 15 | 16 | ret = strrchr(str, 'l'); 17 | return ret ? 0 : 1; 18 | } 19 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/compile_fail-strstr.c: -------------------------------------------------------------------------------- 1 | #define CCAN_STR_DEBUG 1 2 | #include 3 | 4 | int main(void) 5 | { 6 | #ifdef FAIL 7 | #if !HAVE_TYPEOF 8 | #error We need typeof to check strstr. 9 | #endif 10 | #else 11 | const 12 | #endif 13 | char *ret; 14 | const char *str = "hello"; 15 | 16 | ret = strstr(str, "hell"); 17 | return ret ? 0 : 1; 18 | } 19 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/debug.c: -------------------------------------------------------------------------------- 1 | /* We can't use the normal "#include the .c file" trick, since this is 2 | contaminated by str.h's macro overrides. So we put it in all tests 3 | like this. */ 4 | #define CCAN_STR_DEBUG 1 5 | #include 6 | -------------------------------------------------------------------------------- /ccan/ccan/str/test/run-STR_MAX_CHARS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(void) 8 | { 9 | char str[1000]; 10 | struct { 11 | uint8_t u1byte; 12 | int8_t s1byte; 13 | uint16_t u2byte; 14 | int16_t s2byte; 15 | uint32_t u4byte; 16 | int32_t s4byte; 17 | uint64_t u8byte; 18 | int64_t s8byte; 19 | void *ptr; 20 | } types; 21 | 22 | plan_tests(13); 23 | 24 | memset(&types, 0xFF, sizeof(types)); 25 | 26 | /* Hex versions */ 27 | sprintf(str, "0x%llx", (unsigned long long)types.u1byte); 28 | ok1(strlen(str) < STR_MAX_CHARS(types.u1byte)); 29 | sprintf(str, "0x%llx", (unsigned long long)types.u2byte); 30 | ok1(strlen(str) < STR_MAX_CHARS(types.u2byte)); 31 | sprintf(str, "0x%llx", (unsigned long long)types.u4byte); 32 | ok1(strlen(str) < STR_MAX_CHARS(types.u4byte)); 33 | sprintf(str, "0x%llx", (unsigned long long)types.u8byte); 34 | ok1(strlen(str) < STR_MAX_CHARS(types.u8byte)); 35 | 36 | /* Decimal versions */ 37 | sprintf(str, "%u", types.u1byte); 38 | ok1(strlen(str) < STR_MAX_CHARS(types.u1byte)); 39 | sprintf(str, "%d", types.s1byte); 40 | ok1(strlen(str) < STR_MAX_CHARS(types.s1byte)); 41 | sprintf(str, "%u", types.u2byte); 42 | ok1(strlen(str) < STR_MAX_CHARS(types.u2byte)); 43 | sprintf(str, "%d", types.s2byte); 44 | ok1(strlen(str) < STR_MAX_CHARS(types.s2byte)); 45 | sprintf(str, "%u", types.u4byte); 46 | ok1(strlen(str) < STR_MAX_CHARS(types.u4byte)); 47 | sprintf(str, "%d", types.s4byte); 48 | ok1(strlen(str) < STR_MAX_CHARS(types.s4byte)); 49 | sprintf(str, "%llu", (unsigned long long)types.u8byte); 50 | ok1(strlen(str) < STR_MAX_CHARS(types.u8byte)); 51 | sprintf(str, "%lld", (long long)types.s8byte); 52 | ok1(strlen(str) < STR_MAX_CHARS(types.s8byte)); 53 | 54 | /* Pointer version. */ 55 | sprintf(str, "%p", types.ptr); 56 | ok1(strlen(str) < STR_MAX_CHARS(types.ptr)); 57 | 58 | return exit_status(); 59 | } 60 | -------------------------------------------------------------------------------- /ccan/ccan/take/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/take/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * take - routines to mark pointers to be consumed by called functions. 7 | * 8 | * This code helps to implement ownership transfer on a per-arg basis: 9 | * the caller wraps the pointer argument in take() and the callee checks 10 | * taken() to see if it should consume it. 11 | * 12 | * Author: Rusty Russell 13 | * License: CC0 (Public domain) 14 | * 15 | * Example: 16 | * // Given "foo/bar.c" outputs basename is bar.c 17 | * #include 18 | * #include 19 | * 20 | * // Dumb basename program and driver. 21 | * static char *base(const char *file TAKES) 22 | * { 23 | * const char *p = strrchr(file, '/'); 24 | * if (!p) 25 | * p = file; 26 | * else 27 | * p++; 28 | * 29 | * // Use arg in place if we're allowed. 30 | * if (taken(file)) 31 | * return memmove((char *)file, p, strlen(p)+1); 32 | * else 33 | * return strdup(p); 34 | * } 35 | * 36 | * int main(int argc, char *argv[]) 37 | * { 38 | * char *b; 39 | * 40 | * if (argc > 1) // Mangle in place. 41 | * b = base(take(argv[1])); 42 | * else 43 | * b = base("test/string"); 44 | * 45 | * printf("basename is %s\n", b); 46 | * return 0; 47 | * } 48 | */ 49 | int main(int argc, char *argv[]) 50 | { 51 | if (argc != 2) 52 | return 1; 53 | 54 | if (strcmp(argv[1], "depends") == 0) { 55 | printf("ccan/likely\n"); 56 | printf("ccan/str\n"); 57 | return 0; 58 | } 59 | 60 | return 1; 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/tal/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/benchmark/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-O3 -Wall -flto -I../../.. 2 | #CFLAGS=-O3 -Wall -I../../.. 3 | #CFLAGS=-g -Wall -I../../.. 4 | LDFLAGS=-O3 -flto 5 | LDLIBS=-lrt 6 | 7 | all: speed samba-allocs 8 | 9 | speed: speed.o tal.o talloc.o time.o list.o take.o str.o 10 | samba-allocs: samba-allocs.o tal.o talloc.o time.o list.o take.o 11 | 12 | tal.o: ../tal.c 13 | $(CC) $(CFLAGS) -c -o $@ $< 14 | str.o: ../str/str.c 15 | $(CC) $(CFLAGS) -c -o $@ $< 16 | talloc.o: ../../talloc/talloc.c 17 | $(CC) $(CFLAGS) -c -o $@ $< 18 | time.o: ../../time/time.c 19 | $(CC) $(CFLAGS) -c -o $@ $< 20 | list.o: ../../list/list.c 21 | $(CC) $(CFLAGS) -c -o $@ $< 22 | take.o: ../../take/take.c 23 | $(CC) $(CFLAGS) -c -o $@ $< 24 | 25 | clean: 26 | rm -f speed samba-allocs *.o 27 | -------------------------------------------------------------------------------- /ccan/ccan/tal/grab_file-Makefile: -------------------------------------------------------------------------------- 1 | ccan/tal/grab_file-Makefile: ccan/tal/grab_file/grab_file.h ccan/tal/grab_file/grab_file.c ccan/tal/grab_file/_info 2 | ccan/tal/grab_file.o: ccan/tal/grab_file/grab_file.o 3 | -------------------------------------------------------------------------------- /ccan/ccan/tal/grab_file/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/LGPL-2.1 -------------------------------------------------------------------------------- /ccan/ccan/tal/grab_file/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/grab_file - file helper routines 7 | * 8 | * This contains simple functions for getting the contents of a file. 9 | * 10 | * Example: 11 | * #include 12 | * #include 13 | * #include 14 | * #include 15 | * #include // for tal_free 16 | * 17 | * int main(int argc, char *argv[]) 18 | * { 19 | * char *file; 20 | * 21 | * if (argc > 2) 22 | * err(1, "Takes 0 or 1 arguments"); 23 | * file = grab_file(NULL, argv[1]); 24 | * if (!file) 25 | * err(1, "Could not read file %s", argv[1]); 26 | * if (strlen(file)+1 != tal_count(file)) 27 | * printf("File contains NUL characters\n"); 28 | * else if (tal_count(file) == 1) 29 | * printf("File contains nothing\n"); 30 | * else if (strchr(file, '\n')) 31 | * printf("File contains multiple lines\n"); 32 | * else 33 | * printf("File contains one line\n"); 34 | * tal_free(file); 35 | * 36 | * return 0; 37 | * } 38 | * 39 | * License: LGPL (v2.1 or any later version) 40 | * Author: Rusty Russell 41 | */ 42 | int main(int argc, char *argv[]) 43 | { 44 | if (argc != 2) 45 | return 1; 46 | 47 | if (strcmp(argv[1], "depends") == 0) { 48 | printf("ccan/tal\n"); 49 | printf("ccan/noerr\n"); 50 | return 0; 51 | } 52 | if (strcmp(argv[1], "testdepends") == 0) { 53 | printf("ccan/tal/str\n"); 54 | return 0; 55 | } 56 | 57 | return 1; 58 | } 59 | -------------------------------------------------------------------------------- /ccan/ccan/tal/grab_file/grab_file.c: -------------------------------------------------------------------------------- 1 | /* Licensed under LGPLv2+ - see LICENSE file for details */ 2 | #include "grab_file.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | void *grab_fd(const void *ctx, int fd) 12 | { 13 | int ret; 14 | size_t max, size; 15 | char *buffer; 16 | struct stat st; 17 | 18 | size = 0; 19 | 20 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) 21 | max = st.st_size; 22 | else 23 | max = 16384; 24 | 25 | buffer = tal_arr(ctx, char, max+1); 26 | while ((ret = read(fd, buffer + size, max - size)) != 0) { 27 | if (ret < 0) { 28 | if (errno == EINTR) 29 | continue; 30 | return tal_free(buffer); 31 | } 32 | size += ret; 33 | if (size == max) { 34 | size_t extra = max; 35 | if (extra > 1024 * 1024) 36 | extra = 1024 * 1024; 37 | 38 | if (!tal_resize(&buffer, max+extra+1)) 39 | return NULL; 40 | 41 | max += extra; 42 | } 43 | } 44 | buffer[size] = '\0'; 45 | tal_resize(&buffer, size+1); 46 | 47 | return buffer; 48 | } 49 | 50 | void *grab_file(const void *ctx, const char *filename) 51 | { 52 | int fd; 53 | char *buffer; 54 | 55 | if (!filename) 56 | fd = dup(STDIN_FILENO); 57 | else 58 | fd = open(filename, O_RDONLY, 0); 59 | 60 | if (fd < 0) 61 | return NULL; 62 | 63 | buffer = grab_fd(ctx, fd); 64 | close_noerr(fd); 65 | return buffer; 66 | } 67 | -------------------------------------------------------------------------------- /ccan/ccan/tal/grab_file/test/run-grab.c: -------------------------------------------------------------------------------- 1 | /* This is test for grab_file() function 2 | */ 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int 13 | main(void) 14 | { 15 | unsigned int i; 16 | char **split, *str; 17 | int length; 18 | struct stat st; 19 | 20 | str = grab_file(NULL, "test/run-grab.c"); 21 | split = tal_strsplit(str, str, "\n", STR_EMPTY_OK); 22 | length = strlen(split[0]); 23 | ok1(!strcmp(split[0], "/* This is test for grab_file() function")); 24 | for (i = 1; split[i]; i++) 25 | length += strlen(split[i]); 26 | ok1(!strcmp(split[i-1], "/* End of grab_file() test */")); 27 | if (stat("test/run-grab.c", &st) != 0) 28 | /* FIXME: ditto */ 29 | if (stat("ccan/tal/grab_file/test/run-grab.c", &st) != 0) 30 | err(1, "Could not stat self"); 31 | ok1(st.st_size == length + i); 32 | tal_free(str); 33 | 34 | return 0; 35 | } 36 | 37 | /* End of grab_file() test */ 38 | -------------------------------------------------------------------------------- /ccan/ccan/tal/link-Makefile: -------------------------------------------------------------------------------- 1 | ccan/tal/link-Makefile: ccan/tal/link/link.h ccan/tal/link/link.c ccan/tal/link/_info 2 | ccan/tal/link.o: ccan/tal/link/link.o 3 | -------------------------------------------------------------------------------- /ccan/ccan/tal/link/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/path-Makefile: -------------------------------------------------------------------------------- 1 | ccan/tal/path-Makefile: ccan/tal/path/path.h ccan/tal/path/path.c ccan/tal/path/_info 2 | ccan/tal/path.o: ccan/tal/path/path.o 3 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/path/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/path - routines to manipulate paths 7 | * 8 | * This code helps manage paths. 9 | * 10 | * License: BSD-MIT 11 | * Author: Rusty Russell 12 | * 13 | * Example: 14 | * // Program to print out full path names, recursively. 15 | * #include 16 | * #include 17 | * #include 18 | * #include 19 | * #include 20 | * 21 | * static void dump(const char *dir) 22 | * { 23 | * struct dirent *di; 24 | * DIR *d = opendir(dir); 25 | * if (!d) { 26 | * warn("Failed to open %s", dir); 27 | * return; 28 | * } 29 | * printf("%s\n", dir); 30 | * while ((di = readdir(d)) != NULL) { 31 | * char *path; 32 | * if (streq(di->d_name, ".") || streq(di->d_name, "..")) 33 | * continue; 34 | * path = path_join(NULL, dir, di->d_name); 35 | * if (path_is_dir(path)) 36 | * dump(path); 37 | * tal_free(path); 38 | * } 39 | * closedir(d); 40 | * } 41 | * 42 | * int main(void) 43 | * { 44 | * dump(path_cwd(NULL)); 45 | * return 0; 46 | * } 47 | */ 48 | int main(int argc, char *argv[]) 49 | { 50 | /* Expect exactly one argument */ 51 | if (argc != 2) 52 | return 1; 53 | 54 | if (strcmp(argv[1], "depends") == 0) { 55 | printf("ccan/str\n"); 56 | printf("ccan/take\n"); 57 | printf("ccan/tal\n"); 58 | printf("ccan/tal/str\n"); 59 | return 0; 60 | } 61 | 62 | return 1; 63 | } 64 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/path - routines to manipulate paths 7 | * 8 | * This code helps manage paths. 9 | * 10 | * License: BSD-MIT 11 | * Author: Rusty Russell 12 | * 13 | * Example: 14 | * // Program to print out full path names, recursively. 15 | * #include 16 | * #include 17 | * #include 18 | * #include 19 | * #include 20 | * 21 | * static void dump(const char *dir) 22 | * { 23 | * struct dirent *di; 24 | * DIR *d = opendir(dir); 25 | * if (!d) { 26 | * warn("Failed to open %s", dir); 27 | * return; 28 | * } 29 | * printf("%s\n", dir); 30 | * while ((di = readdir(d)) != NULL) { 31 | * char *path; 32 | * if (streq(di->d_name, ".") || streq(di->d_name, "..")) 33 | * continue; 34 | * path = path_join(NULL, dir, di->d_name); 35 | * if (path_is_dir(path)) 36 | * dump(path); 37 | * tal_free(path); 38 | * } 39 | * closedir(d); 40 | * } 41 | * 42 | * int main(void) 43 | * { 44 | * dump(path_cwd(NULL)); 45 | * return 0; 46 | * } 47 | */ 48 | int main(int argc, char *argv[]) 49 | { 50 | /* Expect exactly one argument */ 51 | if (argc != 2) 52 | return 1; 53 | 54 | if (strcmp(argv[1], "depends") == 0) { 55 | printf("ccan/str\n"); 56 | printf("ccan/take\n"); 57 | printf("ccan/tal\n"); 58 | printf("ccan/tal/str\n"); 59 | return 0; 60 | } 61 | 62 | return 1; 63 | } 64 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-basename.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(26); 10 | 11 | path = path_basename(ctx, "/usr/lib"); 12 | ok1(streq(path, "lib")); 13 | ok1(tal_parent(path) == ctx); 14 | path = path_basename(ctx, "/usr/"); 15 | ok1(streq(path, "usr")); 16 | ok1(tal_parent(path) == ctx); 17 | path = path_basename(ctx, "/usr//"); 18 | ok1(streq(path, "usr")); 19 | ok1(tal_parent(path) == ctx); 20 | path = path_basename(ctx, "usr"); 21 | ok1(streq(path, "usr")); 22 | ok1(tal_parent(path) == ctx); 23 | path = path_basename(ctx, "/"); 24 | ok1(streq(path, "/")); 25 | ok1(tal_parent(path) == ctx); 26 | path = path_basename(ctx, "//"); 27 | ok1(streq(path, "/")); 28 | ok1(tal_parent(path) == ctx); 29 | path = path_basename(ctx, "."); 30 | ok1(streq(path, ".")); 31 | ok1(tal_parent(path) == ctx); 32 | path = path_basename(ctx, "./"); 33 | ok1(streq(path, ".")); 34 | ok1(tal_parent(path) == ctx); 35 | path = path_basename(ctx, ".."); 36 | ok1(streq(path, "..")); 37 | ok1(tal_parent(path) == ctx); 38 | path = path_basename(ctx, "../"); 39 | ok1(streq(path, "..")); 40 | ok1(tal_parent(path) == ctx); 41 | tal_free(ctx); 42 | 43 | ctx = tal_strdup(NULL, "ctx"); 44 | ok1(!tal_first(ctx)); 45 | 46 | /* Test take */ 47 | path = path_basename(ctx, take(tal_strdup(ctx, ".."))); 48 | ok1(streq(path, "..")); 49 | ok1(tal_parent(path) == ctx); 50 | ok1(tal_first(ctx) == path && !tal_next(ctx, path)); 51 | tal_free(path); 52 | ok1(path_basename(ctx, take(NULL)) == NULL); 53 | ok1(!tal_first(ctx)); 54 | 55 | tal_free(ctx); 56 | 57 | return exit_status(); 58 | } 59 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-canon.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char cwd[1024], *path, *path2, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(15); 10 | 11 | if (!getcwd(cwd, sizeof(cwd))) 12 | abort(); 13 | 14 | unlink("run-canon-link"); 15 | rmdir("run-canon-foo"); 16 | if (mkdir("run-canon-foo", 0700) != 0) 17 | abort(); 18 | if (symlink("run-canon-foo", "run-canon-link") != 0) 19 | abort(); 20 | 21 | path = path_canon(ctx, "run-canon-foo"); 22 | ok1(tal_parent(path) == ctx); 23 | ok1(strends(path, "run-canon-foo")); 24 | ok1(strstarts(path, cwd)); 25 | ok1(path[strlen(cwd)] == PATH_SEP); 26 | ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo")); 27 | tal_free(path); 28 | 29 | ok1(!path_canon(ctx, take(NULL))); 30 | ok1(tal_first(ctx) == NULL); 31 | 32 | /* Test take doesn't leak. */ 33 | ok1(tal_first(ctx) == NULL); 34 | path = path_canon(ctx, take(tal_strdup(ctx, "run-canon-foo"))); 35 | ok1(strends(path, "run-canon-foo")); 36 | ok1(strstarts(path, cwd)); 37 | ok1(path[strlen(cwd)] == PATH_SEP); 38 | ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo")); 39 | ok1(tal_first(ctx) == path && tal_next(ctx, path) == NULL); 40 | path2 = path_canon(ctx, "run-canon-link"); 41 | ok1(streq(path2, path)); 42 | 43 | unlink("run-canon-link"); 44 | if (symlink(".", "run-canon-link") != 0) 45 | abort(); 46 | 47 | path = path_canon(ctx, "run-canon-link"); 48 | ok1(streq(path, cwd)); 49 | 50 | tal_free(ctx); 51 | 52 | return exit_status(); 53 | } 54 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-cwd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char path1[1024], *cwd, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | /* This is how many tests you plan to run */ 10 | plan_tests(5); 11 | 12 | if (!getcwd(path1, sizeof(path1))) 13 | abort(); 14 | 15 | cwd = path_cwd(ctx); 16 | ok1(cwd); 17 | ok1(tal_parent(cwd) == ctx); 18 | tal_free(cwd); 19 | 20 | rmdir("run-cwd-long-long-long-name/bar-long-long-long-long-name"); 21 | rmdir("run-cwd-long-long-long-name"); 22 | if (mkdir("run-cwd-long-long-long-name", 0700) != 0) 23 | abort(); 24 | if (mkdir("run-cwd-long-long-long-name/bar-long-long-long-long-name", 0700) != 0) 25 | abort(); 26 | if (chdir("run-cwd-long-long-long-name/bar-long-long-long-long-name") != 0) 27 | abort(); 28 | 29 | cwd = path_cwd(ctx); 30 | ok1(cwd); 31 | ok1(tal_parent(cwd) == ctx); 32 | ok1(strends(cwd, 33 | "run-cwd-long-long-long-name/bar-long-long-long-long-name")); 34 | tal_free(ctx); 35 | 36 | return exit_status(); 37 | } 38 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-dirname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(26); 10 | 11 | path = path_dirname(ctx, "/usr/lib"); 12 | ok1(streq(path, "/usr")); 13 | ok1(tal_parent(path) == ctx); 14 | path = path_dirname(ctx, "/usr/"); 15 | ok1(streq(path, "/")); 16 | ok1(tal_parent(path) == ctx); 17 | path = path_dirname(ctx, "/usr//"); 18 | ok1(streq(path, "/")); 19 | ok1(tal_parent(path) == ctx); 20 | path = path_dirname(ctx, "usr"); 21 | ok1(streq(path, ".")); 22 | ok1(tal_parent(path) == ctx); 23 | path = path_dirname(ctx, "/"); 24 | ok1(streq(path, "/")); 25 | ok1(tal_parent(path) == ctx); 26 | path = path_dirname(ctx, "//"); 27 | ok1(streq(path, "/")); 28 | ok1(tal_parent(path) == ctx); 29 | path = path_dirname(ctx, "."); 30 | ok1(streq(path, ".")); 31 | ok1(tal_parent(path) == ctx); 32 | path = path_dirname(ctx, "./"); 33 | ok1(streq(path, ".")); 34 | ok1(tal_parent(path) == ctx); 35 | path = path_dirname(ctx, ".."); 36 | ok1(streq(path, ".")); 37 | ok1(tal_parent(path) == ctx); 38 | path = path_dirname(ctx, "../"); 39 | ok1(streq(path, ".")); 40 | ok1(tal_parent(path) == ctx); 41 | tal_free(ctx); 42 | 43 | ctx = tal_strdup(NULL, "ctx"); 44 | ok1(!tal_first(ctx)); 45 | 46 | /* Test take */ 47 | path = path_dirname(ctx, take(tal_strdup(ctx, ".."))); 48 | ok1(streq(path, ".")); 49 | ok1(tal_parent(path) == ctx); 50 | ok1(tal_first(ctx) == path && !tal_next(ctx, path)); 51 | tal_free(path); 52 | ok1(path_dirname(ctx, take(NULL)) == NULL); 53 | ok1(!tal_first(ctx)); 54 | 55 | tal_free(ctx); 56 | 57 | return exit_status(); 58 | } 59 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-ext_off.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | plan_tests(9); 8 | 9 | ok1(path_ext_off("foo") == 3); 10 | ok1(path_ext_off(".foo") == 4); 11 | ok1(path_ext_off("bar.foo") == 3); 12 | ok1(path_ext_off("bar/foo") == 7); 13 | ok1(path_ext_off("bar/.foo") == 8); 14 | ok1(path_ext_off(".bar/foo") == 8); 15 | ok1(path_ext_off("foo.bar/foo") == 11); 16 | ok1(path_ext_off("foo.bar/foo.") == 11); 17 | ok1(path_ext_off("foo.bar/foo..") == 12); 18 | return exit_status(); 19 | } 20 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-is_abs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | plan_tests(5); 8 | 9 | ok1(path_is_abs(PATH_SEP_STR "foo")); 10 | ok1(!path_is_abs("foo")); 11 | ok1(!path_is_abs("foo" PATH_SEP_STR)); 12 | 13 | ok1(path_is_abs(PATH_SEP_STR "foo" PATH_SEP_STR)); 14 | ok1(path_is_abs(PATH_SEP_STR ".")); 15 | return exit_status(); 16 | } 17 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-is_dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 11 | 12 | plan_tests(6); 13 | 14 | if (!getcwd(cwd, sizeof(cwd))) 15 | abort(); 16 | 17 | unlink("run-is_dir-dir-link"); 18 | unlink("run-is_dir-file-link"); 19 | unlink("run-is_dir-dir/file"); 20 | rmdir("run-is_dir-dir"); 21 | if (mkdir("run-is_dir-dir", 0700) != 0) 22 | abort(); 23 | if (symlink("run-is_dir-dir", "run-is_dir-dir-link") != 0) 24 | abort(); 25 | if (symlink("run-is_dir-dir/file", "run-is_dir-file-link") != 0) 26 | abort(); 27 | close(open("run-is_dir-dir/file", O_WRONLY|O_CREAT, 0600)); 28 | 29 | ok1(path_is_dir("run-is_dir-dir-link")); 30 | ok1(!path_is_dir("run-is_dir-file-link")); 31 | ok1(!path_is_dir("run-is_dir-dir/file")); 32 | ok1(path_is_dir("run-is_dir-dir")); 33 | 34 | path = path_join(ctx, cwd, "run-is_dir-dir/file"); 35 | ok1(!path_is_dir(path)); 36 | ok1(path_is_dir(cwd)); 37 | 38 | tal_free(ctx); 39 | 40 | return exit_status(); 41 | } 42 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-is_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 11 | 12 | plan_tests(7); 13 | 14 | if (!getcwd(cwd, sizeof(cwd))) 15 | abort(); 16 | 17 | unlink("run-is_file-dir-link"); 18 | unlink("run-is_file-file-link"); 19 | unlink("run-is_file-dir/file"); 20 | rmdir("run-is_file-dir"); 21 | if (mkdir("run-is_file-dir", 0700) != 0) 22 | abort(); 23 | if (symlink("run-is_file-dir", "run-is_file-dir-link") != 0) 24 | abort(); 25 | if (symlink("run-is_file-dir/file", "run-is_file-file-link") != 0) 26 | abort(); 27 | close(open("run-is_file-dir/file", O_WRONLY|O_CREAT, 0600)); 28 | 29 | ok1(!path_is_file("run-is_file-dir-link")); 30 | ok1(path_is_file("run-is_file-file-link")); 31 | ok1(path_is_file("run-is_file-dir/file")); 32 | ok1(!path_is_file("run-is_file-dir")); 33 | ok1(!path_is_file("run-is_file-nonexist")); 34 | 35 | path = path_join(ctx, cwd, "run-is_file-dir/file"); 36 | ok1(path_is_file(path)); 37 | ok1(!path_is_file(cwd)); 38 | 39 | tal_free(ctx); 40 | 41 | return exit_status(); 42 | } 43 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-pushd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | struct path_pushd *pd; 8 | char path1[1024], path2[1024], *ctx = tal_strdup(NULL, "ctx"); 9 | 10 | /* This is how many tests you plan to run */ 11 | plan_tests(19); 12 | 13 | /* Test pushd/popd */ 14 | if (!getcwd(path1, sizeof(path1))) 15 | abort(); 16 | 17 | pd = path_pushd(NULL, "non-existent-dir"); 18 | ok1(errno == ENOENT); 19 | ok1(!pd); 20 | 21 | errno = -100; 22 | pd = path_pushd(ctx, take(tal_strdup(ctx, "non-existent-dir"))); 23 | ok1(errno == ENOENT); 24 | ok1(!pd); 25 | ok1(!tal_first(ctx)); 26 | 27 | errno = -100; 28 | pd = path_pushd(ctx, take(NULL)); 29 | ok1(!pd); 30 | ok1(!tal_first(ctx)); 31 | ok1(errno == -100); 32 | 33 | pd = path_pushd(ctx, "/tmp"); 34 | ok1(pd); 35 | ok1(tal_parent(pd) == ctx); 36 | 37 | if (!getcwd(path2, sizeof(path2))) 38 | abort(); 39 | 40 | ok1(streq(path2, "/tmp")); 41 | path_popd(pd); 42 | 43 | if (!getcwd(path2, sizeof(path2))) 44 | abort(); 45 | ok1(streq(path2, path1)); 46 | 47 | pd = path_pushd(ctx, take(tal_strdup(ctx, "/tmp"))); 48 | ok1(pd); 49 | ok1(tal_parent(pd) == ctx); 50 | path_popd(pd); 51 | if (!getcwd(path2, sizeof(path2))) 52 | abort(); 53 | ok1(streq(path2, path1)); 54 | ok1(!tal_first(ctx)); 55 | 56 | /* Without fchdir, we can't push a path which no longer exists. */ 57 | if (mkdir("run-pushd-dir", 0700) != 0) 58 | abort(); 59 | if (chdir("run-pushd-dir") != 0) 60 | abort(); 61 | if (rmdir("../run-pushd-dir") != 0) 62 | abort(); 63 | 64 | pd = path_pushd(ctx, path1); 65 | #if HAVE_FCHDIR 66 | ok1(pd); 67 | ok1(path_popd(pd)); 68 | #else 69 | ok1(errno == ENOENT); 70 | ok1(!pd); 71 | #endif 72 | ok1(!tal_first(ctx)); 73 | tal_free(ctx); 74 | return exit_status(); 75 | } 76 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-readlink.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *link, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(12); 10 | 11 | unlink("run-readlink-link"); 12 | 13 | link = path_readlink(ctx, "run-readlink-link"); 14 | ok1(errno == ENOENT); 15 | ok1(!link); 16 | 17 | link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link"))); 18 | ok1(errno == ENOENT); 19 | ok1(!link); 20 | ok1(tal_first(ctx) == NULL); 21 | 22 | if (symlink("/tmp", "run-readlink-link") != 0) 23 | abort(); 24 | 25 | link = path_readlink(ctx, "run-readlink-link"); 26 | ok1(tal_parent(link) == ctx); 27 | ok1(streq(link, "/tmp")); 28 | tal_free(link); 29 | 30 | link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link"))); 31 | ok1(tal_parent(link) == ctx); 32 | ok1(streq(link, "/tmp")); 33 | ok1(tal_first(ctx) == link && tal_next(ctx, link) == NULL); 34 | 35 | unlink("run-readlink-link"); 36 | 37 | if (symlink("some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name", "run-readlink-link") != 0) 38 | abort(); 39 | 40 | link = path_readlink(ctx, "run-readlink-link"); 41 | ok1(tal_parent(link) == ctx); 42 | ok1(streq(link, "some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name")); 43 | tal_free(ctx); 44 | 45 | return exit_status(); 46 | } 47 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/path/test/run-rel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(19); 10 | 11 | if (!getcwd(cwd, sizeof(cwd))) 12 | abort(); 13 | 14 | unlink("run-rel-link"); 15 | rmdir("run-rel-foo"); 16 | if (mkdir("run-rel-foo", 0700) != 0) 17 | abort(); 18 | if (symlink("run-rel-foo", "run-rel-link") != 0) 19 | abort(); 20 | 21 | path = path_rel(ctx, ".", "run-rel-foo"); 22 | ok1(streq(path, "run-rel-foo")); 23 | ok1(tal_parent(path) == ctx); 24 | tal_free(path); 25 | 26 | path = path_rel(ctx, "run-rel-foo", "."); 27 | ok1(streq(path, "..")); 28 | ok1(tal_parent(path) == ctx); 29 | tal_free(path); 30 | 31 | path = path_rel(ctx, ".", "run-rel-link"); 32 | /* This doesn't specify whether it preserves links. */ 33 | ok1(streq(path, "run-rel-link") || streq(path, "run-rel-foo")); 34 | ok1(tal_parent(path) == ctx); 35 | tal_free(path); 36 | 37 | path = path_rel(ctx, "/", "."); 38 | ok1(streq(path, cwd + 1)); 39 | ok1(tal_parent(path) == ctx); 40 | tal_free(path); 41 | 42 | path = path_rel(ctx, "run-rel-foo", "run-rel-foo"); 43 | ok1(streq(path, ".")); 44 | ok1(tal_parent(path) == ctx); 45 | tal_free(path); 46 | 47 | path = path_rel(ctx, take(tal_strdup(ctx, ".")), "run-rel-foo"); 48 | ok1(streq(path, "run-rel-foo")); 49 | ok1(tal_parent(path) == ctx); 50 | tal_free(path); 51 | ok1(tal_first(ctx) == NULL); 52 | 53 | path = path_rel(ctx, ".", take(tal_strdup(ctx, "run-rel-foo"))); 54 | ok1(streq(path, "run-rel-foo")); 55 | ok1(tal_parent(path) == ctx); 56 | tal_free(path); 57 | ok1(tal_first(ctx) == NULL); 58 | 59 | path = path_rel(ctx, take(tal_strdup(ctx, ".")), 60 | take(tal_strdup(ctx, "run-rel-foo"))); 61 | ok1(streq(path, "run-rel-foo")); 62 | ok1(tal_parent(path) == ctx); 63 | tal_free(path); 64 | ok1(tal_first(ctx) == NULL); 65 | 66 | tal_free(ctx); 67 | 68 | return exit_status(); 69 | } 70 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-basename.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(26); 10 | 11 | path = path_basename(ctx, "/usr/lib"); 12 | ok1(streq(path, "lib")); 13 | ok1(tal_parent(path) == ctx); 14 | path = path_basename(ctx, "/usr/"); 15 | ok1(streq(path, "usr")); 16 | ok1(tal_parent(path) == ctx); 17 | path = path_basename(ctx, "/usr//"); 18 | ok1(streq(path, "usr")); 19 | ok1(tal_parent(path) == ctx); 20 | path = path_basename(ctx, "usr"); 21 | ok1(streq(path, "usr")); 22 | ok1(tal_parent(path) == ctx); 23 | path = path_basename(ctx, "/"); 24 | ok1(streq(path, "/")); 25 | ok1(tal_parent(path) == ctx); 26 | path = path_basename(ctx, "//"); 27 | ok1(streq(path, "/")); 28 | ok1(tal_parent(path) == ctx); 29 | path = path_basename(ctx, "."); 30 | ok1(streq(path, ".")); 31 | ok1(tal_parent(path) == ctx); 32 | path = path_basename(ctx, "./"); 33 | ok1(streq(path, ".")); 34 | ok1(tal_parent(path) == ctx); 35 | path = path_basename(ctx, ".."); 36 | ok1(streq(path, "..")); 37 | ok1(tal_parent(path) == ctx); 38 | path = path_basename(ctx, "../"); 39 | ok1(streq(path, "..")); 40 | ok1(tal_parent(path) == ctx); 41 | tal_free(ctx); 42 | 43 | ctx = tal_strdup(NULL, "ctx"); 44 | ok1(!tal_first(ctx)); 45 | 46 | /* Test take */ 47 | path = path_basename(ctx, take(tal_strdup(ctx, ".."))); 48 | ok1(streq(path, "..")); 49 | ok1(tal_parent(path) == ctx); 50 | ok1(tal_first(ctx) == path && !tal_next(path)); 51 | tal_free(path); 52 | ok1(path_basename(ctx, take(NULL)) == NULL); 53 | ok1(!tal_first(ctx)); 54 | 55 | tal_free(ctx); 56 | 57 | return exit_status(); 58 | } 59 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-canon.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char cwd[1024], *path, *path2, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(15); 10 | 11 | if (!getcwd(cwd, sizeof(cwd))) 12 | abort(); 13 | 14 | unlink("run-canon-link"); 15 | rmdir("run-canon-foo"); 16 | if (mkdir("run-canon-foo", 0700) != 0) 17 | abort(); 18 | if (symlink("run-canon-foo", "run-canon-link") != 0) 19 | abort(); 20 | 21 | path = path_canon(ctx, "run-canon-foo"); 22 | ok1(tal_parent(path) == ctx); 23 | ok1(strends(path, "run-canon-foo")); 24 | ok1(strstarts(path, cwd)); 25 | ok1(path[strlen(cwd)] == PATH_SEP); 26 | ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo")); 27 | tal_free(path); 28 | 29 | ok1(!path_canon(ctx, take(NULL))); 30 | ok1(tal_first(ctx) == NULL); 31 | 32 | /* Test take doesn't leak. */ 33 | ok1(tal_first(ctx) == NULL); 34 | path = path_canon(ctx, take(tal_strdup(ctx, "run-canon-foo"))); 35 | ok1(strends(path, "run-canon-foo")); 36 | ok1(strstarts(path, cwd)); 37 | ok1(path[strlen(cwd)] == PATH_SEP); 38 | ok1(strlen(path) == strlen(cwd) + 1 + strlen("run-canon-foo")); 39 | ok1(tal_first(ctx) == path && tal_next(path) == NULL); 40 | path2 = path_canon(ctx, "run-canon-link"); 41 | ok1(streq(path2, path)); 42 | 43 | unlink("run-canon-link"); 44 | if (symlink(".", "run-canon-link") != 0) 45 | abort(); 46 | 47 | path = path_canon(ctx, "run-canon-link"); 48 | ok1(streq(path, cwd)); 49 | 50 | tal_free(ctx); 51 | 52 | return exit_status(); 53 | } 54 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-cwd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char path1[1024], *cwd, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | /* This is how many tests you plan to run */ 10 | plan_tests(5); 11 | 12 | if (!getcwd(path1, sizeof(path1))) 13 | abort(); 14 | 15 | cwd = path_cwd(ctx); 16 | ok1(cwd); 17 | ok1(tal_parent(cwd) == ctx); 18 | tal_free(cwd); 19 | 20 | rmdir("run-cwd-long-long-long-name/bar-long-long-long-long-name"); 21 | rmdir("run-cwd-long-long-long-name"); 22 | if (mkdir("run-cwd-long-long-long-name", 0700) != 0) 23 | abort(); 24 | if (mkdir("run-cwd-long-long-long-name/bar-long-long-long-long-name", 0700) != 0) 25 | abort(); 26 | if (chdir("run-cwd-long-long-long-name/bar-long-long-long-long-name") != 0) 27 | abort(); 28 | 29 | cwd = path_cwd(ctx); 30 | ok1(cwd); 31 | ok1(tal_parent(cwd) == ctx); 32 | ok1(strends(cwd, 33 | "run-cwd-long-long-long-name/bar-long-long-long-long-name")); 34 | tal_free(ctx); 35 | 36 | return exit_status(); 37 | } 38 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-dirname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(26); 10 | 11 | path = path_dirname(ctx, "/usr/lib"); 12 | ok1(streq(path, "/usr")); 13 | ok1(tal_parent(path) == ctx); 14 | path = path_dirname(ctx, "/usr/"); 15 | ok1(streq(path, "/")); 16 | ok1(tal_parent(path) == ctx); 17 | path = path_dirname(ctx, "/usr//"); 18 | ok1(streq(path, "/")); 19 | ok1(tal_parent(path) == ctx); 20 | path = path_dirname(ctx, "usr"); 21 | ok1(streq(path, ".")); 22 | ok1(tal_parent(path) == ctx); 23 | path = path_dirname(ctx, "/"); 24 | ok1(streq(path, "/")); 25 | ok1(tal_parent(path) == ctx); 26 | path = path_dirname(ctx, "//"); 27 | ok1(streq(path, "/")); 28 | ok1(tal_parent(path) == ctx); 29 | path = path_dirname(ctx, "."); 30 | ok1(streq(path, ".")); 31 | ok1(tal_parent(path) == ctx); 32 | path = path_dirname(ctx, "./"); 33 | ok1(streq(path, ".")); 34 | ok1(tal_parent(path) == ctx); 35 | path = path_dirname(ctx, ".."); 36 | ok1(streq(path, ".")); 37 | ok1(tal_parent(path) == ctx); 38 | path = path_dirname(ctx, "../"); 39 | ok1(streq(path, ".")); 40 | ok1(tal_parent(path) == ctx); 41 | tal_free(ctx); 42 | 43 | ctx = tal_strdup(NULL, "ctx"); 44 | ok1(!tal_first(ctx)); 45 | 46 | /* Test take */ 47 | path = path_dirname(ctx, take(tal_strdup(ctx, ".."))); 48 | ok1(streq(path, ".")); 49 | ok1(tal_parent(path) == ctx); 50 | ok1(tal_first(ctx) == path && !tal_next(path)); 51 | tal_free(path); 52 | ok1(path_dirname(ctx, take(NULL)) == NULL); 53 | ok1(!tal_first(ctx)); 54 | 55 | tal_free(ctx); 56 | 57 | return exit_status(); 58 | } 59 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-ext_off.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | plan_tests(9); 8 | 9 | ok1(path_ext_off("foo") == 3); 10 | ok1(path_ext_off(".foo") == 4); 11 | ok1(path_ext_off("bar.foo") == 3); 12 | ok1(path_ext_off("bar/foo") == 7); 13 | ok1(path_ext_off("bar/.foo") == 8); 14 | ok1(path_ext_off(".bar/foo") == 8); 15 | ok1(path_ext_off("foo.bar/foo") == 11); 16 | ok1(path_ext_off("foo.bar/foo.") == 11); 17 | ok1(path_ext_off("foo.bar/foo..") == 12); 18 | return exit_status(); 19 | } 20 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-is_abs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | plan_tests(5); 8 | 9 | ok1(path_is_abs(PATH_SEP_STR "foo")); 10 | ok1(!path_is_abs("foo")); 11 | ok1(!path_is_abs("foo" PATH_SEP_STR)); 12 | 13 | ok1(path_is_abs(PATH_SEP_STR "foo" PATH_SEP_STR)); 14 | ok1(path_is_abs(PATH_SEP_STR ".")); 15 | return exit_status(); 16 | } 17 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-is_dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 11 | 12 | plan_tests(6); 13 | 14 | if (!getcwd(cwd, sizeof(cwd))) 15 | abort(); 16 | 17 | unlink("run-is_dir-dir-link"); 18 | unlink("run-is_dir-file-link"); 19 | unlink("run-is_dir-dir/file"); 20 | rmdir("run-is_dir-dir"); 21 | if (mkdir("run-is_dir-dir", 0700) != 0) 22 | abort(); 23 | if (symlink("run-is_dir-dir", "run-is_dir-dir-link") != 0) 24 | abort(); 25 | if (symlink("run-is_dir-dir/file", "run-is_dir-file-link") != 0) 26 | abort(); 27 | close(open("run-is_dir-dir/file", O_WRONLY|O_CREAT, 0600)); 28 | 29 | ok1(path_is_dir("run-is_dir-dir-link")); 30 | ok1(!path_is_dir("run-is_dir-file-link")); 31 | ok1(!path_is_dir("run-is_dir-dir/file")); 32 | ok1(path_is_dir("run-is_dir-dir")); 33 | 34 | path = path_join(ctx, cwd, "run-is_dir-dir/file"); 35 | ok1(!path_is_dir(path)); 36 | ok1(path_is_dir(cwd)); 37 | 38 | tal_free(ctx); 39 | 40 | return exit_status(); 41 | } 42 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-is_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 11 | 12 | plan_tests(7); 13 | 14 | if (!getcwd(cwd, sizeof(cwd))) 15 | abort(); 16 | 17 | unlink("run-is_file-dir-link"); 18 | unlink("run-is_file-file-link"); 19 | unlink("run-is_file-dir/file"); 20 | rmdir("run-is_file-dir"); 21 | if (mkdir("run-is_file-dir", 0700) != 0) 22 | abort(); 23 | if (symlink("run-is_file-dir", "run-is_file-dir-link") != 0) 24 | abort(); 25 | if (symlink("run-is_file-dir/file", "run-is_file-file-link") != 0) 26 | abort(); 27 | close(open("run-is_file-dir/file", O_WRONLY|O_CREAT, 0600)); 28 | 29 | ok1(!path_is_file("run-is_file-dir-link")); 30 | ok1(path_is_file("run-is_file-file-link")); 31 | ok1(path_is_file("run-is_file-dir/file")); 32 | ok1(!path_is_file("run-is_file-dir")); 33 | ok1(!path_is_file("run-is_file-nonexist")); 34 | 35 | path = path_join(ctx, cwd, "run-is_file-dir/file"); 36 | ok1(path_is_file(path)); 37 | ok1(!path_is_file(cwd)); 38 | 39 | tal_free(ctx); 40 | 41 | return exit_status(); 42 | } 43 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-pushd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | struct path_pushd *pd; 8 | char path1[1024], path2[1024], *ctx = tal_strdup(NULL, "ctx"); 9 | 10 | /* This is how many tests you plan to run */ 11 | plan_tests(19); 12 | 13 | /* Test pushd/popd */ 14 | if (!getcwd(path1, sizeof(path1))) 15 | abort(); 16 | 17 | pd = path_pushd(NULL, "non-existent-dir"); 18 | ok1(errno == ENOENT); 19 | ok1(!pd); 20 | 21 | errno = -100; 22 | pd = path_pushd(ctx, take(tal_strdup(ctx, "non-existent-dir"))); 23 | ok1(errno == ENOENT); 24 | ok1(!pd); 25 | ok1(!tal_first(ctx)); 26 | 27 | errno = -100; 28 | pd = path_pushd(ctx, take(NULL)); 29 | ok1(!pd); 30 | ok1(!tal_first(ctx)); 31 | ok1(errno == -100); 32 | 33 | pd = path_pushd(ctx, "/tmp"); 34 | ok1(pd); 35 | ok1(tal_parent(pd) == ctx); 36 | 37 | if (!getcwd(path2, sizeof(path2))) 38 | abort(); 39 | 40 | ok1(streq(path2, "/tmp")); 41 | path_popd(pd); 42 | 43 | if (!getcwd(path2, sizeof(path2))) 44 | abort(); 45 | ok1(streq(path2, path1)); 46 | 47 | pd = path_pushd(ctx, take(tal_strdup(ctx, "/tmp"))); 48 | ok1(pd); 49 | ok1(tal_parent(pd) == ctx); 50 | path_popd(pd); 51 | if (!getcwd(path2, sizeof(path2))) 52 | abort(); 53 | ok1(streq(path2, path1)); 54 | ok1(!tal_first(ctx)); 55 | 56 | /* Without fchdir, we can't push a path which no longer exists. */ 57 | if (mkdir("run-pushd-dir", 0700) != 0) 58 | abort(); 59 | if (chdir("run-pushd-dir") != 0) 60 | abort(); 61 | if (rmdir("../run-pushd-dir") != 0) 62 | abort(); 63 | 64 | pd = path_pushd(ctx, path1); 65 | #if HAVE_FCHDIR 66 | ok1(pd); 67 | ok1(path_popd(pd)); 68 | #else 69 | ok1(errno == ENOENT); 70 | ok1(!pd); 71 | #endif 72 | ok1(!tal_first(ctx)); 73 | tal_free(ctx); 74 | return exit_status(); 75 | } 76 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-readlink.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *link, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(12); 10 | 11 | unlink("run-readlink-link"); 12 | 13 | link = path_readlink(ctx, "run-readlink-link"); 14 | ok1(errno == ENOENT); 15 | ok1(!link); 16 | 17 | link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link"))); 18 | ok1(errno == ENOENT); 19 | ok1(!link); 20 | ok1(tal_first(ctx) == NULL); 21 | 22 | if (symlink("/tmp", "run-readlink-link") != 0) 23 | abort(); 24 | 25 | link = path_readlink(ctx, "run-readlink-link"); 26 | ok1(tal_parent(link) == ctx); 27 | ok1(streq(link, "/tmp")); 28 | tal_free(link); 29 | 30 | link = path_readlink(ctx, take(tal_strdup(ctx, "run-readlink-link"))); 31 | ok1(tal_parent(link) == ctx); 32 | ok1(streq(link, "/tmp")); 33 | ok1(tal_first(ctx) == link && tal_next(link) == NULL && tal_first(link) == NULL); 34 | 35 | unlink("run-readlink-link"); 36 | 37 | if (symlink("some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name", "run-readlink-link") != 0) 38 | abort(); 39 | 40 | link = path_readlink(ctx, "run-readlink-link"); 41 | ok1(tal_parent(link) == ctx); 42 | ok1(streq(link, "some-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-name")); 43 | tal_free(ctx); 44 | 45 | return exit_status(); 46 | } 47 | -------------------------------------------------------------------------------- /ccan/ccan/tal/path/test/run-rel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx"); 8 | 9 | plan_tests(19); 10 | 11 | if (!getcwd(cwd, sizeof(cwd))) 12 | abort(); 13 | 14 | unlink("run-rel-link"); 15 | rmdir("run-rel-foo"); 16 | if (mkdir("run-rel-foo", 0700) != 0) 17 | abort(); 18 | if (symlink("run-rel-foo", "run-rel-link") != 0) 19 | abort(); 20 | 21 | path = path_rel(ctx, ".", "run-rel-foo"); 22 | ok1(streq(path, "run-rel-foo")); 23 | ok1(tal_parent(path) == ctx); 24 | tal_free(path); 25 | 26 | path = path_rel(ctx, "run-rel-foo", "."); 27 | ok1(streq(path, "..")); 28 | ok1(tal_parent(path) == ctx); 29 | tal_free(path); 30 | 31 | path = path_rel(ctx, ".", "run-rel-link"); 32 | /* This doesn't specify whether it preserves links. */ 33 | ok1(streq(path, "run-rel-link") || streq(path, "run-rel-foo")); 34 | ok1(tal_parent(path) == ctx); 35 | tal_free(path); 36 | 37 | path = path_rel(ctx, "/", "."); 38 | ok1(streq(path, cwd + 1)); 39 | ok1(tal_parent(path) == ctx); 40 | tal_free(path); 41 | 42 | path = path_rel(ctx, "run-rel-foo", "run-rel-foo"); 43 | ok1(streq(path, ".")); 44 | ok1(tal_parent(path) == ctx); 45 | tal_free(path); 46 | 47 | path = path_rel(ctx, take(tal_strdup(ctx, ".")), "run-rel-foo"); 48 | ok1(streq(path, "run-rel-foo")); 49 | ok1(tal_parent(path) == ctx); 50 | tal_free(path); 51 | ok1(tal_first(ctx) == NULL); 52 | 53 | path = path_rel(ctx, ".", take(tal_strdup(ctx, "run-rel-foo"))); 54 | ok1(streq(path, "run-rel-foo")); 55 | ok1(tal_parent(path) == ctx); 56 | tal_free(path); 57 | ok1(tal_first(ctx) == NULL); 58 | 59 | path = path_rel(ctx, take(tal_strdup(ctx, ".")), 60 | take(tal_strdup(ctx, "run-rel-foo"))); 61 | ok1(streq(path, "run-rel-foo")); 62 | ok1(tal_parent(path) == ctx); 63 | tal_free(path); 64 | ok1(tal_first(ctx) == NULL); 65 | 66 | tal_free(ctx); 67 | 68 | return exit_status(); 69 | } 70 | -------------------------------------------------------------------------------- /ccan/ccan/tal/stack-Makefile: -------------------------------------------------------------------------------- 1 | ccan/tal/stack-Makefile: ccan/tal/stack/stack.c ccan/tal/stack/stack.h ccan/tal/stack/_info 2 | ccan/tal/stack.o: ccan/tal/stack/stack.o 3 | -------------------------------------------------------------------------------- /ccan/ccan/tal/stack/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/stack/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/stack - stack of tal contexts (inspired by talloc_stack) 7 | * 8 | * Implement a stack of tal contexts. A new (empty) context is pushed on top 9 | * of the stack using tal_newframe and it is popped/freed using tal_free(). 10 | * tal_curframe() can be used to get the stack's top context. 11 | * 12 | * tal_stack can be used to implement per-function temporary allocation context 13 | * to help mitigating memory leaks, but unlike the plain tal approach it does not 14 | * require the caller to pass a destination context for returning allocated 15 | * values. Instead, allocated values are moved to the parent context using 16 | * tal_steal(tal_parent(tmp_ctx), ptr). 17 | * 18 | * Example: 19 | * #include 20 | * #include 21 | * 22 | * static int *do_work(void) 23 | * { 24 | * int *retval = NULL; 25 | * tal_t *tmp_ctx = tal_newframe(); 26 | * 27 | * int *val = talz(tmp_ctx, int); 28 | * assert(val != NULL); 29 | * 30 | * // ... do something with val ... 31 | * 32 | * if (retval >= 0) { 33 | * // steal to parent cxt so it survives tal_free() 34 | * tal_steal(tal_parent(tmp_ctx), val); 35 | * retval = val; 36 | * } 37 | * tal_free(tmp_ctx); 38 | * return retval; 39 | * } 40 | * 41 | * int main(void) 42 | * { 43 | * tal_t *tmp_ctx = tal_newframe(); 44 | * int *val = do_work(); 45 | * if (val) { 46 | * // ... do something with val ... 47 | * } 48 | * // val is eventually freed 49 | * tal_free(tmp_ctx); 50 | * return 0; 51 | * } 52 | * 53 | * License: BSD-MIT 54 | * Author: Delio Brignoli 55 | */ 56 | int main(int argc, char *argv[]) 57 | { 58 | /* Expect exactly one argument */ 59 | if (argc != 2) 60 | return 1; 61 | 62 | if (strcmp(argv[1], "depends") == 0) { 63 | printf("ccan/tal\n"); 64 | return 0; 65 | } 66 | 67 | return 1; 68 | } 69 | -------------------------------------------------------------------------------- /ccan/ccan/tal/stack/stack.c: -------------------------------------------------------------------------------- 1 | /* Licensed under BSD-MIT - see LICENSE file for details */ 2 | 3 | #include 4 | #include 5 | 6 | static tal_t *h = NULL; 7 | 8 | static void _free_frame(tal_t *o) 9 | { 10 | h = tal_parent(o); 11 | } 12 | 13 | tal_t *tal_newframe_(const char *label) 14 | { 15 | h = tal_label(h, char, label); 16 | assert(h != NULL); 17 | tal_add_destructor(h, _free_frame); 18 | return h; 19 | } 20 | 21 | tal_t *tal_curframe(void) 22 | { 23 | return h; 24 | } 25 | -------------------------------------------------------------------------------- /ccan/ccan/tal/stack/stack.h: -------------------------------------------------------------------------------- 1 | /* Licensed under BSD-MIT - see LICENSE file for details */ 2 | #ifndef CCAN_TAL_STACK_H 3 | #define CCAN_TAL_STACK_H 4 | 5 | #include 6 | 7 | /** 8 | * tal_newframe - allocate and return a new nested tal context 9 | * 10 | * Allocates and push a new tal context on top of the stack. 11 | * The context must be freed using tal_free() which will also pop it 12 | * off the stack, which will also free all its nested contexts, if any. 13 | * 14 | * NOTE: this function is not threadsafe. 15 | * 16 | * Example: 17 | * tal_t *ctx = tal_newframe(); 18 | * // ... do something with ctx ... 19 | * tal_free(ctx); 20 | */ 21 | #define tal_newframe(void) tal_newframe_(TAL_LABEL(tal_stack, "")); 22 | 23 | tal_t *tal_newframe_(const char *label); 24 | 25 | /** 26 | * tal_curframe - return the current 'tal_stack frame' 27 | * 28 | * Returns the context currently on top of the stack. The initial context 29 | * (before any tal_newframe() call) is the tal 'NULL' context. 30 | * 31 | * NOTE: this function is not threadsafe. 32 | */ 33 | tal_t *tal_curframe(void); 34 | #endif /* CCAN_TAL_STACK_H */ 35 | -------------------------------------------------------------------------------- /ccan/ccan/tal/stack/test/run-stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | tal_t *parent, *cur; 8 | 9 | plan_tests(8); 10 | 11 | /* initial frame is NULL */ 12 | ok1(tal_curframe() == NULL); 13 | 14 | /* create new frame and make sure all is OK */ 15 | cur = tal_newframe(); 16 | ok1(tal_curframe() == cur); 17 | ok1(tal_parent(cur) == NULL); 18 | 19 | /* create another frame */ 20 | parent = cur; 21 | cur = tal_newframe(); 22 | ok1(tal_curframe() == cur); 23 | ok1(tal_parent(cur) == parent); 24 | 25 | /* unwind */ 26 | tal_free(cur); 27 | ok1(tal_curframe() == parent); 28 | cur = tal_curframe(); 29 | ok1(tal_parent(cur) == NULL); 30 | tal_free(cur); 31 | ok1(tal_curframe() == NULL); 32 | 33 | tal_cleanup(); 34 | return exit_status(); 35 | } 36 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str-Makefile: -------------------------------------------------------------------------------- 1 | ccan/tal/str-Makefile: ccan/tal/str/str.h ccan/tal/str/str.c ccan/tal/str/_info 2 | ccan/tal/str.o: ccan/tal/str/str.o 3 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/str/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/str - string helper routines which use tal 7 | * 8 | * This is a grab bag of functions for string operations, designed to enhance 9 | * the standard string.h; these are separated from the non-tal-needing 10 | * string utilities in "str.h". Each string created by this library 11 | * will have tal_count() equal to strlen() + 1 (assuming you didn't create 12 | * a string containing a NUL, such as using tal_fmt("%c", 0)). 13 | * 14 | * Example: 15 | * #include 16 | * #include 17 | * #include 18 | * 19 | * // Dumb demo program to double-linespace a file. 20 | * int main(int argc, char *argv[]) 21 | * { 22 | * char *textfile; 23 | * char **lines; 24 | * 25 | * if (argc > 2) 26 | * errx(1, "Takes 0 or 1 arguments"); 27 | * // Grab lines in file. 28 | * textfile = grab_file(NULL, argv[1]); 29 | * if (!textfile) 30 | * err(1, "Failed reading %s", argv[1]); 31 | * lines = tal_strsplit(textfile, textfile, "\n", STR_EMPTY_OK); 32 | * 33 | * // Join them back together with two linefeeds. 34 | * printf("%s", tal_strjoin(textfile, lines, "\n\n", STR_TRAIL)); 35 | * 36 | * // Free everything, just because we can. 37 | * tal_free(textfile); 38 | * return 0; 39 | * } 40 | * 41 | * License: BSD-MIT 42 | * Author: Rusty Russell 43 | */ 44 | int main(int argc, char *argv[]) 45 | { 46 | if (argc != 2) 47 | return 1; 48 | 49 | if (strcmp(argv[1], "depends") == 0) { 50 | printf("ccan/str\n"); 51 | #ifdef TAL_USE_TALLOC 52 | printf("ccan/tal/talloc\n"); 53 | #else 54 | printf("ccan/tal\n"); 55 | #endif 56 | printf("ccan/take\n"); 57 | return 0; 58 | } 59 | 60 | return 1; 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/str/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/BSD-MIT -------------------------------------------------------------------------------- /ccan/ccan/tal/str/str/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/str - string helper routines which use tal 7 | * 8 | * This is a grab bag of functions for string operations, designed to enhance 9 | * the standard string.h; these are separated from the non-tal-needing 10 | * string utilities in "str.h". 11 | * 12 | * Example: 13 | * #include 14 | * #include 15 | * #include 16 | * 17 | * // Dumb demo program to double-linespace a file. 18 | * int main(int argc, char *argv[]) 19 | * { 20 | * char *textfile; 21 | * char **lines; 22 | * 23 | * // Grab lines in file. 24 | * textfile = grab_file(NULL, argv[1]); 25 | * if (!textfile) 26 | * err(1, "Failed reading %s", argv[1]); 27 | * lines = tal_strsplit(textfile, textfile, "\n", STR_EMPTY_OK); 28 | * 29 | * // Join them back together with two linefeeds. 30 | * printf("%s", tal_strjoin(textfile, lines, "\n\n", STR_TRAIL)); 31 | * 32 | * // Free everything, just because we can. 33 | * tal_free(textfile); 34 | * return 0; 35 | * } 36 | * 37 | * License: BSD-MIT 38 | * Author: Rusty Russell 39 | */ 40 | int main(int argc, char *argv[]) 41 | { 42 | if (argc != 2) 43 | return 1; 44 | 45 | if (strcmp(argv[1], "depends") == 0) { 46 | printf("ccan/str\n"); 47 | #ifdef TAL_USE_TALLOC 48 | printf("ccan/tal/talloc\n"); 49 | #else 50 | printf("ccan/tal\n"); 51 | #endif 52 | printf("ccan/take\n"); 53 | return 0; 54 | } 55 | 56 | return 1; 57 | } 58 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/str/test/helper.h: -------------------------------------------------------------------------------- 1 | /* tal/talloc can't implement tal_first/tal_next. */ 2 | #ifdef TAL_USE_TALLOC 3 | static inline bool no_children(const void *ctx) 4 | { 5 | return talloc_total_blocks(ctx) == 1; 6 | } 7 | 8 | static inline bool single_child(const void *ctx, const void *child) 9 | { 10 | return talloc_total_blocks(ctx) == 2 && tal_parent(child) == ctx; 11 | } 12 | #else 13 | static inline bool no_children(const void *ctx) 14 | { 15 | return !tal_first(ctx); 16 | } 17 | 18 | static inline bool single_child(const void *ctx, const void *child) 19 | { 20 | return tal_first(ctx) == child && !tal_next(ctx, child); 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/str/test/run-take.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "helper.h" 5 | 6 | int main(void) 7 | { 8 | char *parent, *c; 9 | 10 | plan_tests(14); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | c = tal_strdup(parent, "hello"); 16 | 17 | c = tal_strdup(parent, take(c)); 18 | ok1(strcmp(c, "hello") == 0); 19 | ok1(tal_parent(c) == parent); 20 | 21 | c = tal_strndup(parent, take(c), 5); 22 | ok1(strcmp(c, "hello") == 0); 23 | ok1(tal_parent(c) == parent); 24 | 25 | c = tal_strndup(parent, take(c), 3); 26 | ok1(strcmp(c, "hel") == 0); 27 | ok1(tal_parent(c) == parent); 28 | tal_free(c); 29 | 30 | c = tal_strdup(parent, "hello %s"); 31 | c = tal_fmt(parent, take(c), "there"); 32 | ok1(strcmp(c, "hello there") == 0); 33 | ok1(tal_parent(c) == parent); 34 | /* No leftover allocations. */ 35 | tal_free(c); 36 | ok1(no_children(parent)); 37 | 38 | tal_free(parent); 39 | ok1(!taken_any()); 40 | 41 | /* NULL pass-through. */ 42 | c = NULL; 43 | ok1(tal_strdup(NULL, take(c)) == NULL); 44 | ok1(tal_strndup(NULL, take(c), 5) == NULL); 45 | ok1(tal_fmt(NULL, take(c), 0) == NULL); 46 | 47 | return exit_status(); 48 | } 49 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/test/helper.h: -------------------------------------------------------------------------------- 1 | /* tal/talloc can't implement tal_first/tal_next. */ 2 | #ifdef TAL_USE_TALLOC 3 | static inline bool no_children(const void *ctx) 4 | { 5 | return talloc_total_blocks(ctx) == 1; 6 | } 7 | 8 | static inline bool single_child(const void *ctx, const void *child) 9 | { 10 | return talloc_total_blocks(ctx) == 2 && tal_parent(child) == ctx; 11 | } 12 | #else 13 | static inline bool no_children(const void *ctx) 14 | { 15 | return !tal_first(ctx); 16 | } 17 | 18 | static inline bool single_child(const void *ctx, const void *child) 19 | { 20 | return tal_first(ctx) == child && !tal_next(child) && !tal_first(child); 21 | } 22 | #endif 23 | -------------------------------------------------------------------------------- /ccan/ccan/tal/str/test/run-take.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "helper.h" 5 | 6 | int main(void) 7 | { 8 | char *parent, *c; 9 | 10 | plan_tests(14); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | c = tal_strdup(parent, "hello"); 16 | 17 | c = tal_strdup(parent, take(c)); 18 | ok1(strcmp(c, "hello") == 0); 19 | ok1(tal_parent(c) == parent); 20 | 21 | c = tal_strndup(parent, take(c), 5); 22 | ok1(strcmp(c, "hello") == 0); 23 | ok1(tal_parent(c) == parent); 24 | 25 | c = tal_strndup(parent, take(c), 3); 26 | ok1(strcmp(c, "hel") == 0); 27 | ok1(tal_parent(c) == parent); 28 | tal_free(c); 29 | 30 | c = tal_strdup(parent, "hello %s"); 31 | c = tal_fmt(parent, take(c), "there"); 32 | ok1(strcmp(c, "hello there") == 0); 33 | ok1(tal_parent(c) == parent); 34 | /* No leftover allocations. */ 35 | tal_free(c); 36 | ok1(no_children(parent)); 37 | 38 | tal_free(parent); 39 | ok1(!taken_any()); 40 | 41 | /* NULL pass-through. */ 42 | c = NULL; 43 | ok1(tal_strdup(NULL, take(c)) == NULL); 44 | ok1(tal_strndup(NULL, take(c), 5) == NULL); 45 | ok1(tal_fmt(NULL, take(c), 0) == NULL); 46 | 47 | return exit_status(); 48 | } 49 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/LGPL-3 -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/talloc - an implementation of the tal interface in terms of talloc. 7 | * 8 | * Tal and talloc are both hierarchical allocators, but have different APIs. 9 | * The tal API is mostly a subset of talloc, but if your project already 10 | * uses talloc then having both tal and talloc pointers is confusing, and 11 | * a waste of resources. 12 | * 13 | * The standard convention to tell ccan modules to use this instead of 14 | * ccan/tal is to define TAL_USE_TALLOC, usually on the commandline. 15 | * 16 | * Bugs: 17 | * tal_first() and tal_next() can't be implemented. 18 | * tal_set_backend() can only change the error function. 19 | * 20 | * License: LGPL 21 | */ 22 | int main(int argc, char *argv[]) 23 | { 24 | if (argc != 2) 25 | return 1; 26 | 27 | if (strcmp(argv[1], "depends") == 0) { 28 | printf("ccan/take\n"); 29 | printf("ccan/typesafe_cb\n"); 30 | printf("ccan/compiler\n"); 31 | printf("ccan/likely\n"); 32 | printf("ccan/str\n"); 33 | printf("talloc\n"); 34 | return 0; 35 | } 36 | 37 | if (strcmp(argv[1], "libs") == 0) { 38 | printf("talloc\n"); 39 | return 0; 40 | } 41 | 42 | return 1; 43 | } 44 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/LICENSE: -------------------------------------------------------------------------------- 1 | ../../../licenses/LGPL-3 -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/_info: -------------------------------------------------------------------------------- 1 | #include "config.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * tal/talloc - an implementation of the tal interface in terms of talloc. 7 | * 8 | * Tal and talloc are both hierarchical allocators, but have different APIs. 9 | * The tal API is mostly a subset of talloc, but if your project already 10 | * uses talloc then having both tal and talloc pointers is confusing, and 11 | * a waste of resources. 12 | * 13 | * The standard convention to tell ccan modules to use this instead of 14 | * ccan/tal is to define TAL_USE_TALLOC, usually on the commandline. 15 | * 16 | * Bugs: 17 | * tal_first() and tal_next() can't be implemented. 18 | * tal_set_backend() can only change the error function. 19 | * 20 | * License: LGPL 21 | */ 22 | int main(int argc, char *argv[]) 23 | { 24 | if (argc != 2) 25 | return 1; 26 | 27 | if (strcmp(argv[1], "depends") == 0) { 28 | printf("ccan/take\n"); 29 | printf("ccan/typesafe_cb\n"); 30 | printf("ccan/compiler\n"); 31 | printf("ccan/likely\n"); 32 | printf("ccan/str\n"); 33 | printf("talloc\n"); 34 | return 0; 35 | } 36 | 37 | if (strcmp(argv[1], "libs") == 0) { 38 | printf("talloc\n"); 39 | return 0; 40 | } 41 | 42 | return 1; 43 | } 44 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4]; 8 | int i; 9 | 10 | plan_tests(11); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | /* Zeroing allocations. */ 16 | for (i = 0; i < 4; i++) { 17 | c[i] = talz(parent, char); 18 | ok1(*c[i] == '\0'); 19 | tal_free(c[i]); 20 | } 21 | 22 | /* Array allocation. */ 23 | for (i = 0; i < 4; i++) { 24 | c[i] = tal_arr(parent, char, 4); 25 | strcpy(c[i], "abc"); 26 | tal_free(c[i]); 27 | } 28 | 29 | /* Zeroing array allocation. */ 30 | for (i = 0; i < 4; i++) { 31 | c[i] = tal_arrz(parent, char, 4); 32 | ok1(!c[i][0] && !c[i][1] && !c[i][2] && !c[i][3]); 33 | strcpy(c[i], "abc"); 34 | tal_free(c[i]); 35 | } 36 | 37 | /* Resizing. */ 38 | c[0] = tal_arrz(parent, char, 4); 39 | ok1(tal_resize(&c[0], 6)); 40 | strcpy(c[0], "hello"); 41 | tal_free(c[0]); 42 | ok1(talloc_total_blocks(parent) == 1); 43 | tal_free(parent); 44 | 45 | return exit_status(); 46 | } 47 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-count.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *p1, *p2; 8 | 9 | plan_tests(12); 10 | 11 | p1 = tal(NULL, char); 12 | ok1(p1); 13 | ok1(tal_count(p1) == 1); 14 | 15 | p2 = tal_arr(p1, char, 1); 16 | ok1(p2); 17 | ok1(tal_count(p2) == 1); 18 | ok1(tal_resize(&p2, 2)); 19 | ok1(tal_count(p2) == 2); 20 | ok1(tal_check(NULL, NULL)); 21 | tal_free(p2); 22 | 23 | p2 = tal_arrz(p1, char, 7); 24 | ok1(p2); 25 | ok1(tal_count(p2) == 7); 26 | ok1(tal_resize(&p2, 0)); 27 | ok1(tal_count(p2) == 0); 28 | ok1(tal_check(NULL, NULL)); 29 | tal_free(p2); 30 | tal_free(p1); 31 | 32 | return exit_status(); 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-destructor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static char *parent, *child; 6 | static int destroy_count; 7 | 8 | /* Parent gets destroyed first. */ 9 | static void destroy_parent(char *p) 10 | { 11 | ok1(p == parent); 12 | ok1(destroy_count == 0); 13 | /* Can still access child. */ 14 | *child = '1'; 15 | destroy_count++; 16 | } 17 | 18 | static void destroy_child(char *p) 19 | { 20 | ok1(p == child); 21 | ok1(destroy_count == 1); 22 | /* Can still access parent (though destructor has been called). */ 23 | *parent = '1'; 24 | destroy_count++; 25 | } 26 | 27 | static void destroy_inc(char *p) 28 | { 29 | destroy_count++; 30 | } 31 | 32 | int main(void) 33 | { 34 | char *child2; 35 | 36 | plan_tests(18); 37 | 38 | destroy_count = 0; 39 | parent = tal(NULL, char); 40 | child = tal(parent, char); 41 | ok1(tal_add_destructor(parent, destroy_parent)); 42 | ok1(tal_add_destructor(child, destroy_child)); 43 | tal_free(parent); 44 | ok1(destroy_count == 2); 45 | 46 | destroy_count = 0; 47 | parent = tal(NULL, char); 48 | child = tal(parent, char); 49 | ok1(tal_add_destructor(parent, destroy_parent)); 50 | ok1(tal_add_destructor(child, destroy_child)); 51 | ok1(tal_del_destructor(child, destroy_child)); 52 | tal_free(parent); 53 | ok1(destroy_count == 1); 54 | 55 | destroy_count = 0; 56 | parent = tal(NULL, char); 57 | child = tal(parent, char); 58 | child2 = tal(parent, char); 59 | ok1(tal_add_destructor(parent, destroy_inc)); 60 | ok1(tal_add_destructor(parent, destroy_inc)); 61 | ok1(tal_add_destructor(child, destroy_inc)); 62 | ok1(tal_add_destructor(child2, destroy_inc)); 63 | tal_free(parent); 64 | ok1(destroy_count == 4); 65 | 66 | return exit_status(); 67 | } 68 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-expand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *a; 8 | const int arr[] = { 1, 2 }; 9 | 10 | plan_tests(14); 11 | talloc_enable_null_tracking_no_autofree(); 12 | 13 | a = tal_arrz(NULL, int, 1); 14 | ok1(a); 15 | 16 | ok1(tal_expand(&a, arr, 2)); 17 | ok1(tal_count(a) == 3); 18 | ok1(a[0] == 0); 19 | ok1(a[1] == 1); 20 | ok1(a[2] == 2); 21 | 22 | ok1(tal_expand(&a, take(tal_arrz(NULL, int, 1)), 1)); 23 | ok1(tal_count(a) == 4); 24 | ok1(a[0] == 0); 25 | ok1(a[1] == 1); 26 | ok1(a[2] == 2); 27 | ok1(a[3] == 0); 28 | ok1(talloc_total_blocks(NULL) == 2); 29 | ok1(talloc_total_blocks(a) == 1); 30 | 31 | tal_free(a); 32 | 33 | talloc_disable_null_tracking(); 34 | return exit_status(); 35 | } 36 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-free.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static void destroy_errno(char *p) 7 | { 8 | errno = ENOENT; 9 | } 10 | 11 | int main(void) 12 | { 13 | char *p; 14 | 15 | plan_tests(2); 16 | 17 | p = tal(NULL, char); 18 | ok1(tal_add_destructor(p, destroy_errno)); 19 | 20 | /* Errno save/restored across free. */ 21 | errno = EINVAL; 22 | tal_free(p); 23 | ok1(errno == EINVAL); 24 | 25 | return exit_status(); 26 | } 27 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-named-debug.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_DEBUG 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(6); 12 | 13 | p = tal(NULL, int); 14 | ok1(strcmp(tal_name(p), __FILE__ ":13:int") == 0); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | p = tal_arr(NULL, int, 2); 30 | ok1(strcmp(tal_name(p), __FILE__ ":29:int[]") == 0); 31 | tal_free(p); 32 | 33 | return exit_status(); 34 | } 35 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-named-nolabels.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_NO_LABELS 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(5); 12 | 13 | p = tal(NULL, int); 14 | ok1(tal_name(p) == NULL); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | return exit_status(); 30 | } 31 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-named.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *p; 8 | char name[] = "test name"; 9 | 10 | plan_tests(6); 11 | 12 | p = tal(NULL, int); 13 | ok1(strcmp(tal_name(p), "int") == 0); 14 | 15 | tal_set_name(p, "some literal"); 16 | ok1(strcmp(tal_name(p), "some literal") == 0); 17 | 18 | tal_set_name(p, name); 19 | ok1(strcmp(tal_name(p), name) == 0); 20 | /* You can't reuse my pointer though! */ 21 | ok1(tal_name(p) != name); 22 | 23 | tal_set_name(p, "some other literal"); 24 | ok1(strcmp(tal_name(p), "some other literal") == 0); 25 | 26 | tal_free(p); 27 | 28 | p = tal_arr(NULL, int, 2); 29 | ok1(strcmp(tal_name(p), "int[]") == 0); 30 | tal_free(p); 31 | 32 | return exit_status(); 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-steal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *p[5]; 8 | unsigned int i; 9 | 10 | plan_tests(9); 11 | 12 | p[0] = tal(NULL, char); 13 | for (i = 1; i < 5; i++) 14 | p[i] = tal(p[i-1], char); 15 | 16 | tal_check(NULL, "check"); 17 | /* Steal node with no children. */ 18 | ok1(tal_steal(p[0], p[4]) == p[4]); 19 | tal_check(NULL, "check"); 20 | /* Noop steal. */ 21 | ok1(tal_steal(p[0], p[4]) == p[4]); 22 | tal_check(NULL, "check"); 23 | /* Steal with children. */ 24 | ok1(tal_steal(p[0], p[1]) == p[1]); 25 | tal_check(NULL, "check"); 26 | /* Noop steal. */ 27 | ok1(tal_steal(p[0], p[1]) == p[1]); 28 | tal_check(NULL, "check"); 29 | /* Steal from direct child. */ 30 | ok1(tal_steal(p[0], p[2]) == p[2]); 31 | tal_check(NULL, "check"); 32 | 33 | ok1(tal_parent(p[1]) == p[0]); 34 | ok1(tal_parent(p[2]) == p[0]); 35 | ok1(tal_parent(p[3]) == p[2]); 36 | ok1(tal_parent(p[4]) == p[0]); 37 | tal_free(p[0]); 38 | 39 | return exit_status(); 40 | } 41 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run-take.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c; 8 | 9 | plan_tests(21); 10 | 11 | /* We can take NULL. */ 12 | ok1(take(NULL) == NULL); 13 | ok1(is_taken(NULL)); 14 | ok1(taken(NULL)); /* Undoes take() */ 15 | ok1(!is_taken(NULL)); 16 | ok1(!taken(NULL)); 17 | 18 | parent = tal(NULL, char); 19 | ok1(parent); 20 | 21 | ok1(take(parent) == parent); 22 | ok1(is_taken(parent)); 23 | ok1(taken(parent)); /* Undoes take() */ 24 | ok1(!is_taken(parent)); 25 | ok1(!taken(parent)); 26 | 27 | c = tal(parent, char); 28 | *c = 'h'; 29 | c = tal_dup(parent, char, take(c), 1, 0); 30 | ok1(c[0] == 'h'); 31 | ok1(tal_parent(c) == parent); 32 | 33 | c = tal_dup(parent, char, take(c), 1, 2); 34 | ok1(c[0] == 'h'); 35 | strcpy(c, "hi"); 36 | ok1(tal_parent(c) == parent); 37 | 38 | /* dup must reparent child. */ 39 | c = tal_dup(NULL, char, take(c), 1, 0); 40 | ok1(c[0] == 'h'); 41 | ok1(tal_parent(c) == NULL); 42 | 43 | /* No leftover allocations. */ 44 | tal_free(c); 45 | ok1(talloc_total_blocks(parent) == 1); 46 | 47 | tal_free(parent); 48 | ok1(!taken_any()); 49 | 50 | /* NULL pass-through. */ 51 | c = NULL; 52 | ok1(tal_dup(NULL, char, take(c), 5, 5) == NULL); 53 | ok1(!taken_any()); 54 | 55 | return exit_status(); 56 | } 57 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/talloc/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4]; 8 | int i, j; 9 | 10 | plan_tests(9); 11 | 12 | /* tal_free(NULL) works. */ 13 | ok1(tal_free(NULL) == NULL); 14 | 15 | parent = tal(NULL, char); 16 | ok1(parent); 17 | ok1(tal_parent(parent) == NULL); 18 | ok1(tal_parent(NULL) == NULL); 19 | 20 | for (i = 0; i < 4; i++) 21 | c[i] = tal(parent, char); 22 | 23 | for (i = 0; i < 4; i++) 24 | ok1(tal_parent(c[i]) == parent); 25 | 26 | /* Free parent. */ 27 | ok1(tal_free(parent) == NULL); 28 | 29 | parent = tal(NULL, char); 30 | 31 | /* Test freeing in every order */ 32 | for (i = 0; i < 4; i++) { 33 | for (j = 0; j < 4; j++) 34 | c[j] = tal(parent, char); 35 | 36 | tal_free(c[i]); 37 | tal_free(c[(i+1) % 4]); 38 | tal_free(c[(i+2) % 4]); 39 | tal_free(c[(i+3) % 4]); 40 | } 41 | tal_free(parent); 42 | 43 | return exit_status(); 44 | } 45 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4]; 8 | int i; 9 | 10 | plan_tests(11); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | /* Zeroing allocations. */ 16 | for (i = 0; i < 4; i++) { 17 | c[i] = talz(parent, char); 18 | ok1(*c[i] == '\0'); 19 | tal_free(c[i]); 20 | } 21 | 22 | /* Array allocation. */ 23 | for (i = 0; i < 4; i++) { 24 | c[i] = tal_arr(parent, char, 4); 25 | strcpy(c[i], "abc"); 26 | tal_free(c[i]); 27 | } 28 | 29 | /* Zeroing array allocation. */ 30 | for (i = 0; i < 4; i++) { 31 | c[i] = tal_arrz(parent, char, 4); 32 | ok1(!c[i][0] && !c[i][1] && !c[i][2] && !c[i][3]); 33 | strcpy(c[i], "abc"); 34 | tal_free(c[i]); 35 | } 36 | 37 | /* Resizing. */ 38 | c[0] = tal_arrz(parent, char, 4); 39 | ok1(tal_resize(&c[0], 6)); 40 | strcpy(c[0], "hello"); 41 | tal_free(c[0]); 42 | ok1(talloc_total_blocks(parent) == 1); 43 | tal_free(parent); 44 | 45 | return exit_status(); 46 | } 47 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-count.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *p1, *p2; 8 | 9 | plan_tests(12); 10 | 11 | p1 = tal(NULL, char); 12 | ok1(p1); 13 | ok1(tal_count(p1) == 1); 14 | 15 | p2 = tal_arr(p1, char, 1); 16 | ok1(p2); 17 | ok1(tal_count(p2) == 1); 18 | ok1(tal_resize(&p2, 2)); 19 | ok1(tal_count(p2) == 2); 20 | ok1(tal_check(NULL, NULL)); 21 | tal_free(p2); 22 | 23 | p2 = tal_arrz(p1, char, 7); 24 | ok1(p2); 25 | ok1(tal_count(p2) == 7); 26 | ok1(tal_resize(&p2, 0)); 27 | ok1(tal_count(p2) == 0); 28 | ok1(tal_check(NULL, NULL)); 29 | tal_free(p2); 30 | tal_free(p1); 31 | 32 | return exit_status(); 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-destructor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static char *parent, *child; 6 | static int destroy_count; 7 | 8 | /* Parent gets destroyed first. */ 9 | static void destroy_parent(char *p) 10 | { 11 | ok1(p == parent); 12 | ok1(destroy_count == 0); 13 | /* Can still access child. */ 14 | *child = '1'; 15 | destroy_count++; 16 | } 17 | 18 | static void destroy_child(char *p) 19 | { 20 | ok1(p == child); 21 | ok1(destroy_count == 1); 22 | /* Can still access parent (though destructor has been called). */ 23 | *parent = '1'; 24 | destroy_count++; 25 | } 26 | 27 | static void destroy_inc(char *p) 28 | { 29 | destroy_count++; 30 | } 31 | 32 | int main(void) 33 | { 34 | char *child2; 35 | 36 | plan_tests(18); 37 | 38 | destroy_count = 0; 39 | parent = tal(NULL, char); 40 | child = tal(parent, char); 41 | ok1(tal_add_destructor(parent, destroy_parent)); 42 | ok1(tal_add_destructor(child, destroy_child)); 43 | tal_free(parent); 44 | ok1(destroy_count == 2); 45 | 46 | destroy_count = 0; 47 | parent = tal(NULL, char); 48 | child = tal(parent, char); 49 | ok1(tal_add_destructor(parent, destroy_parent)); 50 | ok1(tal_add_destructor(child, destroy_child)); 51 | ok1(tal_del_destructor(child, destroy_child)); 52 | tal_free(parent); 53 | ok1(destroy_count == 1); 54 | 55 | destroy_count = 0; 56 | parent = tal(NULL, char); 57 | child = tal(parent, char); 58 | child2 = tal(parent, char); 59 | ok1(tal_add_destructor(parent, destroy_inc)); 60 | ok1(tal_add_destructor(parent, destroy_inc)); 61 | ok1(tal_add_destructor(child, destroy_inc)); 62 | ok1(tal_add_destructor(child2, destroy_inc)); 63 | tal_free(parent); 64 | ok1(destroy_count == 4); 65 | 66 | return exit_status(); 67 | } 68 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-expand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *a; 8 | const int arr[] = { 1, 2 }; 9 | 10 | plan_tests(14); 11 | talloc_enable_null_tracking_no_autofree(); 12 | 13 | a = tal_arrz(NULL, int, 1); 14 | ok1(a); 15 | 16 | ok1(tal_expand(&a, arr, 2)); 17 | ok1(tal_count(a) == 3); 18 | ok1(a[0] == 0); 19 | ok1(a[1] == 1); 20 | ok1(a[2] == 2); 21 | 22 | ok1(tal_expand(&a, take(tal_arrz(NULL, int, 1)), 1)); 23 | ok1(tal_count(a) == 4); 24 | ok1(a[0] == 0); 25 | ok1(a[1] == 1); 26 | ok1(a[2] == 2); 27 | ok1(a[3] == 0); 28 | ok1(talloc_total_blocks(NULL) == 2); 29 | ok1(talloc_total_blocks(a) == 1); 30 | 31 | tal_free(a); 32 | 33 | talloc_disable_null_tracking(); 34 | return exit_status(); 35 | } 36 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-free.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static void destroy_errno(char *p) 7 | { 8 | errno = ENOENT; 9 | } 10 | 11 | int main(void) 12 | { 13 | char *p; 14 | 15 | plan_tests(2); 16 | 17 | p = tal(NULL, char); 18 | ok1(tal_add_destructor(p, destroy_errno)); 19 | 20 | /* Errno save/restored across free. */ 21 | errno = EINVAL; 22 | tal_free(p); 23 | ok1(errno == EINVAL); 24 | 25 | return exit_status(); 26 | } 27 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-named-debug.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_DEBUG 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(6); 12 | 13 | p = tal(NULL, int); 14 | ok1(strcmp(tal_name(p), __FILE__ ":13:int") == 0); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | p = tal_arr(NULL, int, 2); 30 | ok1(strcmp(tal_name(p), __FILE__ ":29:int[]") == 0); 31 | tal_free(p); 32 | 33 | return exit_status(); 34 | } 35 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-named-nolabels.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_NO_LABELS 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(5); 12 | 13 | p = tal(NULL, int); 14 | ok1(tal_name(p) == NULL); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | return exit_status(); 30 | } 31 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-named.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *p; 8 | char name[] = "test name"; 9 | 10 | plan_tests(6); 11 | 12 | p = tal(NULL, int); 13 | ok1(strcmp(tal_name(p), "int") == 0); 14 | 15 | tal_set_name(p, "some literal"); 16 | ok1(strcmp(tal_name(p), "some literal") == 0); 17 | 18 | tal_set_name(p, name); 19 | ok1(strcmp(tal_name(p), name) == 0); 20 | /* You can't reuse my pointer though! */ 21 | ok1(tal_name(p) != name); 22 | 23 | tal_set_name(p, "some other literal"); 24 | ok1(strcmp(tal_name(p), "some other literal") == 0); 25 | 26 | tal_free(p); 27 | 28 | p = tal_arr(NULL, int, 2); 29 | ok1(strcmp(tal_name(p), "int[]") == 0); 30 | tal_free(p); 31 | 32 | return exit_status(); 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-steal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *p[5]; 8 | unsigned int i; 9 | 10 | plan_tests(9); 11 | 12 | p[0] = tal(NULL, char); 13 | for (i = 1; i < 5; i++) 14 | p[i] = tal(p[i-1], char); 15 | 16 | tal_check(NULL, "check"); 17 | /* Steal node with no children. */ 18 | ok1(tal_steal(p[0], p[4]) == p[4]); 19 | tal_check(NULL, "check"); 20 | /* Noop steal. */ 21 | ok1(tal_steal(p[0], p[4]) == p[4]); 22 | tal_check(NULL, "check"); 23 | /* Steal with children. */ 24 | ok1(tal_steal(p[0], p[1]) == p[1]); 25 | tal_check(NULL, "check"); 26 | /* Noop steal. */ 27 | ok1(tal_steal(p[0], p[1]) == p[1]); 28 | tal_check(NULL, "check"); 29 | /* Steal from direct child. */ 30 | ok1(tal_steal(p[0], p[2]) == p[2]); 31 | tal_check(NULL, "check"); 32 | 33 | ok1(tal_parent(p[1]) == p[0]); 34 | ok1(tal_parent(p[2]) == p[0]); 35 | ok1(tal_parent(p[3]) == p[2]); 36 | ok1(tal_parent(p[4]) == p[0]); 37 | tal_free(p[0]); 38 | 39 | return exit_status(); 40 | } 41 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run-take.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c; 8 | 9 | plan_tests(21); 10 | 11 | /* We can take NULL. */ 12 | ok1(take(NULL) == NULL); 13 | ok1(is_taken(NULL)); 14 | ok1(taken(NULL)); /* Undoes take() */ 15 | ok1(!is_taken(NULL)); 16 | ok1(!taken(NULL)); 17 | 18 | parent = tal(NULL, char); 19 | ok1(parent); 20 | 21 | ok1(take(parent) == parent); 22 | ok1(is_taken(parent)); 23 | ok1(taken(parent)); /* Undoes take() */ 24 | ok1(!is_taken(parent)); 25 | ok1(!taken(parent)); 26 | 27 | c = tal(parent, char); 28 | *c = 'h'; 29 | c = tal_dup(parent, char, take(c)); 30 | ok1(c[0] == 'h'); 31 | ok1(tal_parent(c) == parent); 32 | 33 | c = tal_dup_arr(parent, char, take(c), 1, 2); 34 | ok1(c[0] == 'h'); 35 | strcpy(c, "hi"); 36 | ok1(tal_parent(c) == parent); 37 | 38 | /* dup must reparent child. */ 39 | c = tal_dup(NULL, char, take(c)); 40 | ok1(c[0] == 'h'); 41 | ok1(tal_parent(c) == NULL); 42 | 43 | /* No leftover allocations. */ 44 | tal_free(c); 45 | ok1(talloc_total_blocks(parent) == 1); 46 | 47 | tal_free(parent); 48 | ok1(!taken_any()); 49 | 50 | /* NULL pass-through. */ 51 | c = NULL; 52 | ok1(tal_dup_arr(NULL, char, take(c), 5, 5) == NULL); 53 | ok1(!taken_any()); 54 | 55 | return exit_status(); 56 | } 57 | -------------------------------------------------------------------------------- /ccan/ccan/tal/talloc/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4]; 8 | int i, j; 9 | 10 | plan_tests(9); 11 | 12 | /* tal_free(NULL) works. */ 13 | ok1(tal_free(NULL) == NULL); 14 | 15 | parent = tal(NULL, char); 16 | ok1(parent); 17 | ok1(tal_parent(parent) == NULL); 18 | ok1(tal_parent(NULL) == NULL); 19 | 20 | for (i = 0; i < 4; i++) 21 | c[i] = tal(parent, char); 22 | 23 | for (i = 0; i < 4; i++) 24 | ok1(tal_parent(c[i]) == parent); 25 | 26 | /* Free parent. */ 27 | ok1(tal_free(parent) == NULL); 28 | 29 | parent = tal(NULL, char); 30 | 31 | /* Test freeing in every order */ 32 | for (i = 0; i < 4; i++) { 33 | for (j = 0; j < 4; j++) 34 | c[j] = tal(parent, char); 35 | 36 | tal_free(c[i]); 37 | tal_free(c[(i+1) % 4]); 38 | tal_free(c[(i+2) % 4]); 39 | tal_free(c[(i+3) % 4]); 40 | } 41 | tal_free(parent); 42 | 43 | return exit_status(); 44 | } 45 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4]; 8 | int i; 9 | 10 | plan_tests(11); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | /* Zeroing allocations. */ 16 | for (i = 0; i < 4; i++) { 17 | c[i] = talz(parent, char); 18 | ok1(*c[i] == '\0'); 19 | tal_free(c[i]); 20 | } 21 | 22 | /* Array allocation. */ 23 | for (i = 0; i < 4; i++) { 24 | c[i] = tal_arr(parent, char, 4); 25 | strcpy(c[i], "abc"); 26 | tal_free(c[i]); 27 | } 28 | 29 | /* Zeroing array allocation. */ 30 | for (i = 0; i < 4; i++) { 31 | c[i] = tal_arrz(parent, char, 4); 32 | ok1(!c[i][0] && !c[i][1] && !c[i][2] && !c[i][3]); 33 | strcpy(c[i], "abc"); 34 | tal_free(c[i]); 35 | } 36 | 37 | /* Resizing. */ 38 | c[0] = tal_arrz(parent, char, 4); 39 | ok1(tal_resize(&c[0], 6)); 40 | strcpy(c[0], "hello"); 41 | tal_free(c[0]); 42 | ok1(tal_first(parent) == NULL); 43 | 44 | tal_free(parent); 45 | 46 | tal_cleanup(); 47 | return exit_status(); 48 | } 49 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-destructor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static char *parent, *child; 6 | static int destroy_count; 7 | 8 | /* Parent gets destroyed first. */ 9 | static void destroy_parent(char *p) 10 | { 11 | ok1(p == parent); 12 | ok1(destroy_count == 0); 13 | /* Can still access child. */ 14 | *child = '1'; 15 | destroy_count++; 16 | } 17 | 18 | static void destroy_child(char *p) 19 | { 20 | ok1(p == child); 21 | ok1(destroy_count == 1); 22 | /* Can still access parent (though destructor has been called). */ 23 | *parent = '1'; 24 | destroy_count++; 25 | } 26 | 27 | static void destroy_inc(char *p UNNEEDED) 28 | { 29 | destroy_count++; 30 | } 31 | 32 | static void remove_own_destructor(char *p) 33 | { 34 | destroy_count++; 35 | ok1(!tal_del_destructor(p, remove_own_destructor)); 36 | } 37 | 38 | int main(void) 39 | { 40 | char *child2; 41 | 42 | plan_tests(21); 43 | 44 | destroy_count = 0; 45 | parent = tal(NULL, char); 46 | child = tal(parent, char); 47 | ok1(tal_add_destructor(parent, destroy_parent)); 48 | ok1(tal_add_destructor(child, destroy_child)); 49 | tal_free(parent); 50 | ok1(destroy_count == 2); 51 | 52 | destroy_count = 0; 53 | parent = tal(NULL, char); 54 | child = tal(parent, char); 55 | ok1(tal_add_destructor(parent, destroy_parent)); 56 | ok1(tal_add_destructor(child, destroy_child)); 57 | ok1(tal_del_destructor(child, destroy_child)); 58 | tal_free(parent); 59 | ok1(destroy_count == 1); 60 | 61 | destroy_count = 0; 62 | parent = tal(NULL, char); 63 | child = tal(parent, char); 64 | child2 = tal(parent, char); 65 | ok1(tal_add_destructor(parent, destroy_inc)); 66 | ok1(tal_add_destructor(parent, destroy_inc)); 67 | ok1(tal_add_destructor(child, destroy_inc)); 68 | ok1(tal_add_destructor(child2, destroy_inc)); 69 | tal_free(parent); 70 | ok1(destroy_count == 4); 71 | 72 | destroy_count = 0; 73 | parent = tal(NULL, char); 74 | ok1(tal_add_destructor(parent, remove_own_destructor)); 75 | tal_free(parent); 76 | ok1(destroy_count == 1); 77 | 78 | tal_cleanup(); 79 | return exit_status(); 80 | } 81 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-expand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *a; 8 | const int arr[] = { 1, 2 }; 9 | 10 | plan_tests(13); 11 | 12 | a = tal_arrz(NULL, int, 1); 13 | ok1(a); 14 | 15 | ok1(tal_expand(&a, arr, 2)); 16 | ok1(tal_count(a) == 3); 17 | ok1(a[0] == 0); 18 | ok1(a[1] == 1); 19 | ok1(a[2] == 2); 20 | 21 | ok1(tal_expand(&a, take(tal_arrz(NULL, int, 1)), 1)); 22 | ok1(tal_count(a) == 4); 23 | ok1(a[0] == 0); 24 | ok1(a[1] == 1); 25 | ok1(a[2] == 2); 26 | ok1(a[3] == 0); 27 | ok1(tal_first(NULL) == a && !tal_next(a) && !tal_first(a)); 28 | 29 | tal_free(a); 30 | 31 | tal_cleanup(); 32 | return exit_status(); 33 | } 34 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-free.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static void destroy_errno(char *p UNNEEDED) 6 | { 7 | /* Errno restored for all the destructors. */ 8 | ok1(errno == EINVAL); 9 | errno = ENOENT; 10 | } 11 | 12 | int main(void) 13 | { 14 | char *p; 15 | 16 | plan_tests(5); 17 | 18 | p = tal(NULL, char); 19 | ok1(tal_add_destructor(p, destroy_errno)); 20 | ok1(tal_add_destructor(p, destroy_errno)); 21 | 22 | /* Errno save/restored across free. */ 23 | errno = EINVAL; 24 | tal_free(p); 25 | ok1(errno == EINVAL); 26 | 27 | tal_cleanup(); 28 | return exit_status(); 29 | } 30 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-groups-grow.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_DEBUG 2 | #include 3 | #include 4 | #include 5 | 6 | static size_t num_allocated; 7 | 8 | static void *alloc_account(size_t len) 9 | { 10 | num_allocated++; 11 | return malloc(len); 12 | } 13 | 14 | static void free_account(void *p) 15 | { 16 | num_allocated--; 17 | return free(p); 18 | } 19 | 20 | #define NUM_ALLOCS 1000 21 | 22 | int main(void) 23 | { 24 | void *p, *c[NUM_ALLOCS]; 25 | int i; 26 | size_t allocated_after_first; 27 | 28 | plan_tests(1); 29 | 30 | tal_set_backend(alloc_account, NULL, free_account, NULL); 31 | 32 | p = tal(NULL, char); 33 | c[0] = tal(p, char); 34 | 35 | allocated_after_first = num_allocated; 36 | for (i = 1; i < NUM_ALLOCS; i++) 37 | c[i] = tal(p, char); 38 | 39 | /* Now free them all. */ 40 | for (i = 0; i < NUM_ALLOCS; i++) 41 | tal_free(c[i]); 42 | 43 | /* We can expect some residue from having any child, but limited! */ 44 | ok1(num_allocated <= allocated_after_first); 45 | tal_free(p); 46 | tal_cleanup(); 47 | return exit_status(); 48 | } 49 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-iter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NUM 1000 6 | 7 | static int set_children(const tal_t *parent, char val) 8 | { 9 | char *iter; 10 | int num = 0; 11 | 12 | for (iter = tal_first(parent); iter; iter = tal_next(iter)) { 13 | ok1(*iter == '0'); 14 | *iter = val; 15 | num++; 16 | num += set_children(iter, val); 17 | } 18 | return num; 19 | } 20 | 21 | static void check_children(const tal_t *parent, char val) 22 | { 23 | const char *iter; 24 | 25 | for (iter = tal_first(parent); iter; iter = tal_next(iter)) { 26 | ok1(*iter == val); 27 | check_children(iter, val); 28 | } 29 | } 30 | 31 | int main(void) 32 | { 33 | char *p[NUM] = { NULL }; 34 | int i; 35 | 36 | plan_tests(NUM + 1 + NUM); 37 | 38 | /* Create a random tree */ 39 | for (i = 0; i < NUM; i++) { 40 | p[i] = tal(p[rand() % (i + 1)], char); 41 | *p[i] = '0'; 42 | } 43 | 44 | i = set_children(NULL, '1'); 45 | ok1(i == NUM); 46 | 47 | check_children(NULL, '1'); 48 | for (i = NUM-1; i >= 0; i--) 49 | tal_free(p[i]); 50 | 51 | tal_cleanup(); 52 | return exit_status(); 53 | } 54 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-named-debug.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_DEBUG 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(6); 12 | 13 | p = tal(NULL, int); 14 | ok1(strcmp(tal_name(p), __FILE__ ":13:int") == 0); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | p = tal_arr(NULL, int, 2); 30 | ok1(strcmp(tal_name(p), __FILE__ ":29:int[]") == 0); 31 | tal_free(p); 32 | 33 | tal_cleanup(); 34 | return exit_status(); 35 | } 36 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-named-nolabels.c: -------------------------------------------------------------------------------- 1 | #define CCAN_TAL_NO_LABELS 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int *p; 9 | char name[] = "test name"; 10 | 11 | plan_tests(5); 12 | 13 | p = tal(NULL, int); 14 | ok1(tal_name(p) == NULL); 15 | 16 | tal_set_name(p, "some literal"); 17 | ok1(strcmp(tal_name(p), "some literal") == 0); 18 | 19 | tal_set_name(p, name); 20 | ok1(strcmp(tal_name(p), name) == 0); 21 | /* You can't reuse my pointer though! */ 22 | ok1(tal_name(p) != name); 23 | 24 | tal_set_name(p, "some other literal"); 25 | ok1(strcmp(tal_name(p), "some other literal") == 0); 26 | 27 | tal_free(p); 28 | 29 | tal_cleanup(); 30 | return exit_status(); 31 | } 32 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-named.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int *p; 8 | char name[] = "test name"; 9 | 10 | plan_tests(6); 11 | 12 | p = tal(NULL, int); 13 | ok1(strcmp(tal_name(p), "int") == 0); 14 | 15 | tal_set_name(p, "some literal"); 16 | ok1(strcmp(tal_name(p), "some literal") == 0); 17 | 18 | tal_set_name(p, name); 19 | ok1(strcmp(tal_name(p), name) == 0); 20 | /* You can't reuse my pointer though! */ 21 | ok1(tal_name(p) != name); 22 | 23 | tal_set_name(p, "some other literal"); 24 | ok1(strcmp(tal_name(p), "some other literal") == 0); 25 | 26 | tal_free(p); 27 | 28 | p = tal_arr(NULL, int, 2); 29 | ok1(strcmp(tal_name(p), "int[]") == 0); 30 | tal_free(p); 31 | 32 | tal_cleanup(); 33 | return exit_status(); 34 | } 35 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-resizez.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c; 8 | int i; 9 | 10 | plan_tests(1 + 3 * 100 + 98); 11 | 12 | parent = tal(NULL, char); 13 | ok1(parent); 14 | 15 | for (i = 0; i < 100; i++) { 16 | c = tal_arr(parent, char, 1); 17 | ok1(tal_resizez(&c, i)); 18 | ok1(tal_count(c) == i); 19 | ok1(tal_parent(c) == parent); 20 | if (i > 1) 21 | ok1(c[i-1] == '\0'); 22 | } 23 | tal_free(parent); 24 | 25 | tal_cleanup(); 26 | return exit_status(); 27 | } 28 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-steal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *p[5]; 8 | unsigned int i; 9 | 10 | plan_tests(9); 11 | 12 | p[0] = tal(NULL, char); 13 | for (i = 1; i < 5; i++) 14 | p[i] = tal(p[i-1], char); 15 | 16 | tal_check(NULL, "check"); 17 | /* Steal node with no children. */ 18 | ok1(tal_steal(p[0], p[4]) == p[4]); 19 | tal_check(NULL, "check"); 20 | /* Noop steal. */ 21 | ok1(tal_steal(p[0], p[4]) == p[4]); 22 | tal_check(NULL, "check"); 23 | /* Steal with children. */ 24 | ok1(tal_steal(p[0], p[1]) == p[1]); 25 | tal_check(NULL, "check"); 26 | /* Noop steal. */ 27 | ok1(tal_steal(p[0], p[1]) == p[1]); 28 | tal_check(NULL, "check"); 29 | /* Steal from direct child. */ 30 | ok1(tal_steal(p[0], p[2]) == p[2]); 31 | tal_check(NULL, "check"); 32 | 33 | ok1(tal_parent(p[1]) == p[0]); 34 | ok1(tal_parent(p[2]) == p[0]); 35 | ok1(tal_parent(p[3]) == p[2]); 36 | ok1(tal_parent(p[4]) == p[0]); 37 | tal_free(p[0]); 38 | 39 | tal_cleanup(); 40 | return exit_status(); 41 | } 42 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-take.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c; 8 | 9 | plan_tests(22); 10 | 11 | /* We can take NULL. */ 12 | ok1(take(NULL) == NULL); 13 | ok1(is_taken(NULL)); 14 | ok1(taken(NULL)); /* Undoes take() */ 15 | ok1(!is_taken(NULL)); 16 | ok1(!taken(NULL)); 17 | 18 | parent = tal(NULL, char); 19 | ok1(parent); 20 | 21 | ok1(take(parent) == parent); 22 | ok1(is_taken(parent)); 23 | ok1(taken(parent)); /* Undoes take() */ 24 | ok1(!is_taken(parent)); 25 | ok1(!taken(parent)); 26 | 27 | c = tal(parent, char); 28 | *c = 'h'; 29 | c = tal_dup(parent, char, take(c)); 30 | ok1(c[0] == 'h'); 31 | ok1(tal_parent(c) == parent); 32 | 33 | c = tal_dup_arr(parent, char, take(c), 1, 2); 34 | ok1(c[0] == 'h'); 35 | strcpy(c, "hi"); 36 | ok1(tal_parent(c) == parent); 37 | 38 | /* dup must reparent child. */ 39 | c = tal_dup(NULL, char, take(c)); 40 | ok1(c[0] == 'h'); 41 | ok1(tal_parent(c) == NULL); 42 | 43 | /* No leftover allocations. */ 44 | tal_free(c); 45 | ok1(tal_first(parent) == NULL); 46 | 47 | /* tal_resize should return a taken pointer. */ 48 | c = take(tal_arr(parent, char, 5)); 49 | tal_resize(&c, 100); 50 | ok1(taken(c)); 51 | tal_free(c); 52 | 53 | tal_free(parent); 54 | ok1(!taken_any()); 55 | 56 | /* NULL pass-through. */ 57 | c = NULL; 58 | ok1(tal_dup_arr(NULL, char, take(c), 5, 5) == NULL); 59 | ok1(!taken_any()); 60 | 61 | tal_cleanup(); 62 | return exit_status(); 63 | } 64 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run-test-backend.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Make sure it always uses our allocation/resize/free fns! */ 5 | static bool my_alloc_called; 6 | 7 | static void *my_alloc(size_t len) 8 | { 9 | my_alloc_called = true; 10 | return (char *)malloc(len + 16) + 16; 11 | } 12 | 13 | static void my_free(void *p) 14 | { 15 | if (p) 16 | free((char *)p - 16); 17 | } 18 | 19 | static void *my_realloc(void *old, size_t new_size) 20 | { 21 | return (char *)realloc((char *)old - 16, new_size + 16) + 16; 22 | } 23 | 24 | #define free ((void (*)(void *))abort) 25 | #define malloc ((void *(*)(size_t))abort) 26 | #define realloc ((void *(*)(void *, size_t))abort) 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #define NUM_ALLOCS 1000 33 | 34 | static void destroy_p(void *p UNNEEDED) 35 | { 36 | } 37 | 38 | int main(void) 39 | { 40 | void *p, *c[NUM_ALLOCS]; 41 | int i; 42 | char *name; 43 | 44 | /* Mostly we rely on the allocator (or valgrind) crashing. */ 45 | plan_tests(1); 46 | 47 | tal_set_backend(my_alloc, my_realloc, my_free, NULL); 48 | 49 | p = tal(NULL, char); 50 | ok1(my_alloc_called); 51 | 52 | /* Adding properties makes us allocated. */ 53 | tal_add_destructor(p, destroy_p); 54 | 55 | tal_set_name(p, "test"); 56 | name = tal_arr(NULL, char, 6); 57 | strcpy(name, "test2"); 58 | tal_set_name(p, name); 59 | /* makes us free old name */ 60 | tal_set_name(p, name); 61 | tal_free(name); 62 | 63 | /* Add lots of children. */ 64 | for (i = 0; i < NUM_ALLOCS; i++) 65 | c[i] = tal(p, char); 66 | 67 | /* Now steal a few. */ 68 | for (i = 1; i < NUM_ALLOCS / 2; i++) 69 | tal_steal(c[0], c[i]); 70 | 71 | /* Now free individual ones.. */ 72 | for (i = NUM_ALLOCS / 2; i < NUM_ALLOCS; i++) 73 | tal_free(c[i]); 74 | 75 | /* Finally, free the parent. */ 76 | tal_free(p); 77 | 78 | tal_cleanup(); 79 | return exit_status(); 80 | } 81 | -------------------------------------------------------------------------------- /ccan/ccan/tal/test/run.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *parent, *c[4], *p; 8 | int i, j; 9 | 10 | plan_tests(14); 11 | 12 | /* tal_free(NULL) works. */ 13 | ok1(tal_free(NULL) == NULL); 14 | 15 | parent = tal(NULL, char); 16 | ok1(parent); 17 | ok1(tal_parent(parent) == NULL); 18 | ok1(tal_parent(NULL) == NULL); 19 | 20 | for (i = 0; i < 4; i++) 21 | c[i] = tal(parent, char); 22 | 23 | for (i = 0; i < 4; i++) 24 | ok1(tal_parent(c[i]) == parent); 25 | 26 | /* Iteration test. */ 27 | i = 0; 28 | for (p = tal_first(parent); p; p = tal_next(p)) { 29 | *p = '1'; 30 | i++; 31 | } 32 | ok1(i == 4); 33 | ok1(*c[0] == '1'); 34 | ok1(*c[1] == '1'); 35 | ok1(*c[2] == '1'); 36 | ok1(*c[3] == '1'); 37 | 38 | /* Free parent. */ 39 | ok1(tal_free(parent) == NULL); 40 | 41 | parent = tal(NULL, char); 42 | 43 | /* Test freeing in every order */ 44 | for (i = 0; i < 4; i++) { 45 | for (j = 0; j < 4; j++) 46 | c[j] = tal(parent, char); 47 | 48 | tal_free(c[i]); 49 | debug_tal(to_tal_hdr(parent)); 50 | tal_free(c[(i+1) % 4]); 51 | debug_tal(to_tal_hdr(parent)); 52 | tal_free(c[(i+2) % 4]); 53 | debug_tal(to_tal_hdr(parent)); 54 | tal_free(c[(i+3) % 4]); 55 | debug_tal(to_tal_hdr(parent)); 56 | } 57 | tal_free(parent); 58 | 59 | tal_cleanup(); 60 | return exit_status(); 61 | } 62 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/LICENSE: -------------------------------------------------------------------------------- 1 | ../../licenses/CC0 -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-cast_if_type-promotable.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void _set_some_value(void *val) 5 | { 6 | (void)val; 7 | } 8 | 9 | #define set_some_value(expr) \ 10 | _set_some_value(typesafe_cb_cast(void *, long, (expr))) 11 | 12 | int main(void) 13 | { 14 | #ifdef FAIL 15 | bool x = 0; 16 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 17 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 18 | #endif 19 | #else 20 | long x = 0; 21 | #endif 22 | set_some_value(x); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb-int.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void _callback(void (*fn)(void *arg), void *arg); 5 | void _callback(void (*fn)(void *arg), void *arg) 6 | { 7 | fn(arg); 8 | } 9 | 10 | /* Callback is set up to warn if arg isn't a pointer (since it won't 11 | * pass cleanly to _callback's second arg. */ 12 | #define callback(fn, arg) \ 13 | _callback(typesafe_cb(void, (fn), (arg)), (arg)) 14 | 15 | void my_callback(int something); 16 | void my_callback(int something) 17 | { 18 | (void)something; 19 | } 20 | 21 | int main(void) 22 | { 23 | #ifdef FAIL 24 | /* This fails due to arg, not due to cast. */ 25 | callback(my_callback, 100); 26 | #endif 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void _register_callback(void (*cb)(void *arg), void *arg) 5 | { 6 | (void)cb; 7 | (void)arg; 8 | } 9 | 10 | #define register_callback(cb, arg) \ 11 | _register_callback(typesafe_cb(void, void *, (cb), (arg)), (arg)) 12 | 13 | static void my_callback(char *p) 14 | { 15 | (void)p; 16 | } 17 | 18 | int main(void) 19 | { 20 | char str[] = "hello world"; 21 | #ifdef FAIL 22 | int *p; 23 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 24 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 25 | #endif 26 | #else 27 | char *p; 28 | #endif 29 | p = NULL; 30 | 31 | /* This should work always. */ 32 | register_callback(my_callback, str); 33 | 34 | /* This will fail with FAIL defined */ 35 | register_callback(my_callback, p); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb_cast-multi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int x; 6 | }; 7 | 8 | struct bar { 9 | int x; 10 | }; 11 | 12 | struct baz { 13 | int x; 14 | }; 15 | 16 | struct any { 17 | int x; 18 | }; 19 | 20 | struct other { 21 | int x; 22 | }; 23 | 24 | static void take_any(struct any *any) 25 | { 26 | (void)any; 27 | } 28 | 29 | int main(void) 30 | { 31 | #ifdef FAIL 32 | struct other 33 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 34 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 35 | #endif 36 | #else 37 | struct foo 38 | #endif 39 | *arg = NULL; 40 | take_any(typesafe_cb_cast3(struct any *, 41 | struct foo *, struct bar *, struct baz *, 42 | arg)); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb_cast.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void _set_some_value(void *val); 4 | 5 | void _set_some_value(void *val) 6 | { 7 | (void)val; 8 | } 9 | 10 | #define set_some_value(expr) \ 11 | _set_some_value(typesafe_cb_cast(void *, unsigned long, (expr))) 12 | 13 | int main(void) 14 | { 15 | #ifdef FAIL 16 | int x = 0; 17 | set_some_value(x); 18 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 19 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 20 | #endif 21 | #else 22 | void *p = 0; 23 | set_some_value(p); 24 | #endif 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb_postargs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void _register_callback(void (*cb)(void *arg, int x), void *arg) 5 | { 6 | (void)cb; 7 | (void)arg; 8 | } 9 | #define register_callback(cb, arg) \ 10 | _register_callback(typesafe_cb_postargs(void, void *, (cb), (arg), int), (arg)) 11 | 12 | static void my_callback(char *p, int x) 13 | { 14 | (void)p; 15 | (void)x; 16 | } 17 | 18 | int main(void) 19 | { 20 | #ifdef FAIL 21 | int *p; 22 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 23 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 24 | #endif 25 | #else 26 | char *p; 27 | #endif 28 | p = NULL; 29 | register_callback(my_callback, p); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_fail-typesafe_cb_preargs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static void _register_callback(void (*cb)(int x, void *arg), void *arg) 5 | { 6 | (void)cb; 7 | (void)arg; 8 | } 9 | 10 | #define register_callback(cb, arg) \ 11 | _register_callback(typesafe_cb_preargs(void, void *, (cb), (arg), int), (arg)) 12 | 13 | static void my_callback(int x, char *p) 14 | { 15 | (void)p; 16 | (void)x; 17 | } 18 | 19 | int main(void) 20 | { 21 | #ifdef FAIL 22 | int *p; 23 | #if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P 24 | #error "Unfortunately we don't fail if typesafe_cb_cast is a noop." 25 | #endif 26 | #else 27 | char *p; 28 | #endif 29 | p = NULL; 30 | register_callback(my_callback, p); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_ok-typesafe_cb-NULL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* NULL args for callback function should be OK for normal and _def. */ 5 | 6 | static void _register_callback(void (*cb)(const void *arg), const void *arg) 7 | { 8 | (void)cb; 9 | (void)arg; 10 | } 11 | 12 | #define register_callback(cb, arg) \ 13 | _register_callback(typesafe_cb(void, const void *, (cb), (arg)), (arg)) 14 | 15 | int main(void) 16 | { 17 | register_callback(NULL, "hello world"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_ok-typesafe_cb-undefined.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* const args in callbacks should be OK. */ 5 | 6 | static void _register_callback(void (*cb)(void *arg), void *arg) 7 | { 8 | (void)cb; 9 | (void)arg; 10 | } 11 | 12 | #define register_callback(cb, arg) \ 13 | _register_callback(typesafe_cb(void, void *, (cb), (arg)), (arg)) 14 | 15 | static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg) 16 | { 17 | (void)cb; 18 | (void)arg; 19 | } 20 | 21 | #define register_callback_pre(cb, arg) \ 22 | _register_callback_pre(typesafe_cb_preargs(void, void *, (cb), (arg), int), (arg)) 23 | 24 | static void _register_callback_post(void (*cb)(void *arg, int x), void *arg) 25 | { 26 | (void)cb; 27 | (void)arg; 28 | } 29 | 30 | #define register_callback_post(cb, arg) \ 31 | _register_callback_post(typesafe_cb_postargs(void, void *, (cb), (arg), int), (arg)) 32 | 33 | struct undefined; 34 | 35 | static void my_callback(struct undefined *undef) 36 | { 37 | (void)undef; 38 | } 39 | 40 | static void my_callback_pre(int x, struct undefined *undef) 41 | { 42 | (void)x; 43 | (void)undef; 44 | } 45 | 46 | static void my_callback_post(struct undefined *undef, int x) 47 | { 48 | (void)undef; 49 | (void)x; 50 | } 51 | 52 | int main(void) 53 | { 54 | struct undefined *handle = NULL; 55 | 56 | register_callback(my_callback, handle); 57 | register_callback_pre(my_callback_pre, handle); 58 | register_callback_post(my_callback_post, handle); 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_ok-typesafe_cb-vars.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* const args in callbacks should be OK. */ 5 | 6 | static void _register_callback(void (*cb)(void *arg), void *arg) 7 | { 8 | (void)cb; 9 | (void)arg; 10 | } 11 | 12 | #define register_callback(cb, arg) \ 13 | _register_callback(typesafe_cb(void, void *, (cb), (arg)), (arg)) 14 | 15 | static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg) 16 | { 17 | (void)cb; 18 | (void)arg; 19 | } 20 | 21 | #define register_callback_pre(cb, arg) \ 22 | _register_callback_pre(typesafe_cb_preargs(void, void *, (cb), (arg), int), (arg)) 23 | 24 | static void _register_callback_post(void (*cb)(void *arg, int x), void *arg) 25 | { 26 | (void)cb; 27 | (void)arg; 28 | } 29 | 30 | #define register_callback_post(cb, arg) \ 31 | _register_callback_post(typesafe_cb_postargs(void, void *, (cb), (arg), int), (arg)) 32 | 33 | struct undefined; 34 | 35 | static void my_callback(struct undefined *undef) 36 | { 37 | (void)undef; 38 | } 39 | 40 | static void my_callback_pre(int x, struct undefined *undef) 41 | { 42 | (void)x; 43 | (void)undef; 44 | } 45 | 46 | static void my_callback_post(struct undefined *undef, int x) 47 | { 48 | (void)x; 49 | (void)undef; 50 | } 51 | 52 | int main(void) 53 | { 54 | struct undefined *handle = NULL; 55 | void (*cb)(struct undefined *undef) = my_callback; 56 | void (*pre)(int x, struct undefined *undef) = my_callback_pre; 57 | void (*post)(struct undefined *undef, int x) = my_callback_post; 58 | 59 | register_callback(cb, handle); 60 | register_callback_pre(pre, handle); 61 | register_callback_post(post, handle); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /ccan/ccan/typesafe_cb/test/compile_ok-typesafe_cb_cast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct foo { 5 | int x; 6 | }; 7 | 8 | struct bar { 9 | int x; 10 | }; 11 | 12 | struct baz { 13 | int x; 14 | }; 15 | 16 | struct any { 17 | int x; 18 | }; 19 | 20 | static void take_any(struct any *any) 21 | { 22 | (void)any; 23 | } 24 | 25 | int main(void) 26 | { 27 | /* Otherwise we get unused warnings for these. */ 28 | struct foo *foo = NULL; 29 | struct bar *bar = NULL; 30 | struct baz *baz = NULL; 31 | 32 | take_any(typesafe_cb_cast3(struct any *, 33 | struct foo *, struct bar *, struct baz *, 34 | foo)); 35 | take_any(typesafe_cb_cast3(struct any *, 36 | struct foo *, struct bar *, struct baz *, 37 | bar)); 38 | take_any(typesafe_cb_cast3(struct any *, 39 | struct foo *, struct bar *, struct baz *, 40 | baz)); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /ccan/licenses/BSD-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a copy 2 | of this software and associated documentation files (the "Software"), to deal 3 | in the Software without restriction, including without limitation the rights 4 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 | copies of the Software, and to permit persons to whom the Software is 6 | furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in 9 | all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 | THE SOFTWARE. 18 | -------------------------------------------------------------------------------- /dump.h: -------------------------------------------------------------------------------- 1 | #ifndef BITCOIN_ITERATE_DUMP_H 2 | #define BITCOIN_ITERATE_DUMP_H 3 | #include "types.h" 4 | #include "io.h" 5 | 6 | void print_hex(const void *data, size_t len); 7 | void print_hash(const u8 *hash); 8 | void print_reversed_hash(const u8 *hash); 9 | 10 | void dump_block_header(const struct bitcoin_block *b); 11 | void dump_tx(const struct bitcoin_transaction *tx); 12 | void dump_tx_input(const struct bitcoin_transaction_input *input); 13 | void dump_tx_output(const struct bitcoin_transaction_output *output); 14 | 15 | #endif /* BITCOIN_ITERATE_DUMP_H */ 16 | -------------------------------------------------------------------------------- /io.h: -------------------------------------------------------------------------------- 1 | #ifndef BITCOIN_PARSE_IO_H 2 | #define BITCOIN_PARSE_IO_H 3 | #include 4 | #include 5 | #include 6 | /* These functions all exit on errors. */ 7 | 8 | /* We save the name for error reporting. */ 9 | struct file { 10 | const char *name; 11 | int fd; 12 | off_t len; 13 | void *mmap; 14 | }; 15 | 16 | /* O_NOCTTY doesn't make sense for normal files, so overload it */ 17 | #define O_NO_MMAP O_NOCTTY 18 | 19 | /* Just return mmap pointer, or read into buffer and return that. */ 20 | void *file_read(struct file *f, off_t off, size_t size, void *buf); 21 | 22 | /* Write back if not mmaped. */ 23 | void file_write(struct file *f, off_t off, size_t size, const void *buf); 24 | 25 | /* Append to end of file. Assumes we're the only ones modifying size! */ 26 | void file_append(struct file *f, const void *buf, size_t size); 27 | 28 | /* Open file (perm 0600 if O_CREAT), truncate to len if != 0 */ 29 | void file_open(struct file *f, const char *name, off_t len, int oflags); 30 | 31 | void file_close(struct file *f); 32 | #endif /* BITCOIN_PARSE_IO_H */ 33 | -------------------------------------------------------------------------------- /parse.h: -------------------------------------------------------------------------------- 1 | #ifndef BITCOIN_PARSE_PARSE_H 2 | #define BITCOIN_PARSE_PARSE_H 3 | #include "types.h" 4 | #include "io.h" 5 | 6 | struct space; 7 | 8 | /* Step 1: Fast forward *off to head of next block, or false if not found. */ 9 | bool next_block_header_prefix(struct file *f, off_t *off, const u32 marker); 10 | 11 | /* Step 2: Now, read in the block header, and calculate double-SHA*/ 12 | bool 13 | read_bitcoin_block_header(struct bitcoin_block *block, 14 | struct file *f, off_t *off, 15 | u8 block_md[SHA256_DIGEST_LENGTH], 16 | const u32 marker); 17 | 18 | /* Step 3: Either skip all the transactions, or... */ 19 | void skip_bitcoin_transactions(const struct bitcoin_block *b, 20 | off_t block_start, off_t *off); 21 | 22 | /* ... read them in (call this repeatedly). Allocates off ctx. */ 23 | void read_bitcoin_transaction(struct space *space, 24 | struct bitcoin_transaction *t, 25 | struct file *f, off_t *off); 26 | #endif /* BITCOIN_PARSE_PARSE_H */ 27 | -------------------------------------------------------------------------------- /space.h: -------------------------------------------------------------------------------- 1 | #ifndef BITCOIN_ITERATE_SPACE_H 2 | #define BITCOIN_ITERATE_SPACE_H 3 | #include 4 | #include 5 | 6 | /* Simple bump allocator: 5MB should be enough for 4MB blocks */ 7 | struct space { 8 | char buf[5 * 1024 * 1024]; 9 | size_t off; 10 | }; 11 | 12 | static inline void space_init(struct space *space) 13 | { 14 | space->off = 0; 15 | } 16 | 17 | static inline void *space_alloc(struct space *space, size_t bytes) 18 | { 19 | void *p = space->buf + space->off; 20 | 21 | /* If this fails, enlarge buf[] above */ 22 | assert(space->off + bytes <= sizeof(space->buf)); 23 | 24 | space->off += bytes; 25 | return p; 26 | } 27 | 28 | #define space_alloc_arr(space, type, num) \ 29 | ((type *)space_alloc((space), sizeof(type) * (num))) 30 | 31 | #endif /* BITCOIN_ITERATE_SPACE_H */ 32 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | #ifndef BITCOIN_PARSE_TYPES_H 2 | #define BITCOIN_PARSE_TYPES_H 3 | #include 4 | #include 5 | 6 | /* We unpack varints for our in-memory representation */ 7 | #define varint_t u64 8 | 9 | /* These are little endian on disk. */ 10 | struct bitcoin_block { 11 | u32 D9B4BEF9; 12 | u32 len; 13 | u32 version; 14 | u8 prev_hash[32]; 15 | u8 merkle_hash[32]; 16 | u32 timestamp; 17 | u32 target; 18 | u32 nonce; 19 | varint_t transaction_count; 20 | }; 21 | 22 | struct bitcoin_transaction { 23 | u32 version; 24 | varint_t input_count; 25 | struct bitcoin_transaction_input *input; 26 | varint_t output_count; 27 | struct bitcoin_transaction_output *output; 28 | u32 lock_time; 29 | 30 | /* We calculate these as we read in transaction: */ 31 | u8 sha256[SHA256_DIGEST_LENGTH]; 32 | /* Length of non-segwit part */ 33 | u32 non_swlen; 34 | /* Total length, including segwit part */ 35 | u32 total_len; 36 | }; 37 | 38 | struct bitcoin_transaction_output { 39 | u64 amount; 40 | varint_t script_length; 41 | u8 *script; 42 | }; 43 | 44 | struct bitcoin_transaction_input { 45 | u8 hash[32]; 46 | u32 index; /* output number referred to by above */ 47 | varint_t script_length; 48 | u8 *script; 49 | varint_t num_witness; 50 | u8 **witness; 51 | u32 sequence_number; 52 | }; 53 | 54 | #define OP_PUSHDATA1 0x4C 55 | #define OP_PUSHDATA2 0x4D 56 | #define OP_PUSHDATA4 0x4E 57 | #define OP_NOP 0x61 58 | #define OP_RETURN 0x6A 59 | #define OP_DUP 0x76 60 | #define OP_EQUALVERIFY 0x88 61 | #define OP_CHECKSIG 0xAC 62 | #define OP_HASH160 0xA9 63 | 64 | #endif /* BITCOIN_PARSE_TYPES_H */ 65 | --------------------------------------------------------------------------------