diff --git a/list.c b/list.c index 8c5b478303df65b412df0868929f2d78b9b6f277..e1d4ffd5c4acc9d784a1bfa1f985730d59cf831e 100644 --- a/list.c +++ b/list.c @@ -23,6 +23,17 @@ *head = new; } void list_files_recursively(char *base_path, Node **head) { + struct stat statbuf; + if (stat(base_path, &statbuf) == -1) { + perror("stat"); + return; + } + + if (S_ISREG(statbuf.st_mode)) { + add_file_path(head, base_path); + return; + } + char path[2048]; struct dirent *dp; DIR *dir = opendir(base_path); @@ -39,11 +50,10 @@ fprintf(stderr, "Path too long: %s/%s\n", base_path, dp->d_name); continue; } - struct stat statbuf; if (stat(path, &statbuf) != -1) { if (S_ISDIR(statbuf.st_mode)) { list_files_recursively(path, head); - } else { + } else if (S_ISREG(statbuf.st_mode)) { add_file_path(head, path); } } diff --git a/main.c b/main.c index 0a2ddd8a7f4bff4f7bc1c335b8eb6e70c0c7fb41..4ff4ab562a242ffc0ffeb53048b3acfa43ebe980 100644 --- a/main.c +++ b/main.c @@ -177,13 +177,13 @@ return NULL; } int main(int argc, char *argv[]) { - if (argc < 3) { - printf("Usage: %s \n", argv[0]); + if (argc < 2) { + printf("Usage: %s [directory|file]\n", argv[0]); return 1; } const char *cfname = argv[1]; - char *directory = argv[2]; + char *directory = (argc > 2) ? argv[2] : "."; TSLanguage *tree_sitter_c(void); TSLanguage *tree_sitter_python(void);