rmyorston / busybox-w32

WIN32 native port of BusyBox.
https://frippery.org/busybox
Other
670 stars 124 forks source link

Redirection to wildcard filenames not supported #391

Closed SnDream closed 5 months ago

SnDream commented 5 months ago

When the redirected destination filename has a wildcard character, busybox-w32 cannot properly find the destination file corresponding to the wildcard character.

D:/Projects/testarea $ touch abc.txt
D:/Projects/testarea $ echo "test" > a*.txt
sh: can't create a*.txt: Invalid argument
D:/Projects/testarea $ cat a*.txt
D:/Projects/testarea $ rm a*.txt
D:/Projects/testarea $ busybox
BusyBox v1.37.0-FRP-5236-g7dff7f376 (2023-12-06 10:33:16 GMT)
(mingw64-gcc 13.2.1-5.fc39; mingw64-crt 11.0.0-2.fc39; glob; Unicode)

For busybox under Linux, the file can be found.

/mnt/d/Projects/testarea $ touch abc.txt
/mnt/d/Projects/testarea $ echo "test" > a*.txt
/mnt/d/Projects/testarea $ cat a*.txt
test
/mnt/d/Projects/testarea $ rm a*.txt
/mnt/d/Projects/testarea $ busybox
BusyBox v1.30.1 (Ubuntu 1:1.30.1-7ubuntu3) multi-call binary.
rmyorston commented 5 months ago

Wildcards aren't expanded in redirections in BusyBox ash, dash or busybox-w32 ash.

On Linux:

$ busybox sh
~ $ mkdir iss391
~ $ cd iss391
~/iss391 $ touch abc.txt
~/iss391 $ echo test >a*.txt
~/iss391 $ ls -l
total 4
-rw-r--r--. 1 rmy rmy 0 Mar 25 15:10  abc.txt
-rw-r--r--. 1 rmy rmy 5 Mar 25 15:10 'a*.txt'
~/iss391 $ cat a*.txt
test
~/iss391 $
$ dash
$ mkdir iss391_dash/
$ cd iss391_dash/
$ touch abc.txt
$ echo test >a*.txt
$ ls -l
total 4
-rw-r--r--. 1 rmy rmy 0 Mar 25 15:08  abc.txt
-rw-r--r--. 1 rmy rmy 5 Mar 25 15:08 'a*.txt'
$ cat a*.txt
test
$ 

The observed behaviour with busybox-w32 ash on Windows arises because * isn't allowed in filenames so the file a*.txt can't be created.

Other shells (e.g. bash, ksh) do expand wildcards in redirections.

rmyorston commented 5 months ago

According to POSIX, pathname expansion in redirection is optional:

If the redirection operator is "<<" or "<<-", the word that follows the redirection operator shall be subjected to quote removal; it is unspecified whether any of the other expansions occur. For the other redirection operators, the word that follows the redirection operator shall be subjected to tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion shall not be performed on the word by a non-interactive shell; an interactive shell may perform it, but shall do so only when the expansion would result in one word.

SnDream commented 5 months ago

Oh, that makes sense.