Skip to content

Commit

Permalink
Implement -user and -group.
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Feb 14, 2016
1 parent 30d7fa2 commit 9cba877
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
83 changes: 83 additions & 0 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
*********************************************************************/

#include "bfs.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -427,6 +430,82 @@ static struct expr *parse_depth(struct parser_state *state, const char *option,
return new_option(state, option);
}

/**
* Parse -group.
*/
static struct expr *parse_group(struct parser_state *state, const char *option) {
struct expr *expr = parse_test_sdata(state, option, eval_gid);
if (!expr) {
return NULL;
}

const char *error;

errno = 0;
struct group *grp = getgrnam(expr->sdata);
if (grp) {
expr->idata = grp->gr_gid;
} else if (errno != 0) {
error = strerror(errno);
goto error;
} else if (isdigit(expr->sdata[0])) {
if (!parse_int(state, expr->sdata, &expr->idata)) {
goto fail;
}
} else {
error = "No such group";
goto error;
}

return expr;

error:
pretty_error(state->cmdline->stderr_colors,
"%s %s: %s\n", option, expr->sdata, error);

fail:
free_expr(expr);
return NULL;
}

/**
* Parse -user.
*/
static struct expr *parse_user(struct parser_state *state, const char *option) {
struct expr *expr = parse_test_sdata(state, option, eval_uid);
if (!expr) {
return NULL;
}

const char *error;

errno = 0;
struct passwd *pwd = getpwnam(expr->sdata);
if (pwd) {
expr->idata = pwd->pw_uid;
} else if (errno != 0) {
error = strerror(errno);
goto error;
} else if (isdigit(expr->sdata[0])) {
if (!parse_int(state, expr->sdata, &expr->idata)) {
goto fail;
}
} else {
error = "No such user";
goto error;
}

return expr;

error:
pretty_error(state->cmdline->stderr_colors,
"%s %s: %s\n", option, expr->sdata, error);

fail:
free_expr(expr);
return NULL;
}

/**
* Set the FNM_CASEFOLD flag, if supported.
*/
Expand Down Expand Up @@ -680,6 +759,8 @@ static struct expr *parse_literal(struct parser_state *state) {
case 'g':
if (strcmp(arg, "-gid") == 0) {
return parse_test_icmp(state, arg, eval_gid);
} else if (strcmp(arg, "-group") == 0) {
return parse_group(state, arg);
}
break;

Expand Down Expand Up @@ -786,6 +867,8 @@ static struct expr *parse_literal(struct parser_state *state) {
case 'u':
if (strcmp(arg, "-uid") == 0) {
return parse_test_icmp(state, arg, eval_uid);
} else if (strcmp(arg, "-user") == 0) {
return parse_user(state, arg);
}
break;

Expand Down
18 changes: 17 additions & 1 deletion tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,23 @@ function test_0042() {
find_diff -L "$links" -lname '[AQ]' 2>/dev/null
}

for i in {1..42}; do
function test_0043() {
find_diff -L "$basic" -user "$(id -un)"
}

function test_0044() {
find_diff -L "$basic" -user "$(id -u)"
}

function test_0045() {
find_diff -L "$basic" -group "$(id -gn)"
}

function test_0046() {
find_diff -L "$basic" -group "$(id -g)"
}

for i in {1..46}; do
test="test_$(printf '%04d' $i)"
"$test" "$dir"
status=$?
Expand Down

0 comments on commit 9cba877

Please sign in to comment.