---
 sbin/fsck_ffs/Makefile    |   27 ++++++++++++-------------
 sbin/fsck_ffs/ea.c        |    2 -
 sbin/fsck_ffs/fsck.h      |    1 
 sbin/fsck_ffs/fsck_ffs.8  |    7 ++----
 sbin/fsck_ffs/fsutil.c    |   13 ++++++++++--
 sbin/fsck_ffs/gjournal.c  |    4 +++
 sbin/fsck_ffs/inode.c     |    3 +-
 sbin/fsck_ffs/main.c      |   49 ++++++++++++++++++++++++++++++++++++++++++++--
 sbin/fsck_ffs/pass1.c     |    2 +
 sbin/fsck_ffs/setup.c     |   14 +++++++++++++
 sbin/fsck_ffs/utilities.c |    6 +++--
 11 files changed, 102 insertions(+), 26 deletions(-)

--- a/sbin/fsck_ffs/gjournal.c
+++ b/sbin/fsck_ffs/gjournal.c
@@ -54,6 +54,9 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+
+#if HAVE_UFS_SNAPSHOT
+
 #include <sys/param.h>
 #include <sys/disklabel.h>
 #include <sys/mount.h>
@@ -774,3 +777,4 @@ gjournal_check(const char *filesys)
 	/* Write back updated statistics and super-block. */
 	putdisk();
 }
+#endif
--- a/sbin/fsck_ffs/ea.c
+++ b/sbin/fsck_ffs/ea.c
@@ -38,7 +38,7 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/time.h>
-#include <sys/stdint.h>
+#include <stdint.h>
 
 #include <ufs/ufs/dinode.h>
 #include <ufs/ufs/dir.h>
--- a/sbin/fsck_ffs/fsck_ffs.8
+++ b/sbin/fsck_ffs/fsck_ffs.8
@@ -30,11 +30,11 @@
 .\" $FreeBSD$
 .\"
 .Dd January 25, 2009
-.Dt FSCK_FFS 8
+.Dt FSCK.UFS 8
 .Os
 .Sh NAME
-.Nm fsck_ffs ,
-.Nm fsck_ufs
+.Nm fsck.ffs ,
+.Nm fsck.ufs
 .Nd file system consistency check and interactive repair
 .Sh SYNOPSIS
 .Nm
@@ -349,5 +349,4 @@ are fully enumerated and explained in Ap
 .Xr fstab 5 ,
 .Xr fsck 8 ,
 .Xr fsdb 8 ,
-.Xr newfs 8 ,
 .Xr reboot 8
--- a/sbin/fsck_ffs/fsutil.c
+++ b/sbin/fsck_ffs/fsutil.c
@@ -38,7 +38,9 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#if HAVE_BSD_SYSCTL
 #include <sys/sysctl.h>
+#endif
 #include <sys/disklabel.h>
 #include <sys/stat.h>
 #include <sys/disklabel.h>
@@ -55,6 +57,7 @@ __FBSDID("$FreeBSD$");
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <signal.h>
 #include <unistd.h>
 
 #include "fsck.h"
@@ -277,11 +280,13 @@ ckfini(int markclean)
 	if (bkgrdflag) {
 		unlink(snapname);
 		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
+#ifdef HAVE_BSD_SYSCTL
 			cmd.value = FS_UNCLEAN;
 			cmd.size = markclean ? -1 : 1;
 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
 			    &cmd, sizeof cmd) == -1)
 				rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
+#endif
 			if (!preen) {
 				printf("\n***** FILE SYSTEM MARKED %s *****\n",
 				    markclean ? "CLEAN" : "DIRTY");
@@ -639,7 +644,7 @@ getpathname(char *namebuf, ino_t curdir,
 }
 
 void
-catch(int sig __unused)
+catch(int sig)
 {
 
 	ckfini(0);
@@ -652,7 +657,7 @@ catch(int sig __unused)
  * so that reboot sequence may be interrupted.
  */
 void
-catchquit(int sig __unused)
+catchquit(int sig)
 {
 	printf("returning to single-user after file system check\n");
 	returntosingle = 1;
@@ -721,11 +726,13 @@ pfatal(const char *fmt, ...)
 		 * Force foreground fsck to clean up inconsistency.
 		 */
 		if (bkgrdflag) {
+#if HAVE_BSD_SYSCTL
 			cmd.value = FS_NEEDSFSCK;
 			cmd.size = 1;
 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
 			    &cmd, sizeof cmd) == -1)
 				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
+#endif
 			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
 			ckfini(0);
 			exit(EEXIT);
@@ -742,6 +749,7 @@ pfatal(const char *fmt, ...)
 	/*
 	 * Force foreground fsck to clean up inconsistency.
 	 */
+#if HAVE_BSD_SYSCTL
 	if (bkgrdflag) {
 		cmd.value = FS_NEEDSFSCK;
 		cmd.size = 1;
@@ -749,6 +757,7 @@ pfatal(const char *fmt, ...)
 		    &cmd, sizeof cmd) == -1)
 			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
 	}
