1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* pushpull - simple marshalling/unmarshalling routines
*
* This code lets you clearly add simple types into a buffer (the push
* functions) and remove them (the pull functions). The buffer stores
* the values as little-endian for machine portability. The pull functions
* don't need to be checked on every call, but error state is kept so you
* can check if there was an error at the end.
*
* The normal way to use this is to create your own higher-level marshal
* and unmarshal functions in terms of these.
*
* Author: Rusty Russell <rusty@rustcorp.com.au>
* License: CC0 (Public domain)
*
* Example:
* #include <ccan/pushpull/push.h>
* #include <ccan/pushpull/pull.h>
* #include <ccan/err/err.h>
* #include <string.h>
* #include <stdio.h>
* #include <unistd.h>
*
* int main(int argc, char *argv[])
* {
* if (argv[1] && !strcmp(argv[1], "push")) {
* int i;
* char *buf = malloc(1);
* size_t len = 0;
*
* // We ignore allocation failure!
* for (i = 2; i < argc; i++)
* push_u32(&buf, &len, atol(argv[i]));
*
* write(STDOUT_FILENO, buf, len);
* } else if (argc == 2 && !strcmp(argv[1], "pull")) {
* int r, max = 32;
* size_t len = 0;
* char *buf = malloc(max);
* const char *p;
* uint32_t val;
*
* while ((r = read(STDIN_FILENO, buf+len, max-len)) > 0) {
* len += r;
* if (len == max) {
* max *= 2;
* // We crash on allocation failure
* buf = realloc(buf, max);
* }
* }
*
* p = buf;
* while (pull_u32(&p, &len, &val))
* printf("%u ", val);
* } else
* errx(1, "Usage: %s [push|pull] [<number>...]", argv[0]);
* return 0;
* }
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/endian\n");
return 0;
}
return 1;
}
|