static char * FindModule(const char *module, const char *dirname, const char **subdirlist, PatternPtr patterns) { char buf[PATH_MAX + 1]; char *name = NULL; const char **subdirs = NULL; const char **s; if (strlen(dirname) > PATH_MAX) return NULL; subdirs = InitSubdirs(subdirlist); if (!subdirs) return NULL; for (s = subdirs; *s; s++) { if ((strlen(dirname) + strlen(*s)) > PATH_MAX) continue; strcpy(buf, dirname); strcat(buf, *s); if ((name = FindModuleInSubdir(buf, module))) break; } FreeSubdirs(subdirs); return name; }
static char * FindModule(const char *module, const char *dir, const char **subdirlist, PatternPtr patterns) { char buf[PATH_MAX + 1]; char *dirpath = NULL; char *name = NULL; struct stat stat_buf; int len, dirlen; char *fp; DIR *d; const char **subdirs = NULL; PatternPtr p = NULL; const char **s; struct dirent *dp; regmatch_t match[2]; subdirs = InitSubdirs(subdirlist); if (!subdirs) return NULL; #ifndef __EMX__ dirpath = (char *)dir; #else dirpath = xalloc(strlen(dir) + 10); strcpy(dirpath, (char *)__XOS2RedirRoot(dir)); #endif if (strlen(dirpath) > PATH_MAX) return NULL; /*xf86Msg(X_INFO,"OS2DIAG: FindModule: dirpath=%s\n",dirpath); */ for (s = subdirs; *s; s++) { if ((dirlen = strlen(dirpath) + strlen(*s)) > PATH_MAX) continue; strcpy(buf, dirpath); strcat(buf, *s); /*xf86Msg(X_INFO,"OS2DIAG: FindModule: buf=%s\n",buf); */ fp = buf + dirlen; if (stat(buf, &stat_buf) == 0 && S_ISDIR(stat_buf.st_mode) && (d = opendir(buf))) { if (buf[dirlen - 1] != '/') { buf[dirlen++] = '/'; fp++; } while ((dp = readdir(d))) { if (dirlen + strlen(dp->d_name) + 1 > PATH_MAX) continue; strcpy(fp, dp->d_name); if (!(stat(buf, &stat_buf) == 0 && S_ISREG(stat_buf.st_mode))) continue; for (p = patterns; p->pattern; p++) { if (regexec(&p->rex, dp->d_name, 2, match, 0) == 0 && match[1].rm_so != -1) { len = match[1].rm_eo - match[1].rm_so; if (len == strlen(module) && strncmp(module, dp->d_name + match[1].rm_so, len) == 0) { /*xf86Msg(X_INFO,"OS2DIAG: matching %s\n",buf); */ name = buf; break; } } } if (name) break; } closedir(d); if (name) break; } } FreeSubdirs(subdirs); if (dirpath != dir) xfree(dirpath); if (name) { return xstrdup(name); } return NULL; }