=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.323 retrieving revision 1.327 diff -u -p -r1.323 -r1.327 --- mandoc/main.c 2019/04/30 18:51:57 1.323 +++ mandoc/main.c 2019/05/03 18:39:34 1.327 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.323 2019/04/30 18:51:57 schwarze Exp $ */ +/* $Id: main.c,v 1.327 2019/05/03 18:39:34 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014-2019 Ingo Schwarze @@ -21,6 +21,7 @@ #include #include #include /* MACHINE */ +#include #include #include @@ -125,7 +126,7 @@ main(int argc, char *argv[]) char *conf_file, *defpaths, *auxpaths; char *oarg, *tagarg; unsigned char *uc; - size_t i, sz; + size_t i, sz, ssz; int prio, best_prio; enum outmode outmode; int fd, startdir; @@ -299,12 +300,9 @@ main(int argc, char *argv[]) search.outkey = oarg; else { while (oarg != NULL) { - thisarg = oarg; if (manconf_output(&conf.output, - strsep(&oarg, ","), 0) == 0) - continue; - warnx("-O %s: Bad argument", thisarg); - return (int)MANDOCLEVEL_BADARG; + strsep(&oarg, ","), 0) == -1) + return (int)MANDOCLEVEL_BADARG; } } } @@ -355,7 +353,7 @@ main(int argc, char *argv[]) } else if (argc > 1 && ((uc = (unsigned char *)argv[0]) != NULL) && ((isdigit(uc[0]) && (uc[1] == '\0' || - (isalpha(uc[1]) && uc[2] == '\0'))) || + isalpha(uc[1]))) || (uc[0] == 'n' && uc[1] == '\0'))) { search.sec = (char *)uc; argv++; @@ -434,7 +432,7 @@ main(int argc, char *argv[]) if (outmode == OUTMODE_ONE) { argc = 1; - best_prio = 20; + best_prio = 40; } else if (outmode == OUTMODE_ALL) argc = (int)sz; @@ -453,10 +451,21 @@ main(int argc, char *argv[]) sec = res[i].file; sec += strcspn(sec, "123456789"); if (sec[0] == '\0') - continue; + continue; /* No section at all. */ prio = sec_prios[sec[0] - '1']; - if (sec[1] != '/') - prio += 10; + if (search.sec != NULL) { + ssz = strlen(search.sec); + if (strncmp(sec, search.sec, ssz) == 0) + sec += ssz; + } else + sec++; /* Prefer without suffix. */ + if (*sec != '/') + prio += 10; /* Wrong dir name. */ + if (search.sec != NULL && + (strlen(sec) <= ssz + 3 || + strcmp(sec + strlen(sec) - ssz, + search.sec) != 0)) + prio += 20; /* Wrong file ext. */ if (prio >= best_prio) continue; best_prio = prio; @@ -702,6 +711,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath, const char *sec, const char *arch, const char *name, struct manpage **res, size_t *ressz) { + struct stat sb; glob_t globinfo; struct manpage *page; char *file; @@ -711,13 +721,13 @@ fs_lookup(const struct manpaths *paths, size_t ipath, form = FORM_SRC; mandoc_asprintf(&file, "%s/man%s/%s.%s", paths->paths[ipath], sec, name, sec); - if (access(file, R_OK) != -1) + if (stat(file, &sb) != -1) goto found; free(file); mandoc_asprintf(&file, "%s/cat%s/%s.0", paths->paths[ipath], sec, name); - if (access(file, R_OK) != -1) { + if (stat(file, &sb) != -1) { form = FORM_CAT; goto found; } @@ -726,7 +736,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath, if (arch != NULL) { mandoc_asprintf(&file, "%s/man%s/%s/%s.%s", paths->paths[ipath], sec, arch, name, sec); - if (access(file, R_OK) != -1) + if (stat(file, &sb) != -1) goto found; free(file); } @@ -740,13 +750,16 @@ fs_lookup(const struct manpaths *paths, size_t ipath, if (globres == 0) file = mandoc_strdup(*globinfo.gl_pathv); globfree(&globinfo); - if (globres == 0) - goto found; + if (globres == 0) { + if (stat(file, &sb) != -1) + goto found; + free(file); + } if (res != NULL || ipath + 1 != paths->sz) return 0; mandoc_asprintf(&file, "%s.%s", name, sec); - globres = access(file, R_OK); + globres = stat(file, &sb); free(file); return globres != -1;