Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Created March 1, 2020 09:44
Show Gist options
  • Save ryanwoodsmall/e03323480bbcc8a79dae06beec7e5892 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/e03323480bbcc8a79dae06beec7e5892 to your computer and use it in GitHub Desktop.
findbin.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* findbin(char* s);
char* findbin(char* s)
{
char* p = getenv("PATH");
char* a = "/";
char* t = strtok(p, ":");
while(t != NULL){
char* e;
e = malloc(strlen(t) + strlen(a) + strlen(s) + 1);
sprintf(e, "%s%s%s", t, a, s);
if(access(e, X_OK) != -1){
return(e);
}
free(e);
t = strtok(NULL, ":");
}
return(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment