fi ls spacing

This commit is contained in:
Freya Murphy 2023-04-28 13:11:56 -04:00
parent 3b53b4c96d
commit 5b710c02b4

View file

@ -52,8 +52,12 @@ struct FileListInfo {
static DIR* get_directory(char* path) { static DIR* get_directory(char* path) {
DIR* d = opendir(path); DIR* d = opendir(path);
if (d == NULL) { if (d == NULL) {
if (errno == ENOTDIR) {
printf("\x1b[0m%s is a a file\n", path);
} else {
printf("\x1b[0merror: failed to open directory '%s': %s\n", path, strerror(errno)); printf("\x1b[0merror: failed to open directory '%s': %s\n", path, strerror(errno));
} }
}
return d; return d;
} }
@ -267,13 +271,14 @@ static void push_file(
(*size)++; (*size)++;
} }
static void recurse_directory(char* path, struct Flags* flags) { static bool recurse_directory(char* path, struct Flags* flags) {
DIR* d; DIR* d;
struct dirent* file; struct dirent* file;
bool first = true; bool first = true;
d = get_directory(path); d = get_directory(path);
if (d == NULL) return; if (d == NULL) return false;
int capacity = 8; int capacity = 8;
int size = 0; int size = 0;
@ -300,7 +305,7 @@ static void recurse_directory(char* path, struct Flags* flags) {
closedir(d); closedir(d);
d = get_directory(path); d = get_directory(path);
if (d == NULL) return; if (d == NULL) return false;
while((file = readdir(d)) != NULL) { while((file = readdir(d)) != NULL) {
if (file->d_type != DT_DIR) continue; if (file->d_type != DT_DIR) continue;
@ -312,16 +317,17 @@ static void recurse_directory(char* path, struct Flags* flags) {
} }
closedir(d); closedir(d);
return true;
} }
static void list_directory(char* path, struct Flags* flags) { static bool list_directory(char* path, struct Flags* flags) {
if (flags->recurse) { if (flags->recurse) {
recurse_directory(path, flags); return recurse_directory(path, flags);
return;
} }
DIR* d = get_directory(path); DIR* d = get_directory(path);
if (d == NULL) return; if (d == NULL) return false;
int capacity = 8; int capacity = 8;
int size = 0; int size = 0;
@ -340,6 +346,7 @@ static void list_directory(char* path, struct Flags* flags) {
free(files); free(files);
closedir(d); closedir(d);
return true;
} }
static void help() { static void help() {
@ -403,10 +410,9 @@ COMMAND(ls) {
if (titled && !flags.recurse) { if (titled && !flags.recurse) {
printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR); printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR);
} }
list_directory(argv[i], &flags); if (list_directory(argv[i], &flags) && i + 1 != argc)
if (titled && !flags.recurse) printf("\n"); if (titled && !flags.recurse) printf("\n");
} }
if (!flags.more_info) printf("\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }