Skip to content

Commit

Permalink
Search in binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
Theldus committed May 30, 2021
1 parent beff589 commit 1988b95
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
10 changes: 8 additions & 2 deletions ag_src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,16 @@ void search_buf(int worker_id, const char *buf, const size_t buf_len,
}
}
} else if (binary) {
print_binary_file_matches(dir_full_path);
if (has_ag_init) {
add_local_result(worker_id, dir_full_path, matches,
matches_len, buf, LIBAG_FLG_BINARY);
} else {
print_binary_file_matches(dir_full_path);
}
} else {
if (has_ag_init) {
add_local_result(worker_id, dir_full_path, matches, matches_len, buf);
add_local_result(worker_id, dir_full_path, matches,
matches_len, buf, LIBAG_FLG_TEXT);
} else {
print_file_matches(dir_full_path, buf, buf_len, matches, matches_len);
}
Expand Down
2 changes: 1 addition & 1 deletion ag_src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void search_dir(ignores *ig, const char *base_path, const char *path, const int
/* libag 'private' routines and variables. */
extern int add_local_result(int worker_id, const char *file,
const match_t matches[], const size_t matches_len,
const char *buf);
const char *buf, int flags);

extern int init_local_results(int worker_id);
extern int has_ag_init;
Expand Down
5 changes: 4 additions & 1 deletion libag.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ static int reset_local_results(int reset)
* @param matches Matches list.
* @param matches_len Matches list length.
* @param buf File read buffer.
* @param flags Optional flags, such as binary file indicator.
*
* @return Returns 0 if success, -1 otherwise.
*/
int add_local_result(int worker_id, const char *file,
const match_t matches[], const size_t matches_len,
const char *buf)
const char *buf, int flags)
{
struct thrd_result *t_rslt;
struct ag_result **ag_rslt;
Expand Down Expand Up @@ -178,6 +179,7 @@ int add_local_result(int worker_id, const char *file,
return (-1);

ag_rslt[idx]->nmatches = matches_len;
ag_rslt[idx]->flags = flags;

/* Allocate and adds the matches into the matches list. */
ag_rslt[idx]->matches = malloc(sizeof(struct ag_match *) * matches_len);
Expand Down Expand Up @@ -458,6 +460,7 @@ int ag_set_config(struct ag_config *ag_config)
return (-1);
opts.workers = ag_config->num_workers;
opts.stats = ag_config->stats;
opts.search_binary_files = ag_config->search_binary_files;
memcpy(&config, ag_config, sizeof(struct ag_config));
return (0);
}
Expand Down
11 changes: 10 additions & 1 deletion libag.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
#define LIBAG_MANUAL_WORKERS 1
#define LIBAG_ONSEARCH_WORKERS 2

/* Result flags. */
#define LIBAG_FLG_TEXT 1
#define LIBAG_FLG_BINARY 2

/**
* Structure that holds a single result, i.e: a file
* that may contains multiples matches.
Expand All @@ -54,6 +58,7 @@
size_t byte_end;
char *match;
} **matches;
int flags;
};

/**
Expand Down Expand Up @@ -165,7 +170,11 @@
/*
* Enable a few stats for the last search.
*/
int stats; /* 0 disable, != 0 enable. */
int stats; /* 0 disable (default), != 0 enable. */
/*
* Search binary files.
*/
int search_binary_files; /* 0 disable (default), != 0 enable. */
};

/* Library forward declarations. */
Expand Down

0 comments on commit 1988b95

Please sign in to comment.