update SVN properties
[reactos.git] / rosapps / sysutils / mkdosfs / mkdosfs.c
index 9c6e0ed..ab126c5 100644 (file)
@@ -24,9 +24,9 @@
    - New options -A, -S, -C
    - Support for filesystems > 2GB
    - FAT32 support
-   \r
-   Port to work under Windows NT/2K/XP Dec 2002 by\r
-   Jens-Uwe Mager <jum@anubis.han.de>\r
+   
+   Port to work under Windows NT/2K/XP Dec 2002 by
+   Jens-Uwe Mager <jum@anubis.han.de>
 
    Copying:     Copyright 1993, 1994 David Hudson (dave@humbug.demon.co.uk)
 
 /* Include the header files */
 
 #include "../version.h"
-\r
-#ifdef _WIN32\r
-#define _WIN32_WINNT   0x0400\r
-#include <windows.h>\r
-#include <winioctl.h>\r
-#define __LITTLE_ENDIAN        1234\r
-#define __BIG_ENDIAN   4321\r
-#define __BYTE_ORDER   __LITTLE_ENDIAN\r
-#define inline\r
-#define __attribute__(x)\r
-#define BLOCK_SIZE             512\r
+
+#ifdef _WIN32
+#define _WIN32_WINNT   0x0400
+#include <windows.h>
+#include <winioctl.h>
+#define __LITTLE_ENDIAN        1234
+#define __BIG_ENDIAN   4321
+#define __BYTE_ORDER   __LITTLE_ENDIAN
+#define inline
+#define __attribute__(x)
+#define BLOCK_SIZE             512
 #else
 #include <linux/hdreg.h>
 #include <linux/fs.h>
 #include <linux/fd.h>
 #include <endian.h>
 #include <mntent.h>
-#include <signal.h>\r
-#include <sys/ioctl.h>\r
-#include <unistd.h>\r
+#include <signal.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
 #endif
-#include <fcntl.h>\r
+#include <fcntl.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <time.h>\r
+#include <time.h>
 
 #if __BYTE_ORDER == __BIG_ENDIAN
 
 #define CT_LE_L(v) (v)
 
 #endif /* __BIG_ENDIAN */
