I had the following code, which prints the files in a folder that have an ending. It looks also by default files with the following ending "SCL_10m.tif". This is the version that worked so far:
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>
// Linked-List
typedef struct nlist{
char *data;
struct nlist *next;
}Node;
// Function to replace a string with another
// string
char* str_replace(char* string, const char* substr, const char* replacement) {
char* tok = NULL;
char* newstr = NULL;
char* oldstr = NULL;
int oldstr_len = 0;
int substr_len = 0;
int replacement_len = 0;
newstr = strdup(string);
substr_len = strlen(substr);
replacement_len = strlen(replacement);
if (substr == NULL || replacement == NULL) {
return newstr;
}
while ((tok = strstr(newstr, substr))) {
oldstr = newstr;
oldstr_len = strlen(oldstr);
newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));
if (newstr == NULL) {
free(oldstr);
return NULL;
}
memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, replacement_len);
memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (tok - oldstr));
memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);
free(oldstr);
}
free(string);
return newstr;
}
Node* insert(Node*, char*);
void show(Node*);
//Function to insert an element in the linked-list
Node* insert(Node *Head, char *value)
{
Node *new_string;
new_string = (Node *)malloc(sizeof(Node));
new_string->data = malloc(strlen(value)+1);
strcpy(new_string->data,value);
Node *check;
check = (Node *)malloc(sizeof(Node));
if(Head == NULL){
Head = new_string;
Head->next = NULL;
}
else{
check = Head;
while(check->next != NULL)
check = check->next;
check->next = new_string;
new_string->next = NULL;
}
return Head;
}
//Function to show the elements in the linked-list
void show(Node *Head)
{
Node *check;
check = (Node *)malloc(sizeof(Node));
check = Head;
if (check == NULL){
return;
}
while(check != NULL) {
printf("%s", check->data);
check=check->next;
}
printf("\n");
}
//Function to find the files in the folder recursively based on
// a wildcard, and adding them to a linked-list
void listFilesRecursively(char *path, char *suffix);
//checks if a string ends with a substring
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
void listFilesRecursively(char *basePath, char *suffix)
{
char path[1000];
struct dirent *dp;
DIR *dir = opendir(basePath);
Node *Head = NULL;
if (!dir)
return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if (string_ends_with(path, suffix))
Head = insert(Head, path);
listFilesRecursively(path, suffix);
}
}
show(Head);
closedir(dir);
}
int main()
{
char path[100];
char suffix[100];
// Input path from user
// Suffix Band Sentinel-2 of Type B02_10m.tif
printf("Enter path to list files: ");
scanf("%s", path);
printf("Enter the wildcard: ");
scanf("%s", suffix);
char *SCL_suffix = "SCL_10m.tif";
listFilesRecursively(path, suffix);
listFilesRecursively(path, SCL_suffix);
return 0;
}
I started working on this code, making some changes, the most important was that instead of printing the linked list generated in the function listFilesRecursively by calling show(Head), then I wanted to return the lists to the main function to make other processes (iterate over both lists simultaneously, that's future work).
However after doing that, and calling the function listFilesRecursively twice, in the main(), I run into a segmentation fault error.
According to some sources a segmentation fault occurs if a pass or return a pointer from a function that points something on the stack, a stack being a region of memory that stores temporary variables. I have been checking the listFIlesRecursively function, messing around with the pointers within, but I cannot get my head around the problem. A bit of help on the topic would be appreaciated.
Here is the modified version of my code that causes the segmentation fault.
include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>
// Node Structure of Linked-List
typedef struct nlist{
char *data;
struct nlist *next;
}Node;
// Function to replace a string with another
// string
char* str_replace(char* string, const char* substr, const char* replacement) {
char* tok = NULL;
char* newstr = NULL;
char* oldstr = NULL;
int oldstr_len = 0;
int substr_len = 0;
int replacement_len = 0;
newstr = strdup(string);
substr_len = strlen(substr);
replacement_len = strlen(replacement);
if (substr == NULL || replacement == NULL) {
return newstr;
}
while ((tok = strstr(newstr, substr))) {
oldstr = newstr;
oldstr_len = strlen(oldstr);
newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));
if (newstr == NULL) {
free(oldstr);
return NULL;
}
memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, replacement_len);
memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (tok - oldstr));
memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);
free(oldstr);
}
free(string);
return newstr;
}
Node* insert(Node*, char*);
void show(Node*);
// Function to insert an element to the Linked-List
Node* insert(Node *Head, char *value)
{
Node *new_string;
new_string = (Node *)malloc(sizeof(Node));
new_string->data = malloc(strlen(value)+1);
strcpy(new_string->data,value);
Node *check;
check = (Node *)malloc(sizeof(Node));
if(Head == NULL){
Head = new_string;
Head->next = NULL;
}
else{
check = Head;
while(check->next != NULL)
check = check->next;
check->next = new_string;
new_string->next = NULL;
}
return Head;
}
//Function to show the elements of the Linked-List
void show(Node *Head)
{
Node *check;
check = (Node *)malloc(sizeof(Node));
check = Head;
if (check == NULL){
return;
}
while(check != NULL) {
printf("%s", check->data);
check=check->next;
}
printf("\n");
}
//Function to check if a string finishes with a suffix
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
Node *listFilesRecursively(char*, char*);
//Function to find the files in a directory based on a wildcard
Node *listFilesRecursively(char *basePath, char *suffix)
{
char path[1000];
struct dirent *dp;
DIR *dir = opendir(basePath);
Node *Head = NULL;
Node *Head_scl = NULL;
//if (!dir)
// return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if (string_ends_with(path, suffix))
Head = insert(Head, path);
listFilesRecursively(path, suffix);
}
}
//show(Head);
closedir(dir);
return Head;
}
int main()
{
char path[100];
char suffix[100];
// Input path from user
// Suffix Band Sentinel-2 of Type B02_10m.tif
printf("Enter path to list files: ");
scanf("%s", path);
printf("Enter the wildcard: ");
scanf("%s", suffix);
Node *B02_head;
Node *SCL_head;
B02_head = listFilesRecursively(path, suffix);
char *suffix_scl = "SCL_10m.tif";
SCL_head = listFilesRecursively(path, suffix_scl);
show(B02_head);
show(SCL_head);
return 0;
}