+#endif
 	ckfini(0);
 	exit(EEXIT);
 }
--- a/sbin/fsck_ffs/inode.c
+++ b/sbin/fsck_ffs/inode.c
@@ -36,7 +36,7 @@ static const char sccsid[] = "@(#)inode.
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
-#include <sys/stdint.h>
+#include <stdint.h>
 #include <sys/time.h>
 #include <sys/sysctl.h>
 
@@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <pwd.h>
 #include <string.h>
+#include <time.h>
 
 #include "fsck.h"
 
--- a/sbin/fsck_ffs/main.c
+++ b/sbin/fsck_ffs/main.c
@@ -45,9 +45,17 @@ __FBSDID("$FreeBSD$");
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/time.h>
+#ifdef HAVE_BSD_STATFS
 #include <sys/mount.h>
+#else
+#include <sys/statvfs.h>
+#define statfs statvfs
+#define f_flags f_flag
+#endif
 #include <sys/resource.h>
+#ifdef HAVE_BSD_SYSCTL
 #include <sys/sysctl.h>
+#endif
 #include <sys/uio.h>
 #include <sys/disklabel.h>
 
@@ -62,14 +70,18 @@ __FBSDID("$FreeBSD$");
 #include <paths.h>
 #include <stdint.h>
 #include <string.h>
+#include <time.h>
+#include <signal.h>
 
 #include "fsck.h"
 
-static void usage(void) __dead2;
+static void usage(void);
 static int argtoi(int flag, const char *req, const char *str, int base);
 static int checkfilesys(char *filesys);
 static int chkdoreload(struct statfs *mntp);
+#ifdef HAVE_BSD_STATFS
 static struct statfs *getmntpt(const char *);
+#endif
 
 int
 main(int argc, char *argv[])
@@ -79,10 +91,12 @@ main(int argc, char *argv[])
 	struct itimerval itimerval;
 	int ret = 0;
 
+	setprogname(argv[0]);
+
 	sync();
 	skipclean = 1;
 	inoopt = 0;
