When I run the following function I get a SIGSEGV. I can’t figure out why…
Can anybody help? Point me in the right direction? I is ment as a part of a larger program which scans the directory hierarchy for duplicate files.
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
int main ( int argc , char *argv[]) {
GError *error = NULL;
const gchar* filename = NULL;
gchar *directory_path = "/tmp";
GDir* dp = g_dir_open (directory_path, 0, &error);
if (error) {
g_warning("g_dir_open() failed: %s\n", error->message);
g_clear_error(&error);
return 1;
}
while ( (filename = g_dir_read_name(dp)) ){
filename = g_dir_read_name(dp);
gchar* path = g_build_filename (directory_path, filename, NULL);
printf("%s\n", filename);
g_free (path);
}
return 0;
}
Maybe get rid of the second
filename = g_dir_read_name(dp);(the first line inside of the loop)When it does the loop test condition, it already assigns
filenameto the next entry in the dir. If you run that line again from within the loop, it will attempt to read one more entry after the last one. If there are an odd number of files in the directory,filenamecould be pointing to a null value on the last execution of the loop.