diff options
| -rw-r--r-- | list.c | 14 | ||||
| -rw-r--r-- | main.c | 6 |
2 files changed, 15 insertions, 5 deletions
| @@ -23,6 +23,17 @@ void add_file_path(Node **head, char *file_path) { | |||
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | void list_files_recursively(char *base_path, Node **head) { | 25 | void list_files_recursively(char *base_path, Node **head) { |
| 26 | struct stat statbuf; | ||
| 27 | if (stat(base_path, &statbuf) == -1) { | ||
| 28 | perror("stat"); | ||
| 29 | return; | ||
| 30 | } | ||
| 31 | |||
| 32 | if (S_ISREG(statbuf.st_mode)) { | ||
| 33 | add_file_path(head, base_path); | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 26 | char path[2048]; | 37 | char path[2048]; |
| 27 | struct dirent *dp; | 38 | struct dirent *dp; |
| 28 | DIR *dir = opendir(base_path); | 39 | DIR *dir = opendir(base_path); |
| @@ -39,11 +50,10 @@ void list_files_recursively(char *base_path, Node **head) { | |||
| 39 | continue; | 50 | continue; |
| 40 | } | 51 | } |
| 41 | 52 | ||
| 42 | struct stat statbuf; | ||
| 43 | if (stat(path, &statbuf) != -1) { | 53 | if (stat(path, &statbuf) != -1) { |
| 44 | if (S_ISDIR(statbuf.st_mode)) { | 54 | if (S_ISDIR(statbuf.st_mode)) { |
| 45 | list_files_recursively(path, head); | 55 | list_files_recursively(path, head); |
| 46 | } else { | 56 | } else if (S_ISREG(statbuf.st_mode)) { |
| 47 | add_file_path(head, path); | 57 | add_file_path(head, path); |
| 48 | } | 58 | } |
| 49 | } | 59 | } |
| @@ -177,13 +177,13 @@ const char *get_file_extension(const char *file_path) { | |||
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | int main(int argc, char *argv[]) { | 179 | int main(int argc, char *argv[]) { |
| 180 | if (argc < 3) { | 180 | if (argc < 2) { |
| 181 | printf("Usage: %s <search term> <directory>\n", argv[0]); | 181 | printf("Usage: %s <search term> [directory|file]\n", argv[0]); |
| 182 | return 1; | 182 | return 1; |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | const char *cfname = argv[1]; | 185 | const char *cfname = argv[1]; |
| 186 | char *directory = argv[2]; | 186 | char *directory = (argc > 2) ? argv[2] : "."; |
| 187 | 187 | ||
| 188 | TSLanguage *tree_sitter_c(void); | 188 | TSLanguage *tree_sitter_c(void); |
| 189 | TSLanguage *tree_sitter_python(void); | 189 | TSLanguage *tree_sitter_python(void); |
