[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl
From: |
Russ Allbery |
Subject: |
Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules. |
Date: |
Thu, 06 Dec 2007 14:02:28 -0800 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) |
Ralf Wildenhues <address@hidden> writes:
> +# $FILE_NAME
> +# open_quote ($FILE_NAME)
> +# -----------------------
> +# If the string $S is a well-behaved file name, simply return it.
> +# If it starts with white space, prepend `./'. Return the new string.
> +sub open_quote($)
> +{
> + my ($s) = @_;
> + if ($s =~ m!^\s!)
> + {
> + $s = "./$s";
> + }
> + return $s;
> +}
If you're going to use the two-argument format, you should also add a nul
byte to the end of the file name ($s .= "\0";):
The filename passed to 2-argument (or 1-argument) form of open()
will have leading and trailing whitespace deleted, and the
normal redirection characters honored. This property, known as
"magic open", can often be used to good effect. A user could
specify a filename of "rsh cat file |", or you could change
certain filenames as needed:
$filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/;
open(FH, $filename) or die "Can't open $filename: $!";
Use 3-argument form to open a file with arbitrary weird
characters in it,
open(FOO, '<', $file);
otherwise it's necessary to protect any leading and trailing
whitespace:
$file =~ s#^(\s)#./$1#;
open(FOO, "< $file\0");
If the string ends in a nul byte, trailing whitespace will be significant;
otherwise, it isn't.
--
Russ Allbery (address@hidden) <http://www.eyrie.org/~eagle/>
- [PATCH 0/10] Whitespace robustness patch series, Ralf Wildenhues, 2007/12/04
- [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Ralf Wildenhues, 2007/12/04
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Russ Allbery, 2007/12/04
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Paul Eggert, 2007/12/04
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Eric Blake, 2007/12/05
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Ralf Wildenhues, 2007/12/06
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Eric Blake, 2007/12/12
- Re: [PATCH 1/10] Proper file name escaping in Autoconf programs and Perl modules., Ralf Wildenhues, 2007/12/13
[PATCH 2/10] * lib/autom4te.in: Quote @address@hidden, Ralf Wildenhues, 2007/12/04
[PATCH 3/10] Fix Autotest for whitespace in `pwd`., Ralf Wildenhues, 2007/12/04
[PATCH 4/10] Proper config.status --file/--header and $srcdir escaping., Ralf Wildenhues, 2007/12/04
[PATCH 5/10] Fix testsuite program wrapper for whitespace in `pwd`., Ralf Wildenhues, 2007/12/04
[PATCH 6/10] * tests/m4sh.at (LINENO): Quote $0., Ralf Wildenhues, 2007/12/04