=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.40 retrieving revision 1.47 diff -u -p -r1.40 -r1.47 --- mandoc/cgi.c 2012/03/23 09:03:07 1.40 +++ mandoc/cgi.c 2014/01/05 20:41:04 1.47 @@ -1,6 +1,6 @@ -/* $Id: cgi.c,v 1.40 2012/03/23 09:03:07 kristaps Exp $ */ +/* $Id: cgi.c,v 1.47 2014/01/05 20:41:04 schwarze Exp $ */ /* - * Copyright (c) 2011 Kristaps Dzonsons + * Copyright (c) 2011, 2012 Kristaps Dzonsons * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,7 +18,6 @@ #include "config.h" #endif -#include #include #include @@ -35,6 +34,13 @@ #include #include +#if defined(__sun) +/* for stat() */ +#include +#include +#include +#endif + #include "apropos_db.h" #include "mandoc.h" #include "mdoc.h" @@ -43,7 +49,7 @@ #include "manpath.h" #include "mandocdb.h" -#ifdef __linux__ +#if defined(__linux__) || defined(__sun) # include #else # include @@ -495,15 +501,22 @@ resp_baddb(void) static void resp_search(struct res *r, size_t sz, void *arg) { - int i; + size_t i, matched; const struct req *req; req = (const struct req *)arg; if (sz > 0) assert(req->q.manroot >= 0); + + for (matched = i = 0; i < sz; i++) + if (r[i].matched) + matched++; - if (1 == sz) { + if (1 == matched) { + for (i = 0; i < sz; i++) + if (r[i].matched) + break; /* * If we have just one result, then jump there now * without any delay. @@ -511,21 +524,19 @@ resp_search(struct res *r, size_t sz, void *arg) puts("Status: 303 See Other"); printf("Location: http://%s%s/show/%d/%u/%u.html?", host, progname, req->q.manroot, - r[0].volume, r[0].rec); + r[i].volume, r[i].rec); http_printquery(req); puts("\n" "Content-Type: text/html; charset=utf-8\n"); return; } - qsort(r, sz, sizeof(struct res), cmp); - resp_begin_html(200, NULL); resp_searchform(req); puts("
"); - if (0 == sz) { + if (0 == matched) { puts("

\n" "No results found.\n" "

\n" @@ -534,9 +545,13 @@ resp_search(struct res *r, size_t sz, void *arg) return; } + qsort(r, sz, sizeof(struct res), cmp); + puts(""); - for (i = 0; i < (int)sz; i++) { + for (i = 0; i < sz; i++) { + if ( ! r[i].matched) + continue; printf("\n" "
\n" "d_name, &sb); + if (S_IFREG & sb.st_mode) +#else if (DT_REG == d->d_type) +#endif if (0 == strcmp(d->d_name, "catman.conf")) return(1); + } return(0); } @@ -1106,9 +1134,12 @@ pathgen(DIR *dir, char *path, struct req *req) DIR *cd; int rc; size_t sz, ssz; +#if defined(__sun) + struct stat sb; +#endif - sz = strlcat(path, "/", MAXPATHLEN); - if (sz >= MAXPATHLEN) { + sz = strlcat(path, "/", PATH_MAX); + if (sz >= PATH_MAX) { fprintf(stderr, "%s: Path too long", path); return; } @@ -1121,13 +1152,19 @@ pathgen(DIR *dir, char *path, struct req *req) rc = 0; while (0 == rc && NULL != (d = readdir(dir))) { - if (DT_DIR != d->d_type || strcmp(d->d_name, "etc")) +#if defined(__sun) + stat(d->d_name, &sb); + if (!(S_IFDIR & sb.st_mode) +#else + if (DT_DIR != d->d_type +#endif + || strcmp(d->d_name, "etc")) continue; path[(int)sz] = '\0'; - ssz = strlcat(path, d->d_name, MAXPATHLEN); + ssz = strlcat(path, d->d_name, PATH_MAX); - if (ssz >= MAXPATHLEN) { + if (ssz >= PATH_MAX) { fprintf(stderr, "%s: Path too long", path); return; } else if (NULL == (cd = opendir(path))) { @@ -1170,13 +1207,19 @@ pathgen(DIR *dir, char *path, struct req *req) rewinddir(dir); while (NULL != (d = readdir(dir))) { - if (DT_DIR != d->d_type || '.' == d->d_name[0]) +#if defined(__sun) + stat(d->d_name, &sb); + if (!(S_IFDIR & sb.st_mode) +#else + if (DT_DIR != d->d_type +#endif + || '.' == d->d_name[0]) continue; path[(int)sz] = '\0'; - ssz = strlcat(path, d->d_name, MAXPATHLEN); + ssz = strlcat(path, d->d_name, PATH_MAX); - if (ssz >= MAXPATHLEN) { + if (ssz >= PATH_MAX) { fprintf(stderr, "%s: Path too long", path); return; } else if (NULL == (cd = opendir(path))) {