diff --git a/src/task_find.c b/src/task_find.c index f930b456ce585e1876dd57639944933b2001af5d..883e0644ba0cb65375aded01d77b7106923ed43b 100644 --- a/src/task_find.c +++ b/src/task_find.c @@ -235,7 +235,8 @@ struct linux_dirent64 { #define BUF_SIZE (32*1024) static int do_user_find(unsigned char d_type, int parent_dfd, const char *name, - const char *search_name, int search_type, bool debug) { + const char *search_name, int search_type, bool debug, + bool print_libc) { int err = 0; size_t namelen = strlen(name); @@ -243,12 +244,15 @@ static int do_user_find(unsigned char d_type, int parent_dfd, const char *name, return 0; } - bool match = - search_type != -1 && d_type == search_type - || search_name != NULL && strcmp(search_name, name) == 0; + bool match = (search_type != -1 && d_type == search_type) + || (search_name != NULL && strcmp(search_name, name) == 0); if (match) { - write(STDOUT_FILENO, name, namelen); - write(STDOUT_FILENO, "\n", 1); + if (print_libc) { + printf("%s\n", name); + } else { + write(STDOUT_FILENO, name, namelen); + write(STDOUT_FILENO, "\n", 1); + } } if (d_type != DT_DIR) { @@ -261,8 +265,12 @@ static int do_user_find(unsigned char d_type, int parent_dfd, const char *name, return 0; } - write(STDOUT_FILENO, name, namelen); - write(STDOUT_FILENO, "/\n", 2); + if (print_libc) { + printf("%s/\n", name); + } else { + write(STDOUT_FILENO, name, namelen); + write(STDOUT_FILENO, "/\n", 2); + } while (true) { char buf[BUF_SIZE]; @@ -277,7 +285,8 @@ static int do_user_find(unsigned char d_type, int parent_dfd, const char *name, for (int bpos = 0; bpos < nread;) { struct linux_dirent64 *d = (struct linux_dirent64 *) (buf + bpos); - err = do_user_find(d->d_type, dfd, d->d_name, search_name, search_type, debug); + err = do_user_find(d->d_type, dfd, d->d_name, search_name, search_type, + debug, print_libc); if (err) { goto close; } @@ -286,20 +295,25 @@ static int do_user_find(unsigned char d_type, int parent_dfd, const char *name, } close: - write(STDOUT_FILENO, "..\n", 3); + if (print_libc) { + printf("..\n"); + } else { + write(STDOUT_FILENO, "..\n", 3); + } close(dfd); return err; } static int user_find(const char *path, const char *search_name, - bool debug, int search_type) { - return do_user_find(DT_DIR, AT_FDCWD, path, search_name, search_type, debug); + bool debug, int search_type, bool print_libc) { + return do_user_find(DT_DIR, AT_FDCWD, path, search_name, search_type, debug, print_libc); } int main(int argc, char **argv) { bool debug = false; bool variant_bpf = true; + bool print_libc = true; const char *path; const char *name = NULL; @@ -308,7 +322,7 @@ int main(int argc, char **argv) int optc; opterr = 0; - while ((optc = getopt(argc, argv, "D" "v:" "n:" "t:")) != -1) { + while ((optc = getopt(argc, argv, "D" "v:" "n:" "t:" "p:")) != -1) { switch (optc) { case 'D': debug = true; @@ -322,8 +336,11 @@ int main(int argc, char **argv) case 't': type_str = optarg; break; + case 'p': + print_libc = strcmp("libc", optarg) == 0; + break; default: - fprintf(stderr, "error: invalid option\n"); + fprintf(stderr, "error: invalid option %c\n", optc); print_usage(argv); return EXIT_FAILURE; } @@ -381,7 +398,7 @@ int main(int argc, char **argv) if (variant_bpf) { ret = bpf_find(path, name, debug, type); } else { - ret = user_find(path, name, debug, type); + ret = user_find(path, name, debug, type, print_libc); } return ret;