[Midnightbsd-cvs] src [11221] trunk/sbin/fsck_msdosfs/main.c: sync with freebsd

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jul 1 17:06:17 EDT 2018


Revision: 11221
          http://svnweb.midnightbsd.org/src/?rev=11221
Author:   laffer1
Date:     2018-07-01 17:06:17 -0400 (Sun, 01 Jul 2018)
Log Message:
-----------
sync with freebsd

Modified Paths:
--------------
    trunk/sbin/fsck_msdosfs/Makefile
    trunk/sbin/fsck_msdosfs/boot.c
    trunk/sbin/fsck_msdosfs/check.c
    trunk/sbin/fsck_msdosfs/dir.c
    trunk/sbin/fsck_msdosfs/dosfs.h
    trunk/sbin/fsck_msdosfs/ext.h
    trunk/sbin/fsck_msdosfs/fat.c
    trunk/sbin/fsck_msdosfs/fsck_msdosfs.8
    trunk/sbin/fsck_msdosfs/main.c

Property Changed:
----------------
    trunk/sbin/fsck_msdosfs/fsck_msdosfs.8

Modified: trunk/sbin/fsck_msdosfs/Makefile
===================================================================
--- trunk/sbin/fsck_msdosfs/Makefile	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/Makefile	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,5 +1,6 @@
+# $MidnightBSD$
 #	$NetBSD: Makefile,v 1.6 1997/05/08 21:11:11 gwr Exp $
-# $MidnightBSD$
+# $FreeBSD: stable/10/sbin/fsck_msdosfs/Makefile 241807 2012-10-21 12:01:19Z uqs $
 
 FSCK=	${.CURDIR}/../fsck
 .PATH: ${FSCK}
@@ -9,6 +10,5 @@
 SRCS=	main.c check.c boot.c fat.c dir.c fsutil.c
 
 CFLAGS+= -I${FSCK}
-WARNS?=	2
 
 .include <bsd.prog.mk>

Modified: trunk/sbin/fsck_msdosfs/boot.c
===================================================================
--- trunk/sbin/fsck_msdosfs/boot.c	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/boot.c	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -26,9 +27,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: boot.c,v 1.9 2003/07/24 19:25:46 ws Exp $");
+__RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp ");
 static const char rcsid[] =
-  "$MidnightBSD$";
+  "$FreeBSD: stable/10/sbin/fsck_msdosfs/boot.c 300099 2016-05-18 00:33:08Z pfg $";
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -48,8 +49,8 @@
 	int ret = FSOK;
 	int i;
 	
-	if (read(dosfs, block, sizeof block) != sizeof block) {
-		perror("could not read boot block");
+	if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
+		perr("could not read boot block");
 		return FSFATAL;
 	}
 
@@ -103,7 +104,7 @@
 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
-			perror("could not read fsinfo block");
+			perr("could not read fsinfo block");
 			return FSFATAL;
 		}
 		if (memcmp(fsinfo, "RRaA", 4)
@@ -131,7 +132,7 @@
 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
 				    || write(dosfs, fsinfo, sizeof fsinfo)
 				    != sizeof fsinfo) {
-					perror("Unable to write bpbFSInfo");
+					perr("Unable to write bpbFSInfo");
 					return FSFATAL;
 				}
 				ret = FSBOOTMOD;
@@ -151,7 +152,7 @@
 		    SEEK_SET)
 		    != boot->bpbBackup * boot->bpbBytesPerSec
 		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
-			perror("could not read backup bootblock");
+			perr("could not read backup bootblock");
 			return FSFATAL;
 		}
 		backup[65] = block[65];				/* XXX */
@@ -221,7 +222,7 @@
 		break;
 	}
 
-	if (boot->NumFatEntries < boot->NumClusters) {
+	if (boot->NumFatEntries < boot->NumClusters - CLUST_FIRST) {
 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
 		       boot->NumClusters, boot->FATsecs);
 		return FSFATAL;
@@ -242,7 +243,7 @@
 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
-		perror("could not read fsinfo block");
+		perr("could not read fsinfo block");
 		return FSFATAL;
 	}
 	fsinfo[0x1e8] = (u_char)boot->FSFree;
