2 b2b17923 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 2c02675e 2022-12-14 op * Permission to use, copy, modify, and distribute this software for any
5 2c02675e 2022-12-14 op * purpose with or without fee is hereby granted, provided that the above
6 2c02675e 2022-12-14 op * copyright notice and this permission notice appear in all copies.
8 2c02675e 2022-12-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2c02675e 2022-12-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2c02675e 2022-12-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2c02675e 2022-12-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2c02675e 2022-12-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2c02675e 2022-12-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2c02675e 2022-12-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2c02675e 2022-12-14 op #include <err.h>
18 2c02675e 2022-12-14 op #include <stdio.h>
19 2c02675e 2022-12-14 op #include <stdlib.h>
21 2c02675e 2022-12-14 op #include "tmpl.h"
22 2c02675e 2022-12-14 op #include "lists.h"
24 2c02675e 2022-12-14 op int base(struct template *, struct tailhead *);
25 62eab86e 2023-09-13 op int my_write(void *, const void *, size_t);
28 62eab86e 2023-09-13 op my_write(void *arg, const void *s, size_t len)
30 62eab86e 2023-09-13 op FILE *fp = arg;
32 62eab86e 2023-09-13 op if (fwrite(s, 1, len, fp) < 0)
39 2c02675e 2022-12-14 op main(int argc, char **argv)
41 2c02675e 2022-12-14 op struct template *tp;
42 2c02675e 2022-12-14 op struct tailhead head;
43 2c02675e 2022-12-14 op struct entry *np;
46 62eab86e 2023-09-13 op /* use a ridiculously small buffer in regress */
48 62eab86e 2023-09-13 op if ((tp = template(stdout, my_write, buf, sizeof(buf))) == NULL)
49 2c02675e 2022-12-14 op err(1, "template");
51 2c02675e 2022-12-14 op TAILQ_INIT(&head);
52 2c02675e 2022-12-14 op for (i = 0; i < 2; ++i) {
53 2c02675e 2022-12-14 op if ((np = calloc(1, sizeof(*np))) == NULL)
54 2c02675e 2022-12-14 op err(1, "calloc");
55 2c02675e 2022-12-14 op if (asprintf(&np->text, "%d", i+1) == -1)
56 2c02675e 2022-12-14 op err(1, "asprintf");
57 2c02675e 2022-12-14 op TAILQ_INSERT_TAIL(&head, np, entries);
60 62eab86e 2023-09-13 op if (base(tp, &head) == -1 ||
61 62eab86e 2023-09-13 op template_flush(tp) == -1)
65 2c02675e 2022-12-14 op while ((np = TAILQ_FIRST(&head))) {
66 2c02675e 2022-12-14 op TAILQ_REMOVE(&head, np, entries);
67 2c02675e 2022-12-14 op free(np->text);
71 62eab86e 2023-09-13 op if (base(tp, &head) == -1 ||
72 62eab86e 2023-09-13 op template_flush(tp) == -1)
76 0ec0e499 2023-09-12 op template_free(tp);