From 486b2c21631681e46725585deae4c68965bcb849 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Wed, 20 Jan 2016 21:52:34 +1900 Subject: [PATCH] [PATCH v2 01/16] add util/search-path.{c, h} to test for executables in $PATH --- 03/10f8a3e25d5920f820af44f60443cfc651138f | 164 ++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 03/10f8a3e25d5920f820af44f60443cfc651138f diff --git a/03/10f8a3e25d5920f820af44f60443cfc651138f b/03/10f8a3e25d5920f820af44f60443cfc651138f new file mode 100644 index 000000000..edaf1a5c8 --- /dev/null +++ b/03/10f8a3e25d5920f820af44f60443cfc651138f @@ -0,0 +1,164 @@ +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 079656DE1B25 + for ; Tue, 19 Jan 2016 18:53:18 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.024 +X-Spam-Level: +X-Spam-Status: No, score=-0.024 tagged_above=-999 required=5 + tests=[AWL=-0.024] 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 OjRJebmO2VO5 for ; + Tue, 19 Jan 2016 18:53:15 -0800 (PST) +Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108]) + by arlo.cworth.org (Postfix) with ESMTP id 5BBF26DE13FB + for ; Tue, 19 Jan 2016 18:53:15 -0800 (PST) +Received: from fifthhorseman.net (unknown [38.109.115.130]) + by che.mayfirst.org (Postfix) with ESMTPSA id 6CAECF987 + for ; Tue, 19 Jan 2016 21:53:10 -0500 (EST) +Received: by fifthhorseman.net (Postfix, from userid 1000) + id C82351FEE3; Tue, 19 Jan 2016 18:53:10 -0800 (PST) +From: Daniel Kahn Gillmor +To: Notmuch Mail +Subject: [PATCH v2 01/16] add util/search-path.{c, + h} to test for executables in $PATH +Date: Tue, 19 Jan 2016 21:52:34 -0500 +Message-Id: <1453258369-7366-2-git-send-email-dkg@fifthhorseman.net> +X-Mailer: git-send-email 2.7.0.rc3 +In-Reply-To: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net> +References: <1453258369-7366-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: Wed, 20 Jan 2016 02:53:18 -0000 + +This is a utility function we can use to see whether an executable is +available. +--- + util/Makefile.local | 2 +- + util/search-path.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ + util/search-path.h | 24 +++++++++++++++++++++++ + 3 files changed, 80 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..5eac367 +--- /dev/null ++++ b/util/search-path.c +@@ -0,0 +1,55 @@ ++#include "search-path.h" ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++ ++notmuch_bool_t ++test_for_executable(const char* exename) ++{ ++ char *c = NULL, *save = NULL, *tok; ++ size_t n; ++ int dfd = -1; ++ notmuch_bool_t ret = FALSE; ++ ++ if (strchr(exename, '/')) { ++ if (0 == access(exename, X_OK)) ++ return TRUE; ++ else ++ return FALSE; ++ } ++ ++ c = getenv("PATH"); ++ if (c) ++ c = talloc_strdup(NULL, c); ++ else { ++ n = confstr(_CS_PATH, NULL, 0); ++ c = (char*)talloc_size(NULL, n); ++ if (!c) ++ return FALSE; ++ confstr(_CS_PATH, c, n); ++ } ++ ++ tok = strtok_r(c, ":", &save); ++ while (tok) { ++ dfd = open(tok, O_DIRECTORY | O_RDONLY); ++ if (dfd != -1) { ++ if (!faccessat(dfd, exename, X_OK, 0)) { ++ ret = TRUE; ++ goto done; ++ } ++ close(dfd); ++ } ++ tok = strtok_r(NULL, ":", &save); ++ } ++done: ++ if (dfd != -1) ++ close(dfd); ++ if (c) ++ talloc_free(c); ++ return ret; ++} +diff --git a/util/search-path.h b/util/search-path.h +new file mode 100644 +index 0000000..727d0b3 +--- /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.7.0.rc3 + -- 2.26.2