Core dump issue

Viewed 41

I would like to create a C program that retrieves the inode information of a file (or a directory file) and put it as a parameter and display the file creation date but I have a segmentation problem. I don't know how to fix it. PS: I should use Ctime.

#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char * argv[]) {
    struct stat *buffer = NULL;
    buffer = malloc(sizeof(struct stat));
    stat (argv[1],buffer);
printf("Date de création du fichier:  %s", ctime(&buffer->st_ctime));
printf("Numéro inode: %ld\n", buffer->st_ino);
return 0;
}

enter image description here

2 Answers

I made a number of changes to the code. I'll detail them below.

% cc -o core core.c ; ./core test.txt
Date de création du fichier:  Sun Sep 11 12:03:26 2022
Numéro inode: 41714431
% cat core.c
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char * argv[]) {
    struct stat *buffer = NULL;

    if (argc < 2 || argv[1] == NULL) {
        printf("Need a file name\n");
        exit(1);
    }
    buffer = malloc(sizeof(struct stat));
    if (buffer == NULL) {
        perror("malloc:");
        exit(1);
    }

    int ret = stat (argv[1], buffer);
    if (ret) {
        perror("stat:");
        exit(1);
    }

    printf("Date de création du fichier:  %s", ctime(&buffer->st_ctime));
    printf("Numéro inode: %llu\n", buffer->st_ino);
    return 0;
}
  • You should include <time.h> as you are using ctime().
  • Checking there is at least one argument (the argc & argv[1] check) is important.
  • The memory allocation could fail, so I verify we have memory allocated.
  • The stat() could also fail, let us verify that is a success.
  • I changed the %ld to %llu to better match st_ino.

First of all, there's no need to allocate struct stat on the heap. Just use a local stack variable

struct stat buf;
int err = stat(argc[1], &buf);

Next, the error comes from the missing declaration for char *ctime(). When the C compiler has no declaration for ctime(), it assumes a return type of int. Since this doesn't match the format string of %s, anything can happen.


You can see this, when you add compiler warnings

$ gcc -Wall -Wextra a.c
/tmp/a.c: In function ‘main’:
/tmp/a.c:13:45: warning: implicit declaration of function ‘ctime’ [-Wimplicit-function-declaration]
   13 | printf("Date de création du fichier:  %s", ctime(&buffer->st_ctime));
      |                                             ^~~~~
/tmp/a.c:13:41: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   13 | printf("Date de création du fichier:  %s", ctime(&buffer->st_ctime));
      |                                        ~^   ~~~~~~~~~~~~~~~~~~~~~~~~
      |                                         |   |
      |                                         |   int
      |                                         char *
      |                                        %d

You may fix this by declaring ctime() through

#include <time.h>

Now, the program runs without dumping core.

Related