Blame


1 6509b181 2022-12-30 thomas /*
2 96b8c570 2022-12-30 thomas * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 6509b181 2022-12-30 thomas *
4 6509b181 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 6509b181 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 6509b181 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
7 6509b181 2022-12-30 thomas *
8 6509b181 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6509b181 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6509b181 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6509b181 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6509b181 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6509b181 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6509b181 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 6509b181 2022-12-30 thomas */
16 6509b181 2022-12-30 thomas
17 6509b181 2022-12-30 thomas #include <err.h>
18 6509b181 2022-12-30 thomas #include <stdio.h>
19 6509b181 2022-12-30 thomas #include <stdlib.h>
20 6509b181 2022-12-30 thomas
21 6509b181 2022-12-30 thomas #include "tmpl.h"
22 6509b181 2022-12-30 thomas #include "lists.h"
23 6509b181 2022-12-30 thomas
24 6509b181 2022-12-30 thomas int base(struct template *, struct tailhead *);
25 6509b181 2022-12-30 thomas int my_putc(struct template *, int);
26 6509b181 2022-12-30 thomas int my_puts(struct template *, const char *);
27 6509b181 2022-12-30 thomas
28 6509b181 2022-12-30 thomas int
29 6509b181 2022-12-30 thomas my_putc(struct template *tp, int c)
30 6509b181 2022-12-30 thomas {
31 6509b181 2022-12-30 thomas FILE *fp = tp->tp_arg;
32 6509b181 2022-12-30 thomas
33 6509b181 2022-12-30 thomas if (putc(c, fp) < 0)
34 6509b181 2022-12-30 thomas return (-1);
35 6509b181 2022-12-30 thomas
36 6509b181 2022-12-30 thomas return (0);
37 6509b181 2022-12-30 thomas }
38 6509b181 2022-12-30 thomas
39 6509b181 2022-12-30 thomas int
40 6509b181 2022-12-30 thomas my_puts(struct template *tp, const char *s)
41 6509b181 2022-12-30 thomas {
42 6509b181 2022-12-30 thomas FILE *fp = tp->tp_arg;
43 6509b181 2022-12-30 thomas
44 6509b181 2022-12-30 thomas if (fputs(s, fp) < 0)
45 6509b181 2022-12-30 thomas return (-1);
46 6509b181 2022-12-30 thomas
47 6509b181 2022-12-30 thomas return (0);
48 6509b181 2022-12-30 thomas }
49 6509b181 2022-12-30 thomas
50 6509b181 2022-12-30 thomas int
51 6509b181 2022-12-30 thomas main(int argc, char **argv)
52 6509b181 2022-12-30 thomas {
53 6509b181 2022-12-30 thomas struct template *tp;
54 6509b181 2022-12-30 thomas struct tailhead head;
55 6509b181 2022-12-30 thomas struct entry *np;
56 6509b181 2022-12-30 thomas int i;
57 6509b181 2022-12-30 thomas
58 6509b181 2022-12-30 thomas if ((tp = template(stdout, my_puts, my_putc)) == NULL)
59 6509b181 2022-12-30 thomas err(1, "template");
60 6509b181 2022-12-30 thomas
61 6509b181 2022-12-30 thomas TAILQ_INIT(&head);
62 6509b181 2022-12-30 thomas for (i = 0; i < 2; ++i) {
63 6509b181 2022-12-30 thomas if ((np = calloc(1, sizeof(*np))) == NULL)
64 6509b181 2022-12-30 thomas err(1, "calloc");
65 6509b181 2022-12-30 thomas if (asprintf(&np->text, "%d", i+1) == -1)
66 6509b181 2022-12-30 thomas err(1, "asprintf");
67 6509b181 2022-12-30 thomas TAILQ_INSERT_TAIL(&head, np, entries);
68 6509b181 2022-12-30 thomas }
69 6509b181 2022-12-30 thomas
70 6509b181 2022-12-30 thomas if (base(tp, &head) == -1)
71 6509b181 2022-12-30 thomas return (1);
72 6509b181 2022-12-30 thomas puts("");
73 6509b181 2022-12-30 thomas
74 6509b181 2022-12-30 thomas while ((np = TAILQ_FIRST(&head))) {
75 6509b181 2022-12-30 thomas TAILQ_REMOVE(&head, np, entries);
76 6509b181 2022-12-30 thomas free(np->text);
77 6509b181 2022-12-30 thomas free(np);
78 6509b181 2022-12-30 thomas }
79 6509b181 2022-12-30 thomas
80 6509b181 2022-12-30 thomas if (base(tp, &head) == -1)
81 6509b181 2022-12-30 thomas return (1);
82 6509b181 2022-12-30 thomas puts("");
83 6509b181 2022-12-30 thomas
84 6509b181 2022-12-30 thomas free(tp);
85 6509b181 2022-12-30 thomas
86 6509b181 2022-12-30 thomas return (0);
87 6509b181 2022-12-30 thomas }