Module Name:    src
Committed By:   rillig
Date:           Sun Dec 13 20:14:48 UTC 2020

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

Log Message:
make(1): add str_basename to reduce duplicate code

The function basename from POSIX has a few unfortunate properties, it is
allowed to return a pointer to static memory.  This is too unreliable,
therefore this trivial own implementation.


To generate a diff of this commit:
cvs rdiff -u -r1.187 -r1.188 src/usr.bin/make/arch.c
cvs rdiff -u -r1.251 -r1.252 src/usr.bin/make/dir.c
cvs rdiff -u -r1.386 -r1.387 src/usr.bin/make/job.c
cvs rdiff -u -r1.497 -r1.498 src/usr.bin/make/main.c
cvs rdiff -u -r1.158 -r1.159 src/usr.bin/make/meta.c
cvs rdiff -u -r1.170 -r1.171 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.479 -r1.480 src/usr.bin/make/parse.c
cvs rdiff -u -r1.329 -r1.330 src/usr.bin/make/suff.c
cvs rdiff -u -r1.732 -r1.733 src/usr.bin/make/var.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/arch.c
diff -u src/usr.bin/make/arch.c:1.187 src/usr.bin/make/arch.c:1.188
--- src/usr.bin/make/arch.c:1.187	Sun Dec  6 18:13:17 2020
+++ src/usr.bin/make/arch.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: arch.c,v 1.187 2020/12/06 18:13:17 rillig Exp $	*/
+/*	$NetBSD: arch.c,v 1.188 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -125,7 +125,7 @@
 #include "config.h"
 
 /*	"@(#)arch.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: arch.c,v 1.187 2020/12/06 18:13:17 rillig Exp $");
+MAKE_RCSID("$NetBSD: arch.c,v 1.188 2020/12/13 20:14:48 rillig Exp $");
 
 typedef struct List ArchList;
 typedef struct ListNode ArchListNode;
@@ -420,9 +420,7 @@ ArchStatMember(const char *archive, cons
 	 * Because of space constraints and similar things, files are archived
 	 * using their basename, not the entire path.
 	 */
