commit 845e90bbe2645ffbbd6562e848ddbd7a5da8968d
parent f09ace14a82152c0fb72adb9d3c7424d632e9152
Author: aabacchus <ben@bvnf.space>
Date:   Mon, 13 Sep 2021 14:42:23 +0100
ls: add exec detection to -F
Diffstat:
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/src/ls.c b/src/ls.c
@@ -1,8 +1,10 @@
 #include <dirent.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/stat.h>
 
 int
 recursive_list_dirs(const char *directory)
@@ -88,6 +90,17 @@ main(int argc, char *argv[])
 			if(show_suffix) {
 				switch(ent->d_type) {
 					case DT_REG:
+						/* check if executable */
+						struct stat st;
+						if(fstatat(dirfd(dir),
+								 ent->d_name,
+								 &st,
+								 AT_SYMLINK_NOFOLLOW) < 0) {
+							perror(ent->d_name);
+							return -1;
+						}
+						if((st.st_mode & S_IEXEC) != 0)
+							suffix = '*';
 						break;
 					case DT_FIFO:
 						suffix = '|';
@@ -99,10 +112,11 @@ main(int argc, char *argv[])
 						suffix = '=';
 						break;
 				}
-				/* TODO: add suffix '*' if executable */
 			}
-
-			printf("%s%c%c", ent->d_name, suffix, separator);
+			if(suffix != 0)
+				printf("%s%c%c", ent->d_name, suffix, separator);
+			else
+				printf("%s%c", ent->d_name, separator);
 		}
 	}
 	puts("");