[FORMAT]
[reactos.git] / reactos / base / system / format / format.c
old mode 100755 (executable)
new mode 100644 (file)
index 8c356e6..73d3b07
@@ -1,13 +1,17 @@
 // Copyright (c) 1998 Mark Russinovich
 // Systems Internals
 // http://www.sysinternals.com
+
+#define WIN32_NO_STATUS
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <windows.h>
+#include <windef.h>
+#include <winbase.h>
+#include <winnls.h>
+#include <winuser.h>
 #include <winternl.h>
 #include <fmifs/fmifs.h>
 #include <tchar.h>
+
 #include "resource.h"
 
 // Globals
@@ -20,7 +24,7 @@ BOOL  CompressDrive = FALSE;
 BOOL   GotALabel = FALSE;
 LPTSTR Label = _T("");
 LPTSTR Drive = NULL;
-LPTSTR Format = _T("FAT");
+LPTSTR FileSystem = _T("FAT");
 
 TCHAR  RootDirectory[MAX_PATH];
 TCHAR  LabelString[12];
@@ -109,7 +113,7 @@ static int ParseCommandLine( int argc, TCHAR *argv[] )
                        if( !_tcsnicmp( &argv[i][1], _T("FS:"), 3 )) {
 
                                if( gotFormat) return -1;
-                               Format = &argv[i][4];
+                               FileSystem = &argv[i][4];
                                gotFormat = TRUE;
 
 
@@ -234,20 +238,24 @@ FormatExCallback (
 BOOLEAN LoadFMIFSEntryPoints()
 {
        HMODULE hFmifs = LoadLibrary( _T("fmifs.dll") );
-       if( !(void*) GetProcAddress( hFmifs, "FormatEx" ) ) {
+       if (hFmifs == NULL) {
+               return FALSE;
+       }
 
+       if( !(void*) GetProcAddress( hFmifs, "FormatEx" ) ) {
+               FreeLibrary(hFmifs);
                return FALSE;
        }
 
        if( !((void *) GetProcAddress( hFmifs,
                        "EnableVolumeCompression" )) ) {
-
+               FreeLibrary(hFmifs);
                return FALSE;
        }
 
        if( !((void *) GetProcAddress( hFmifs,
                        "QueryAvailableFileSystemFormat" )) ) {
-
+               FreeLibrary(hFmifs);
                return FALSE;
        }
 
@@ -272,7 +280,7 @@ static VOID Usage( LPTSTR ProgramName )
        WCHAR szFormatW[MAX_PATH];
        DWORD Index = 0;
        BYTE dummy;
-       BOOLEAN lastestVersion;
+       BOOLEAN latestVersion;
 
        LoadStringAndOem( GetModuleHandle(NULL), STRING_HELP, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
        if (!LoadFMIFSEntryPoints())
@@ -282,9 +290,9 @@ static VOID Usage( LPTSTR ProgramName )
        }
 
        szFormats[0] = 0;
-       while (QueryAvailableFileSystemFormat(Index++, szFormatW, &dummy, &dummy, &lastestVersion))
+       while (QueryAvailableFileSystemFormat(Index++, szFormatW, &dummy, &dummy, &latestVersion))
        {
-               if (!lastestVersion)
+               if (!latestVersion)
                        continue;
                if (szFormats[0])
                        _tcscat(szFormats, _T(", "));
@@ -324,7 +332,7 @@ _tmain(int argc, TCHAR *argv[])
        DWORD flags, maxComponent;
        ULARGE_INTEGER freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes;
 #ifndef UNICODE
-       WCHAR RootDirectoryW[MAX_PATH], FormatW[MAX_PATH], LabelW[MAX_PATH];
+       WCHAR RootDirectoryW[MAX_PATH], FileSystemW[MAX_PATH], LabelW[MAX_PATH];
 #endif
        TCHAR szMsg[RC_STRING_MAX_SIZE];
 
@@ -370,25 +378,52 @@ _tmain(int argc, TCHAR *argv[])
        // See if the drive is removable or not
        //
        driveType = GetDriveType( RootDirectory );
-
-       if( driveType == 0 ) {
-               LoadStringAndOem( GetModuleHandle(NULL), STRING_ERROR_DRIVE_TYPE, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
-               PrintWin32Error( szMsg, GetLastError());
-               return -1;
-       }
-       else if ( driveType == 1 )
+       switch (driveType)
        {
-               LoadString( GetModuleHandle(NULL), STRING_NO_VOLUME, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
-               PrintWin32Error( szMsg, GetLastError());
-               return -1;
-       }
+               case DRIVE_UNKNOWN :
+                       LoadStringAndOem( GetModuleHandle(NULL), STRING_ERROR_DRIVE_TYPE, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
+                       PrintWin32Error( szMsg, GetLastError());
+                       return -1;
 
-       if( driveType != DRIVE_FIXED ) {
-               LoadStringAndOem( GetModuleHandle(NULL), STRING_INSERT_DISK, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
-               _tprintf(szMsg, RootDirectory[0] );
-               _fgetts( input, sizeof(input)/2, stdin );
+               case DRIVE_REMOTE:
+               case DRIVE_CDROM:
+                       LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_SUPPORT, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
+                       _tprintf(szMsg);
+                       return -1;
+
+               case DRIVE_NO_ROOT_DIR:
+                       LoadString( GetModuleHandle(NULL), STRING_NO_VOLUME, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
+                       PrintWin32Error( szMsg, GetLastError());
+                       return -1;
 
-               media = FMIFS_FLOPPY;
+               case DRIVE_REMOVABLE:
+                       LoadStringAndOem( GetModuleHandle(NULL), STRING_INSERT_DISK, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
+                       _tprintf(szMsg, RootDirectory[0] );
+                       _fgetts( input, sizeof(input)/2, stdin );
+                       media = FMIFS_FLOPPY;
+                       break;
+
+               case DRIVE_FIXED:
+               case DRIVE_RAMDISK:
+                       media = FMIFS_HARDDISK;
+                       break;
+       }
+
+       // Reject attempts to format the system drive
+       {
+               TCHAR path[MAX_PATH + 1];
+               UINT rc;
+               rc = GetWindowsDirectory(path, MAX_PATH);
+               if (rc == 0 || rc > MAX_PATH)
+                       // todo: Report "Unable to query system directory"
+                       return -1;
+               if (_totlower(path[0]) == _totlower(Drive[0]))
+               {
+                       // todo: report "Cannot format system drive"
+                       LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_SUPPORT, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
+                       _tprintf(szMsg);
+                       return -1;
+               }
        }
 
        //
@@ -452,7 +487,6 @@ _tmain(int argc, TCHAR *argv[])
                                return 0;
                        }
                }
-               media = FMIFS_HARDDISK;
        }
 
        //
@@ -492,12 +526,12 @@ _tmain(int argc, TCHAR *argv[])
        //
 #ifndef UNICODE
        MultiByteToWideChar(CP_ACP, 0, RootDirectory, -1, RootDirectoryW, MAX_PATH);
-       MultiByteToWideChar(CP_ACP, 0, Format, -1, FormatW, MAX_PATH);
+       MultiByteToWideChar(CP_ACP, 0, FileSystem, -1, FileSystemW, MAX_PATH);
        MultiByteToWideChar(CP_ACP, 0, Label, -1, LabelW, MAX_PATH);
-       FormatEx( RootDirectoryW, media, FormatW, LabelW, QuickFormat,
+       FormatEx( RootDirectoryW, media, FileSystemW, LabelW, QuickFormat,
                        ClusterSize, FormatExCallback );
 #else
-       FormatEx( RootDirectory, media, Format, Label, QuickFormat,
+       FormatEx( RootDirectory, media, FileSystem, Label, QuickFormat,
                        ClusterSize, FormatExCallback );
 #endif
        if( Error ) return -1;
@@ -583,4 +617,3 @@ _tmain(int argc, TCHAR *argv[])
 
        return 0;
 }
-