@@ -257,7 +258,7 @@
 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
 	    || write(dosfs, fsinfo, sizeof fsinfo)
 	    != sizeof fsinfo) {
-		perror("Unable to write bpbFSInfo");
+		perr("Unable to write bpbFSInfo");
 		return FSFATAL;
 	}
 	/*

Modified: trunk/sbin/fsck_msdosfs/check.c
===================================================================
--- trunk/sbin/fsck_msdosfs/check.c	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/check.c	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -26,9 +27,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: check.c,v 1.10 2000/04/25 23:02:51 jdolecek Exp $");
+__RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
 static const char rcsid[] =
-  "$MidnightBSD$";
+  "$FreeBSD: stable/10/sbin/fsck_msdosfs/check.c 268784 2014-07-17 02:33:40Z pfg $";
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -67,7 +68,7 @@
 		printf("\n");
 
 	if (dosfs < 0) {
-		perror("Can't open");
+		perr("Can't open `%s'", fname);
 		printf("\n");
 		return 8;
 	}
@@ -142,7 +143,7 @@
 		goto out;
 
 	/* now write the FATs */
-	if (mod & FSFATMOD) {
+	if (mod & (FSFATMOD|FSFIXFAT)) {
 		if (ask(1, "Update FATs")) {
 			mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
 			if (mod & FSFATAL)

Modified: trunk/sbin/fsck_msdosfs/dir.c
===================================================================
--- trunk/sbin/fsck_msdosfs/dir.c	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/dir.c	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -28,9 +29,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: dir.c,v 1.14 1998/08/25 19:18:15 ross Exp $");
+__RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
 static const char rcsid[] =
-  "$MidnightBSD$";
+  "$FreeBSD: stable/10/sbin/fsck_msdosfs/dir.c 326843 2017-12-14 07:55:11Z delphij $";
 #endif /* not lint */
 
 #include <stdio.h>
@@ -153,7 +154,7 @@
 /*
  * The stack of unread directories
  */
-struct dirTodoNode *pendingDirectories = NULL;
+static struct dirTodoNode *pendingDirectories = NULL;
 
 /*
  * Return the full pathname for a directory entry.
@@ -218,18 +219,19 @@
 	int b1, b2;
 	cl_t cl;
 	int ret = FSOK;
+	size_t len;
 
 	b1 = boot->bpbRootDirEnts * 32;
 	b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec;
 
-	if ((buffer = malloc( b1 > b2 ? b1 : b2)) == NULL) {
-		perror("No space for directory buffer");
+	if ((buffer = malloc(len = b1 > b2 ? b1 : b2)) == NULL) {
+		perr("No space for directory buffer (%zu)", len);
 		return FSFATAL;
 	}
 
-	if ((delbuf = malloc(b2)) == NULL) {
+	if ((delbuf = malloc(len = b2)) == NULL) {
 		free(buffer);
-		perror("No space for directory delbuf");
+		perr("No space for directory delbuf (%zu)", len);
 		return FSFATAL;
 	}
 
@@ -236,7 +238,7 @@
 	if ((rootDir = newDosDirEntry()) == NULL) {
 		free(buffer);
 		free(delbuf);
-		perror("No space for directory entry");
+		perr("No space for directory entry");
 		return FSFATAL;
 	}
 
@@ -328,7 +330,7 @@
 		off *= boot->bpbBytesPerSec;
 		if (lseek(f, off, SEEK_SET) != off
 		    || read(f, delbuf, clsz) != clsz) {
-			perror("Unable to read directory");
+			perr("Unable to read directory");
 			return FSFATAL;
 		}
 		while (s < e) {
@@ -337,7 +339,7 @@
 		}
 		if (lseek(f, off, SEEK_SET) != off
 		    || write(f, delbuf, clsz) != clsz) {
-			perror("Unable to write directory");
+			perr("Unable to write directory");
 			return FSFATAL;
 		}
 		if (startcl == endcl)
@@ -418,13 +420,14 @@
 		      fullpath(dir));
 		if (ask(1, "Drop superfluous clusters")) {
 			cl_t cl;
-			u_int32_t sz = 0;
+			u_int32_t sz, len;
 
-			for (cl = dir->head; (sz += boot->ClusterSize) <
-			    dir->size;)
+			for (cl = dir->head, len = sz = 0;
+			    (sz += boot->ClusterSize) < dir->size; len++)
 				cl = fat[cl].next;
 			clearchain(boot, fat, fat[cl].next);
 			fat[cl].next = CLUST_EOF;
+			fat[dir->head].length = len;
 			return FSFATMOD;
 		} else
 			return FSERROR;
@@ -475,7 +478,7 @@
 		off *= boot->bpbBytesPerSec;
 		if (lseek(f, off, SEEK_SET) != off
 		    || read(f, buffer, last) != last) {
-			perror("Unable to read directory");
+			perr("Unable to read directory");
 			return FSFATAL;
 		}
 		last /= 32;
@@ -617,7 +620,7 @@
 			dirent.name[8] = '\0';
 			for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
 				dirent.name[k] = '\0';
-			if (dirent.name[k] != '\0')
+			if (k < 0 || dirent.name[k] != '\0')
 				k++;
 			if (dirent.name[0] == SLOT_E5)
 				dirent.name[0] = 0xe5;
@@ -821,7 +824,7 @@
 
 				/* create directory tree node */
 				if (!(d = newDosDirEntry())) {
-					perror("No space for directory");
+					perr("No space for directory");
 					return FSFATAL;
 				}
 				memcpy(d, &dirent, sizeof(struct dosDirEntry));
@@ -830,7 +833,7 @@
 
 				/* Enter this directory into the todo list */
 				if (!(n = newDirTodo())) {
-					perror("No space for todo list");
+					perr("No space for todo list");
 					return FSFATAL;
 				}
 				n->next = pendingDirectories;
@@ -851,7 +854,7 @@
 			last *= 32;
 			if (lseek(f, off, SEEK_SET) != off
 			    || write(f, buffer, last) != last) {
-				perror("Unable to write directory");
+				perr("Unable to write directory");
 				return FSFATAL;
 			}
 			mod &= ~THISMOD;
@@ -870,7 +873,7 @@
 		last *= 32;
 		if (lseek(f, off, SEEK_SET) != off
 		    || write(f, buffer, last) != last) {
-			perror("Unable to write directory");
+			perr("Unable to write directory");
 			return FSFATAL;
 		}
 		mod &= ~THISMOD;
@@ -923,6 +926,7 @@
 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
 {
 	struct dosDirEntry d;
+	int len;
 	u_char *p;
 
 	if (!ask(1, "Reconnect"))
@@ -941,7 +945,7 @@
 	if (!lfbuf) {
 		lfbuf = malloc(boot->ClusterSize);
 		if (!lfbuf) {
-			perror("No space for buffer");
+			perr("No space for buffer");
 			return FSFATAL;
 		}
 		p = NULL;
@@ -965,7 +969,7 @@
 		    + boot->ClusterOffset * boot->bpbBytesPerSec;
 		if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
 		    || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
-			perror("could not read LOST.DIR");
+			perr("could not read LOST.DIR");
 			return FSFATAL;
 		}
 		p = lfbuf;
@@ -974,14 +978,15 @@
 	boot->NumFiles++;
 	/* Ensure uniqueness of entry here!				XXX */
 	memset(&d, 0, sizeof d);
-	(void)snprintf(d.name, sizeof(d.name), "%u", head);
+	/* worst case -1 = 4294967295, 10 digits */
+	len = snprintf(d.name, sizeof(d.name), "%u", head);
 	d.flags = 0;
 	d.head = head;
 	d.size = fat[head].length * boot->ClusterSize;
 
-	memset(p, 0, 32);
-	memset(p, ' ', 11);
-	memcpy(p, d.name, strlen(d.name));
+	memcpy(p, d.name, len);
+	memset(p + len, ' ', 11 - len);
+	memset(p + 11, 0, 32 - 11);
 	p[26] = (u_char)d.head;
 	p[27] = (u_char)(d.head >> 8);
 	if (boot->ClustMask == CLUST32_MASK) {
@@ -995,7 +1000,7 @@
 	fat[head].flags |= FAT_USED;
 	if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
 	    || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
-		perror("could not write LOST.DIR");
+		perr("could not write LOST.DIR");
 		return FSFATAL;
 	}
 	return FSDIRMOD;

Modified: trunk/sbin/fsck_msdosfs/dosfs.h
===================================================================
--- trunk/sbin/fsck_msdosfs/dosfs.h	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/dosfs.h	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -24,7 +25,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *	$NetBSD: dosfs.h,v 1.4 1997/01/03 14:32:48 ws Exp $
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sbin/fsck_msdosfs/dosfs.h 203874 2010-02-14 12:31:28Z kib $
  */
 
 #ifndef DOSFS_H

Modified: trunk/sbin/fsck_msdosfs/ext.h
===================================================================
--- trunk/sbin/fsck_msdosfs/ext.h	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/ext.h	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -22,11 +23,11 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *	$NetBSD: ext.h,v 1.6 2000/04/25 23:02:51 jdolecek Exp $
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sbin/fsck_msdosfs/ext.h 268784 2014-07-17 02:33:40Z pfg $
  */
 
 #ifndef EXT_H
-#define EXT_H
+#define	EXT_H
 
 #include <sys/types.h>
 
@@ -71,8 +72,8 @@
 #define	FSFATMOD	4		/* The FAT was modified */
 #define	FSERROR		8		/* Some unrecovered error remains */
 #define	FSFATAL		16		/* Some unrecoverable error occurred */
-#define FSDIRTY		32		/* File system is dirty */
-#define FSFIXFAT	64		/* Fix file system FAT */
+#define	FSDIRTY		32		/* File system is dirty */
+#define	FSFIXFAT	64		/* Fix file system FAT */
 
 /*
  * read a boot block in a machine independent fashion and translate
@@ -133,7 +134,7 @@
 /*
  * Return the type of a reserved cluster as text
  */
-char *rsrvdcltype(cl_t);
+const char *rsrvdcltype(cl_t);
 
 /*
  * Clear a cluster chain in a FAT

Modified: trunk/sbin/fsck_msdosfs/fat.c
===================================================================
--- trunk/sbin/fsck_msdosfs/fat.c	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/fat.c	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -26,9 +27,9 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
+__RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
 static const char rcsid[] =
-  "$MidnightBSD$";
+  "$FreeBSD: stable/10/sbin/fsck_msdosfs/fat.c 268968 2014-07-21 23:23:20Z pfg $";
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -69,6 +70,7 @@
 	off_t off;
 	u_char *buffer;
 	int ret = 0;
+	size_t len;
 
 	if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
 		return 0;
@@ -76,20 +78,20 @@
 	off = boot->bpbResSectors;
 	off *= boot->bpbBytesPerSec;
 
-	buffer = malloc(boot->bpbBytesPerSec);
+	buffer = malloc(len = boot->bpbBytesPerSec);
 	if (buffer == NULL) {
-		perror("No space for FAT");
+		perr("No space for FAT sectors (%zu)", len);
 		return 1;
 	}
 
 	if (lseek(fs, off, SEEK_SET) != off) {
-		perror("Unable to read FAT");
+		perr("Unable to read FAT");
 		goto err;
 	}
 
 	if ((size_t)read(fs, buffer, boot->bpbBytesPerSec) !=
 	    boot->bpbBytesPerSec) {
-		perror("Unable to read FAT");
+		perr("Unable to read FAT");
 		goto err;
 	}
 
@@ -163,10 +165,11 @@
 _readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
 {
 	off_t off;
+	size_t len;
 
-	*buffer = malloc(boot->FATsecs * boot->bpbBytesPerSec);
+	*buffer = malloc(len = boot->FATsecs * boot->bpbBytesPerSec);
 	if (*buffer == NULL) {
-		perror("No space for FAT");
+		perr("No space for FAT sectors (%zu)", len);
 		return 0;
 	}
 
@@ -174,13 +177,13 @@
 	off *= boot->bpbBytesPerSec;
 
 	if (lseek(fs, off, SEEK_SET) != off) {
-		perror("Unable to read FAT");
+		perr("Unable to read FAT");
 		goto err;
 	}
 
 	if ((size_t)read(fs, *buffer, boot->FATsecs * boot->bpbBytesPerSec)
 	    != boot->FATsecs * boot->bpbBytesPerSec) {
-		perror("Unable to read FAT");
+		perr("Unable to read FAT");
 		goto err;
 	}
 
@@ -207,10 +210,10 @@
 
 	if (!_readfat(fs, boot, no, &buffer))
 		return FSFATAL;
-		
+
 	fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
 	if (fat == NULL) {
-		perror("No space for FAT");
+		perr("No space for FAT clusters (%zu)", len);
 		free(buffer);
 		return FSFATAL;
 	}
@@ -240,7 +243,7 @@
 			ret |= FSDIRTY;
 		else {
 			/* just some odd byte sequence in FAT */
-				
+
 			switch (boot->ClustMask) {
 			case CLUST32_MASK:
 				pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
@@ -260,7 +263,7 @@
 				break;
 			}
 
-	
+
 			if (ask(1, "Correct"))
 				ret |= FSFIXFAT;
 		}
@@ -318,7 +321,7 @@
 /*
  * Get type of reserved cluster
  */
-char *
+const char *
 rsrvdcltype(cl_t cl)
 {
 	if (cl == CLUST_FREE)
@@ -428,13 +431,21 @@
 }
 
 int
-tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
+tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *truncp)
 {
 	if (ask(0, "Clear chain starting at %u", head)) {
 		clearchain(boot, fat, head);
 		return FSFATMOD;
 	} else if (ask(0, "Truncate")) {
-		*trunc = CLUST_EOF;
+		uint32_t len;
+		cl_t p;
+
+		for (p = head, len = 0;
+		    p >= CLUST_FIRST && p < boot->NumClusters;
+		    p = fat[p].next, len++)
+			continue;
+		*truncp = CLUST_EOF;
+		fat[head].length = len;
 		return FSFATMOD;
 	} else
 		return FSERROR;
@@ -463,7 +474,8 @@
 
 		/* follow the chain and mark all clusters on the way */
 		for (len = 0, p = head;
-		     p >= CLUST_FIRST && p < boot->NumClusters;
+		     p >= CLUST_FIRST && p < boot->NumClusters &&
+		     fat[p].head != head;
 		     p = fat[p].next) {
 			fat[p].head = head;
 			len++;
@@ -484,10 +496,10 @@
 			continue;
 
 		/* follow the chain to its end (hopefully) */
-		for (p = head;
+		for (len = fat[head].length, p = head;
 		     (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
 		     p = n)
-			if (fat[n].head != head)
+			if (fat[n].head != head || len-- < 2)
 				break;
 		if (n >= CLUST_EOFS)
 			continue;
@@ -495,15 +507,21 @@
 		if (n == CLUST_FREE || n >= CLUST_RSRVD) {
 			pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
 			      head, rsrvdcltype(n));
+clear:
 			ret |= tryclear(boot, fat, head, &fat[p].next);
 			continue;
 		}
 		if (n < CLUST_FIRST || n >= boot->NumClusters) {
 			pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
-			      head, n);
-			ret |= tryclear(boot, fat, head, &fat[p].next);
-			continue;
+			    head, n);
+			goto clear;
 		}
+		if (head == fat[n].head) {
+			pwarn("Cluster chain starting at %u loops at cluster %u\n",
+			
+			    head, p);
+			goto clear;
+		}
 		pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
 		      head, fat[n].head, n);
 		conf = tryclear(boot, fat, head, &fat[p].next);
@@ -549,7 +567,7 @@
 
 	buffer = malloc(fatsz = boot->FATsecs * boot->bpbBytesPerSec);
 	if (buffer == NULL) {
-		perror("No space for FAT");
+		perr("No space for FAT sectors (%zu)", fatsz);
 		return FSFATAL;
 	}
 	memset(buffer, 0, fatsz);
@@ -598,7 +616,7 @@
 		free(old_fat);
 		p += count;
 	}
-			
+
 	for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
 		switch (boot->ClustMask) {
 		case CLUST32_MASK:
@@ -619,13 +637,15 @@
 		default:
 			if (fat[cl].next == CLUST_FREE)
 				boot->NumFree++;
-			if (cl + 1 < boot->NumClusters
-			    && fat[cl + 1].next == CLUST_FREE)
+			*p++ = (u_char)fat[cl].next;
+			*p = (u_char)((fat[cl].next >> 8) & 0xf);
+			cl++;
+			if (cl >= boot->NumClusters)
+				break;
+			if (fat[cl].next == CLUST_FREE)
 				boot->NumFree++;
-			*p++ = (u_char)fat[cl].next;
-			*p++ = (u_char)((fat[cl].next >> 8) & 0xf)
-			       |(u_char)(fat[cl+1].next << 4);
-			*p++ = (u_char)(fat[++cl].next >> 4);
+			*p++ |= (u_char)(fat[cl + 1].next << 4);
+			*p++ = (u_char)(fat[cl + 1].next >> 4);
 			break;
 		}
 	}