-\r
-#ifdef _WIN32\r
-\r
-typedef unsigned char __u8;\r
-typedef unsigned short __u16;\r
-typedef unsigned int __u32;\r
-typedef unsigned __int64 __u64;\r
-typedef __int64 loff_t;\r
-typedef __int64 ll_t;\r
-\r
-extern char *optarg;\r
-extern int optind;\r
-extern int opterr;\r
-extern int optopt;\r
-int getopt(int argc, char *const argv[], const char * optstring);\r
-\r
-static int is_device = 0;\r
-\r
-#define open   WIN32open\r
-#define close  WIN32close\r
-#define read   WIN32read\r
-#define write  WIN32write\r
-#define llseek WIN32llseek\r
-\r
-#define O_SHORT_LIVED   _O_SHORT_LIVED\r
-#define O_ACCMODE       3\r
-#define O_NONE          3\r
-#define O_BACKUP        0x10000\r
-#define O_SHARED        0x20000\r
-\r
-static int WIN32open(const char *path, int oflag, ...)\r
-{\r
-       HANDLE fh;\r
-       DWORD desiredAccess;\r
-       DWORD shareMode;\r
-       DWORD creationDisposition;\r
-       DWORD flagsAttributes = FILE_ATTRIBUTE_NORMAL;\r
-       SECURITY_ATTRIBUTES securityAttributes;\r
-       va_list ap;\r
-       int pmode;\r
-       int trunc = FALSE;\r
-\r
-       securityAttributes.nLength = sizeof(securityAttributes);\r
-       securityAttributes.lpSecurityDescriptor = NULL;\r
-       securityAttributes.bInheritHandle = oflag & O_NOINHERIT ? FALSE : TRUE;\r
-       switch (oflag & O_ACCMODE) {\r
-       case O_RDONLY:\r
-               desiredAccess = GENERIC_READ;\r
-               shareMode = FILE_SHARE_READ;\r
-               break;\r
-       case O_WRONLY:\r
-               desiredAccess = GENERIC_WRITE;\r
-               shareMode = 0;\r
-               break;\r
-       case O_RDWR:\r
-               desiredAccess = GENERIC_READ|GENERIC_WRITE;\r
-               shareMode = 0;\r
-               break;\r
-       case O_NONE:\r
-               desiredAccess = 0;\r
-               shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;\r
-       }\r
-       if (oflag & O_APPEND) {\r
-               desiredAccess |= FILE_APPEND_DATA|SYNCHRONIZE;\r
-               shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;\r
-       }\r
-       if (oflag & O_SHARED)\r
-               shareMode |= FILE_SHARE_READ|FILE_SHARE_WRITE;\r
-        switch (oflag & (O_CREAT|O_EXCL|O_TRUNC)) {\r
-       case 0:\r
-       case O_EXCL:\r
-               creationDisposition = OPEN_EXISTING;\r
-               break;\r
-       case O_CREAT:\r
-               creationDisposition = OPEN_ALWAYS;\r
-               break;\r
-       case O_CREAT|O_EXCL:\r
-       case O_CREAT|O_TRUNC|O_EXCL:\r
-               creationDisposition = CREATE_NEW;\r
-               break;\r
-       case O_TRUNC:\r
-       case O_TRUNC|O_EXCL:\r
-               creationDisposition = TRUNCATE_EXISTING;\r
-               break;\r
-       case O_CREAT|O_TRUNC:\r
-               creationDisposition = OPEN_ALWAYS;\r
-               trunc = TRUE;\r
-               break;\r
-        }\r
-       if (oflag & O_CREAT) {\r
-               va_start(ap, oflag);\r
-               pmode = va_arg(ap, int);\r
-               va_end(ap);\r
-               if ((pmode & 0222) == 0)\r
-                       flagsAttributes |= FILE_ATTRIBUTE_READONLY;\r
-       }\r
-       if (oflag & O_TEMPORARY) {\r
-               flagsAttributes |= FILE_FLAG_DELETE_ON_CLOSE;\r
-               desiredAccess |= DELETE;\r
-       }\r
-       if (oflag & O_SHORT_LIVED)\r
-               flagsAttributes |= FILE_ATTRIBUTE_TEMPORARY;\r
-       if (oflag & O_SEQUENTIAL)\r
-               flagsAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;\r
-       else if (oflag & O_RANDOM)\r
-               flagsAttributes |= FILE_FLAG_RANDOM_ACCESS;\r
-       if (oflag & O_BACKUP)\r
-               flagsAttributes |= FILE_FLAG_BACKUP_SEMANTICS;\r
-       if ((fh = CreateFile(path, desiredAccess, shareMode, &securityAttributes,\r
-                               creationDisposition, flagsAttributes, NULL)) == INVALID_HANDLE_VALUE) {\r
-               errno = GetLastError();\r
-               return -1;\r
-       }\r
-       if (trunc) {\r
-               if (!SetEndOfFile(fh)) {\r
-                       errno = GetLastError();\r
-                       CloseHandle(fh);\r
-                       DeleteFile(path);\r
-                       return -1;\r
-               }\r
-       }\r
-       return (int)fh;\r
-}\r
-\r
-static int WIN32close(int fd)\r
-{\r
-       if (!CloseHandle((HANDLE)fd)) {\r
-               errno = GetLastError();\r
-               return -1;\r
-       }\r
-       return 0;\r
-}\r
-\r
-static int WIN32read(int fd, void *buf, unsigned int len)\r
-{\r
-       DWORD actualLen;\r
-\r
-       if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {\r
-               errno = GetLastError();\r
-               if (errno == ERROR_BROKEN_PIPE)\r
-                       return 0;\r
-               else\r
-                       return -1;\r
-       }\r
-       return (int)actualLen;\r
-}\r
-\r
-static int WIN32write(int fd, void *buf, unsigned int len)\r
-{\r
-       DWORD actualLen;\r
-\r
-       if (!WriteFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {\r
-               errno = GetLastError();\r
-               return -1;\r
-       }\r
-       return (int)actualLen;\r
-}\r
-\r
-static loff_t WIN32llseek(int fd, loff_t offset, int whence)\r
-{\r
-       long lo, hi;\r
-       DWORD err;\r
-\r
-       lo = offset & 0xffffffff;\r
-       hi = offset >> 32;\r
-       lo = SetFilePointer((HANDLE)fd, lo, &hi, whence);\r
-       if (lo == 0xFFFFFFFF && (err = GetLastError()) != NO_ERROR) {\r
-               errno = err;\r
-               return -1;\r
-       }\r
-       return ((loff_t)hi << 32) | (off_t)lo;\r
-}\r
-\r
-int fsctl(int fd, int code)\r
-{\r
-       DWORD ret;\r
-       if (!DeviceIoControl((HANDLE)fd, code, NULL, 0, NULL, 0, &ret, NULL)) {\r
-               errno = GetLastError();\r
-               return -1;\r
-       }\r
-       return 0; \r
-}\r
-\r
-#else\r
-\r
-#define O_NOINHERIT    0\r
-#define O_TEMPORARY    0\r
-#define O_SHORT_LIVED  0\r
-#define O_SEQUENTIAL   0\r
-#define O_RANDOM       0\r
-#define O_BACKUP       0\r
-#define O_SHARED       0\r
-#ifndef O_NONE\r
-# define O_NONE        0\r
-#endif\r
-\r
+
+#ifdef _WIN32
+
+typedef unsigned char __u8;
+typedef unsigned short __u16;
+typedef unsigned int __u32;
+typedef unsigned __int64 __u64;
+typedef __int64 loff_t;
+typedef __int64 ll_t;
+
+extern char *optarg;
+extern int optind;
+extern int opterr;
+extern int optopt;
+int getopt(int argc, char *const argv[], const char * optstring);
+
+static int is_device = 0;
+
+#define open   WIN32open
+#define close  WIN32close
+#define read   WIN32read
+#define write  WIN32write
+#define llseek WIN32llseek
+
+#define O_SHORT_LIVED   _O_SHORT_LIVED
+#define O_ACCMODE       3
+#define O_NONE          3
+#define O_BACKUP        0x10000
+#define O_SHARED        0x20000
+
+static int WIN32open(const char *path, int oflag, ...)
+{
+       HANDLE fh;
+       DWORD desiredAccess;
+       DWORD shareMode;
+       DWORD creationDisposition;
+       DWORD flagsAttributes = FILE_ATTRIBUTE_NORMAL;
+       SECURITY_ATTRIBUTES securityAttributes;
+       va_list ap;
+       int pmode;
+       int trunc = FALSE;
+
+       securityAttributes.nLength = sizeof(securityAttributes);
+       securityAttributes.lpSecurityDescriptor = NULL;
+       securityAttributes.bInheritHandle = oflag & O_NOINHERIT ? FALSE : TRUE;
+       switch (oflag & O_ACCMODE) {
+       case O_RDONLY:
+               desiredAccess = GENERIC_READ;
+               shareMode = FILE_SHARE_READ;
+               break;
+       case O_WRONLY:
+               desiredAccess = GENERIC_WRITE;
+               shareMode = 0;
+               break;
+       case O_RDWR:
+               desiredAccess = GENERIC_READ|GENERIC_WRITE;
+               shareMode = 0;
+               break;
+       case O_NONE:
+               desiredAccess = 0;
+               shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
+       }
+       if (oflag & O_APPEND) {
+               desiredAccess |= FILE_APPEND_DATA|SYNCHRONIZE;
+               shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
+       }
+       if (oflag & O_SHARED)
+               shareMode |= FILE_SHARE_READ|FILE_SHARE_WRITE;
+        switch (oflag & (O_CREAT|O_EXCL|O_TRUNC)) {
+       case 0:
+       case O_EXCL:
+               creationDisposition = OPEN_EXISTING;
+               break;
+       case O_CREAT:
+               creationDisposition = OPEN_ALWAYS;
+               break;
+       case O_CREAT|O_EXCL:
+       case O_CREAT|O_TRUNC|O_EXCL:
+               creationDisposition = CREATE_NEW;
+               break;
+       case O_TRUNC:
+       case O_TRUNC|O_EXCL:
+               creationDisposition = TRUNCATE_EXISTING;
+               break;
+       case O_CREAT|O_TRUNC:
+               creationDisposition = OPEN_ALWAYS;
+               trunc = TRUE;
+               break;
+        }
+       if (oflag & O_CREAT) {
+               va_start(ap, oflag);
+               pmode = va_arg(ap, int);
+               va_end(ap);
+               if ((pmode & 0222) == 0)
+                       flagsAttributes |= FILE_ATTRIBUTE_READONLY;
+       }
+       if (oflag & O_TEMPORARY) {
+               flagsAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
+               desiredAccess |= DELETE;
+       }
+       if (oflag & O_SHORT_LIVED)
+               flagsAttributes |= FILE_ATTRIBUTE_TEMPORARY;
+       if (oflag & O_SEQUENTIAL)
+               flagsAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
+       else if (oflag & O_RANDOM)
+               flagsAttributes |= FILE_FLAG_RANDOM_ACCESS;
+       if (oflag & O_BACKUP)
+               flagsAttributes |= FILE_FLAG_BACKUP_SEMANTICS;
+       if ((fh = CreateFile(path, desiredAccess, shareMode, &securityAttributes,
+                               creationDisposition, flagsAttributes, NULL)) == INVALID_HANDLE_VALUE) {
+               errno = GetLastError();
+               return -1;
+       }
+       if (trunc) {
+               if (!SetEndOfFile(fh)) {
+                       errno = GetLastError();
+                       CloseHandle(fh);
+                       DeleteFile(path);
+                       return -1;
+               }
+       }
+       return (int)fh;
+}
+
+static int WIN32close(int fd)
+{
+       if (!CloseHandle((HANDLE)fd)) {
+               errno = GetLastError();
+               return -1;
+       }
+       return 0;
+}
+
+static int WIN32read(int fd, void *buf, unsigned int len)
+{
+       DWORD actualLen;
+
+       if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {
+               errno = GetLastError();
+               if (errno == ERROR_BROKEN_PIPE)
+                       return 0;
+               else
+                       return -1;
+       }
+       return (int)actualLen;
+}
+
+static int WIN32write(int fd, void *buf, unsigned int len)
+{
+       DWORD actualLen;
+
+       if (!WriteFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {
+               errno = GetLastError();
+               return -1;
+       }
+       return (int)actualLen;
+}
+
+static loff_t WIN32llseek(int fd, loff_t offset, int whence)
+{
+       long lo, hi;
+       DWORD err;
+
+       lo = offset & 0xffffffff;
+       hi = offset >> 32;
+       lo = SetFilePointer((HANDLE)fd, lo, &hi, whence);
+       if (lo == 0xFFFFFFFF && (err = GetLastError()) != NO_ERROR) {
+               errno = err;
+               return -1;
+       }
+       return ((loff_t)hi << 32) | (off_t)lo;
+}
+
+int fsctl(int fd, int code)
+{
+       DWORD ret;
+       if (!DeviceIoControl((HANDLE)fd, code, NULL, 0, NULL, 0, &ret, NULL)) {
+               errno = GetLastError();
+               return -1;
+       }
+       return 0; 
+}
+
+#else
+
+#define O_NOINHERIT    0
+#define O_TEMPORARY    0
+#define O_SHORT_LIVED  0
+#define O_SEQUENTIAL   0
+#define O_RANDOM       0
+#define O_BACKUP       0
+#define O_SHARED       0
+#ifndef O_NONE
+# define O_NONE        0
+#endif
+
 typedef long long ll_t;
 /* Use the _llseek system call directly, because there (once?) was a bug in
  * the glibc implementation of it. */
@@ -327,8 +327,8 @@ static loff_t llseek( int fd, loff_t offset, int whence )
     return actual;
 }
 #endif
-\r
-#endif\r
+
+#endif
 
 /* Constant definitions */
 
@@ -402,9 +402,9 @@ cdiv (int a, int b)
 
 /* __attribute__ ((packed)) is used on all structures to make gcc ignore any
  * alignments */
-\r
-#ifdef _WIN32\r
-#pragma pack(push, 1)\r
+
+#ifdef _WIN32
+#pragma pack(push, 1)
 #endif
 struct msdos_volume_info {
   __u8         drive_number;   /* BIOS drive number */
@@ -476,10 +476,10 @@ struct msdos_dir_entry
     __u16      time, date, start;      /* time, date and first cluster */
     __u32      size;                   /* file size (in bytes) */
   } __attribute__ ((packed));
-\r
-#ifdef _WIN32\r
-#pragma pack(pop)\r
-#endif\r
+
+#ifdef _WIN32
+#pragma pack(pop)
+#endif
 
 /* The "boot code" we put into the filesystem... it writes a message and
    tells the user to try again */
@@ -559,12 +559,12 @@ static void check_blocks (void);
 static void get_list_blocks (char *filename);
 static int valid_offset (int fd, loff_t offset);
 static int count_blocks (char *filename);
-static void check_mount (char *device_name);\r
-#ifdef _WIN32\r
-static void establish_params (void);\r
+static void check_mount (char *device_name);
+#ifdef _WIN32
+static void establish_params (void);
 #else
 static void establish_params (int device_num, int size);
-#endif\r
+#endif
 static void setup_tables (void);
 static void write_tables (void);
 
@@ -692,13 +692,13 @@ check_blocks (void)
       printf ("Searching for bad blocks ");
       fflush (stdout);
     }
