Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/mkdep mkdep: avoid memory allocation in findcc
details: https://anonhg.NetBSD.org/src/rev/5db05524eb9c
branches: trunk
changeset: 1023034:5db05524eb9c
user: rillig <rillig%NetBSD.org@localhost>
date: Fri Aug 20 06:36:10 2021 +0000
description:
mkdep: avoid memory allocation in findcc
This change takes the idea of handling strings as pairs in the form
(start, len) by Robert Elz from
https://mail-index.netbsd.org/source-changes-d/2021/08/20/msg013427.html
and expands it by avoiding one more memory allocation, for iterating the
PATH environment variable.
No functional change.
diffstat:
tests/usr.bin/mkdep/Makefile | 3 +-
tests/usr.bin/mkdep/t_findcc.sh | 29 ++++++++++++++++++-
usr.bin/mkdep/findcc.c | 63 ++++++++++++++++++----------------------
3 files changed, 58 insertions(+), 37 deletions(-)
diffs (158 lines):
diff -r 57b1c22b5236 -r 5db05524eb9c tests/usr.bin/mkdep/Makefile
--- a/tests/usr.bin/mkdep/Makefile Fri Aug 20 05:45:19 2021 +0000
+++ b/tests/usr.bin/mkdep/Makefile Fri Aug 20 06:36:10 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.2 2021/08/11 20:42:26 rillig Exp $
+# $NetBSD: Makefile,v 1.3 2021/08/20 06:36:10 rillig Exp $
.include <bsd.own.mk>
@@ -13,5 +13,6 @@
SRCS= h_findcc.c findcc.c
CPPFLAGS+= -I${NETBSDSRCDIR}/usr.bin/mkdep
MAN.h_findcc= # none
+WARNS= 6
.include <bsd.test.mk>
diff -r 57b1c22b5236 -r 5db05524eb9c tests/usr.bin/mkdep/t_findcc.sh
--- a/tests/usr.bin/mkdep/t_findcc.sh Fri Aug 20 05:45:19 2021 +0000
+++ b/tests/usr.bin/mkdep/t_findcc.sh Fri Aug 20 06:36:10 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_findcc.sh,v 1.2 2021/08/20 05:45:19 rillig Exp $
+# $NetBSD: t_findcc.sh,v 1.3 2021/08/20 06:36:10 rillig Exp $
#
# Copyright (c) 2021 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -51,6 +51,31 @@
"$(atf_get_srcdir)"/h_findcc 'echo'
}
+# A plain program name is searched in the PATH and, in this example, it is
+# found in '/bin', which comes second in the PATH.
+#
+atf_test_case base_found_second
+base_found_second_body() {
+ atf_check -o "inline:/bin/echo$n" \
+ env -i PATH='/nonexistent:/bin' \
+ "$(atf_get_srcdir)"/h_findcc 'echo'
+}
+
+# A plain program name is searched in the PATH and, in this example, it is
+# found in './bin', a relative path in the PATH, which is rather unusual in
+# practice.
+#
+atf_test_case base_found_reldir
+base_found_reldir_body() {
+ mkdir bin
+ echo '#! /bin/sh' > 'bin/reldir-echo'
+ chmod +x 'bin/reldir-echo'
+
+ atf_check -o "inline:bin/reldir-echo$n" \
+ env -i PATH='/nonexistent:bin' \
+ "$(atf_get_srcdir)"/h_findcc 'reldir-echo'
+}
+
# The C compiler can be specified as a program with one or more arguments.
# If the program name is a plain name without any slash, the argument is
# discarded.
@@ -143,6 +168,8 @@
atf_init_test_cases() {
atf_add_test_case base_not_found
atf_add_test_case base_found
+ atf_add_test_case base_found_second
+ atf_add_test_case base_found_reldir
atf_add_test_case base_arg_found
atf_add_test_case rel_not_found
diff -r 57b1c22b5236 -r 5db05524eb9c usr.bin/mkdep/findcc.c
--- a/usr.bin/mkdep/findcc.c Fri Aug 20 05:45:19 2021 +0000
+++ b/usr.bin/mkdep/findcc.c Fri Aug 20 06:36:10 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: findcc.c,v 1.9 2021/08/20 05:45:19 rillig Exp $ */
+/* $NetBSD: findcc.c,v 1.10 2021/08/20 06:36:10 rillig Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
#if !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\
All rights reserved.");
-__RCSID("$NetBSD: findcc.c,v 1.9 2021/08/20 05:45:19 rillig Exp $");
+__RCSID("$NetBSD: findcc.c,v 1.10 2021/08/20 06:36:10 rillig Exp $");
#endif /* not lint */
#include <sys/param.h>
@@ -49,47 +49,40 @@
#include "findcc.h"
char *
-findcc(const char *cc_command)
+findcc(const char *progname)
{
- char *progname, *path, *dir, *next;
- char buffer[MAXPATHLEN];
-
- if ((progname = strdup(cc_command)) == NULL)
- return NULL;
+ char *cc;
+ const char *path, *dir;
+ char buffer[MAXPATHLEN];
+ size_t progname_len, dir_len;
- if ((next = strchr(progname, ' ')) != NULL)
- *next = '\0';
+ progname_len = strcspn(progname, " ");
- if (strchr(progname, '/') != NULL) {
- if (access(progname, X_OK) == 0)
- return progname;
- free(progname);
+ if (memchr(progname, '/', progname_len) != NULL) {
+ if ((cc = strndup(progname, progname_len)) == NULL)
+ return NULL;
+ if (access(cc, X_OK) == 0)
+ return cc;
+ free(cc);
return NULL;
}
- if (((path = getenv("PATH")) == NULL) ||
- ((path = strdup(path)) == NULL)) {
- free(progname);
+ if ((path = getenv("PATH")) == NULL)
return NULL;
+
+ for (dir = path; *dir != '\0'; ) {
+ dir_len = strcspn(dir, ":");
+
+ if ((size_t)snprintf(buffer, sizeof(buffer), "%.*s/%.*s",
+ (int)dir_len, dir, (int)progname_len, progname)
+ < sizeof(buffer)
+ && access(buffer, X_OK) == 0)
+ return strdup(buffer);
+
+ dir += dir_len;
+ if (*dir == ':')
+ dir++;
}
- dir = path;
- while (dir != NULL) {
- if ((next = strchr(dir, ':')) != NULL)
- *next++ = '\0';
-
- if (snprintf(buffer, sizeof(buffer),
- "%s/%s", dir, progname) < (int)sizeof(buffer)) {
- if (access(buffer, X_OK) == 0) {
- free(path);
- free(progname);
- return strdup(buffer);
- }
- }
- dir = next;
- }
-
- free(path);
- free(progname);
return NULL;
}
Home |
Main Index |
Thread Index |
Old Index