Blame


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