bugaevc / wl-clipboard

Command-line copy/paste utilities for Wayland
GNU General Public License v3.0
1.62k stars 59 forks source link

wl-copy incorrect syntax deletes file content #240

Closed Faulhait closed 2 weeks ago

Faulhait commented 2 weeks ago

Issue description

Running wl-copy > filename.txt instead of < deletes the files context and put's the terminal in a waiting state that may be cancelled with ctrl + c.

Expected behavior

Running said command should give a shell error. Ideally the command should not require < since the command is always used in the same way (filename.txt > wl-copy is not a command) and does not have any paste functions.

Metadata

wl-clipboard 2.2.1 zsh 5.9 (x86_64-pc-linux-gnu)

charmander commented 2 weeks ago

Redirection is shell syntax and you’ll encounter the same problem with every command.

Add setopt noclobber to your .zshrc.

bugaevc commented 2 weeks ago

Indeed, this has nothing to do with wl-clipboard, and everything to do with your shell that you invoke wl-clipboard from. wl-copy doesn't do any of the redirections nor parse or handle the < syntax, it just reads data from its standard input stream (stdin).

The shell syntax gives you conciseness — you just write < filename.txt instead of

int fd = open("filename.txt", O_RDONLY);
if (fd < 0) {
    // handle the error...
}
dup2(fd, 0);
close(fd);

— but it's much easier to mistype < as > than it would be to mistype O_RDONLY as O_WRONLY | O_TRUNC.