From 2d99b964ff6eb373ddcbcaa7047b5ae6fe7bc6fe Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Fri, 21 Mar 2014 14:26:55 -0400 Subject: [PATCH] avoid a printf in the library In general, we don't want libraries to send data to the standard file descriptors. There are more that need fixing. Note: this introduces a new API (psl_suffix_count() and psl_suffix_exception_count) to enable the same sort of output from the test. But this new API seems to imply the internal structure of the public suffix list. Do we want to expose this API? There could be some other PSL mechanism (e.g. DBOUND) that doesn't have these counts, and a drop-in replacement would not know what to return here. --- include/libpsl.h | 8 ++++++++ src/psl.c | 15 +++++++++++++-- tests/test-is-tld.c | 3 +++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/libpsl.h b/include/libpsl.h index fd2a404..9a7c934 100644 --- a/include/libpsl.h +++ b/include/libpsl.h @@ -55,6 +55,14 @@ psl_ctx_t * int psl_is_tld(const psl_ctx_t *psl, const char *domain); +/* does not include exceptions */ +int + psl_suffix_count(const psl_ctx_t *psl); +/* just counts exceptions */ +int + psl_suffix_exception_count(const psl_ctx_t *psl); + + PSL_END_DECLS #endif /* _LIBPSL_LIBPSL_H */ diff --git a/src/psl.c b/src/psl.c index 5a2f819..cd3e5fa 100644 --- a/src/psl.c +++ b/src/psl.c @@ -312,14 +312,25 @@ psl_ctx_t *psl_load_file(const char *fname) _vector_sort(psl->suffix_exceptions); _vector_sort(psl->suffixes); - printf("loaded %d (%d/%d) suffixes\n", nsuffixes, psl->suffixes->cur, psl->suffix_exceptions->cur); - } else fprintf(stderr, _("Failed to open PSL file '%s'\n"), fname); return psl; } + +/* does not include exceptions */ +int psl_suffix_count(const psl_ctx_t *psl) +{ + return psl->suffixes->cur; +} +/* just counts exceptions */ +int psl_suffix_exception_count(const psl_ctx_t *psl) +{ + return psl->suffix_exceptions->cur; +} + + void psl_free(psl_ctx_t **psl) { if (psl && *psl) { diff --git a/tests/test-is-tld.c b/tests/test-is-tld.c index 0f63b9b..1868af0 100644 --- a/tests/test-is-tld.c +++ b/tests/test-is-tld.c @@ -64,6 +64,9 @@ static void test_psl(void) psl = psl_load_file(DATADIR "/effective_tld_names.dat"); + printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl)); + + for (it = 0; it < countof(test_data); it++) { const struct test_data *t = &test_data[it]; int result = psl_is_tld(psl, t->domain);