-  currently_testing = 0;\r
+  currently_testing = 0;
 #ifndef _WIN32
   if (verbose)
     {
       signal (SIGALRM, alarm_intr);
       alarm (5);
-    }\r
+    }
 #endif
   try = TEST_BUFFER_BLOCKS;
   while (currently_testing < blocks)
@@ -757,7 +757,7 @@ get_list_blocks (char *filename)
            (badblocks > 1) ? "s" : "");
 }
 
-\r
+
 #ifndef _WIN32
 /* Given a file descriptor and an offset, check whether the offset is a valid offset for the file - return FALSE if it
    isn't valid or TRUE if it is */
@@ -773,44 +773,44 @@ valid_offset (int fd, loff_t offset)
     return FALSE;
   return TRUE;
 }
-#endif\r
+#endif
 
 
 /* Given a filename, look to see how many blocks of BLOCK_SIZE are present, returning the answer */
 
 static int
 count_blocks (char *filename)
-{\r
-#ifdef _WIN32\r
-       int fd;\r
-       DISK_GEOMETRY geom;\r
-       BY_HANDLE_FILE_INFORMATION hinfo;\r
-       DWORD ret;\r
-       loff_t len = 0;\r
-\r
-       if ((fd = open(filename, O_RDONLY)) < 0) {\r
-               perror(filename);\r
-               exit(1);\r
-       }\r
-       /*\r
-        * This should probably use IOCTL_DISK_GET_LENGTH_INFO here, but\r
-        * this ioctl is only available in XP and up.\r
-        */\r
-       if (is_device) {\r
-               if (!DeviceIoControl((HANDLE)fd, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geom, sizeof(geom), &ret, NULL)) {\r
-                       errno = GetLastError();\r
-                       die("unable to get length for '%s'");\r
-               }\r
-               len = geom.Cylinders.QuadPart*geom.TracksPerCylinder*geom.SectorsPerTrack*BLOCK_SIZE;\r
-       } else {\r
-               if (!GetFileInformationByHandle((HANDLE)fd, &hinfo)) {\r
-                               errno = GetLastError();\r
-                               die("unable to get length for '%s'");\r
-               }\r
-               len = ((loff_t)hinfo.nFileSizeHigh << 32) | (loff_t)hinfo.nFileSizeLow;\r
-       }\r
-       close(fd);\r
-       return len/BLOCK_SIZE;\r
+{
+#ifdef _WIN32
+       int fd;
+       DISK_GEOMETRY geom;
+       BY_HANDLE_FILE_INFORMATION hinfo;
+       DWORD ret;
+       loff_t len = 0;
+
+       if ((fd = open(filename, O_RDONLY)) < 0) {
+               perror(filename);
+               exit(1);
+       }
+       /*
+        * This should probably use IOCTL_DISK_GET_LENGTH_INFO here, but
+        * this ioctl is only available in XP and up.
+        */
+       if (is_device) {
+               if (!DeviceIoControl((HANDLE)fd, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geom, sizeof(geom), &ret, NULL)) {
+                       errno = GetLastError();
+                       die("unable to get length for '%s'");
+               }
+               len = geom.Cylinders.QuadPart*geom.TracksPerCylinder*geom.SectorsPerTrack*BLOCK_SIZE;
+       } else {
+               if (!GetFileInformationByHandle((HANDLE)fd, &hinfo)) {
+                               errno = GetLastError();
+                               die("unable to get length for '%s'");
+               }
+               len = ((loff_t)hinfo.nFileSizeHigh << 32) | (loff_t)hinfo.nFileSizeLow;
+       }
+       close(fd);
+       return len/BLOCK_SIZE;
 #else
   loff_t high, low;
   int fd;
@@ -836,7 +836,7 @@ count_blocks (char *filename)
   valid_offset (fd, 0);
   close (fd);
 
-  return (low + 1) / BLOCK_SIZE;\r
+  return (low + 1) / BLOCK_SIZE;
 #endif
 }
 
@@ -845,7 +845,7 @@ count_blocks (char *filename)
 
 static void
 check_mount (char *device_name)
-{\r
+{
 #ifndef _WIN32
   FILE *f;
   struct mntent *mnt;
@@ -855,70 +855,70 @@ check_mount (char *device_name)
   while ((mnt = getmntent (f)) != NULL)
     if (strcmp (device_name, mnt->mnt_fsname) == 0)
       die ("%s contains a mounted file system.");
-  endmntent (f);\r
+  endmntent (f);
 #endif
 }
 
 
 /* Establish the geometry and media parameters for the device */
-#ifdef _WIN32\r
-static void\r
-establish_params (void)\r
-{\r
-       DISK_GEOMETRY geometry;\r
-       DWORD ret;\r
-\r
-       if (!is_device) {\r
-               bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */\r
-               bs.dir_entries[0] = (char) 0;\r
-               bs.dir_entries[1] = (char) 2;\r
-               /* For FAT32, use 4k clusters on sufficiently large file systems,\r
-                * otherwise 1 sector per cluster. This is also what M$'s format\r
-                * command does for FAT32. */\r
-               bs.cluster_size = (char)\r
-                (size_fat == 32 ?\r
-            ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :\r
-             4); /* FAT12 and FAT16: start at 4 sectors per cluster */\r
-               return;\r
-       }\r
-       if (!DeviceIoControl((HANDLE)dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof(geometry), &ret, NULL)) {\r
-               errno = GetLastError();\r
-               die ("unable to get geometry for '%s'");\r
-       }\r
-    bs.secs_track = geometry.SectorsPerTrack;\r
-    bs.heads = geometry.TracksPerCylinder;\r
-       switch (geometry.MediaType) {\r
-       case F3_1Pt44_512:\r
-               bs.media = (char) 0xf9;\r
-               bs.cluster_size = (char) 2;\r
-               bs.dir_entries[0] = (char) 112;\r
-               bs.dir_entries[1] = (char) 0;\r
-               break;\r
-       case F3_2Pt88_512:\r
-               bs.media = (char) 0xf0;\r
-               bs.cluster_size = (char)(atari_format ? 2 : 1);\r
-               bs.dir_entries[0] = (char) 224;\r
-               bs.dir_entries[1] = (char) 0;\r
-               break;\r
-       case F3_720_512:\r
-               bs.media = (char) 0xfd;\r
-               bs.cluster_size = (char) 2;\r
-               bs.dir_entries[0] = (char) 112;\r
-               bs.dir_entries[1] = (char) 0;\r
-               break;\r
-       default:\r
-               bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */\r
-               bs.dir_entries[0] = (char) 0;\r
-               bs.dir_entries[1] = (char) 2;\r
-               /* For FAT32, use 4k clusters on sufficiently large file systems,\r
-                * otherwise 1 sector per cluster. This is also what M$'s format\r
-                * command does for FAT32. */\r
-               bs.cluster_size = (char)\r
-                (size_fat == 32 ?\r
-            ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :\r
-             4); /* FAT12 and FAT16: start at 4 sectors per cluster */\r
-       }\r
-}\r
+#ifdef _WIN32
+static void
+establish_params (void)
+{
+       DISK_GEOMETRY geometry;
+       DWORD ret;
+
+       if (!is_device) {
+               bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
+               bs.dir_entries[0] = (char) 0;
+               bs.dir_entries[1] = (char) 2;
+               /* For FAT32, use 4k clusters on sufficiently large file systems,
+                * otherwise 1 sector per cluster. This is also what M$'s format
+                * command does for FAT32. */
+               bs.cluster_size = (char)
+                (size_fat == 32 ?
+            ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :
+             4); /* FAT12 and FAT16: start at 4 sectors per cluster */
+               return;
+       }
+       if (!DeviceIoControl((HANDLE)dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof(geometry), &ret, NULL)) {
+               errno = GetLastError();
+               die ("unable to get geometry for '%s'");
+       }
+    bs.secs_track = geometry.SectorsPerTrack;
+    bs.heads = geometry.TracksPerCylinder;
+       switch (geometry.MediaType) {
+       case F3_1Pt44_512:
+               bs.media = (char) 0xf9;
+               bs.cluster_size = (char) 2;
+               bs.dir_entries[0] = (char) 112;
+               bs.dir_entries[1] = (char) 0;
+               break;
+       case F3_2Pt88_512:
+               bs.media = (char) 0xf0;
+               bs.cluster_size = (char)(atari_format ? 2 : 1);
+               bs.dir_entries[0] = (char) 224;
+               bs.dir_entries[1] = (char) 0;
+               break;
+       case F3_720_512:
+               bs.media = (char) 0xfd;
+               bs.cluster_size = (char) 2;
+               bs.dir_entries[0] = (char) 112;
+               bs.dir_entries[1] = (char) 0;
+               break;
+       default:
+               bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
+               bs.dir_entries[0] = (char) 0;
+               bs.dir_entries[1] = (char) 2;
+               /* For FAT32, use 4k clusters on sufficiently large file systems,
+                * otherwise 1 sector per cluster. This is also what M$'s format
+                * command does for FAT32. */
+               bs.cluster_size = (char)
+                (size_fat == 32 ?
+            ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :
+             4); /* FAT12 and FAT16: start at 4 sectors per cluster */
+       }
+}
 #else
 static void
 establish_params (int device_num,int size)
@@ -1099,7 +1099,7 @@ establish_params (int device_num,int size)
             4); /* FAT12 and FAT16: start at 4 sectors per cluster */
     }
 }
