I poked around in the code for Nautilus, but the function for adding support for ".hidden" depends on several functions that appear to be from gnome, as well as is quite integrated with the rest of Nautilus's view of the current directory. So instead I took a shot at porting to C based solely on the info I could find on google. I'm pretty sure I made some mistakes in regards to memory management alone, although it does compile and work. Any improvements or pointers to what I did wrong would be appreciated.
Code:
static char** hidden_file_lines;
static long int hidden_file_count;
static void load_hidden_file(const char* a_dir) {
char* name;
FILE* fp;
char line[PATH_MAX];
hidden_file_count = 0;
hidden_file_lines = (char**)malloc(hidden_file_count * sizeof(char*));
name = (char*)malloc((strlen(a_dir) + 9) * sizeof(char));
name = strcpy(name, a_dir);
name = strcat(name, "/.hidden");
if ((fp = fopen(name, "r")) != NULL) {
while (fgets(line, PATH_MAX, fp) != NULL) {
if (strlen(line) == 1) continue;
hidden_file_count++;
hidden_file_lines =
(char**)realloc((void*)hidden_file_lines,
hidden_file_count * sizeof(char*));
hidden_file_lines[hidden_file_count - 1] =
(char*)calloc(strlen(line) - 1, sizeof(char));
hidden_file_lines[hidden_file_count - 1] =
memcpy(hidden_file_lines[hidden_file_count - 1],
line, (strlen(line) - 1));
}
}
fclose(fp);
free(name);
return;
}
static int is_hidden(const char* a_entry) {
long int i;
if (a_entry[0] == '.') {
return 1;
}
for (i = 0; i < hidden_file_count; i++) {
if (strcmp(a_entry, hidden_file_lines[i]) == 0) {
return 1;
}
}
return 0;
}
static void release_hidden_file() {
long int i;
for (i = 0; i < hidden_file_count; i++) {
free(hidden_file_lines[i]);
}
free(hidden_file_lines);
hidden_file_count = 0;
}
Bookmarks