head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2013.01.15.01.49.23;	author agc;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Add rudimentary support for cmp(1) in the shape of netcmp(1). More to come
on this one, too.
@
text
@#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <err.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// BUFGAP

typedef struct f_t {
	char	*name;
	char	*mapped;
	FILE	*fp;
	size_t	 size;
} f_t;

static int
finit(f_t *file, const char *name)
{
	struct stat	st;

	memset(file, 0x0, sizeof(*file));
	if ((file->fp = fopen(name, "r")) == NULL) {
		return 0;
	}
	fstat(fileno(file->fp), &st);
	file->size = st.st_size;
	file->mapped = mmap(NULL, file->size, PROT_READ, MAP_SHARED, fileno(file->fp), 0);
	// xxx
	file->name = strdup(name);
	return 1;
}

static void
fend(f_t *f)
{
	free(f->name);
	munmap(f->mapped, f->size);
	fclose(f->fp);
	memset(f, 0x0, sizeof(*f));
}

static int
diff(const char *filename1, const char *filename2)
{
	FILE	*pp;
	char	 buf[2048]; // XXX
	char	 cmd[2048]; // XXX
	f_t	 f[2];

	finit(&f[0], filename1);
	finit(&f[1], filename2);
	snprintf(cmd, sizeof(cmd), "diff %s %s", filename1, filename2);
	if ((pp = popen(cmd, "r")) == NULL) {
		return 0;
	}
	while (fgets(buf, sizeof(buf), pp) != NULL) {
		printf("%s", buf);
	}
	pclose(pp);
	fend(&f[0]);
	fend(&f[1]);
	return 1;
}

int
main(int argc, char **argv)
{
	diff(argv[optind], argv[optind + 1]);
	exit(EXIT_SUCCESS);
}
@
