diff options
author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2011-07-11 22:01:52 +0200 |
---|---|---|
committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2011-07-11 22:01:52 +0200 |
commit | e6b76618e72863849b66274617040dce94b09d78 (patch) | |
tree | d90ef42c27aa35f3add7e2041c66d9571135db7a /src | |
parent | 3079a4f2b7b9a1d83a5e51d6899e4bf4d47a234d (diff) |
detect pdf by looking at magic number instead of file extension
Diffstat (limited to 'src')
-rw-r--r-- | src/flpsed.cxx | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/flpsed.cxx b/src/flpsed.cxx index f225036..0507725 100644 --- a/src/flpsed.cxx +++ b/src/flpsed.cxx @@ -127,19 +127,34 @@ void page_sel_fill() { page_sel->select(psed_p->get_page()); } +bool is_pdf(const char *file) { + FILE *f = fopen(file, "r"); + char buf[4] = {'\0'}; + size_t read = 0; + + if (!f) + return false; + + while (read < sizeof(buf) && !feof(f) && !ferror(f)) + read += fread(buf + read, 1, sizeof(buf) - read, f); + fclose(f); + + return (strncmp(buf, "%PDF", sizeof(buf)) == 0); +} + + void open_file(char *file) { FILE *p; int status; const char *args[5]; pid_t pid; - char *dot = strrchr(file, '.'); static const char *title_prefix = "flpsed - "; int title_len = strlen(file) + strlen(title_prefix) + 1; char *title = (char*) malloc(title_len); snprintf(title, title_len, "%s%s", title_prefix, file); - if (dot && strcasecmp(dot, ".pdf") == 0) { + if (is_pdf(file)) { // assume pdf and try to import it using pdftops args[0] = "pdftops"; args[1] = file; |