2 bff58270 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 bff58270 2022-09-19 op * Permission to use, copy, modify, and distribute this software for any
5 bff58270 2022-09-19 op * purpose with or without fee is hereby granted, provided that the above
6 bff58270 2022-09-19 op * copyright notice and this permission notice appear in all copies.
8 bff58270 2022-09-19 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 bff58270 2022-09-19 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 bff58270 2022-09-19 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 bff58270 2022-09-19 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 bff58270 2022-09-19 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 bff58270 2022-09-19 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 bff58270 2022-09-19 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bff58270 2022-09-19 op #include <err.h>
18 bff58270 2022-09-19 op #include <stdio.h>
19 bff58270 2022-09-19 op #include <stdlib.h>
21 bff58270 2022-09-19 op #include "tmpl.h"
22 bff58270 2022-09-19 op #include "lists.h"
24 bff58270 2022-09-19 op int base(struct template *, struct tailhead *);
27 bff58270 2022-09-19 op my_putc(struct template *tp, int c)
29 bff58270 2022-09-19 op FILE *fp = tp->tp_arg;
31 bff58270 2022-09-19 op if (putc(c, fp) < 0) {
32 bff58270 2022-09-19 op tp->tp_ret = -1;
40 bff58270 2022-09-19 op my_puts(struct template *tp, const char *s)
42 bff58270 2022-09-19 op FILE *fp = tp->tp_arg;
44 bff58270 2022-09-19 op if (fputs(s, fp) < 0) {
45 bff58270 2022-09-19 op tp->tp_ret = -1;
53 bff58270 2022-09-19 op main(int argc, char **argv)
55 bff58270 2022-09-19 op struct template *tp;
56 bff58270 2022-09-19 op struct tailhead head;
57 bff58270 2022-09-19 op struct entry *np;
60 bff58270 2022-09-19 op if ((tp = template(stdout, my_puts, my_putc)) == NULL)
61 bff58270 2022-09-19 op err(1, "template");
63 bff58270 2022-09-19 op TAILQ_INIT(&head);
64 bff58270 2022-09-19 op for (i = 0; i < 2; ++i) {
65 bff58270 2022-09-19 op if ((np = calloc(1, sizeof(*np))) == NULL)
66 bff58270 2022-09-19 op err(1, "calloc");
67 bff58270 2022-09-19 op if (asprintf(&np->text, "%d", i+1) == -1)
68 bff58270 2022-09-19 op err(1, "asprintf");
69 bff58270 2022-09-19 op TAILQ_INSERT_TAIL(&head, np, entries);
72 bff58270 2022-09-19 op base(tp, &head);
75 bff58270 2022-09-19 op while ((np = TAILQ_FIRST(&head))) {
76 bff58270 2022-09-19 op TAILQ_REMOVE(&head, np, entries);
77 bff58270 2022-09-19 op free(np->text);
81 bff58270 2022-09-19 op template_reset(tp);
83 bff58270 2022-09-19 op base(tp, &head);