mirror of
https://github.com/troglobit/libite.git
synced 2026-11-03 13:18:33 +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
@@ -14,12 +14,30 @@
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h> /* snprintf() */
|
||||
#include <stdlib.h> /* realloc(), free() */
|
||||
#include <string.h> /* strtok_r() */
|
||||
#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
|
||||
*/
|
||||
@@ -34,7 +52,10 @@ char *which(const char *cmd)
|
||||
}
|
||||
|
||||
if (cmd[0] == '/') {
|
||||
path = strdup(cmd);
|
||||
path = strip_args(strdup(cmd));
|
||||
if (!path)
|
||||
return NULL;
|
||||
|
||||
if (!access(path, X_OK))
|
||||
return path;
|
||||
|
||||
@@ -66,7 +87,8 @@ char *which(const char *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);
|
||||
return path;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user