+ struct config **cfs;
+ int i, o;
+ struct stat sb;
+ char *inm, *ipath, *p, *cpath;
+ DIR *dir;
+ struct dirent *dent;
+ struct pattern *pat;
+
+ cpath = sprintf2("%s/", path);
+ cfs = getconfigs(cpath);
+ for(i = 0; cfs[i] != NULL; i++) {
+ if(cfs[i]->index != NULL) {
+ for(o = 0; cfs[i]->index[o] != NULL; o++) {
+ inm = cfs[i]->index[o];
+ ipath = sprintf2("%s/%s", path, inm);
+ if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
+ handlefile(req, fd, ipath);
+ free(ipath);
+ goto out;
+ }
+ free(ipath);
+
+ ipath = NULL;
+ if(!strchr(inm, '.') && ((dir = opendir(path)) != NULL)) {
+ while((dent = readdir(dir)) != NULL) {
+ /* Ignore backup files.
+ * XXX: There is probably a better and more
+ * extensible way to do this. */
+ if(dent->d_name[strlen(dent->d_name) - 1] == '~')
+ continue;
+ if((p = strchr(dent->d_name, '.')) == NULL)
+ continue;
+ if(strncmp(dent->d_name, inm, strlen(inm)))
+ continue;
+ ipath = sprintf2("%s/%s", path, dent->d_name);
+ if(stat(ipath, &sb) || !S_ISREG(sb.st_mode)) {
+ free(ipath);
+ ipath = NULL;
+ continue;
+ }
+ break;
+ }
+ closedir(dir);
+ }
+ if(ipath != NULL) {
+ handlefile(req, fd, ipath);
+ free(ipath);
+ goto out;
+ }
+ }
+ break;
+ }
+ }
+ if((pat = findmatch(cpath, 0, 1)) != NULL) {
+ handle(req, fd, cpath, pat);
+ goto out;
+ }
+ simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
+
+out:
+ free(cpath);