Blame


1 bff58270 2022-09-19 op /*
2 bff58270 2022-09-19 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 bff58270 2022-09-19 op *
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.
7 bff58270 2022-09-19 op *
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.
15 bff58270 2022-09-19 op */
16 bff58270 2022-09-19 op
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>
20 bff58270 2022-09-19 op
21 bff58270 2022-09-19 op #include "tmpl.h"
22 bff58270 2022-09-19 op #include "lists.h"
23 bff58270 2022-09-19 op
24 bff58270 2022-09-19 op int base(struct template *, struct tailhead *);
25 bff58270 2022-09-19 op
26 bff58270 2022-09-19 op int
27 bff58270 2022-09-19 op my_putc(struct template *tp, int c)
28 bff58270 2022-09-19 op {
29 bff58270 2022-09-19 op FILE *fp = tp->tp_arg;
30 bff58270 2022-09-19 op
31 bff58270 2022-09-19 op if (putc(c, fp) < 0) {
32 bff58270 2022-09-19 op tp->tp_ret = -1;
33 bff58270 2022-09-19 op return (-1);
34 bff58270 2022-09-19 op }
35 bff58270 2022-09-19 op
36 bff58270 2022-09-19 op return (0);
37 bff58270 2022-09-19 op }
38 bff58270 2022-09-19 op
39 bff58270 2022-09-19 op int
40 bff58270 2022-09-19 op my_puts(struct template *tp, const char *s)
41 bff58270 2022-09-19 op {
42 bff58270 2022-09-19 op FILE *fp = tp->tp_arg;
43 bff58270 2022-09-19 op
44 bff58270 2022-09-19 op if (fputs(s, fp) < 0) {
45 bff58270 2022-09-19 op tp->tp_ret = -1;
46 bff58270 2022-09-19 op return (-1);
47 bff58270 2022-09-19 op }
48 bff58270 2022-09-19 op
49 bff58270 2022-09-19 op return (0);
50 bff58270 2022-09-19 op }
51 bff58270 2022-09-19 op
52 bff58270 2022-09-19 op int
53 bff58270 2022-09-19 op main(int argc, char **argv)
54 bff58270 2022-09-19 op {
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;
58 bff58270 2022-09-19 op int i;
59 bff58270 2022-09-19 op
60 bff58270 2022-09-19 op if ((tp = template(stdout, my_puts, my_putc)) == NULL)
61 bff58270 2022-09-19 op err(1, "template");
62 bff58270 2022-09-19 op
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);
70 bff58270 2022-09-19 op }
71 bff58270 2022-09-19 op
72 bff58270 2022-09-19 op base(tp, &head);
73 bff58270 2022-09-19 op puts("");
74 bff58270 2022-09-19 op
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);
78 bff58270 2022-09-19 op free(np);
79 bff58270 2022-09-19 op }
80 bff58270 2022-09-19 op
81 bff58270 2022-09-19 op template_reset(tp);
82 bff58270 2022-09-19 op
83 bff58270 2022-09-19 op base(tp, &head);
84 bff58270 2022-09-19 op puts("");
85 bff58270 2022-09-19 op
86 bff58270 2022-09-19 op free(tp);
87 bff58270 2022-09-19 op
88 bff58270 2022-09-19 op return (0);
89 bff58270 2022-09-19 op }