-#endif\r
+#endif
 
 
 /* Create the filesystem data tables */
@@ -1629,9 +1629,9 @@ static void
 write_tables (void)
 {
   int x;
-  int fat_length;\r
-#ifdef _WIN32\r
-  int blk;\r
+  int fat_length;
+#ifdef _WIN32
+  int blk;
 #endif
 
   fat_length = (size_fat == 32) ?
@@ -1658,17 +1658,17 @@ write_tables (void)
     }
   /* seek to start of FATS and write them all */
   seekto( reserved_sectors*sector_size, "first FAT" );
-  for (x = 1; x <= nr_fats; x++)\r
-#ifdef _WIN32\r
-         /*\r
-          * WIN32 appearently has problems writing very large chunks directly\r
-          * to disk devices. To not produce errors because of resource shortages\r
-          * split up the write in sector size chunks.\r
-          */\r
-         for (blk = 0; blk < fat_length; blk++)\r
-                 writebuf(fat+blk*sector_size, sector_size, "FAT");\r
+  for (x = 1; x <= nr_fats; x++)
+#ifdef _WIN32
+         /*
+          * WIN32 appearently has problems writing very large chunks directly
+          * to disk devices. To not produce errors because of resource shortages
+          * split up the write in sector size chunks.
+          */
+         for (blk = 0; blk < fat_length; blk++)
+                 writebuf(fat+blk*sector_size, sector_size, "FAT");
 #else
