From 3dde2922c3a9efbc8f8f9790408b7f3cda059bed Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 3 Jul 2017 12:34:05 +0200 Subject: [PATCH] Add whichp() predicate function to which() Signed-off-by: Joachim Nilsson --- README.md | 4 +++- lite.h | 1 + which.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d836594..cad4071 100644 --- a/README.md +++ b/README.md @@ -288,10 +288,12 @@ Generic Functions optional listing of file and directory permissions if `show_perms` is set. The `path` argument should be a directory. -- `which(cmd)` +- `which(cmd)`, `whichp(cmd)` C implementation of UNIX which(1). Returns a malloc'ed string with the full path to `cmd` on success, otherwise `NULL`. + + `whichp()` is a predicate function, returns `TRUE` or `FALSE`. GNU Functions diff --git a/lite.h b/lite.h index 1fd8bf8..c714041 100644 --- a/lite.h +++ b/lite.h @@ -86,6 +86,7 @@ void progress (int percent, int max_width); void progress_simple(int percent); char *which (const char *cmd); +int whichp (const char *cmd); #ifndef touch #include /* utimensat() */ diff --git a/which.c b/which.c index 6b69f96..0a53a77 100644 --- a/which.c +++ b/which.c @@ -81,6 +81,22 @@ char *which(const char *cmd) return NULL; } +/* + * Like which(), above, but only answers TRUE(1)/FALSE(0) + */ +int whichp(const char *cmd) +{ + char *path; + + path = which(cmd); + if (path) { + free(path); + return 1; + } + + return 0; +} + /** * Local Variables: * indent-tabs-mode: t