=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.41 retrieving revision 1.51 diff -u -p -r1.41 -r1.51 --- mandoc/cgi.c 2012/03/24 00:31:55 1.41 +++ mandoc/cgi.c 2014/04/23 21:40:47 1.51 @@ -1,6 +1,6 @@ -/* $Id: cgi.c,v 1.41 2012/03/24 00:31:55 kristaps Exp $ */ +/* $Id: cgi.c,v 1.51 2014/04/23 21:40:47 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,15 +34,23 @@ #include #include +#if defined(__sun) +/* for stat() */ +#include +#include +#include +#endif + #include "apropos_db.h" #include "mandoc.h" +#include "mandoc_aux.h" #include "mdoc.h" #include "man.h" #include "main.h" #include "manpath.h" #include "mandocdb.h" -#ifdef __linux__ +#if defined(__linux__) || defined(__sun) # include #else # include @@ -726,14 +733,14 @@ format(const struct req *req, const char *file) struct man *man; void *vp; enum mandoclevel rc; - char opts[MAXPATHLEN + 128]; + char opts[PATH_MAX + 128]; if (-1 == (fd = open(file, O_RDONLY, 0))) { resp_baddb(); return; } - mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); + mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL, NULL); rc = mparse_readfd(mp, fd, file); close(fd); @@ -743,11 +750,11 @@ format(const struct req *req, const char *file) } snprintf(opts, sizeof(opts), "fragment," - "man=%s/search.html?sec=%%S&expr=%%N," + "man=%s/search.html?sec=%%S&expr=Nm~^%%N$," /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/, progname); - mparse_result(mp, &mdoc, &man); + mparse_result(mp, &mdoc, &man, NULL); if (NULL == man && NULL == mdoc) { resp_baddb(); mparse_free(mp); @@ -777,7 +784,7 @@ pg_show(const struct req *req, char *path) struct manpaths ps; size_t sz; char *sub; - char file[MAXPATHLEN]; + char file[PATH_MAX]; const char *cp; int rc, catm; unsigned int vol, rec, mr; @@ -831,10 +838,10 @@ pg_show(const struct req *req, char *path) goto out; } - sz = strlcpy(file, ps.paths[vol], MAXPATHLEN); - assert(sz < MAXPATHLEN); - strlcat(file, "/", MAXPATHLEN); - strlcat(file, MANDOC_IDX, MAXPATHLEN); + sz = strlcpy(file, ps.paths[vol], PATH_MAX); + assert(sz < PATH_MAX); + strlcat(file, "/", PATH_MAX); + strlcat(file, MANDOC_IDX, PATH_MAX); /* Open the index recno(3) database. */ @@ -863,8 +870,8 @@ pg_show(const struct req *req, char *path) resp_baddb(); else { file[(int)sz] = '\0'; - strlcat(file, "/", MAXPATHLEN); - strlcat(file, cp, MAXPATHLEN); + strlcat(file, "/", PATH_MAX); + strlcat(file, cp, PATH_MAX); if (catm) catman(req, file); else @@ -929,7 +936,7 @@ pg_search(const struct req *req, char *path) ep++; while (ep && '\0' != *ep) { - cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *)); + cp = mandoc_reallocarray(cp, sz + 1, sizeof(char *)); start = ep; while ('\0' != *ep && ! isspace((unsigned char)*ep)) ep++; @@ -973,7 +980,7 @@ int main(void) { int i; - char buf[MAXPATHLEN]; + char buf[PATH_MAX]; DIR *cwd; struct req req; char *p, *path, *subpath; @@ -1010,7 +1017,7 @@ main(void) memset(&req, 0, sizeof(struct req)); - strlcpy(buf, ".", MAXPATHLEN); + strlcpy(buf, ".", PATH_MAX); pathgen(cwd, buf, &req); closedir(cwd); @@ -1098,11 +1105,20 @@ static int pathstop(DIR *dir) { struct dirent *d; +#if defined(__sun) + struct stat sb; +#endif - while (NULL != (d = readdir(dir))) + while (NULL != (d = readdir(dir))) { +#if defined(__sun) + stat(d->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); } @@ -1119,9 +1135,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; } @@ -1134,13 +1153,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))) { @@ -1155,9 +1180,8 @@ pathgen(DIR *dir, char *path, struct req *req) if (rc > 0) { /* This also strips the trailing slash. */ path[(int)--sz] = '\0'; - req->p = mandoc_realloc - (req->p, - (req->psz + 1) * sizeof(struct paths)); + req->p = mandoc_reallocarray(req->p, + req->psz + 1, sizeof(struct paths)); /* * Strip out the leading "./" unless we're just a ".", * in which case use an empty string as our name. @@ -1183,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))) {