ln: Make the 'path' argument optional

If 'path' is omitted, we create a link with the basename of whatever
the target is. This matches what other systems do.
This commit is contained in:
Andreas Kling 2020-12-16 23:23:55 +01:00
parent 3d7b8de64f
commit 56701f91f9
Notes: sideshowbarker 2024-07-19 00:47:18 +09:00

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
@ -44,9 +45,15 @@ int main(int argc, char** argv)
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_option(symbolic, "Create a symlink", "symbolic", 's'); args_parser.add_option(symbolic, "Create a symlink", "symbolic", 's');
args_parser.add_positional_argument(target, "Link target", "target"); args_parser.add_positional_argument(target, "Link target", "target");
args_parser.add_positional_argument(path, "Link path", "path"); args_parser.add_positional_argument(path, "Link path", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(argc, argv);
String path_buffer;
if (!path) {
path_buffer = LexicalPath(target).basename();
path = path_buffer.characters();
}
if (symbolic) { if (symbolic) {
int rc = symlink(target, path); int rc = symlink(target, path);
if (rc < 0) { if (rc < 0) {