mirror of
https://github.com/troglobit/libite.git
synced 2026-11-21 19:51:39 +00:00
which: Allow which("ps aux"), which skips any arguments to command
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
10e4a1d057
commit
2c875afe06
2 files changed
+27
-2
No files matched your search
@@ -295,6 +295,9 @@ Generic Functions
|
|||||||
|
|
||||||
`whichp()` is a predicate function, returns `TRUE` or `FALSE`.
|
`whichp()` is a predicate function, returns `TRUE` or `FALSE`.
|
||||||
|
|
||||||
|
**Note:** `which("/bin/ps aux")` will return `/bin/ps`, or `TRUE`,
|
||||||
|
provided of course `/bin/ps` exists.
|
||||||
|
|
||||||
|
|
||||||
GNU Functions
|
GNU Functions
|
||||||
-------------
|
-------------
|
||||||
|
|||||||
@@ -14,12 +14,30 @@
|
|||||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h> /* snprintf() */
|
#include <stdio.h> /* snprintf() */
|
||||||
#include <stdlib.h> /* realloc(), free() */
|
#include <stdlib.h> /* realloc(), free() */
|
||||||
#include <string.h> /* strtok_r() */
|
#include <string.h> /* strtok_r() */
|
||||||
#include <unistd.h> /* access() */
|
#include <unistd.h> /* access() */
|
||||||
|
|
||||||
|
/* Strip any arguments in "/path/to/bin --some args", modifies input string */
|
||||||
|
static char *strip_args(char *path)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* Find first whitespace (space/tab/etc.) */
|
||||||
|
while (path[i] && !isspace(path[i]))
|
||||||
|
i++;
|
||||||
|
path[i] = 0;
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Like which(1), returns a malloc'ed path to cmd on success, or NULL
|
* Like which(1), returns a malloc'ed path to cmd on success, or NULL
|
||||||
*/
|
*/
|
||||||
@@ -34,7 +52,10 @@ char *which(const char *cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cmd[0] == '/') {
|
if (cmd[0] == '/') {
|
||||||
path = strdup(cmd);
|
path = strip_args(strdup(cmd));
|
||||||
|
if (!path)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!access(path, X_OK))
|
if (!access(path, X_OK))
|
||||||
return path;
|
return path;
|
||||||
|
|
||||||
@@ -66,7 +87,8 @@ char *which(const char *cmd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(path, pathlen, "%s/%s", tok, cmd);
|
snprintf(path, pathlen, "%s/%s", tok, cmd);
|
||||||
if (!access(path, X_OK)) {
|
path = strip_args(path);
|
||||||
|
if (!path || !access(path, X_OK)) {
|
||||||
free(env);
|
free(env);
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user