Skip to content

fdmove -c 1 1 cmd fails #14

@emanuele6

Description

@emanuele6

fdmove 1 1 is correctly a no-op, but fdmove -c will fail if you use the same file descriptor:

$ fdmove 1 1 ls -l /proc/self/fd
total 0
lrwx------ 1 emanuele6 wheel 64 Nov 16 09:23 0 -> /dev/pts/7
lrwx------ 1 emanuele6 wheel 64 Nov 16 09:23 1 -> /dev/pts/7
lrwx------ 1 emanuele6 wheel 64 Nov 16 09:23 2 -> /dev/pts/7
lr-x------ 1 emanuele6 wheel 64 Nov 16 09:23 3 -> /proc/3330198/fd
$ fdmove -c 1 1 ls -l /proc/self/fd
fdmove: fatal: unable to move fd 1 to fd 1: Invalid argument

skalibs's fd_copy(to, from) function hardcodes to == from as a failure instead of returning to for successful no-op if it's positive, or just calling dup2(to, from) and letting it do nothing successfully.

https://github.com/skarnet/skalibs/blob/2ea36ebf726bb3a29ff8c6b12a6ff3df9020859f/src/libstddjb/fd_copy.c#L10

When you are doing something like fdmove -c 1 "$fd", the fact that it fails if fd is 1 is unideal.


Slightly related: fdmove is documented as

On a fdmove a b prog... is roughly equivalent to sh -c 'exec prog... a>&b b<&-'. It means: if you write to fd a now, it will have the same effect as writing to fd b beforehand, and you cannot write to fd b anymore.

I think it could be useful to mention that fdmove a b is not simply equivalent to fdmove -c a b fdclose b, because it is a no-op and won't close b if a == b; more like a<&b- in bash/ksh93 rather than a<&b b<&-.

o/
 emanuele6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions