0

I get this error Segmentation fault: 11 I am not sure how to corrected this memory error. Please help me out, any hints are highly appreciated. Thanks! This is my code:

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

int main(int argc, char **argv)
{
    DIR *dp;
    struct dirent *dirp;

    if ((dp = opendir(argv[1])) == NULL)
        printf("can’t open %s", argv[1]);

    while ((dirp = readdir(dp)) != NULL){
        struct stat fileStat;
        stat(dirp->d_name,&fileStat);

        printf(dirp->d_name);
        printf("—————————\n");
        printf("File Size: \t\t%d bytes\n",fileStat.st_size);
        printf("Number of Links: \t%d\n",fileStat.st_nlink);
        printf("File inode: \t\t%d\n",fileStat.st_ino);

        printf("File Permissions: \t");
        printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
        printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
        printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
        printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
        printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
        printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
        printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
        printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
        printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
        printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
        printf("\n\n");

        printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
    } 
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Anastasia Netz
  • 173
  • 2
  • 12
  • Check the return value of `stat()`, I'll bet it's returning an error. – Barmar Apr 05 '16 at 02:24
  • The first argument to `stat()` needs to be the full pathname of the file, you need to concatenate the directory and filename. – Barmar Apr 05 '16 at 02:25

0 Answers0