Module Name:    src
Committed By:   rillig
Date:           Mon Aug 10 19:53:19 UTC 2020

Modified Files:
        src/usr.bin/make: dir.c job.c main.c nonints.h parse.c str.c suff.c

Log Message:
make(1): replace str_concat with str_concat2 and str_concat3

The new functions have a simpler interface, and str_concat3 is even more
general-purpose, since the middle string is no longer required to be
exactly of length 1.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/usr.bin/make/dir.c
cvs rdiff -u -r1.205 -r1.206 src/usr.bin/make/job.c
cvs rdiff -u -r1.302 -r1.303 src/usr.bin/make/main.c
cvs rdiff -u -r1.92 -r1.93 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.250 -r1.251 src/usr.bin/make/parse.c
cvs rdiff -u -r1.58 -r1.59 src/usr.bin/make/str.c
cvs rdiff -u -r1.94 -r1.95 src/usr.bin/make/suff.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.86 src/usr.bin/make/dir.c:1.87
--- src/usr.bin/make/dir.c:1.86	Mon Aug 10 19:30:30 2020
+++ src/usr.bin/make/dir.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.86 2020/08/10 19:30:30 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 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.86 2020/08/10 19:30:30 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 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.86 2020/08/10 19:30:30 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -640,8 +640,7 @@ DirMatchFiles(const char *pattern, Path 
 	{
 	    (void)Lst_AtEnd(expansions,
 			    (isDot ? bmake_strdup(entry->name) :
-			     str_concat(p->name, entry->name,
-					STR_ADDSLASH)));
+			     str_concat3(p->name, "/", entry->name)));
 	}
     }
     return 0;