-    writebuf( fat, fat_length * sector_size, "FAT" );\r
+    writebuf( fat, fat_length * sector_size, "FAT" );
 #endif
   /* Write the root directory directly after the last FAT. This is the root
    * dir area on FAT12/16, and the first cluster on FAT32. */
@@ -1730,22 +1730,22 @@ main (int argc, char **argv)
   int c;
   char *tmp;
   char *listfile = NULL;
-  FILE *msgfile;\r
-#ifdef _WIN32\r
-  static char dev_buf[] = "\\\\.\\X:";\r
+  FILE *msgfile;
+#ifdef _WIN32
+  static char dev_buf[] = "\\\\.\\X:";
 #else
-  struct stat statbuf;\r
+  struct stat statbuf;
 #endif
   int i = 0, pos, ch;
   int create = 0;
   
   if (argc && *argv) {         /* What's the program name? */
     char *p;
-    program_name = *argv;\r
-#ifdef _WIN32\r
-    if ((p = strrchr( program_name, '\\' )))\r
+    program_name = *argv;
+#ifdef _WIN32
+    if ((p = strrchr( program_name, '\\' )))
 #else
-    if ((p = strrchr( program_name, '/' )))\r
+    if ((p = strrchr( program_name, '/' )))
 #endif
        program_name = p+1;
   }
