Bug 15146

Summary: xargs: Allow running interactive commands with -o [PATCH]
Product: Busybox Reporter: Mallory <malloryadams>
Component: OtherAssignee: unassigned
Status: RESOLVED FIXED    
Severity: enhancement CC: busybox-cvs, malloryadams
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Host: Target:
Build:
Attachments: Patch to add -o support https://github.com/MalloryA/busybox/commit/2b66ae249d87c3b1c427cc87862ad6612b27c271

Description Mallory 2022-11-26 17:59:57 UTC
Created attachment 9416 [details]
Patch to add -o support https://github.com/MalloryA/busybox/commit/2b66ae249d87c3b1c427cc87862ad6612b27c271

-o opens /dev/tty and connects it to the stdin of the commands that get
executed.

Example: say we want to delete some files interactively. We cannot do
that with xargs without the -o option because the `rm -i` command does
not receive an interactive stdin. But if we add `-o`, then xargs will
ensure that /dev/tty is connected as stdin and `rm -i` will show us a
prompt and wait:

```
% touch a b c
% echo a b c | xargs -o rm -i
```

The -o option is available in GNU and FreeBSD versions of xargs as non-POSIX extensions.

Attached is a patch generated by `git format-patch`. I put the same patch on GitHub too: https://github.com/MalloryA/busybox/commit/2b66ae249d87c3b1c427cc87862ad6612b27c271
Comment 1 Denys Vlasenko 2022-12-20 14:31:51 UTC
Buggy. You must not dup2 to stdin, you lose the original input.


+               if (dup2(*fd, STDIN_FILENO) != 0)
+                       bb_error_msg_and_die("can't read from /dev/tty");

The error message is wrong. Anyway, use xdup2() which never returns failure.



+               if ((fd = xopen("/dev/tty", O_RDONLY)) == -1)
+                       bb_error_msg_and_die("can't open /dev/tty");

xopen() never returns -1.
Comment 2 Denys Vlasenko 2022-12-22 09:38:53 UTC
Fixed in git.