Index: /trunk/src/kmk/kmkbuiltin/cmp.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cmp.c	(revision 1115)
+++ /trunk/src/kmk/kmkbuiltin/cmp.c	(revision 1115)
@@ -0,0 +1,164 @@
+/*	$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $	*/
+
+/*
+ * Copyright (c) 1987, 1990, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__COPYRIGHT("@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
+	The Regents of the University of California.  All rights reserved.\n");
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)cmp.c	8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: cmp.c,v 1.15 2006/01/19 20:44:57 garbled Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <locale.h>
+
+#include "extern.h"
+
+int	lflag, sflag;
+
+static void usage(void);
+
+int
+main(int argc, char *argv[])
+{
+	struct stat sb1, sb2;
+	off_t skip1 = 0, skip2 = 0;
+	int ch, fd1, fd2, special;
+	char *file1, *file2;
+
+	setlocale(LC_ALL, "");
+
+	while ((ch = getopt(argc, argv, "ls")) != -1)
+		switch (ch) {
+		case 'l':		/* print all differences */
+			lflag = 1;
+			break;
+		case 's':		/* silent run */
+			sflag = 1;
+			break;
+		case '?':
+		default:
+			usage();
+		}
+	argv += optind;
+	argc -= optind;
+
+	if (lflag && sflag)
+		errx(ERR_EXIT, "only one of -l and -s may be specified");
+
+	if (argc < 2 || argc > 4)
+		usage();
+
+	/* Backward compatibility -- handle "-" meaning stdin. */
+	special = 0;
+	if (strcmp(file1 = argv[0], "-") == 0) {
+		special = 1;
+		fd1 = 0;
+		file1 = "stdin";
+	}
+	else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
+		if (!sflag)
+			warn("%s", file1);
+		exit(ERR_EXIT);
+	}
+	if (strcmp(file2 = argv[1], "-") == 0) {
+		if (special)
+			errx(ERR_EXIT,
+				"standard input may only be specified once");
+		special = 1;
+		fd2 = 0;
+		file2 = "stdin";
+	}
+	else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
+		if (!sflag)
+			warn("%s", file2);
+		exit(ERR_EXIT);
+	}
+
+	if (argc > 2) {
+		char *ep;
+
+		errno = 0;
+		skip1 = strtoq(argv[2], &ep, 0);
+		if (errno || ep == argv[2])
+			usage();
+
+		if (argc == 4) {
+			skip2 = strtoq(argv[3], &ep, 0);
+			if (errno || ep == argv[3])
+				usage();
+		}
+	}
+
+	if (!special) {
+		if (fstat(fd1, &sb1))
+			err(ERR_EXIT, "%s", file1);
+		if (!S_ISREG(sb1.st_mode))
+			special = 1;
+		else {
+			if (fstat(fd2, &sb2))
+				err(ERR_EXIT, "%s", file2);
+			if (!S_ISREG(sb2.st_mode))
+				special = 1;
+		}
+	}
+
+	if (special)
+		c_special(fd1, file1, skip1, fd2, file2, skip2);
+	else
+		c_regular(fd1, file1, skip1, sb1.st_size,
+		    fd2, file2, skip2, sb2.st_size);
+	exit(0);
+}
+
+static void
+usage(void)
+{
+
+	(void)fprintf(stderr,
+	    "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
+	exit(ERR_EXIT);
+}
Index: /trunk/src/kmk/kmkbuiltin/cmp_misc.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cmp_misc.c	(revision 1115)
+++ /trunk/src/kmk/kmkbuiltin/cmp_misc.c	(revision 1115)
@@ -0,0 +1,84 @@
+/*	$NetBSD: misc.c,v 1.11 2007/08/22 16:59:19 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)misc.c	8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: misc.c,v 1.11 2007/08/22 16:59:19 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "extern.h"
+
+void
+errmsg(char *file, off_t byte, off_t line)
+{
+	if (lflag)
+		err(ERR_EXIT, "%s: char %lld, line %lld", file,
+		    (long long)byte, (long long)line);
+	else
+		err(ERR_EXIT, "%s", file);
+}
+
+void
+eofmsg(char *file, off_t byte, off_t line)
+{
+	if (!sflag) {
+		if (!lflag)
+			warnx("EOF on %s", file);
+		else {
+		    if (line > 0)
+			    warnx("EOF on %s: char %lld, line %lld",
+				file, (long long)byte, (long long)line);
+		    else
+			    warnx("EOF on %s: char %lld",
+				file, (long long)byte);
+		}
+	}
+	exit(DIFF_EXIT);
+}
+
+void
+diffmsg(char *file1, char *file2, off_t byte, off_t line)
+{
+	if (!sflag)
+		(void)printf("%s %s differ: char %lld, line %lld\n",
+		    file1, file2, (long long)byte, (long long)line);
+	exit(DIFF_EXIT);
+}
Index: /trunk/src/kmk/kmkbuiltin/cmp_regular.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cmp_regular.c	(revision 1115)
+++ /trunk/src/kmk/kmkbuiltin/cmp_regular.c	(revision 1115)
@@ -0,0 +1,118 @@
+/*	$NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: regular.c,v 1.20 2006/06/03 21:47:55 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "extern.h"
+
+void
+c_regular(int fd1, char *file1, off_t skip1, off_t len1,
+    int fd2, char *file2, off_t skip2, off_t len2)
+{
+	u_char ch, *p1, *p2;
+	off_t byte, length, line;
+	int dfound;
+	size_t blk_sz, blk_cnt;
+
+	if (sflag && len1 != len2)
+		exit(1);
+
+	if (skip1 > len1)
+		eofmsg(file1, len1 + 1, 0);
+	len1 -= skip1;
+	if (skip2 > len2)
+		eofmsg(file2, len2 + 1, 0);
+	len2 -= skip2;
+
+	byte = line = 1;
+	dfound = 0;
+	length = MIN(len1, len2);
+	for (blk_sz = 1024 * 1024; length != 0; length -= blk_sz) {
+		if (blk_sz > length)
+			blk_sz = length;
+		p1 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED,
+		    fd1, skip1);
+		if (p1 == MAP_FAILED)
+			goto mmap_failed;
+
+		p2 = mmap(NULL, blk_sz, PROT_READ, MAP_FILE|MAP_SHARED,
+		    fd2, skip2);
+		if (p2 == MAP_FAILED) {
+			munmap(p1, blk_sz);
+			goto mmap_failed;
+		}
+
+		blk_cnt = blk_sz;
+		for (; blk_cnt--; ++p1, ++p2, ++byte) {
+			if ((ch = *p1) != *p2) {
+				if (!lflag) {
+					diffmsg(file1, file2, byte, line);
+					/* NOTREACHED */
+				}
+				dfound = 1;
+				(void)printf("%6lld %3o %3o\n",
+				    (long long)byte, ch, *p2);
+			}
+			if (ch == '\n')
+				++line;
+		}
+		munmap(p1 - blk_sz, blk_sz);
+		munmap(p2 - blk_sz, blk_sz);
+		skip1 += blk_sz;
+		skip2 += blk_sz;
+	}
+
+	if (len1 != len2)
+		eofmsg(len1 > len2 ? file2 : file1, byte, line);
+	if (dfound)
+		exit(DIFF_EXIT);
+	return;
+
+mmap_failed:
+	c_special(fd1, file1, skip1, fd2, file2, skip2);
+}
Index: /trunk/src/kmk/kmkbuiltin/cmp_special.c
===================================================================
--- /trunk/src/kmk/kmkbuiltin/cmp_special.c	(revision 1115)
+++ /trunk/src/kmk/kmkbuiltin/cmp_special.c	(revision 1115)
@@ -0,0 +1,112 @@
+/*	$NetBSD: special.c,v 1.12 2007/08/21 14:09:54 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)special.c	8.3 (Berkeley) 4/2/94";
+#else
+__RCSID("$NetBSD: special.c,v 1.12 2007/08/21 14:09:54 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "extern.h"
+
+void
+c_special(int fd1, char *file1, off_t skip1, int fd2, char *file2, off_t skip2)
+{
+	int ch1, ch2;
+	off_t byte, line;
+	FILE *fp1, *fp2;
+	int dfound;
+
+	dfound = 0;
+	if ((fp1 = fdopen(fd1, "r")) == NULL)
+		err(ERR_EXIT, "%s", file1);
+	if ((fp2 = fdopen(fd2, "r")) == NULL)
+		err(ERR_EXIT, "%s", file2);
+
+	for (byte = line = 1; skip1--; byte++) {
+		ch1 = getc(fp1);
+		if (ch1 == EOF)
+			goto eof;
+		if (ch1 == '\n')
+			line++;
+	}
+	for (byte = line = 1; skip2--; byte++) {
+		ch2 = getc(fp2);
+		if (ch2 == EOF)
+			goto eof;
+		if (ch2 == '\n')
+			line++;
+	}
+	dfound = 0;
+	for (byte = line = 1;; ++byte) {
+		ch1 = getc(fp1);
+		ch2 = getc(fp2);
+		if (ch1 == EOF || ch2 == EOF)
+			break;
+		if (ch1 != ch2) {
+			if (lflag) {
+				dfound = 1;
+				(void)printf("%6lld %3o %3o\n", (long long)byte,
+				    ch1, ch2);
+			} else
+				diffmsg(file1, file2, byte, line);
+				/* NOTREACHED */
+		}
+		if (ch1 == '\n')
+			++line;
+	}
+
+ eof:
+	if (ferror(fp1))
+		errmsg(file1, byte, line);
+	if (ferror(fp2))
+		errmsg(file2, byte, line);
+	if (feof(fp1)) {
+		if (!feof(fp2))
+			eofmsg(file1, byte, line);
+	} else
+		if (feof(fp2))
+			eofmsg(file2, byte, line);
+	(void)fclose(fp1);
+	(void)fclose(fp2);
+	if (dfound)
+		exit(DIFF_EXIT);
+}
