summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-01-06 18:08:13 +0100
committerGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-01-06 18:10:05 +0100
commit28c2599e534f4c033ed8982eb560c16a1c1db2c1 (patch)
tree61f3be465a1fe6e65358ca722dc25cd9eb5e04a5
parent6e7bdec7b361d1bb775ef9139825140666576e85 (diff)
use magic numbers to dectect filetypes
-rwxr-xr-xpdftool.pl21
1 files changed, 9 insertions, 12 deletions
diff --git a/pdftool.pl b/pdftool.pl
index b5f897d..a0566a4 100755
--- a/pdftool.pl
+++ b/pdftool.pl
@@ -436,25 +436,22 @@ sub pdftops {
#
# To avoid to seek into IN, it gonna be copied from WRITE to READ in
# the background, once the filetype has been read
- #
- # TODO: read specifications, to properly detect the filetype
my $filetype;
my ($READ, $WRITE);
pipe $READ, $WRITE or die "Cannot pipe: $!";
- while (not (defined $filetype) && defined (my $l = <$IN>)) {
- print $WRITE ($l) or die "Cannot print: $!";
-
- if (defined $l && $l =~ /^%!PS/) {
- $filetype = "PS";
- } elsif (defined $l && $l =~ /^%PDF/) {
- $filetype = "PDF";
- }
+ # Detect the file type of the input file from its magic number
+ $_ = <$IN>;
+ print $WRITE ($_) or die "Cannot print: $!";
+ if (defined $_ && $_ =~ /^%!/) {
+ $filetype = "PS";
+ } elsif (defined $_ && $_ =~ /^%PDF/) {
+ $filetype = "PDF";
+ } else {
+ die "Error: Input file has an unknown magic number.\n";
}
- die "Cannot recognize the filetype.\n" unless defined $filetype;
-
unless (my $pid = fork) {
# Child: cat $IN > $WRITE in background
die "Cannot fork: $!" unless defined $pid;