From b13728c31b0e0e84589234a25a5d4d72f3594155 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Tue, 23 Nov 2004 03:28:54 +0000 Subject: ftw? is slow. --- src/kmodule.c | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/kmodule.c b/src/kmodule.c index 5b0340ee..e2bcfb96 100644 --- a/src/kmodule.c +++ b/src/kmodule.c @@ -10,7 +10,7 @@ */ #include -#include +#include #include #include #include @@ -19,40 +19,48 @@ #include +#include #include #include - -/* HACK. This is so not thread-safe. */ -static char mod_name[100], cmod_name[100]; - -static int isModule(const char *filename, const struct stat *sb, int flag) { - char *fname = basename(filename); - if ((!strcmp(fname,mod_name) || !strcmp(fname,cmod_name))) { - return 1; +char *setupFile() +{ + struct stat sbuf; + char path[512]; + int fd; + struct utsname utsbuf; + char *buf = NULL; + + uname(&utsbuf); + snprintf(path,512,"/lib/modules/%s/modules.dep",utsbuf.release); + if (!stat(path,&sbuf)) { + fd = open(path,O_RDONLY); + buf = mmap(0,sbuf.st_size,PROT_READ,MAP_SHARED,fd,0); + close(fd); } - return 0; + return buf; } - int isAvailable(char *modulename) { - struct utsname utsbuf; - char path[512]; + char mod_name[100]; + static char *buf = NULL; - uname(&utsbuf); - snprintf(mod_name,100,"%s.ko",modulename); - snprintf(cmod_name,100,"%s.ko.gz",modulename); - snprintf(path,512,"/lib/modules/%s/kernel",utsbuf.release); - /* Do not set the third argument of this function to < 6. Blarg. */ - if (ftw(path,isModule,15) == 1) { - return 1; + if (!buf) { + buf = setupFile(); + if (!buf) + return 0; } + snprintf(mod_name,100,"/%s.ko:",modulename); + if (strstr(buf,mod_name)) + return 1; + snprintf(mod_name,100,"/%s.ko.gz:",modulename); + if (strstr(buf,mod_name)) + return 1; return 0; } - int main(int argc, char **argv) { char *bus = NULL, *class = NULL; -- cgit v1.2.1