I wrote a program that checks if two files (with a different path / name) match or not. The hard and symbolic links between the files are followed to determine it.
I know this is not the best implementation, so I would like some suggestions for improvement, possible problems. What you should change?
code:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
void printUsage()
{
printf("Use: ./leg <name_file1> <name_file2>.\n");
}
int createStat(const char* file, struct stat* fileStat)
{
if(stat(file,fileStat) < 0)
{
fprintf(stderr,"Error! File %s does not exist.\n",file);
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
if(argc < 3)
{
printf("Insufficient number of parameters.\n");
printUsage();
return -1;
}
struct stat fileStat1;
struct stat fileStat2;
if(createStat(argv[1],&fileStat1) == 0)
{
return -1;
}
if(createStat(argv[2],&fileStat2) == 0)
{
return -1;
}
if(S_ISREG(fileStat1.st_mode) && S_ISREG(fileStat2.st_mode)){
if((fileStat1.st_dev == fileStat2.st_dev) && (fileStat1.st_ino == fileStat2.st_ino)){
printf("Files'%s' and '%s' coincide.\n", argv[1], argv[2]);
}
else
printf("Files '%s' and '%s' do not match.\n", argv[1], argv[2]);
}
else
fprintf(stderr,"'%s' and '%s' there are no files.\n",argv[1],argv[2]);
return 0;
}