From 408f5ab7a3d4ee266cf7ed021cc499d115a99fc3 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 21 Nov 2020 22:49:30 +0000 Subject: [PATCH] Added arbitary number of files to check --- .gitignore | 1 + Makefile | 2 +- src/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 83561a8..5dbb27e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ obj/ fcmp-* +test/ diff --git a/Makefile b/Makefile index 0da2f8f..5b09964 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ OPT_FLAGS+= -fgraphite RELEASE_CFLAGS?= -O3 -march=native -flto $(OPT_FLAGS) RELEASE_LDFLAGS?= -O3 -flto -DEBUG_CFLAGS?= -O0 -g +DEBUG_CFLAGS?= -DDEBUG -O0 -g DEBUG_LDFLAGS?= -O0 CFLAGS+= -Wall -pedantic --std=gnu11 $(addprefix -I,$(INCLUDE)) diff --git a/src/main.c b/src/main.c index 9987261..560b0cb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,10 +6,17 @@ static const char* _prog_name = "fcmp"; -__attribute__((noreturn)) void usage() +#ifdef DEBUG +#define __name(d) #d +#define dprintf(fmt, ...) printf("[dbg @" __FILE__ "->%s:%d] " fmt "\n", __func__, __LINE__ __VA_OPT__(,) __VA_ARGS__) +#else +#define dprintf(fmt, ...) +#endif + +__attribute__((noreturn)) static void usage() { fprintf(stderr, "fcmp: compare files for identity\n"); - fprintf(stderr, "usage: %s \n", _prog_name); + fprintf(stderr, "usage: %s \n", _prog_name); exit(-1); } @@ -19,37 +26,90 @@ __attribute__((always_inline)) static inline const void* die_if_null(const void* else return ptr; } +static int unmap_all(mmap_t ptrs[], size_t len) +{ + register int rval=1; + dprintf("Unmapping %lu entries", len); + for (register size_t i=0;ilen != map2->len) rval = 2; + else if (memcmp(map1->ptr, map2->ptr, map1->len) != 0) rval = 1; + + if(!unmap_and_close(*map2)) { + fprintf(stderr, "Failed to unmap and close"); + rval=-1; + } + + return rval; +} + int main(int argc, char** argv) { _prog_name = argv[0]; + const int nrest = argc-2; + if (nrest==0) usage(); + dprintf("There are %d extra files to chk", nrest); const char* f1 = die_if_null(argv[1]); - const char* f2 = die_if_null(argv[2]); + const char* frest[nrest]; + + for (register int i=0;i