which.c: C implementation of UNIX which(1)

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2017-07-02 16:50:58 +02:00
parent 0d70c116d7
commit d018a5215f
7 changed files with 146 additions and 1 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \
fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \
progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \
tempfile.c tree.c
tempfile.c tree.c which.c
libite_la_CPPFLAGS = -D_GNU_SOURCE
libite_la_CFLAGS = -W -Wall -Wextra
+5
View File
@@ -288,6 +288,11 @@ Generic Functions
optional listing of file and directory permissions if `show_perms` is
set. The `path` argument should be a directory.
- `which(cmd)`
C implementation of UNIX which(1). Returns a malloc'ed string with the
full path to `cmd` on success, otherwise `NULL`.
GNU Functions
-------------
+2
View File
@@ -85,6 +85,8 @@ int tree(char *path, int show_perms);
void progress (int percent, int max_width);
void progress_simple(int percent);
char *which (const char *cmd);
#ifndef touch
#include <sys/stat.h> /* utimensat() */
#include <sys/time.h> /* utimensat() on *BSD */
+1
View File
@@ -14,3 +14,4 @@ rsync
tempfile
touch
tree
which
+1
View File
@@ -16,6 +16,7 @@ TESTS += progress
TESTS += rsync
TESTS += tempfile
TESTS += tree
TESTS += which
check_PROGRAMS = $(TESTS)
+47
View File
@@ -0,0 +1,47 @@
#include "check.h"
struct tc {
char *cmd;
int exp;
int res;
};
int main(void)
{
int result = 0;
size_t i;
struct tc test[] = {
{ "ls", 1 },
{ "free", 1 },
{ "modinfo", 1 },
{ "useradd", 1 },
{ "/etc/passwd", 0 },
{ NULL, 0 }
};
for (i = 0; test[i].cmd; i++) {
char *path;
path = which(test[i].cmd);
if (!path) {
test[i].res = 0;
fprintf(stderr, "%20s : Not hot dog\n", test[i].cmd);
} else {
test[i].res = 1;
fprintf(stderr, "%20s : hot dog --> %s\n", test[i].cmd, path);
free(path);
}
}
for (i = 0; test[i].cmd; i++)
result += test[i].exp != test[i].res;
return result;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+89
View File
@@ -0,0 +1,89 @@
/* C implementation of UNIX which(1)
*
* Copyright (c) 2017 Joachim Nilsson <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h> /* snprintf() */
#include <stdlib.h> /* realloc(), free() */
#include <string.h> /* strtok_r() */
#include <unistd.h> /* access() */
/*
* Like which(1), returns a malloc'ed path to cmd on success, or NULL
*/
char *which(const char *cmd)
{
size_t pathlen = 0;
char *ptr, *tok, *env, *path = NULL;
if (!cmd) {
errno = EINVAL;
return NULL;
}
if (cmd[0] == '/') {
path = strdup(cmd);
if (!access(path, X_OK))
return path;
if (path)
free(path);
return NULL;
}
ptr = getenv("PATH");
if (!ptr)
return NULL;
env = strdup(ptr);
if (!env)
return NULL;
tok = strtok_r(env, ":", &ptr);
while (tok) {
size_t len = strlen(tok) + strlen(cmd) + 2;
if (pathlen < len) {
path = realloc(path, len);
if (!path) {
free(env);
return NULL;
}
pathlen = len;
}
snprintf(path, pathlen, "%s/%s", tok, cmd);
if (!access(path, X_OK)) {
free(env);
return path;
}
tok = strtok_r(NULL, ":", &ptr);
}
if (path)
free(path);
free(env);
return NULL;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/