I have written a simple code to read a directory then sort the files by last modified time,but the following code will crash because Segmentation fault (core dumped),(occur when try to access the lsf[0] and lsf[1] ),how to solve the problem,please help,thanks:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
typedef struct {
char *name;
unsigned long lasted;
} sort_file;
char *make_path(char *path) {
char *endp = path + strlen(path);
*endp++ = '/';
return endp;
}
int main(int arc, char **argv) {
const char *n_dir = "/home/psycho/Pictures";
char *dir_name = strdup(n_dir);
char *endp = make_path(dir_name);
DIR *dir;
struct dirent *ent;
struct stat attr;
int size = 150;
int count = 0;
sort_file **lsf = malloc(size * sizeof(sort_file *));
printf("[Logging] : %d => %d \n", sizeof(sort_file), sizeof(sort_file *));
if ((dir = opendir(n_dir)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.') {
continue;
} else if (ent->d_type == DT_DIR) {
strcpy(endp, ent->d_name);
stat(dir_name, &attr);
lsf[count] = malloc(sizeof(sort_file));
lsf[count]->lasted = (unsigned long)attr.st_mtime;
lsf[count]->name = malloc((strlen(ent->d_name) )* sizeof(char));
sprintf(lsf[count]->name, "%s", ent->d_name);
printf("[Logging] : --- %lu --- %d --- %s \n", lsf[count]->lasted,
count, lsf[count]->name);
count++;
}
}
closedir(dir);
} else {
return 0;
}
while (count--) {
printf("[Logging] : --- %lu --- %d --- %s \n", lsf[count]->lasted, count,
lsf[count]->name);
}
return 0;
}
This code includes some memory panics, depend on different compilers different errors may occur.
make_path
, endp
is pointing to last character of dir_name
( '\\0'
) and is trying to change it from '\\0'
to '/'
without inserting the string trailing char ( '\\0'
) at the end and realloc
one more character for dir_name
main
, const char *n_dir = "/home/psycho/Pictures";
do not need to be defined as pointer because length is known at compile time. In fact it should be changed to: const char n_dir[] = "/home/psycho/Pictures";
main
, endp
is not allocated at all so strcpy(endp, ent->d_name);
copies to restricted memory spaces main
, lsf[count]->name = malloc((strlen(ent->d_name) )* sizeof(char));
should allocate one more unit because of trailing '\\0'
: lsf[count]->name = malloc((strlen(ent->d_name)
+ 1 )* sizeof(char));
At last I noticed a logical error: if (ent->d_name[0] == '.') {
... makes program to skip ' . ', ' .. ', and all other directories starting with ' . ' (Linux hidden folders). You can use 0 == (strcmp(ent->d_name, ".") * strcmp(ent->d_name, ".."))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.