From 28c2599e534f4c033ed8982eb560c16a1c1db2c1 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 6 Jan 2012 18:08:13 +0100 Subject: use magic numbers to dectect filetypes --- pdftool.pl | 21 +++++++++------------ 1 file 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; -- cgit v1.2.3