checkpath: fix logic for the writable option

The -W option does not need an argument of its own; it can take the
first path after all other options are processed on the command line.

Also, move the processing for the -W option out of the switch so it will
be in the same loop as the other processing.
This commit is contained in:
William Hubbs 2014-07-13 14:12:36 -05:00
parent 40141244e3
commit 71d6d61b28

View File

@ -187,7 +187,7 @@ parse_owner(struct passwd **user, struct group **group, const char *owner)
#include "_usage.h" #include "_usage.h"
#define extraopts "path1 [path2] [...]" #define extraopts "path1 [path2] [...]"
#define getoptstring "dDfFpm:o:W:" getoptstring_COMMON #define getoptstring "dDfFpm:o:W" getoptstring_COMMON
static const struct option longopts[] = { static const struct option longopts[] = {
{ "directory", 0, NULL, 'd'}, { "directory", 0, NULL, 'd'},
{ "directory-truncate", 0, NULL, 'D'}, { "directory-truncate", 0, NULL, 'D'},
@ -196,7 +196,7 @@ static const struct option longopts[] = {
{ "pipe", 0, NULL, 'p'}, { "pipe", 0, NULL, 'p'},
{ "mode", 1, NULL, 'm'}, { "mode", 1, NULL, 'm'},
{ "owner", 1, NULL, 'o'}, { "owner", 1, NULL, 'o'},
{ "writable", 1, NULL, 'W'}, { "writable", 0, NULL, 'W'},
longopts_COMMON longopts_COMMON
}; };
static const char * const longopts_help[] = { static const char * const longopts_help[] = {
@ -225,6 +225,7 @@ checkpath(int argc, char **argv)
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
bool trunc = false; bool trunc = false;
bool chowner = false; bool chowner = false;
bool writable = false;
while ((opt = getopt_long(argc, argv, getoptstring, while ((opt = getopt_long(argc, argv, getoptstring,
longopts, (int *) 0)) != -1) longopts, (int *) 0)) != -1)
@ -255,9 +256,7 @@ checkpath(int argc, char **argv)
applet, optarg); applet, optarg);
break; break;
case 'W': case 'W':
if (argv[optind] != NULL) writable = true;
ewarn("-W/--writable takes only one path, everything else will be ignored");
exit(!is_writable(optarg));
break; break;
case_RC_COMMON_GETOPT case_RC_COMMON_GETOPT
@ -267,8 +266,8 @@ checkpath(int argc, char **argv)
if (optind >= argc) if (optind >= argc)
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
if (type == inode_unknown) if (writable && type != inode_unknown)
eerrorx("%s: -d, -f, -p, or -W must be specified.", applet); eerrorx("%s: -W cannot be specified along with -d, -f or -p", applet);
if (pw) { if (pw) {
uid = pw->pw_uid; uid = pw->pw_uid;
@ -278,6 +277,8 @@ checkpath(int argc, char **argv)
gid = gr->gr_gid; gid = gr->gr_gid;
while (optind < argc) { while (optind < argc) {
if (writable)
exit(!is_writable(argv[optind]));
if (do_check(argv[optind], uid, gid, mode, type, trunc, chowner)) if (do_check(argv[optind], uid, gid, mode, type, trunc, chowner))
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
optind++; optind++;