@@ -1754,9 +1754,9 @@ main (int argc, char **argv)
   volume_id = (long)create_time;       /* Default volume ID = creation time */
   check_atari();
   
-  printf ("%s " VERSION " (" VERSION_DATE ")\n"\r
-#ifdef _WIN32\r
-         "Win32 port by Jens-Uwe Mager <jum@anubis.han.de>\n"\r
+  printf ("%s " VERSION " (" VERSION_DATE ")\n"
+#ifdef _WIN32
+         "Win32 port by Jens-Uwe Mager <jum@anubis.han.de>\n"
 #endif
           , program_name);
 
@@ -1943,16 +1943,16 @@ main (int argc, char **argv)
        printf( "Unknown option: %c\n", c );
        usage ();
       }
-\r
-  if (optind >= argc)\r
+
+  if (optind >= argc)
          usage();
-  device_name = argv[optind];  /* Determine the number of blocks in the FS */\r
-#ifdef _WIN32\r
-  if (device_name[1] == ':' && device_name[2] == '\0') {\r
-         dev_buf[4] = device_name[0];\r
-         device_name = dev_buf;\r
-         is_device = 1;\r
-  }\r
+  device_name = argv[optind];  /* Determine the number of blocks in the FS */
+#ifdef _WIN32
+  if (device_name[1] == ':' && device_name[2] == '\0') {
+         dev_buf[4] = device_name[0];
+         device_name = dev_buf;
+         is_device = 1;
+  }
 #endif
   if (!create)
     i = count_blocks (device_name); /*  Have a look and see! */
