Module Name:    src
Committed By:   rillig
Date:           Sat Nov  7 12:34:49 UTC 2020

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

Log Message:
make(1): extract ArchiveMember_HasName from ArchFindMember

Comparing a string to a space-padded string is complicated enough to be
extracted to a separate function.

The behavior changes a little bit.  Before, when searching for an archive
member with a short name (one that is space-padded in the archive), that
member was not searched using the AR_EFMT1 archive format.  This doesn't
matter in practice though since no regular archive member has a name
starting with "#1/".


To generate a diff of this commit:
cvs rdiff -u -r1.157 -r1.158 src/usr.bin/make/arch.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.157 src/usr.bin/make/arch.c:1.158
--- src/usr.bin/make/arch.c:1.157	Sat Nov  7 11:36:49 2020
+++ src/usr.bin/make/arch.c	Sat Nov  7 12:34:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: arch.c,v 1.157 2020/11/07 11:36:49 rillig Exp $	*/
+/*	$NetBSD: arch.c,v 1.158 2020/11/07 12:34:49 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.157 2020/11/07 11:36:49 rillig Exp $");
+MAKE_RCSID("$NetBSD: arch.c,v 1.158 2020/11/07 12:34:49 rillig Exp $");
 
 #ifdef TARGET_MACHINE
 #undef MAKE_MACHINE
@@ -675,6 +675,23 @@ ArchSVR4Entry(Arch *ar, char *name, size
 #endif
 
 
+static Boolean
+ArchiveMember_HasName(const struct ar_hdr *hdr,
+		      const char *name, size_t namelen)
+{
+    const size_t ar_name_len = sizeof hdr->ar_name;
+    const char *ar_name = hdr->ar_name;
+
+    if (strncmp(ar_name, name, namelen) != 0)
+	return FALSE;
+
+    if (namelen >= ar_name_len)
+	return namelen == ar_name_len;
+
+    /* hdr->ar_name is space-padded to the right. */
+    return ar_name[namelen] == ' ';
+}
+
 /* Locate a member of an archive, given the path of the archive and the path
  * of the desired member.
  *
@@ -739,24 +756,14 @@ ArchFindMember(const char *archive, cons
 	    return NULL;
 	}
 
-	if (strncmp(member, out_arh->ar_name, tlen) == 0) {
-	    /*
-	     * If the member's name doesn't take up the entire 'name' field,
-	     * we have to be careful of matching prefixes. Names are space-
-	     * padded to the right, so if the character in 'name' at the end
-	     * of the matched string is anything but a space, this isn't the
-	     * member we sought.
-	     */
-	    if (tlen != sizeof out_arh->ar_name &&
-		out_arh->ar_name[tlen] != ' ')
-		goto skip;
-
+	if (ArchiveMember_HasName(out_arh, member, len)) {
 	    /*
-	     * To make life easier, we reposition the file at the start
+	     * To make life easier for callers that want to update the
+	     * archive, we reposition the file at the start
 	     * of the header we just read before we return the stream.
 	     * In a more general situation, it might be better to leave
 	     * the file at the actual member, rather than its header, but
-	     * not here...
+	     * not here.
 	     */
 	    if (fseek(arch, -(long)sizeof *out_arh, SEEK_CUR) != 0) {
 		fclose(arch);
@@ -804,7 +811,6 @@ ArchFindMember(const char *archive, cons
 	}
 #endif
 
-skip:
 	/*
 	 * This isn't the member we're after, so we need to advance the
 	 * stream's pointer to the start of the next header. Files are
@@ -820,10 +826,6 @@ skip:
 	}
     }
 
-    /*
-     * We've looked everywhere, but the member is not to be found. Close the
-     * archive and return NULL -- an error.
-     */
     fclose(arch);
     return NULL;
 }

Reply via email to