harryfei / which-rs

A Rust equivalent of Unix command "which".
MIT License
208 stars 47 forks source link

Find POSIX shell #38

Open demurgos opened 3 years ago

demurgos commented 3 years ago

Hi! I am writing a Rust program that installs a POSIX shell script. In order to configure the shebang line properly, I would like to follow the spec and resolve the absolute sh path.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html#tag_20_117_16

Furthermore, on systems that support executable scripts (the "#!" construct), it is recommended that applications using executable scripts install them using getconf PATH to determine the shell pathname and update the "#!" script appropriately as it is being installed (for example, with sed).

This behavior is close enough to which (but not quite the same) that I feel that this crate would be the best place to implement it. I propose adding a posix_sh function returning the absolute path to the posix shell found this way. The main difference is that the resolution should not use the PATH environment variable but use the getconf function to retrieve PATH (so it is not subject to user-made changes to the PATH variable, for example in a situation where the process is spawned without environment variables).

This difference in behavior is slightly subtle, having this logic built-in here would make it easier for other devs to discover and use.

I am willing to send a PR for this feature if it is OK.

Regarding the implementation, I'd use a platform check to enable this function only on platforms supporting getconf.

harryfei commented 3 years ago

Ok, I think it's reasonable.

demurgos commented 3 years ago

Thanks for the feedback, I'll look into sending a PR in this case.

While prototyping it in my main project, I also found that it would be useful to support not only sh but other POSIX utilities but it does not change much from the implementation point of view.