-	while ((ch = getopt(argc, argv, "b:Bc:CdfFm:npry")) != -1) {
+	while ((ch = getopt(argc, argv, "ab:Bc:CdfFm:npry")) != -1) {
 		switch (ch) {
 		case 'b':
 			skipclean = 0;
@@ -126,6 +140,7 @@ main(int argc, char *argv[])
 			yflag = 0;
 			break;
 
+		case 'a':
 		case 'p':
 			preen++;
 			/*FALLTHROUGH*/
@@ -157,7 +172,9 @@ main(int argc, char *argv[])
 		(void)signal(SIGINT, catch);
 	if (ckclean)
 		(void)signal(SIGQUIT, catchquit);
+#ifdef HAVE_BSD_SIGNALS
 	signal(SIGINFO, infohandler);
+#endif
 	if (bkgrdflag) {
 		signal(SIGALRM, alarmhandler);
 		itimerval.it_interval.tv_sec = 5;
@@ -204,8 +221,10 @@ checkfilesys(char *filesys)
 	ufs2_daddr_t n_ffree, n_bfree;
 	struct dups *dp;
 	struct statfs *mntp;
+#if HAVE_UFS_SNAPSHOT && HAVE_BSD_STATFS
 	struct stat snapdir;
 	struct group *grp;
+#endif
 	ufs2_daddr_t blks;
 	struct iovec *iov;
 	char errmsg[255];
@@ -226,10 +245,14 @@ checkfilesys(char *filesys)
 	 * if it is listed among the mounted file systems. Failing that
 	 * check to see if it is listed in /etc/fstab.
 	 */
+#if HAVE_BSD_STATFS
 	mntp = getmntpt(filesys);
 	if (mntp != NULL)
 		filesys = mntp->f_mntfromname;
 	else
+#else
+	mntp = NULL;
+#endif
 		filesys = blockcheck(filesys);
 	/*
 	 * If -F flag specified, check to see whether a background check
@@ -247,8 +270,10 @@ checkfilesys(char *filesys)
 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
 			exit(5);	/* Not running soft updates */
 		size = MIBSIZE;
+#if HAVE_BSD_SYSCTL
 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
 			exit(6);	/* Lacks kernel support */
+#endif
 		if ((mntp == NULL && sblock.fs_clean == 1) ||
 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
 			exit(7);	/* Filesystem clean, report it now */
@@ -261,6 +286,7 @@ checkfilesys(char *filesys)
 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
 			exit(3);	/* Cannot read superblock */
 		close(fsreadfd);
+#if HAVE_UFS_SNAPSHOT
 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
 			//printf("GJournaled file system detected on %s.\n",
 			//    filesys);
@@ -278,6 +304,7 @@ checkfilesys(char *filesys)
 				    "CANNOT RUN FAST FSCK\n");
 			}
 		}
+#endif
 	}
 	/*
 	 * If we are to do a background check:
@@ -286,6 +313,7 @@ checkfilesys(char *filesys)
 	 *	return created snapshot file
 	 *	if not found, clear bkgrdflag and proceed with normal fsck
 	 */
+#if HAVE_UFS_SNAPSHOT && HAVE_BSD_STATFS
 	if (bkgrdflag) {
 		if (mntp == NULL) {
 			bkgrdflag = 0;
@@ -368,6 +396,7 @@ checkfilesys(char *filesys)
 				filesys = snapname;
 		}
 	}
+#endif
 
 	switch (setup(filesys)) {
 	case 0:
@@ -375,7 +404,9 @@ checkfilesys(char *filesys)
 			pfatal("CAN'T CHECK FILE SYSTEM.");
 		return (0);
 	case -1:
+#if HAVE_UFS_SNAPSHOT && HAVE_BSD_STATFS
 	clean:
+#endif
 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
 		printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
@@ -395,8 +426,10 @@ checkfilesys(char *filesys)
 	 */
 	if (preen == 0) {
 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
+#if HAVE_BSD_STATFS
 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
 			printf("** Root file system\n");
+#endif
 		printf("** Phase 1 - Check Blocks and Sizes\n");
 	}
 	pass1();
@@ -495,8 +528,13 @@ checkfilesys(char *filesys)
 	/*
 	 * Check to see if the file system is mounted read-write.
 	 */
+#if HAVE_BSD_STATFS
 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
 		resolved = 0;
+#else
+	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flag & ST_RDONLY) == 0)
+		resolved = 0;
+#endif
 	ckfini(resolved);
 
 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
@@ -537,6 +575,8 @@ chkdoreload(struct statfs *mntp)
 	 * it unless it is read-write, so we can continue using it
 	 * as safely as possible.
 	 */
+#if HAVE_BSD_STATFS && HAVE_BSD_MOUNT
+
 	if (mntp->f_flags & MNT_RDONLY) {
 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
@@ -560,8 +600,12 @@ chkdoreload(struct statfs *mntp)
 		return (1);
 	}
 	return (0);
+#else
+#warning "FIXME: use linux mount call"
+#endif
 }
 
+#if HAVE_BSD_STATFS
 /*
  * Get the mount point information for name.
  */
@@ -601,6 +645,7 @@ getmntpt(const char *name)
 	statfsp = NULL;
 	return (statfsp);
 }
+#endif
 
 static void
 usage(void)
--- a/sbin/fsck_ffs/pass1.c
+++ b/sbin/fsck_ffs/pass1.c
@@ -357,9 +357,11 @@ checkinode(ino_t inumber, struct inodesc
 	inoinfo(inumber)->ino_type = IFTODT(mode);
 	badblk = dupblk = 0;
 	idesc->id_number = inumber;
+#if HAVE_UFS_SNAPSHOT
 	if (DIP(dp, di_flags) & SF_SNAPSHOT)
 		idesc->id_type = SNAP;
 	else
+#endif
 		idesc->id_type = ADDR;
 	(void)ckinode(dp, idesc);
 	if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
--- a/sbin/fsck_ffs/setup.c
+++ b/sbin/fsck_ffs/setup.c
@@ -36,11 +36,14 @@ static const char sccsid[] = "@(#)setup.
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #define FSTYPENAMES
 #include <sys/disklabel.h>
 #include <sys/file.h>
+#if HAVE_BSD_SYSCTL
 #include <sys/sysctl.h>
+#endif
 
 #include <ufs/ufs/dinode.h>
 #include <ufs/ffs/fs.h>
@@ -51,6 +54,7 @@ __FBSDID("$FreeBSD$");
 #include <limits.h>
 #include <stdint.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "fsck.h"
 
@@ -87,6 +91,7 @@ setup(char *dev)
 		}
 		return (0);
 	}
