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.
This commit is contained in:
rillig 2020-12-13 20:14:48 +00:00
parent 9ab63ee0aa
commit 7f604d4793
9 changed files with 41 additions and 73 deletions

View File

@ -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, const char *member, Boolean addToCache)
* 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, const char *member, struct ar_hdr *out_arh,
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, const char *member, struct ar_hdr *out_arh,
* 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);

View File

@ -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, SearchPath *path)
* 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, SearchPath *path)
* 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)

View File

@ -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

View File

@ -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();

View File

@ -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);

View File

@ -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(); }

View File

@ -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, size_t lineno)
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);

View File

@ -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, CandidateSearcher *cs)
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, CandidateSearcher *cs)
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 *targ)
*/
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);
}
}

View File

@ -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 *buf, void *dummy MAKE_ATTR_UNUSED)
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.