From e76b079fe6951ea28acdc700069b9d33e9f69c7a Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Sun, 8 Aug 2021 13:59:30 +0100 Subject: [PATCH] Fix issue #59 Stop duplicated link from showing for Apache server configured with IconsAreLinks option. --- src/link.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/link.c b/src/link.c index cdf7909..f08866b 100644 --- a/src/link.c +++ b/src/link.c @@ -127,6 +127,26 @@ static LinkType linkname_to_LinkType(const char *linkname) return LINK_UNINITIALISED_FILE; } +/** + * \brief check if two link names are equal, after taking the '/' into account. + */ +static int linknames_equal(char *linkname, const char *linkname_new) +{ + if (!strncmp(linkname, linkname_new, MAX_FILENAME_LEN)) { + return 1; + } + + /* check if the link names differ by a single '/' */ + if (!strncmp(linkname, linkname_new, strnlen(linkname, MAX_FILENAME_LEN))) { + size_t linkname_new_len = strnlen(linkname_new, MAX_FILENAME_LEN); + if ( (linkname_new_len - strnlen(linkname, MAX_FILENAME_LEN) == 1) && + (linkname_new[linkname_new_len - 1] == '/')) { + return 1; + } + } + return 0; +} + /** * Shamelessly copied and pasted from: * https://github.com/google/gumbo-parser/blob/master/examples/find_links.cc @@ -141,8 +161,19 @@ static void HTML_to_LinkTable(GumboNode *node, LinkTable *linktbl) (href = gumbo_get_attribute(&node->v.element.attributes, "href"))) { /* if it is valid, copy the link onto the heap */ LinkType type = linkname_to_LinkType(href->value); - if ( (type == LINK_DIR) || (type == LINK_UNINITIALISED_FILE) ) { - LinkTable_add(linktbl, Link_new(href->value, type)); + /* + * We also check if the link being added is the same as the last link. + * This is to prevent duplicated link, if an Apache server has the + * IconsAreLinks option. + */ + size_t comp_len = strnlen(href->value, MAX_FILENAME_LEN); + if (type == LINK_DIR) { + comp_len--; + } + if (((type == LINK_DIR) || (type == LINK_UNINITIALISED_FILE)) && + !linknames_equal(linktbl->links[linktbl->num - 1]->linkname, + href->value)) { + LinkTable_add(linktbl, Link_new(href->value, type)); } } /* Note the recursive call, lol. */