Module Name:    src
Committed By:   rillig
Date:           Wed Aug 12 03:05:57 UTC 2020

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

Log Message:
make(1): make Dir_MakeFlags simpler

This avoids unnecessary string allocations, especially for long lists.

There is no unit test to cover this code.  Since this is an obscure,
undocumented part of make, I wasn't able to come up with a unit test.
Therefore I resorted to write a separate testing program to verify the
expected results.

$ cat <<'EOF' >dir_test.c
#include <stdio.h>

#include "make.h"
#include "dir.h"

int main(int argc, char **argv)
{
    int i;
    Lst lst;
    char *flags;

    lst = Lst_Init(FALSE);
    for (i = 1; i < argc; i++)
        Dir_AddDir(lst, argv[i]);
    flags = Dir_MakeFlags("-I", lst);

    printf("%s\n", flags);
    free(flags);
    return 0;
}
EOF

# Needs some trivial patches to Makefile and main.c
$ make PROG=dir_test EXTRA_SRCS=dir_test.c EXTRA_CPPFLAGS=-DNO_MAIN \
       COPTS.main.c=-Wno-unused-function NOMAN=1

$ ./dir_test a b c

$ mkdir a b c
$ ./dir_test a b c
 -Ia -Ib -Ic
$ rmdir a b c


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/usr.bin/make/dir.c

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.87 src/usr.bin/make/dir.c:1.88
--- src/usr.bin/make/dir.c:1.87	Mon Aug 10 19:53:19 2020
+++ src/usr.bin/make/dir.c	Wed Aug 12 03:05:57 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 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.87 2020/08/10 19:53:19 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 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.87 2020/08/10 19:53:19 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.88 2020/08/12 03:05:57 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1723,26 +1723,22 @@ Dir_CopyDir(void *p)
 char *
 Dir_MakeFlags(const char *flag, Lst path)
 {
-    char *str;			/* the string which will be returned */
-    char *s1, *s2;		/* the current directory preceded by 'flag' */
+    Buffer buf;
     LstNode ln;			/* the node of the current directory */
-    Path *p;			/* the structure describing the current
-				 * directory */
 
-    str = bmake_strdup("");
+    Buf_Init(&buf, 0);
 
     if (Lst_Open(path) == SUCCESS) {
 	while ((ln = Lst_Next(path)) != NULL) {
-	    p = (Path *)Lst_Datum(ln);
-	    s2 = str_concat2(flag, p->name);
-	    str = str_concat3(s1 = str, " ", s2);
-	    free(s1);
-	    free(s2);
+	    Path *p = (Path *)Lst_Datum(ln);
+	    Buf_AddStr(&buf, " ");
+	    Buf_AddStr(&buf, flag);
+	    Buf_AddStr(&buf, p->name);
 	}
 	Lst_Close(path);
     }
 
-    return str;
+    return Buf_Destroy(&buf, FALSE);
 }
 
 /*-

Reply via email to