using system calls to compare text files I am trying to make a script that compares 2 .txt files.

It works, but I wanted to double-check if there's something I missed about these system calls (since I'm new at this). Is there any improvement you would've done or anything else you think can help me improve with it (especially with this mmap thing)?

The script returns 2 if it's the same file and 1 otherwise.

#include&lt;sys/stat.h&gt;
#include&lt;unistd.h&gt;
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;

int main(int argc , char * argv[])
{
    if(argc != 3)
    {
        printf("something wrong with variables\n");
        exit(-1);
    }

    char* filename1 = argv[1];
    char* filename2 = argv[2];
    char* addr1;
    char* addr2;
    struct stat stat_p1;
    struct stat stat_p2;
    int fileSize1=0,fileSize2=0;

    if(stat(filename1,&amp;stat_p1) == -1)
    {
        printf("error occurred while attempting to stat %s\n" , filename1);
        exit(-2);
    }

    if(stat(filename2,&amp;stat_p2) == -1)
    {
        printf("error occurred while attempting to stat %s\n" , filename2);
        exit(-2);
    }

    if((fileSize1=stat_p1.st_size) != (fileSize2=stat_p2.st_size)) //checks if the size is different than its not the same file.
    {
        return 1;
    }
    else
    {
        int fd1,fd2;
        if((fd1 = open(filename1 ,O_RDONLY)) &lt; 0)
        {
            printf("error opening file %s\n",filename1);
            exit(-3);
        }
        if((fd2 = open(filename2 ,O_RDONLY)) &lt; 0)
        {
            printf("error opening file %s\n",filename2);
            exit(-3);
        }

        addr1 = mmap(NULL, fileSize1+ 1, PROT_READ,MAP_PRIVATE, fd1, 0);
        if (addr1 == MAP_FAILED)
        {
            printf("mmap failed\n");
            exit(-4);
        }

        addr2 = mmap(NULL, fileSize2+ 1, PROT_READ,MAP_PRIVATE, fd2, 0);
        if (addr2 == MAP_FAILED)
        {
            printf("mmap failed\n");
            exit(-4);
        }
        for(int i=0;i&lt;fileSize1+1;i++)
        {
            if(addr1[i] != addr2[i])
            {
                return 1;
            }
        }
        close(fd1);
        close(fd2);
    }
    return 2;
}


(It's in the main file because it's meant to be used as part of testing program later.)
Only for the pedantic

With non-2's complement, the below will compare +0 as matching -0.

char* addr1;
char* addr2;
...
if(addr1[i] != addr2[i]) ... // Oops. +0 == -0


Instead use unsigned char *

unsigned char* addr1;
unsigned char* addr2;
...
if (addr1[i] != addr2[i]) ... // OK.  Only one kind of 0


Better yet, use memcmp().

Return values

"... returns 2 if it's the same file and 1 otherwise." --> Not quite.

Code also returns via exit() with other values.

exit(-1);
exit(-3);
exit(-4);


+ 1??

Off by 1.  No need for + 1.  G. Sliepen.

// for(int i=0;i&lt;fileSize1+1;i++)
for (int i=0; i&lt;fileSize1; i++)


Matching type

.st_size is typed as off_t.  I'd expect fileSize1, fileSize2, i as the same.

off_t fileSize1;
...
for (off_t i = 0; i &lt; fileSize1; i++)


Big files

.st_size is a off_t, some signed type.  mmap(void *addr, size_t length, ...) takes a length of size_t, some unsigned type.

To do this right, code may need to call mmap() multiple times as file size can exceed SIZE_MAX.

Something like:

#define CHUNK  (‭1048576‬ /* 1 Meg */)

off_t fileSize1;
fileSize1 = stat_p1.st_size;

....

for (off_t offset = 0; offset &lt; fileSize1; offset += CHUNK) {
  size_t map_size1 = CHUNK;
  if (fileSize1 - offset &lt; CHUNK) {
    map_size1 = (size_t) (fileSize1 - offset);
  }

  addr1 = mmap(NULL, map_size1, PROT_READ,MAP_PRIVATE, fd1, offset);

  // as above for map_size1, addr2

  for (size_t i = 0; i &lt; map_size1; i++)
    ...
  // or
  if (memcmp(addr1, addr2, map_size1) ....