Skip to content

Instantly share code, notes, and snippets.

@dreamflyforever
Created January 11, 2017 09:46
Show Gist options
  • Save dreamflyforever/f2c6dfc8d87076e1b037ccc5bea5a7e8 to your computer and use it in GitHub Desktop.
Save dreamflyforever/f2c6dfc8d87076e1b037ccc5bea5a7e8 to your computer and use it in GitHub Desktop.
From chrome://newtab/
/* Search a wireless tool and return its path */
static char * wiToolsPath(const char * tool)
{
char * path;
int i, nbelems;
static const char * paths [] = {
"/sbin",
"/usr/sbin",
"/usr/local/sbin",
"/bin",
"/usr/bin",
"/usr/local/bin",
"/tmp"
};
nbelems = sizeof(paths) / sizeof(char *);
for (i = 0; i < nbelems; i++)
{
path = searchInside(paths[i], tool);
if (path != NULL)
return path;
}
return NULL;
}
/* Search a file recursively */
static char * searchInside(const char * dir, const char * filename)
{
char * ret;
char * curfile;
struct stat sb;
int len, lentot;
DIR *dp;
struct dirent *ep;
dp = opendir(dir);
if (dp == NULL)
{
return NULL;
}
len = strlen( filename );
lentot = strlen( dir ) + 256 + 2;
curfile = (char *) calloc( 1, lentot );
while ((ep = readdir(dp)) != NULL)
{
memset(curfile, 0, lentot);
sprintf(curfile, "%s/%s", dir, ep->d_name);
//Checking if it's the good file
if ((int)strlen( ep->d_name) == len && !strcmp(ep->d_name, filename))
{
(void)closedir(dp);
return curfile;
}
lstat(curfile, &sb);
//If it's a directory and not a link, try to go inside to search
if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode))
{
//Check if the directory isn't "." or ".."
if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
{
//Recursive call
ret = searchInside(curfile, filename);
if (ret != NULL)
{
(void)closedir(dp);
free( curfile );
return ret;
}
}
}
}
(void)closedir(dp);
free( curfile );
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment