Commit Diff


commit - 826c8cf15933d99e93bcb7940c826773c4d34f86
commit + b858fc9f8b3eee4ec048dcd8083ccfa4384e68db
blob - /dev/null
blob + 7675e9a5b6d87c99364a7f47ca7d5992766b7f04 (mode 644)
--- /dev/null
+++ GNUmakefile
@@ -0,0 +1,26 @@
+# diff-portable
+
+PROG=		diff
+SRCS=		diff.c diff_atomize_text.c diff_main.c diff_myers.c \
+		diff_patience.c diff_output.c
+
+CFLAGS+=	-Wstrict-prototypes -Wunused-variable
+
+
+# Compat sources
+VPATH=		$(CURDIR)/compat
+SRCS+=		getprogname_linux.c recallocarray.c
+CFLAGS+=	-I$(CURDIR)/compat/include
+
+
+# Shouldn't need to change anything below
+all: $(PROG)
+
+$(PROG): $(SRCS:.c=.o)
+
+.c.o:
+	$(CC) -c $(CFLAGS) $<
+
+.PHONY: clean
+clean:
+	rm -f $(PROG) *.o
blob - /dev/null
blob + 0957c6bc106b6188958050f26289383d01c232e8 (mode 644)
--- /dev/null
+++ compat/getprogname_linux.c
@@ -0,0 +1,8 @@
+#define _GNU_SOURCE
+#include <errno.h>
+
+const char *
+getprogname(void)
+{
+	return program_invocation_short_name;
+}
blob - /dev/null
blob + b16aa2e0d80e22365fd914d1ec0cefea62efdd90 (mode 644)
--- /dev/null
+++ compat/include/stdlib.h
@@ -0,0 +1,19 @@
+/*
+ * stdlib.h compatibility shim
+ * Public domain
+ */
+
+#include_next <stdlib.h>
+
+#ifndef DIFFCOMPAT_STDLIB_H
+#define DIFFCOMPAT_STDLIB_H
+
+#include <sys/types.h>
+#include <stdint.h>
+
+const char * getprogname(void);
+
+void *reallocarray(void *, size_t, size_t);
+void *recallocarray(void *, size_t, size_t, size_t);
+
+#endif
blob - /dev/null
blob + f580c7c593a8653c4d477ae3887d422e2d7d32ae (mode 644)
--- /dev/null
+++ compat/include/sys/types.h
@@ -0,0 +1,15 @@
+/*
+ * Public domain
+ * sys/types.h compatibility shim
+ */
+
+#include_next <sys/types.h>
+
+#ifndef DIFFCOMPAT_SYS_TYPES_H
+#define DIFFCOMPAT_SYS_TYPES_H
+
+#if !defined(__dead)
+#define __dead      __attribute__((__noreturn__))
+#endif
+
+#endif
blob - /dev/null
blob + d93abd2da5fd59a9980e40f2cd3c0f897c5e5909 (mode 644)
--- /dev/null
+++ compat/recallocarray.c
@@ -0,0 +1,80 @@
+/*	$OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $	*/
+/*
+ * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
+
+void *
+recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
+{
+	size_t oldsize, newsize;
+	void *newptr;
+
+	if (ptr == NULL)
+		return calloc(newnmemb, size);
+
+	if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+	    newnmemb > 0 && SIZE_MAX / newnmemb < size) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	newsize = newnmemb * size;
+
+	if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+	    oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
+		errno = EINVAL;
+		return NULL;
+	}
+	oldsize = oldnmemb * size;
+	
+	/*
+	 * Don't bother too much if we're shrinking just a bit,
+	 * we do not shrink for series of small steps, oh well.
+	 */
+	if (newsize <= oldsize) {
+		size_t d = oldsize - newsize;
+
+		if (d < oldsize / 2 && d < getpagesize()) {
+			memset((char *)ptr + newsize, 0, d);
+			return ptr;
+		}
+	}
+
+	newptr = malloc(newsize);
+	if (newptr == NULL)
+		return NULL;
+
+	if (newsize > oldsize) {
+		memcpy(newptr, ptr, oldsize);
+		memset((char *)newptr + oldsize, 0, newsize - oldsize);
+	} else
+		memcpy(newptr, ptr, newsize);
+
+	explicit_bzero(ptr, oldsize);
+	free(ptr);
+
+	return newptr;
+}