read from stdin if no domain argument

This commit is contained in:
Tim Ruehsen 2014-06-15 22:32:18 +02:00
parent abc56bc6dd
commit 7cc3eed46b
1 changed files with 29 additions and 2 deletions

View File

@ -32,8 +32,10 @@
# include <config.h> # include <config.h>
#endif #endif
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <libpsl.h> #include <libpsl.h>
static void usage(int err, FILE* f) static void usage(int err, FILE* f)
@ -135,8 +137,33 @@ int main(int argc, const char *const *argv)
exit(2); exit(2);
} }
if (arg >= argv + argc) { if (arg >= argv + argc) {
fprintf(stderr, "No domains given - aborting\n"); if (isatty(STDIN_FILENO)) {
exit(3); char buf[256], *domain;
size_t len;
// read URLs from STDIN
while (fgets(buf, sizeof(buf), stdin)) {
for (domain = buf; isspace(*domain); domain++); // skip leading spaces
if (*domain == '#' || !*domain) continue; // skip empty lines and comments
for (len = strlen(domain); len && isspace(domain[len - 1]); len--); // skip trailing spaces
domain[len] = 0;
if (mode == 1)
printf("%s: %d\n", domain, psl_is_public_suffix(psl, domain));
else if (mode == 2)
printf("%s: %s\n", domain, psl_unregistrable_domain(psl, domain));
else if (mode == 3)
printf("%s: %s\n", domain, psl_registrable_domain(psl, domain));
else if (mode == 4)
printf("%s: %d\n", domain, psl_is_cookie_domain_acceptable(psl, domain, cookie_domain));
}
psl_free(psl);
exit(0);
} else {
fprintf(stderr, "No domains given - aborting\n");
exit(3);
}
} }
} }