From 4ba5c9d015f02eb1fda30de26c23690dd41731fc Mon Sep 17 00:00:00 2001 From: Faraz Vahedi Date: Tue, 8 Oct 2024 13:11:42 +0330 Subject: [PATCH] paste(1): Utilise STAILQ from in lieu of the home-rolled linked-list Signed-off-by: Faraz Vahedi Reviewed by: imp, oshogbo Pull Request: https://github.com/freebsd/freebsd-src/pull/1443 --- usr.bin/paste/paste.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/usr.bin/paste/paste.c b/usr.bin/paste/paste.c index 8114a85a869..fa8be54ebe5 100644 --- a/usr.bin/paste/paste.c +++ b/usr.bin/paste/paste.c @@ -33,6 +33,7 @@ */ #include +#include #include #include @@ -106,43 +107,43 @@ main(int argc, char *argv[]) } typedef struct _list { - struct _list *next; + STAILQ_ENTRY(_list) entries; FILE *fp; int cnt; char *name; } LIST; +static STAILQ_HEAD(head, _list) lh; + static int parallel(char **argv) { + struct head lh; LIST *lp; int cnt; wint_t ich; wchar_t ch; char *p; - LIST *head, *tmp; int opencnt, output; - for (cnt = 0, head = tmp = NULL; (p = *argv); ++argv, ++cnt) { + STAILQ_INIT(&lh); + + for (cnt = 0; (p = *argv); ++argv, ++cnt) { if ((lp = malloc(sizeof(LIST))) == NULL) err(1, NULL); if (p[0] == '-' && !p[1]) lp->fp = stdin; else if (!(lp->fp = fopen(p, "r"))) err(1, "%s", p); - lp->next = NULL; lp->cnt = cnt; lp->name = p; - if (!head) - head = tmp = lp; - else { - tmp->next = lp; - tmp = lp; - } + + STAILQ_INSERT_TAIL(&lh, lp, entries); } for (opencnt = cnt; opencnt;) { - for (output = 0, lp = head; lp; lp = lp->next) { + output = 0; + STAILQ_FOREACH(lp, &lh, entries) { if (!lp->fp) { if (output && lp->cnt && (ch = delim[(lp->cnt - 1) % delimcnt]))