+#if HAVE_UFS_SNAPSHOT
 	if ((statb.st_mode & S_IFMT) != S_IFCHR &&
 	    (statb.st_mode & S_IFMT) != S_IFBLK) {
 		if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
@@ -121,6 +126,7 @@ setup(char *dev)
 			}
 		}
 	}
+#endif
 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
 		if (bkgrdflag) {
 			unlink(snapname);
@@ -131,6 +137,7 @@ setup(char *dev)
 	}
 	if (bkgrdflag) {
 		unlink(snapname);
+#if HAVE_BSD_SYSCTL
 		size = MIBSIZE;
 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
 		    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
@@ -160,6 +167,7 @@ setup(char *dev)
 		}
 		cmd.version = FFS_CMD_VERSION;
 		cmd.handle = fsreadfd;
+#endif
 		fswritefd = -1;
 	}
 	if (preen == 0)
@@ -471,6 +479,8 @@ calcsb(char *dev, int devfd, struct fs *
 		return (0);
 	}
 	lp = getdisklabel(dev, devfd);
+	if (!lp)
+		return (0);
 	if (isdigit(*cp))
 		pp = &lp->d_partitions[0];
 	else
@@ -521,13 +531,17 @@ calcsb(char *dev, int devfd, struct fs *
 static struct disklabel *
 getdisklabel(char *s, int fd)
 {
+#ifdef HAVE_BSD_DISKLABEL
 	static struct disklabel lab;
 
 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
 		if (s == NULL)
+#endif
 			return ((struct disklabel *)NULL);
+#ifdef HAVE_BSD_DISKLABEL
 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
 		errx(EEXIT, "%s: can't read disk label", s);
 	}
 	return (&lab);
+#endif
 }
--- a/sbin/fsck_ffs/utilities.c
+++ b/sbin/fsck_ffs/utilities.c
@@ -108,14 +108,16 @@ retry:
 	return (origname);
 }
 
+#ifdef HAVE_BSD_SIGNALS
 void
-infohandler(int sig __unused)
+infohandler(int sig)
 {
 	got_siginfo = 1;
 }
+#endif
 
 void
-alarmhandler(int sig __unused)
+alarmhandler(int sig)
 {
 	got_sigalarm = 1;
 }
--- a/sbin/fsck_ffs/Makefile
+++ b/sbin/fsck_ffs/Makefile
@@ -1,19 +1,18 @@
 # $FreeBSD$
 #	@(#)Makefile	8.2 (Berkeley) 4/27/95
 
-PROG=	fsck_ffs
-LINKS+=	${BINDIR}/fsck_ffs ${BINDIR}/fsck_ufs
-LINKS+=	${BINDIR}/fsck_ffs ${BINDIR}/fsck_4.2bsd
-MAN=	fsck_ffs.8
-MLINKS=	fsck_ffs.8 fsck_ufs.8 fsck_ffs.8 fsck_4.2bsd.8
-SRCS=	dir.c ea.c fsutil.c inode.c main.c pass1.c pass1b.c pass2.c pass3.c \
-	pass4.c pass5.c setup.c utilities.c ffs_subr.c ffs_tables.c gjournal.c \
-	getmntopts.c
-DPADD=	${LIBUFS}
-LDADD=	-lufs
-WARNS?=	2
-CFLAGS+= -I${.CURDIR} -I${.CURDIR}/../mount
+PROG = fsck.ufs
+LIB = $(PROG)
+LIB_type = static
+SRCS = main.c
+MAN = fsck_ffs.8:fsck.ufs.8
+UFS_SRCS = ../../sys/ufs/ffs/ffs_subr.c ../../sys/ufs/ffs/ffs_tables.c
+LIBSRCS= dir.c ea.c fsutil.c inode.c pass1.c pass1b.c pass2.c pass3.c \
+	pass4.c pass5.c setup.c utilities.c $(UFS_SRCS) gjournal.c \
+	../mount/getmntopts.c
 
-.PATH:	${.CURDIR}/../../sys/ufs/ffs ${.CURDIR}/../mount
+LDADD += -L../../lib/port -lport -L. -l$(LIB) -L../../lib/libufs -lufs -lbsd
+INCLUDES = -I. -I../../lib/libufs -I../mount
+
+include ../../Makefile.common
 
-.include <bsd.prog.mk>
--- a/sbin/fsck_ffs/fsck.h
+++ b/sbin/fsck_ffs/fsck.h
@@ -63,6 +63,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <signal.h>
 
 #define	MAXDUP		10	/* limit on dup blks (per inode) */
 #define	MAXBAD		10	/* limit on bad blks (per inode) */
