0

I wrote a program that checks if two files match or not. How can I modify the program to check if the file does not have enough to access? How can I handle errors better?

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

int main(int argc, char **argv)
{
    if(argc < 3)
    {
        printf("The file names are\n");
        return 1;
    }

  struct stat fileSt1;
    struct stat fileSt2;

    if(stat(argv[1],&fileSt1) < 0)
    {
        printf("Error at first parameter. File not found\n");
        return 1;
    }
    if(stat(argv[2],&fileSt2) < 0)
    {
        printf("Second parameter error. File not found\n");
        return 1;
    }

    if(S_ISREG(fileSt1.st_mode) && S_ISREG(fileSt2.st_mode)){
        if((fileSt1.st_dev == fileSt2.st_dev) && (fileSt1.st_ino == fileSt2.st_ino)){
            printf("Files '%s' and '%s' match\n", argv[1], argv[2]);
        }
        else
            printf("Files '%s' and '%s' they do not match\n", argv[1], argv[2]);
    }
    else
        printf("These are not files.\n");

    return 0;
}
Lorand
  • 31
  • 4

0 Answers0