diff options
-rwxr-xr-x | pdftool.pl | 21 |
1 files changed, 9 insertions, 12 deletions
@@ -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; |