Skip to content

Commit

Permalink
Skip a file if stdout is being redirected to it. This fixes #122
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Feb 9, 2013
1 parent 4f2e18f commit 6c0dcb0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/options.c
Expand Up @@ -226,7 +226,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
if (rv != 0) {
die("Error fstat()ing stdout");
}
statbuf;
opts.stdout_inode = statbuf.st_ino;
}

while ((ch = getopt_long(argc, argv, "A:aB:C:DG:g:fhiLlm:np:QRrSsvVtuUwz", longopts, &opt_index)) != -1) {
Expand Down
3 changes: 3 additions & 0 deletions src/options.h
Expand Up @@ -2,6 +2,8 @@
#define OPTIONS_H

#include <getopt.h>
#include <sys/stat.h>

#include <pcre.h>

#define DEFAULT_CONTEXT_LEN 2
Expand Down Expand Up @@ -53,6 +55,7 @@ typedef struct {
int search_hidden_files;
int search_stream; /* true if tail -F blah | ag */
int stats;
ino_t stdout_inode;
char *query;
int query_len;
char *pager;
Expand Down
5 changes: 5 additions & 0 deletions src/search.c
Expand Up @@ -211,6 +211,11 @@ void search_file(const char *file_full_path) {
goto cleanup;
}

if (opts.stdout_inode != 0 && opts.stdout_inode == statbuf.st_ino) {
log_debug("Skipping %s because stdout is redirected to it", file_full_path);
goto cleanup;
}

if ((statbuf.st_mode & S_IFMT) == 0) {
log_err("%s is not a file. Mode %u. Skipping...", file_full_path, statbuf.st_mode);
goto cleanup;
Expand Down

0 comments on commit 6c0dcb0

Please sign in to comment.