-	const char *lastSlash = strrchr(member, '/');
-	if (lastSlash != NULL)
-		member = lastSlash + 1;
+	member = str_basename(member);
 
 	for (ln = archives.first; ln != NULL; ln = ln->next) {
 		const Arch *a = ln->datum;
@@ -719,7 +717,6 @@ ArchFindMember(const char *archive, cons
 	int size;		/* Size of archive member */
 	char magic[SARMAG];
 	size_t len;
-	const char *lastSlash;
 
 	arch = fopen(archive, mode);
 	if (arch == NULL)
@@ -739,9 +736,7 @@ ArchFindMember(const char *archive, cons
 	 * Because of space constraints and similar things, files are archived
 	 * using their basename, not the entire path.
 	 */
-	lastSlash = strrchr(member, '/');
-	if (lastSlash != NULL)
-		member = lastSlash + 1;
+	member = str_basename(member);
 
 	len = strlen(member);
 

Index: src/usr.bin/make/dir.c
diff -u src/usr.bin/make/dir.c:1.251 src/usr.bin/make/dir.c:1.252
--- src/usr.bin/make/dir.c:1.251	Sun Dec  6 18:13:17 2020
+++ src/usr.bin/make/dir.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.251 2020/12/06 18:13:17 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.252 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -136,7 +136,7 @@
 #include "job.h"
 
 /*	"@(#)dir.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: dir.c,v 1.251 2020/12/06 18:13:17 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.252 2020/12/13 20:14:48 rillig Exp $");
 
 /* A search path is a list of CachedDir structures. A CachedDir has in it the
  * name of the directory and the names of all the files in the directory.
@@ -1048,14 +1048,10 @@ char *
 Dir_FindFile(const char *name, SearchPath *path)
 {
 	char *file;		/* the current filename to check */
-	const char *lastSlash;	/* the last slash in name */
-	const char *base;	/* basename(name) */
 	Boolean seenDotLast = FALSE; /* true if we should search dot last */
 	struct cached_stat cst;	/* Buffer for stat, if necessary */
 	const char *trailing_dot = ".";
-
-	lastSlash = strrchr(name, '/');
-	base = lastSlash != NULL ? lastSlash + 1 : name;
+	const char *base = str_basename(name);
 
 	DEBUG1(DIR, "Searching for %s ...", name);
 
@@ -1079,7 +1075,7 @@ Dir_FindFile(const char *name, SearchPat
 	 * directory component is exactly `./', consult the cached contents
 	 * of each of the directories on the search path.
 	 */
-	if (lastSlash == NULL || (base - name == 2 && *name == '.')) {
+	if (base == name || (base - name == 2 && *name == '.')) {
 		SearchPathNode *ln;
 
 		/*
@@ -1125,7 +1121,7 @@ Dir_FindFile(const char *name, SearchPat
 	 * end).]
 	 * This phase is only performed if the file is *not* absolute.
 	 */
-	if (lastSlash == NULL) {
+	if (base == name) {
 		DEBUG0(DIR, "   failed.\n");
 		misses++;
 		return NULL;
@@ -1357,10 +1353,9 @@ ResolveMovedDepends(GNode *gn)
 {
 	char *fullName;
 
-	char *base = strrchr(gn->name, '/');
-	if (base == NULL)
+	const char *base = str_basename(gn->name);
+	if (base == gn->name)
 		return NULL;
-	base++;
 
 	fullName = Dir_FindFile(base, Suff_FindPath(gn));
 	if (fullName == NULL)

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.386 src/usr.bin/make/job.c:1.387
--- src/usr.bin/make/job.c:1.386	Sun Dec 13 02:01:43 2020
+++ src/usr.bin/make/job.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: job.c,v 1.386 2020/12/13 02:01:43 rillig Exp $	*/
+/*	$NetBSD: job.c,v 1.387 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -143,7 +143,7 @@
 #include "trace.h"
 
 /*	"@(#)job.c	8.2 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: job.c,v 1.386 2020/12/13 02:01:43 rillig Exp $");
+MAKE_RCSID("$NetBSD: job.c,v 1.387 2020/12/13 20:14:48 rillig Exp $");
 
 /*
  * A shell defines how the commands are run.  All commands for a target are
@@ -2111,7 +2111,7 @@ InitShellNameAndPath(void)
 #ifdef DEFSHELL_CUSTOM
 	if (shellName[0] == '/') {
 		shellPath = shellName;
-		shellName = strrchr(shellPath, '/') + 1;
+		shellName = str_basename(shellPath);
 		return;
 	}
 #endif

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.497 src/usr.bin/make/main.c:1.498
--- src/usr.bin/make/main.c:1.497	Sun Dec 13 20:09:02 2020
+++ src/usr.bin/make/main.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.497 2020/12/13 20:09:02 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.498 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -109,7 +109,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.497 2020/12/13 20:09:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.498 2020/12/13 20:14:48 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1350,10 +1350,7 @@ main_Init(int argc, char **argv)
 
 	InitRandom();
 
-	if ((progname = strrchr(argv[0], '/')) != NULL)
-		progname++;
-	else
-		progname = argv[0];
+	progname = str_basename(argv[0]);
 
 	UnlimitFiles();
 

Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.158 src/usr.bin/make/meta.c:1.159
--- src/usr.bin/make/meta.c:1.158	Thu Dec 10 20:49:11 2020
+++ src/usr.bin/make/meta.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.158 2020/12/10 20:49:11 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.159 2020/12/13 20:14:48 rillig Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -500,11 +500,7 @@ meta_create(BuildMon *pbm, GNode *gn)
 	free(mp);
     }
     /* Get the basename of the target */
-    if ((cp = strrchr(tname, '/')) == NULL) {
-	cp = tname;
-    } else {
-	cp++;
-    }
+    cp = str_basename(tname);
 
     fflush(stdout);
 

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.170 src/usr.bin/make/nonints.h:1.171
--- src/usr.bin/make/nonints.h:1.170	Sun Dec 13 02:15:49 2020
+++ src/usr.bin/make/nonints.h	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.170 2020/12/13 02:15:49 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.171 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -98,6 +98,13 @@ unsigned int Cond_save_depth(void);
 
 /* dir.c; see also dir.h */
 
+MAKE_INLINE const char *
+str_basename(const char *pathname)
+{
+	const char *lastSlash = strrchr(pathname, '/');
+	return lastSlash != NULL ? lastSlash + 1 : pathname;
+}
+
 MAKE_INLINE SearchPath *
 SearchPath_New(void)
 { return Lst_New(); }

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.479 src/usr.bin/make/parse.c:1.480
--- src/usr.bin/make/parse.c:1.479	Sun Dec 13 02:15:49 2020
+++ src/usr.bin/make/parse.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.479 2020/12/13 02:15:49 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.480 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -117,7 +117,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.479 2020/12/13 02:15:49 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.480 2020/12/13 20:14:48 rillig Exp $");
 
 /* types and constants */
 
@@ -632,10 +632,8 @@ PrintLocation(FILE *f, const char *fname
 		dir = realpath(dir, dirbuf);
 
 	base = Var_Value(".PARSEFILE", VAR_GLOBAL, &base_freeIt);
-	if (base == NULL) {
-		const char *slash = strrchr(fname, '/');
-		base = slash != NULL ? slash + 1 : fname;
-	}
+	if (base == NULL)
+		base = str_basename(fname);
 
 	(void)fprintf(f, "\"%s/%s\" line %zu: ", dir, base, lineno);
 	bmake_free(base_freeIt);

Index: src/usr.bin/make/suff.c
diff -u src/usr.bin/make/suff.c:1.329 src/usr.bin/make/suff.c:1.330
--- src/usr.bin/make/suff.c:1.329	Mon Dec  7 01:27:08 2020
+++ src/usr.bin/make/suff.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.329 2020/12/07 01:27:08 rillig Exp $	*/
+/*	$NetBSD: suff.c,v 1.330 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -114,7 +114,7 @@
 #include "dir.h"
 
 /*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
-MAKE_RCSID("$NetBSD: suff.c,v 1.329 2020/12/07 01:27:08 rillig Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.330 2020/12/13 20:14:48 rillig Exp $");
 
 typedef List SuffixList;
 typedef ListNode SuffixListNode;
@@ -1171,12 +1171,13 @@ FindCmds(Candidate *targ, CandidateSearc
 	size_t prefLen;		/* The length of the defined prefix */
 	Suffix *suff;		/* Suffix on matching beastie */
 	Candidate *ret;		/* Return value */
-	char *cp;
 
 	tgn = targ->node;
 	prefLen = strlen(targ->prefix);
 
 	for (gln = tgn->children.first; gln != NULL; gln = gln->next) {
+		const char *cp;
+
 		sgn = gln->datum;
 
 		if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(&tgn->commands)) {
@@ -1190,12 +1191,7 @@ FindCmds(Candidate *targ, CandidateSearc
 			continue;
 		}
 
-		cp = strrchr(sgn->name, '/');
-		if (cp == NULL) {
-			cp = sgn->name;
-		} else {
-			cp++;
-		}
+		cp = str_basename(sgn->name);
 		if (strncmp(cp, targ->prefix, prefLen) != 0)
 			continue;
 		/* The node matches the prefix, see if it has a known suffix. */
@@ -1782,36 +1778,22 @@ FindDepsRegularPath(GNode *gn, Candidate
 		 */
 		size_t savep = strlen(gn->path) - targ->suff->nameLen;
 		char savec;
-		char *ptr;
 
 		Suffix_Reassign(&gn->suffix, targ->suff);
 
 		savec = gn->path[savep];
 		gn->path[savep] = '\0';
 
-		if ((ptr = strrchr(gn->path, '/')) != NULL)
-			ptr++;
-		else
-			ptr = gn->path;
-
-		Var_Set(PREFIX, ptr, gn);
+		Var_Set(PREFIX, str_basename(gn->path), gn);
 
 		gn->path[savep] = savec;
 	} else {
-		char *ptr;
-
 		/*
 		 * The .PREFIX gets the full path if the target has no
 		 * known suffix.
 		 */
 		Suffix_Unassign(&gn->suffix);
-
-		if ((ptr = strrchr(gn->path, '/')) != NULL)
-			ptr++;
-		else
-			ptr = gn->path;
-
-		Var_Set(PREFIX, ptr, gn);
+		Var_Set(PREFIX, str_basename(gn->path), gn);
 	}
 }
 

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.732 src/usr.bin/make/var.c:1.733
--- src/usr.bin/make/var.c:1.732	Sun Dec 13 02:15:49 2020
+++ src/usr.bin/make/var.c	Sun Dec 13 20:14:48 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.732 2020/12/13 02:15:49 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.733 2020/12/13 20:14:48 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.732 2020/12/13 02:15:49 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.733 2020/12/13 20:14:48 rillig Exp $");
 
 /* A string that may need to be freed after use. */
 typedef struct FStr {
@@ -1177,9 +1177,7 @@ ModifyWord_Head(const char *word, SepBuf
 static void
 ModifyWord_Tail(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
 {
-	const char *slash = strrchr(word, '/');
-	const char *base = slash != NULL ? slash + 1 : word;
-	SepBuf_AddStr(buf, base);
+	SepBuf_AddStr(buf, str_basename(word));
 }
 
 /* Callback for ModifyWords to implement the :E modifier.

Reply via email to