Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/make make(1): add str_basename to reduce duplicate code
details: https://anonhg.NetBSD.org/src/rev/f3a6bae11203
branches: trunk
changeset: 979105:f3a6bae11203
user: rillig <rillig%NetBSD.org@localhost>
date: Sun Dec 13 20:14:48 2020 +0000
description:
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.
diffstat:
usr.bin/make/arch.c | 13 ++++---------
usr.bin/make/dir.c | 19 +++++++------------
usr.bin/make/job.c | 6 +++---
usr.bin/make/main.c | 9 +++------
usr.bin/make/meta.c | 8 ++------
usr.bin/make/nonints.h | 9 ++++++++-
usr.bin/make/parse.c | 10 ++++------
usr.bin/make/suff.c | 32 +++++++-------------------------
usr.bin/make/var.c | 8 +++-----
9 files changed, 41 insertions(+), 73 deletions(-)
diffs (truncated from 361 to 300 lines):
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/arch.c
--- a/usr.bin/make/arch.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/arch.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
* 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 @@
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 @@
* 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);
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/dir.c
--- a/usr.bin/make/dir.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/dir.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
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 @@
* 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 @@
* 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 @@
{
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)
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/job.c
--- a/usr.bin/make/job.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/job.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
#ifdef DEFSHELL_CUSTOM
if (shellName[0] == '/') {
shellPath = shellName;
- shellName = strrchr(shellPath, '/') + 1;
+ shellName = str_basename(shellPath);
return;
}
#endif
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/main.c
--- a/usr.bin/make/main.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/main.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
InitRandom();
- if ((progname = strrchr(argv[0], '/')) != NULL)
- progname++;
- else
- progname = argv[0];
+ progname = str_basename(argv[0]);
UnlimitFiles();
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/meta.c
--- a/usr.bin/make/meta.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/meta.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
free(mp);
}
/* Get the basename of the target */
- if ((cp = strrchr(tname, '/')) == NULL) {
- cp = tname;
- } else {
- cp++;
- }
+ cp = str_basename(tname);
fflush(stdout);
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/nonints.h Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
/* 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(); }
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/parse.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
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);
diff -r 0a708dda5ff8 -r f3a6bae11203 usr.bin/make/suff.c
--- a/usr.bin/make/suff.c Sun Dec 13 20:09:02 2020 +0000
+++ b/usr.bin/make/suff.c Sun Dec 13 20:14:48 2020 +0000
@@ -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 @@
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 @@
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 @@
*/
size_t savep = strlen(gn->path) - targ->suff->nameLen;
char savec;
- char *ptr;
Suffix_Reassign(&gn->suffix, targ->suff);
Home |
Main Index |
Thread Index |
Old Index