@@ -1987,12 +1987,12 @@ main (int argc, char **argv)
     check_mount (device_name); /* Is the device already mounted? */
     dev = open (device_name, O_RDWR|O_SHARED); /* Is it a suitable device to build the FS on? */
     if (dev < 0)
-      die ("unable to open %s");\r
-#ifdef _WIN32\r
-       if (is_device) {\r
-               if (fsctl(dev, FSCTL_LOCK_VOLUME) == -1)\r
-                       die("unable to lock %s");\r
-       }\r
+      die ("unable to open %s");
+#ifdef _WIN32
+       if (is_device) {
+               if (fsctl(dev, FSCTL_LOCK_VOLUME) == -1)
+                       die("unable to lock %s");
+       }
 #endif
   }
   else {
@@ -2011,11 +2011,11 @@ main (int argc, char **argv)
       if (llseek( dev, 0, SEEK_SET ) != 0)
        die( "seek failed" );
   }
-  \r
-#ifdef _WIN32\r
-  if (!is_device)\r
-         check = 0;\r
-  establish_params();\r
+  
+#ifdef _WIN32
+  if (!is_device)
+         check = 0;
+  establish_params();
 #else
   if (fstat (dev, &statbuf) < 0)
     die ("unable to stat %s");
@@ -2040,7 +2040,7 @@ main (int argc, char **argv)
       die ("Will not try to make filesystem on '%s'");
 
   establish_params (statbuf.st_rdev,statbuf.st_size);  
-                                /* Establish the media parameters */\r
+                                /* Establish the media parameters */
 #endif
 
   setup_tables ();             /* Establish the file system tables */
@@ -2052,14 +2052,14 @@ main (int argc, char **argv)
 
   write_tables ();             /* Write the file system tables away! */
 
-#ifdef _WIN32\r
-       if (is_device) {\r
-               if (fsctl(dev, FSCTL_DISMOUNT_VOLUME) == -1)\r
-                       die("unable to dismount %s");\r
-               if (fsctl(dev, FSCTL_UNLOCK_VOLUME) == -1)\r
-                       die("unable to unlock %s");\r
-       }\r
-#endif\r
+#ifdef _WIN32
+       if (is_device) {
+               if (fsctl(dev, FSCTL_DISMOUNT_VOLUME) == -1)
+                       die("unable to dismount %s");
+               if (fsctl(dev, FSCTL_UNLOCK_VOLUME) == -1)
+                       die("unable to unlock %s");
+       }
+#endif
   exit (0);                    /* Terminate with no errors! */
 }