1+ #define _POSIX_C_SOURCE 200809L
2+ #define _GNU_SOURCE
3+
14#include <nutshell/theme.h>
25#include <jansson.h>
36#include <stdio.h>
47#include <stdlib.h>
58#include <string.h>
69#include <dirent.h>
710#include <unistd.h>
11+ #include <sys/stat.h> // Add this include for struct stat, stat() function, and S_ISREG macro
812
913// Add this helper macro at the top of the file
1014#define THEME_DEBUG (fmt , ...) \
@@ -934,7 +938,17 @@ int theme_command(int argc, char **argv) {
934938 dir = opendir (themes_dir );
935939 if (dir ) {
936940 while ((ent = readdir (dir )) != NULL ) {
937- if (ent -> d_type == DT_REG && strstr (ent -> d_name , ".json" )) {
941+ // Skip . and .. entries
942+ if (strcmp (ent -> d_name , "." ) == 0 || strcmp (ent -> d_name , ".." ) == 0 )
943+ continue ;
944+
945+ // Build the full path
946+ char full_path [512 ];
947+ struct stat st ;
948+ snprintf (full_path , sizeof (full_path ), "%s/%s" , themes_dir , ent -> d_name );
949+
950+ // Check if it's a regular file with .json extension
951+ if (stat (full_path , & st ) == 0 && S_ISREG (st .st_mode ) && strstr (ent -> d_name , ".json" )) {
938952 // Remove .json extension
939953 char theme_name [128 ];
940954 strncpy (theme_name , ent -> d_name , sizeof (theme_name ));
@@ -957,7 +971,17 @@ int theme_command(int argc, char **argv) {
957971 dir = opendir ("./themes" );
958972 if (dir ) {
959973 while ((ent = readdir (dir )) != NULL ) {
960- if (ent -> d_type == DT_REG && strstr (ent -> d_name , ".json" )) {
974+ // Skip . and .. entries
975+ if (strcmp (ent -> d_name , "." ) == 0 || strcmp (ent -> d_name , ".." ) == 0 )
976+ continue ;
977+
978+ // Build the full path
979+ char full_path [512 ];
980+ struct stat st ;
981+ snprintf (full_path , sizeof (full_path ), "./themes/%s" , ent -> d_name );
982+
983+ // Check if it's a regular file with .json extension
984+ if (stat (full_path , & st ) == 0 && S_ISREG (st .st_mode ) && strstr (ent -> d_name , ".json" )) {
961985 // Remove .json extension
962986 char theme_name [128 ];
963987 strncpy (theme_name , ent -> d_name , sizeof (theme_name ));
0 commit comments