trim(8): add another safety net

It is quite easy make a mistake and run something like this:

	trim -f /dev/da0 -r rfile

This would trim the whole device then emit an error on non-existing file -r.

Add another check to prevent this while allowing this form still
for real object names beginning from dash:

	trim -f -- /dev/da0 -r rfile

MFC after:	1 week
This commit is contained in:
Eugene Grosbein 2019-03-14 12:25:16 +00:00
parent 582141f69d
commit 7f65491576
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=345130

View File

@ -40,6 +40,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
@ -104,6 +105,23 @@ main(int argc, char **argv)
/* NOTREACHED */
}
/*
* Safety net: do not allow mistakes like
*
* trim -f /dev/da0 -r rfile
*
* This would trim whole device then error on non-existing file -r.
* Following check prevents this while allowing this form still:
*
* trim -f -- /dev/da0 -r rfile
*/
if (strcmp(argv[optind-1], "--") != 0) {
for (ch = optind; ch < argc; ch++)
if (argv[ch][0] == '-')
usage(name);
}
argv += optind;
argc -= optind;