Module Name:    src
Committed By:   rillig
Date:           Sat Aug 22 09:40:18 UTC 2020

Modified Files:
        src/usr.bin/make: dir.c lst.c lst.h

Log Message:
make(1): add Lst_Append to add an item at the end of the list

The previous variant of using a special case of Lst_InsertAfter was
unnecessarily complicated.  Linked lists are a very basic data
structure, and there is no need to overcomplicate things by introducing
unnecessary conditions and branches.


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/usr.bin/make/dir.c
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/make/lst.c
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/make/lst.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/dir.c
diff -u src/usr.bin/make/dir.c:1.98 src/usr.bin/make/dir.c:1.99
--- src/usr.bin/make/dir.c:1.98	Sat Aug 22 09:03:53 2020
+++ src/usr.bin/make/dir.c	Sat Aug 22 09:40:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.98 2020/08/22 09:03:53 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.99 2020/08/22 09:40:18 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: dir.c,v 1.98 2020/08/22 09:03:53 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.99 2020/08/22 09:40:18 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)dir.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: dir.c,v 1.98 2020/08/22 09:03:53 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.99 2020/08/22 09:40:18 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -768,7 +768,7 @@ DirExpandCurly(const char *word, const c
 	    Dir_Expand(file, path, expansions);
 	    free(file);
 	} else {
-	    (void)Lst_AtEnd(expansions, file);
+	    Lst_AppendS(expansions, file);
 	}
 
 	piece = piece_end + 1;	/* skip over the comma or closing brace */

Index: src/usr.bin/make/lst.c
diff -u src/usr.bin/make/lst.c:1.20 src/usr.bin/make/lst.c:1.21
--- src/usr.bin/make/lst.c:1.20	Sat Aug 22 07:26:34 2020
+++ src/usr.bin/make/lst.c	Sat Aug 22 09:40:18 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.20 2020/08/22 07:26:34 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.21 2020/08/22 09:40:18 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -37,11 +37,11 @@
 #include "make.h"
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: lst.c,v 1.20 2020/08/22 07:26:34 rillig Exp $";
+static char rcsid[] = "$NetBSD: lst.c,v 1.21 2020/08/22 09:40:18 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: lst.c,v 1.20 2020/08/22 07:26:34 rillig Exp $");
+__RCSID("$NetBSD: lst.c,v 1.21 2020/08/22 09:40:18 rillig Exp $");
 #endif /* not lint */
 #endif
 
@@ -64,8 +64,8 @@ struct List {
     LstNode last;		/* last node in list */
 
     /* fields for sequential access */
-    Where lastAccess;		/* Where in the list the last access was */
     Boolean isOpen;		/* true if list has been Lst_Open'ed */
+    Where lastAccess;		/* Where in the list the last access was */
     LstNode curr;		/* current node, if open. NULL if
 				 * *just* opened */
     LstNode prev;		/* Previous node, if open. Used by Lst_Remove */
@@ -280,6 +280,28 @@ Lst_AtEnd(Lst list, void *datum)
     return Lst_InsertAfter(list, end, datum);
 }
 
+/* Add a piece of data at the end of the given list. */
+void
+Lst_AppendS(Lst list, void *datum)
+{
+    LstNode node;
+
+    assert(LstIsValid(list));
+    assert(datum != NULL);
+
+    node = LstNodeNew(datum);
+    node->prev = list->last;
+    node->next = NULL;
+
+    if (list->last == NULL) {
+	list->first = node;
+	list->last = node;
+    } else {
+	list->last->next = node;
+	list->last = node;
+    }
+}
+
 /* Remove the given node from the given list.
  * The datum stored in the node must be freed by the caller, if necessary. */
 void

Index: src/usr.bin/make/lst.h
diff -u src/usr.bin/make/lst.h:1.27 src/usr.bin/make/lst.h:1.28
--- src/usr.bin/make/lst.h:1.27	Fri Aug 21 07:00:32 2020
+++ src/usr.bin/make/lst.h	Sat Aug 22 09:40:18 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: lst.h,v 1.27 2020/08/21 07:00:32 rillig Exp $	*/
+/*	$NetBSD: lst.h,v 1.28 2020/08/22 09:40:18 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -121,6 +121,7 @@ ReturnStatus	Lst_InsertAfter(Lst, LstNod
 ReturnStatus	Lst_AtFront(Lst, void *);
 /* Place an element at the end of a lst. */
 ReturnStatus	Lst_AtEnd(Lst, void *);
+void		Lst_AppendS(Lst, void *);
 /* Remove an element */
 void		Lst_RemoveS(Lst, LstNode);
 /* Replace a node with a new value */

Reply via email to