diff --git a/src/main.c b/src/main.c index 731513b..229e063 100644 --- a/src/main.c +++ b/src/main.c @@ -125,10 +125,9 @@ print_submit_form() "\n", cgiOut); } -/* each page stores comments with ids in [P * (page - 1), P * page) +/* each page stores comments with ids in [P * (page - 1) + 1, P * page + 1) * if page is not specified, then we'll just print last P comments (for now) * TODO: print errors in div - * FIXME: NOW PAGING IS FAULTY, FIX IT */ void print_page() @@ -149,14 +148,15 @@ print_page() id_end = max_id; id_begin = (max_id > COMMENTS_PER_PAGE ? max_id - COMMENTS_PER_PAGE : 1); } else { - /* max_id >= COMMENTS_PER_PAGE * page + /* max_id > COMMENTS_PER_PAGE * (page - 1) + 1 + * max_id - 1 > COMMENTS_PER_PAGE * (page - 1) * perform this check without overflow */ - if (max_id / page < COMMENTS_PER_PAGE) { + if ((max_id + COMMENTS_PER_PAGE - 1) / COMMENTS_PER_PAGE <= (page - 1)) { fprintf(cgiOut, "There is no such page\n"); return; } - id_begin = COMMENTS_PER_PAGE * (page - 1); + id_begin = COMMENTS_PER_PAGE * (page - 1) + 1; id_end = id_begin + COMMENTS_PER_PAGE; if (id_end > max_id) { id_end = max_id;