@@ -634,7 +654,7 @@
 		off *= boot->bpbBytesPerSec;
 		if (lseek(fs, off, SEEK_SET) != off
 		    || (size_t)write(fs, buffer, fatsz) != fatsz) {
-			perror("Unable to write FAT");
+			perr("Unable to write FAT");
 			ret = FSFATAL; /* Return immediately?		XXX */
 		}
 	}
@@ -651,7 +671,7 @@
 	cl_t head;
 	int mod = FSOK;
 	int ret;
-	
+
 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
 		/* find next untravelled chain */
 		if (fat[head].head != head
@@ -675,8 +695,9 @@
 
 	if (boot->bpbFSInfo) {
 		ret = 0;
-		if (boot->FSFree != boot->NumFree) {
-			pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
+		if (boot->FSFree != 0xffffffffU &&
+		    boot->FSFree != boot->NumFree) {
+			pwarn("Free space in FSInfo block (%u) not correct (%u)\n",
 			      boot->FSFree, boot->NumFree);
 			if (ask(1, "Fix")) {
 				boot->FSFree = boot->NumFree;

Modified: trunk/sbin/fsck_msdosfs/fsck_msdosfs.8
===================================================================
--- trunk/sbin/fsck_msdosfs/fsck_msdosfs.8	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/fsck_msdosfs.8	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\"	$NetBSD: fsck_msdos.8,v 1.9 1997/10/17 11:19:58 ws Exp $
 .\"
 .\" Copyright (C) 1995 Wolfgang Solfrank
@@ -23,9 +24,9 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/sbin/fsck_msdosfs/fsck_msdosfs.8 306984 2016-10-10 16:21:34Z sevan $
 .\"
-.Dd June 4, 2009
+.Dd October 3, 2016
 .Dt FSCK_MSDOSFS 8
 .Os
 .Sh NAME
@@ -114,7 +115,10 @@
 .Sh HISTORY
 The
 .Nm
-utility first appeared in
+utility appeared in
+.Nx 1.2 .
+.Nm
+first appeared in
 .Fx 4.4 .
 .Sh BUGS
 The


Property changes on: trunk/sbin/fsck_msdosfs/fsck_msdosfs.8
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/sbin/fsck_msdosfs/main.c
===================================================================
--- trunk/sbin/fsck_msdosfs/main.c	2018-07-01 21:05:37 UTC (rev 11220)
+++ trunk/sbin/fsck_msdosfs/main.c	2018-07-01 21:06:17 UTC (rev 11221)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (C) 1995 Wolfgang Solfrank
  * Copyright (c) 1995 Martin Husemann
@@ -28,7 +29,7 @@
 #ifndef lint
 __RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
 static const char rcsid[] =
-  "$MidnightBSD$";
+  "$FreeBSD: stable/10/sbin/fsck_msdosfs/main.c 236213 2012-05-29 01:48:06Z kevlo $";
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -138,6 +139,7 @@
 
 	va_start(ap, fmt);
 	vsnprintf(prompt, sizeof(prompt), fmt, ap);
+	va_end(ap);
 	if (alwaysyes || rdonly) {
 		printf("%s? %s\n", prompt, rdonly ? "no" : "yes");
 		return !rdonly;



More information about the Midnightbsd-cvs mailing list