From 22f0ec20ea3f9058e8563654cd8135df8729fd8f Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Fri, 8 Jul 2016 11:27:12 +0200 Subject: [PATCH] [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH --- ae/178be00f81b95d36be4c277c35ab87c7212396 | 159 ++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 ae/178be00f81b95d36be4c277c35ab87c7212396 diff --git a/ae/178be00f81b95d36be4c277c35ab87c7212396 b/ae/178be00f81b95d36be4c277c35ab87c7212396 new file mode 100644 index 000000000..45ec66263 --- /dev/null +++ b/ae/178be00f81b95d36be4c277c35ab87c7212396 @@ -0,0 +1,159 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id 23DCC6DE0943 + for ; Fri, 8 Jul 2016 03:13:32 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: 0.135 +X-Spam-Level: +X-Spam-Status: No, score=0.135 tagged_above=-999 required=5 tests=[AWL=0.135] + autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id c9fRaG2bnxFs for ; + Fri, 8 Jul 2016 03:13:24 -0700 (PDT) +Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118]) + by arlo.cworth.org (Postfix) with ESMTP id 8F11E6DE02AC + for ; Fri, 8 Jul 2016 03:13:09 -0700 (PDT) +Received: from fifthhorseman.net (unknown [88.128.80.54]) + by che.mayfirst.org (Postfix) with ESMTPSA id A5144F98B + for ; Fri, 8 Jul 2016 06:13:08 -0400 (EDT) +Received: by fifthhorseman.net (Postfix, from userid 1000) + id 7F87D205C3; Fri, 8 Jul 2016 11:27:34 +0200 (CEST) +From: Daniel Kahn Gillmor +To: Notmuch Mail +Subject: [PATCH v4 01/16] add util/search-path.{c, + h} to test for executables in $PATH +Date: Fri, 8 Jul 2016 11:27:12 +0200 +Message-Id: <1467970047-8013-2-git-send-email-dkg@fifthhorseman.net> +X-Mailer: git-send-email 2.8.1 +In-Reply-To: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net> +References: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 08 Jul 2016 10:13:32 -0000 + +This is a utility function we can use to see whether an executable is +available. +--- + util/Makefile.local | 2 +- + util/search-path.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ + util/search-path.h | 24 ++++++++++++++++++++++++ + 3 files changed, 75 insertions(+), 1 deletion(-) + create mode 100644 util/search-path.c + create mode 100644 util/search-path.h + +diff --git a/util/Makefile.local b/util/Makefile.local +index 905f237..8b2b91b 100644 +--- a/util/Makefile.local ++++ b/util/Makefile.local +@@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir) + + libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ + $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \ +- $(dir)/util.c ++ $(dir)/util.c $(dir)/search-path.c + + libutil_modules := $(libutil_c_srcs:.c=.o) + +diff --git a/util/search-path.c b/util/search-path.c +new file mode 100644 +index 0000000..9da21cb +--- /dev/null ++++ b/util/search-path.c +@@ -0,0 +1,50 @@ ++#include "search-path.h" ++#include ++#include ++#include ++#include ++#include ++#include ++ ++ ++notmuch_bool_t ++test_for_executable (const char *exename) ++{ ++ char *path = NULL, *save = NULL, *tok; ++ notmuch_bool_t ret = FALSE; ++ ++ if (strchr (exename, '/')) { ++ if (0 == access (exename, X_OK)) ++ return TRUE; ++ else ++ return FALSE; ++ } ++ ++ path = getenv ("PATH"); ++ if (path) ++ path = strdup (path); ++ else { ++ size_t n = confstr (_CS_PATH, NULL, 0); ++ path = (char *) malloc (n); ++ if (! path) ++ return FALSE; ++ confstr (_CS_PATH, path, n); ++ } ++ ++ tok = strtok_r (path, ":", &save); ++ while (tok) { ++ int dir_fd = open (tok, O_DIRECTORY | O_RDONLY); ++ if (dir_fd != -1) { ++ int access = faccessat (dir_fd, exename, X_OK, 0); ++ close (dir_fd); ++ if (access == 0) { ++ ret = TRUE; ++ break; ++ } ++ } ++ tok = strtok_r (NULL, ":", &save); ++ } ++ if (path) ++ free (path); ++ return ret; ++} +diff --git a/util/search-path.h b/util/search-path.h +new file mode 100644 +index 0000000..14c4d14 +--- /dev/null ++++ b/util/search-path.h +@@ -0,0 +1,24 @@ ++#ifndef _SEARCH_PATH_H ++#define _SEARCH_PATH_H ++ ++#include "notmuch.h" ++ ++/* can an executable be found with the given name? ++ * ++ * Return TRUE only if we can find something to execute with the ++ * associated name. ++ * ++ * if the name has a '/' in it, we look for it directly with ++ * access(exename, X_OK). ++ * ++ * otherwise, we look for it in $PATH (or in confstr(_CS_PATH), if ++ * $PATH is unset). ++ * ++ * This should match the logic for execvp (as well as matching user ++ * expectations, hopefully). ++ */ ++ ++notmuch_bool_t ++test_for_executable (const char *exename); ++ ++#endif +-- +2.8.1 + -- 2.26.2