Created attachment 193640 [details] patch fixing the issue `unlink -foo` fails, instead of removing a file named `-foo`. The intended behavior of unlink(1) is to remove the file, as can be seen here, when it was added back in 1999: https://svnweb.freebsd.org/base/head/bin/rm/rm.c?r1=53819&r2=54895 Also, this matches POSIX: https://www.unix.com/man-page/posix/1P/unlink/ Patch attached to re-enable the old behavior and also update the documentation.
Thanks for the submission and I'd like to see this get fixed for 12.0. It appears this was broken by r97533, but I don't really understand why that change was made. I guess I can see value in supporting "unlink -- file" but don't see a reason to call getopt. Perhaps we ought to just if (argv == 2) unlink(argv[1]) else if (argv == 3 && argv[1] == "--") unlink(argv[2]) The man page in your patch needs one more minor update: s/Either of these/Any of these/.
A commit references this bug: Author: emaste Date: Wed Sep 12 19:41:17 UTC 2018 New revision: 338620 URL: https://svnweb.freebsd.org/changeset/base/338620 Log: Fix unlink(1) for files starting with - Restore the original behavior of unlink(1), passing the provided filename directly to unlink(2), handling the first argument being "--" correctly. This fixes "unlink -foo", broken in r97533. PR: 228448 Submitted by: Brennan Vincent <brennan@umanwizard.com> (original version) Submitted by: Yuri Pankov Reported by: Brennan Vincent <brennan@umanwizard.com> Reviewed by: emaste, kevans, vangyzen, 0mp Approved by: re (delphij) Differential Revision: https://reviews.freebsd.org/D17132 Changes: head/bin/rm/Makefile head/bin/rm/rm.1 head/bin/rm/rm.c head/bin/rm/tests/ head/bin/rm/tests/Makefile head/bin/rm/tests/Makefile.depend head/bin/rm/tests/rm_test.sh head/etc/mtree/BSD.tests.dist