@@ -981,7 +980,7 @@ DirLookup(Path *p, const char *name MAKE
     if (Hash_FindEntry(&p->files, cp) == NULL)
 	return NULL;
 
-    file = str_concat(p->name, cp, STR_ADDSLASH);
+    file = str_concat3(p->name, "/", cp);
     if (DEBUG(DIR)) {
 	fprintf(debug_file, "   returning %s\n", file);
     }
@@ -1012,7 +1011,7 @@ DirLookupSubdir(Path *p, const char *nam
     char *file;			/* the current filename to check */
 
     if (p != dot) {
-	file = str_concat(p->name, name, STR_ADDSLASH);
+	file = str_concat3(p->name, "/", name);
     } else {
 	/*
 	 * Checking in dot -- DON'T put a leading ./ on the thing.
@@ -1117,7 +1116,7 @@ DirFindDot(Boolean hasSlash MAKE_ATTR_UN
 	}
 	hits += 1;
 	cur->hits += 1;
-	return str_concat(cur->name, cp, STR_ADDSLASH);
+	return str_concat3(cur->name, "/", cp);
     }
 
     return NULL;
@@ -1735,8 +1734,8 @@ Dir_MakeFlags(const char *flag, Lst path
     if (Lst_Open(path) == SUCCESS) {
 	while ((ln = Lst_Next(path)) != NULL) {
 	    p = (Path *)Lst_Datum(ln);
-	    s2 = str_concat(flag, p->name, STR_ADDNONE);
-	    str = str_concat(s1 = str, s2, STR_ADDSPACE);
+	    s2 = str_concat2(flag, p->name);
+	    str = str_concat3(s1 = str, " ", s2);
 	    free(s1);
 	    free(s2);
 	}

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.205 src/usr.bin/make/job.c:1.206
--- src/usr.bin/make/job.c:1.205	Sat Aug  1 14:47:49 2020
+++ src/usr.bin/make/job.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $	*/
+/*	$NetBSD: job.c,v 1.206 2020/08/10 19:53:19 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: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $";
+static char rcsid[] = "$NetBSD: job.c,v 1.206 2020/08/10 19:53:19 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)job.c	8.2 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $");
+__RCSID("$NetBSD: job.c,v 1.206 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2199,7 +2199,7 @@ Shell_Init(void)
 	    shellName++;
 	} else
 #endif
-	shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
+	shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
     }
     if (commandShell->exit == NULL) {
 	commandShell->exit = "";

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.302 src/usr.bin/make/main.c:1.303
--- src/usr.bin/make/main.c:1.302	Sun Aug  9 09:44:14 2020
+++ src/usr.bin/make/main.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -705,7 +705,7 @@ Main_ParseArgLine(const char *line)
 	if (!*line)
 		return;
 
-	buf = str_concat(argv0, line, STR_ADDSPACE);
+	buf = str_concat3(argv0, " ", line);
 	free(p1);
 
 	argv = brk_string(buf, &argc, TRUE, &args);

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.92 src/usr.bin/make/nonints.h:1.93
--- src/usr.bin/make/nonints.h:1.92	Mon Aug 10 19:30:30 2020
+++ src/usr.bin/make/nonints.h	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.92 2020/08/10 19:30:30 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.93 2020/08/10 19:53:19 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -134,14 +134,8 @@ void Parse_SetInput(const char *, int, i
 Lst Parse_MainName(void);
 
 /* str.c */
-
-typedef enum {
-    STR_ADDNONE,		/* just concat the two strings */
-    STR_ADDSPACE,		/* add a space between the two strings */
-    STR_ADDSLASH		/* add a slash between the two strings */
-} StrConcatMode;
-
-char *str_concat(const char *, const char *, StrConcatMode);
+char *str_concat2(const char *, const char *);
+char *str_concat3(const char *, const char *, const char *);
 char **brk_string(const char *, int *, Boolean, char **);
 char *Str_FindSubstring(const char *, const char *);
 Boolean Str_Match(const char *, const char *);

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.250 src/usr.bin/make/parse.c:1.251
--- src/usr.bin/make/parse.c:1.250	Sun Aug  9 13:05:04 2020
+++ src/usr.bin/make/parse.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $";
+static char rcsid[] = "$NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)parse.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2219,7 +2219,7 @@ Parse_include_file(char *file, Boolean i
 		    break;
 		*prefEnd = '\0';
 	    }
-	    newName = str_concat(incdir, file + i, STR_ADDSLASH);
+	    newName = str_concat3(incdir, "/", file + i);
 	    fullname = Dir_FindFile(newName, parseIncPath);
 	    if (fullname == NULL)
 		fullname = Dir_FindFile(newName, dirSearchPath);

Index: src/usr.bin/make/str.c
diff -u src/usr.bin/make/str.c:1.58 src/usr.bin/make/str.c:1.59
--- src/usr.bin/make/str.c:1.58	Mon Aug 10 19:30:30 2020
+++ src/usr.bin/make/str.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $	*/
+/*	$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,49 +69,43 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $";
+static char rcsid[] = "$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char     sccsid[] = "@(#)str.c	5.8 (Berkeley) 6/1/90";
 #else
-__RCSID("$NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $");
+__RCSID("$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif				/* not lint */
 #endif
 
 #include "make.h"
 
-/*-
- * str_concat --
- *	concatenate the two strings, inserting a space or slash between them.
- *
- * returns --
- *	the resulting string in allocated space.
- */
+/* Return the concatenation of s1 and s2, freshly allocated. */
 char *
-str_concat(const char *s1, const char *s2, StrConcatMode mode)
+str_concat2(const char *s1, const char *s2)
 {
 	size_t len1 = strlen(s1);
 	size_t len2 = strlen(s2);
-	char *result = bmake_malloc(len1 + 1 + len2 + 1);
-
-	/* copy first string into place */
+	char *result = bmake_malloc(len1 + len2 + 1);
 	memcpy(result, s1, len1);
-
-	/* add separator character */
-	if (mode == STR_ADDSPACE) {
-		result[len1] = ' ';
-		++len1;
-	} else if (mode == STR_ADDSLASH) {
-		result[len1] = '/';
-		++len1;
-	}
-
-	/* copy second string plus EOS into place */
 	memcpy(result + len1, s2, len2 + 1);
+	return result;
+}
 
+/* Return the concatenation of s1, s2 and s3, freshly allocated. */
+char *
+str_concat3(const char *s1, const char *s2, const char *s3)
+{
+	size_t len1 = strlen(s1);
+	size_t len2 = strlen(s2);
+	size_t len3 = strlen(s3);
+	char *result = bmake_malloc(len1 + len2 + len3 + 1);
+	memcpy(result, s1, len1);
+	memcpy(result + len1, s2, len2);
+	memcpy(result + len1 + len2, s3, len3 + 1);
 	return result;
 }
 

Index: src/usr.bin/make/suff.c
diff -u src/usr.bin/make/suff.c:1.94 src/usr.bin/make/suff.c:1.95
--- src/usr.bin/make/suff.c:1.94	Mon Aug 10 19:30:30 2020
+++ src/usr.bin/make/suff.c	Mon Aug 10 19:53:19 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $	*/
+/*	$NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $";
+static char rcsid[] = "$NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)suff.c	8.4 (Berkeley) 3/21/94";
 #else
-__RCSID("$NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $");
+__RCSID("$NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1222,7 +1222,7 @@ SuffAddSrc(void *sp, void *lsp)
 #endif
     }
     s2 = bmake_malloc(sizeof(Src));
-    s2->file = 	    str_concat(targ->pref, s->name, STR_ADDNONE);
+    s2->file = 	    str_concat2(targ->pref, s->name);
     s2->pref =	    targ->pref;
     s2->parent =    targ;
     s2->node = 	    NULL;
@@ -1821,7 +1821,7 @@ SuffApplyTransform(GNode *tGn, GNode *sG
     /*
      * Locate the transformation rule itself
      */
-    tname = str_concat(s->name, t->name, STR_ADDNONE);
+    tname = str_concat2(s->name, t->name);
     ln = Lst_Find(transforms, tname, SuffGNHasNameP);
     free(tname);
 

Reply via email to