Blame


1 2c02675e 2022-12-14 op /*
2 b2b17923 2022-12-17 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 2c02675e 2022-12-14 op *
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.
7 2c02675e 2022-12-14 op *
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.
15 2c02675e 2022-12-14 op */
16 2c02675e 2022-12-14 op
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>
20 2c02675e 2022-12-14 op
21 2c02675e 2022-12-14 op #include "tmpl.h"
22 2c02675e 2022-12-14 op #include "lists.h"
23 2c02675e 2022-12-14 op
24 2c02675e 2022-12-14 op int base(struct template *, struct tailhead *);
25 2c02675e 2022-12-14 op int my_putc(struct template *, int);
26 2c02675e 2022-12-14 op int my_puts(struct template *, const char *);
27 2c02675e 2022-12-14 op
28 2c02675e 2022-12-14 op int
29 2c02675e 2022-12-14 op my_putc(struct template *tp, int c)
30 2c02675e 2022-12-14 op {
31 2c02675e 2022-12-14 op FILE *fp = tp->tp_arg;
32 2c02675e 2022-12-14 op
33 2c02675e 2022-12-14 op if (putc(c, fp) < 0)
34 2c02675e 2022-12-14 op return (-1);
35 2c02675e 2022-12-14 op
36 2c02675e 2022-12-14 op return (0);
37 2c02675e 2022-12-14 op }
38 2c02675e 2022-12-14 op
39 2c02675e 2022-12-14 op int
40 2c02675e 2022-12-14 op my_puts(struct template *tp, const char *s)
41 2c02675e 2022-12-14 op {
42 2c02675e 2022-12-14 op FILE *fp = tp->tp_arg;
43 2c02675e 2022-12-14 op
44 2c02675e 2022-12-14 op if (fputs(s, fp) < 0)
45 2c02675e 2022-12-14 op return (-1);
46 2c02675e 2022-12-14 op
47 2c02675e 2022-12-14 op return (0);
48 2c02675e 2022-12-14 op }
49 2c02675e 2022-12-14 op
50 2c02675e 2022-12-14 op int
51 2c02675e 2022-12-14 op main(int argc, char **argv)
52 2c02675e 2022-12-14 op {
53 2c02675e 2022-12-14 op struct template *tp;
54 2c02675e 2022-12-14 op struct tailhead head;
55 2c02675e 2022-12-14 op struct entry *np;
56 2c02675e 2022-12-14 op int i;
57 2c02675e 2022-12-14 op
58 2c02675e 2022-12-14 op if ((tp = template(stdout, my_puts, my_putc)) == NULL)
59 2c02675e 2022-12-14 op err(1, "template");
60 2c02675e 2022-12-14 op
61 2c02675e 2022-12-14 op TAILQ_INIT(&head);
62 2c02675e 2022-12-14 op for (i = 0; i < 2; ++i) {
63 2c02675e 2022-12-14 op if ((np = calloc(1, sizeof(*np))) == NULL)
64 2c02675e 2022-12-14 op err(1, "calloc");
65 2c02675e 2022-12-14 op if (asprintf(&np->text, "%d", i+1) == -1)
66 2c02675e 2022-12-14 op err(1, "asprintf");
67 2c02675e 2022-12-14 op TAILQ_INSERT_TAIL(&head, np, entries);
68 2c02675e 2022-12-14 op }
69 2c02675e 2022-12-14 op
70 2c02675e 2022-12-14 op if (base(tp, &head) == -1)
71 2c02675e 2022-12-14 op return (1);
72 2c02675e 2022-12-14 op puts("");
73 2c02675e 2022-12-14 op
74 2c02675e 2022-12-14 op while ((np = TAILQ_FIRST(&head))) {
75 2c02675e 2022-12-14 op TAILQ_REMOVE(&head, np, entries);
76 2c02675e 2022-12-14 op free(np->text);
77 2c02675e 2022-12-14 op free(np);
78 2c02675e 2022-12-14 op }
79 2c02675e 2022-12-14 op
80 2c02675e 2022-12-14 op if (base(tp, &head) == -1)
81 2c02675e 2022-12-14 op return (1);
82 2c02675e 2022-12-14 op puts("");
83 2c02675e 2022-12-14 op
84 2c02675e 2022-12-14 op free(tp);
85 2c02675e 2022-12-14 op
86 2c02675e 2022-12-14 op return (0);
87 2c02675e 2022-12-14 op }