Blob


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