using system calls to compare text files
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) ....