# Perl subroutine to shorten a floppy image by omitting trailing sectors # that are not in use # # AFAIK works on FAT12 and FAT16. Won't work on FAT32 # # 2003.04.28 R. Main. # Fixes for images with > 65535 sectors # # Placed under GNU Public License by Ken Yap, April 2000 package TruncFD; use strict; use IO::Seekable; use POSIX qw(ceil); use constant; use constant DEBUG => 0; sub truncfd ($) { my ($file) = @_; my ($nread, $buffer, $cylinders, $clusters, $fatsectors, $rootdirsectors, @fatents, $i, $lastclus, $clusstart, $lastsector); return -1 if (!defined($file) or !open(F, "$file")); binmode(F); $nread = read(F, $buffer, 512); return -1 if (!defined($nread) or $nread < 512); my ($dummy1, $bytepersect, $sectperclus, $resvsectors, $fats, $rootdirents, $sectors, $dummy2, $sectperfat, $sectpertrk, $heads, $hidsectors, $bigsectors, $dummy3, $fstype) = unpack('a11vCvCvvavvvVVa18a5', $buffer); if ($sectors == 0) { $sectors = $bigsectors; } $cylinders = $sectors / ($sectpertrk * $heads); $fatsectors = $fats * $sectperfat; $rootdirsectors = POSIX::ceil(($rootdirents * 32) / $bytepersect); $clusstart = $resvsectors + $fatsectors + $rootdirsectors; $clusters = int (($sectors - $clusstart) / $sectperclus); return (-s $file) unless $clusters < 65525; $fstype = $clusters < 4085? 'FAT12':'FAT16' if ($fstype ne 'FAT12' && $fstype ne 'FAT16'); if (DEBUG) { print STDERR <