- LoadStringAndOem( GetModuleHandle(NULL), STRING_VOL_COMPRESS, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- _tprintf("%s", szMsg);
- }
- }
-
- //
- // Get the label if we don't have it
- //
- if( !GotALabel ) {
-
- LoadString( GetModuleHandle(NULL), STRING_ENTER_LABEL, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- _tprintf("%s", szMsg);
- _fgetts( input, sizeof(LabelString)/2, stdin );
-
- input[ _tcslen(input)-1] = 0;
- if( !SetVolumeLabel( RootDirectory, input )) {
-
- LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_LABEL, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- PrintWin32Error(szMsg, GetLastError());
- return -1;
- }
- }
-
- if( !GetVolumeInformation( RootDirectory,
- volumeName, sizeof(volumeName)/2,
- &serialNumber, &maxComponent, &flags,
- fileSystem, sizeof(fileSystem)/2)) {
-
- LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_VOLUME, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- PrintWin32Error( szMsg, GetLastError());
- return -1;
- }
-
- //
- // Print out some stuff including the formatted size
- //
- if( !GetDiskFreeSpaceEx( RootDirectory,
- &freeBytesAvailableToCaller,
- &totalNumberOfBytes,
- &totalNumberOfFreeBytes )) {
-
- LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_VOLUME_SIZE, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- PrintWin32Error(szMsg, GetLastError());
- return -1;
- }
-
- LoadStringAndOem( GetModuleHandle(NULL), STRING_FREE_SPACE, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- _tprintf(szMsg, totalNumberOfBytes.QuadPart, totalNumberOfFreeBytes.QuadPart );
-
- //
- // Get the drive's serial number
- //
- if( !GetVolumeInformation( RootDirectory,
- volumeName, sizeof(volumeName)/2,
- &serialNumber, &maxComponent, &flags,
- fileSystem, sizeof(fileSystem)/2)) {
-
- LoadStringAndOem( GetModuleHandle(NULL), STRING_NO_VOLUME, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- PrintWin32Error( szMsg, GetLastError());
- return -1;
- }
- LoadStringAndOem( GetModuleHandle(NULL), STRING_SERIAL_NUMBER, (LPTSTR) szMsg,RC_STRING_MAX_SIZE);
- _tprintf(szMsg, (unsigned int)(serialNumber >> 16),
- (unsigned int)(serialNumber & 0xFFFF) );
-
- return 0;
+ //
+ // Parse command line
+ //
+ badArg = ParseCommandLine(argc, argv);
+ if (badArg)
+ {
+ ConResPrintf(StdErr, STRING_UNKNOW_ARG, argv[badArg]);
+ Usage(argv[0]);
+ return -1;
+ }
+
+ //
+ // Get the drive's format
+ //
+ if (!Drive)
+ {
+ ConResPuts(StdErr, STRING_DRIVE_PARM);
+ Usage(argv[0]);
+ return -1;
+ }
+ else
+ {
+ wcscpy(RootDirectory, Drive);
+ }
+ RootDirectory[2] = L'\\';
+ RootDirectory[3] = L'\0';
+
+ //
+ // See if the drive is removable or not
+ //
+ driveType = GetDriveTypeW(RootDirectory);
+ switch (driveType)
+ {
+ case DRIVE_UNKNOWN :
+ K32LoadStringW(GetModuleHandle(NULL), STRING_ERROR_DRIVE_TYPE, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+
+ case DRIVE_REMOTE:
+ case DRIVE_CDROM:
+ ConResPuts(StdOut, STRING_NO_SUPPORT);
+ return -1;
+
+ case DRIVE_NO_ROOT_DIR:
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+
+ case DRIVE_REMOVABLE:
+ ConResPrintf(StdOut, STRING_INSERT_DISK, RootDirectory[0]);
+ fgetws(input, ARRAYSIZE(input), stdin);
+ media = FMIFS_FLOPPY;
+ break;
+
+ case DRIVE_FIXED:
+ case DRIVE_RAMDISK:
+ media = FMIFS_HARDDISK;
+ break;
+ }
+
+ // Reject attempts to format the system drive
+ {
+ WCHAR path[MAX_PATH + 1];
+ UINT rc;
+ rc = GetWindowsDirectoryW(path, MAX_PATH);
+ if (rc == 0 || rc > MAX_PATH)
+ // todo: Report "Unable to query system directory"
+ return -1;
+ if (towlower(path[0]) == towlower(Drive[0]))
+ {
+ // todo: report "Cannot format system drive"
+ ConResPuts(StdOut, STRING_NO_SUPPORT);
+ return -1;
+ }
+ }
+
+ //
+ // Determine the drive's file system format
+ //
+ if (!GetVolumeInformationW(RootDirectory,
+ volumeName, ARRAYSIZE(volumeName),
+ &serialNumber, &maxComponent, &flags,
+ fileSystem, ARRAYSIZE(fileSystem)))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+
+ if (!GetDiskFreeSpaceExW(RootDirectory,
+ &freeBytesAvailableToCaller,
+ &totalNumberOfBytes,
+ &totalNumberOfFreeBytes))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME_SIZE, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+ ConResPrintf(StdOut, STRING_FILESYSTEM, fileSystem);
+
+ //
+ // Make sure they want to do this
+ //
+ if (driveType == DRIVE_FIXED)
+ {
+ if (volumeName[0])
+ {
+ while (TRUE)
+ {
+ ConResPrintf(StdOut, STRING_LABEL_NAME_EDIT, RootDirectory[0]);
+ fgetws(input, ARRAYSIZE(input), stdin);
+ input[wcslen(input) - 1] = 0;
+
+ if (!wcsicmp(input, volumeName))
+ break;
+
+ ConResPuts(StdOut, STRING_ERROR_LABEL);
+ }
+ }
+
+ ConResPrintf(StdOut, STRING_YN_FORMAT, RootDirectory[0]);
+
+ K32LoadStringW(GetModuleHandle(NULL), STRING_YES_NO_FAQ, szMsg, ARRAYSIZE(szMsg));
+ while (TRUE)
+ {
+ fgetws(input, ARRAYSIZE(input), stdin);
+ if (_wcsnicmp(&input[0], &szMsg[0], 1) == 0) break;
+ if (_wcsnicmp(&input[0], &szMsg[1], 1) == 0)
+ {
+ ConPuts(StdOut, L"\n");
+ return 0;
+ }
+ }
+ }
+
+ //
+ // Tell the user we're doing a long format if appropriate
+ //
+ if (!QuickFormat)
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_VERIFYING, szMsg, ARRAYSIZE(szMsg));
+ if (totalNumberOfBytes.QuadPart > 1024*1024*10)
+ {
+ ConPrintf(StdOut, L"%s %luM\n", szMsg, (DWORD)(totalNumberOfBytes.QuadPart/(1024*1024)));
+ }
+ else
+ {
+ ConPrintf(StdOut, L"%s %.1fM\n", szMsg,
+ ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0));
+ }
+ }
+ else
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_FAST_FMT, szMsg, ARRAYSIZE(szMsg));
+ if (totalNumberOfBytes.QuadPart > 1024*1024*10)
+ {
+ ConPrintf(StdOut, L"%s %luM\n", szMsg, (DWORD)(totalNumberOfBytes.QuadPart/(1024*1024)));
+ }
+ else
+ {
+ ConPrintf(StdOut, L"%s %.2fM\n", szMsg,
+ ((float)(LONGLONG)totalNumberOfBytes.QuadPart)/(float)(1024.0*1024.0));
+ }
+ ConResPuts(StdOut, STRING_CREATE_FSYS);
+ }
+
+ //
+ // Format away!
+ //
+ FormatEx(RootDirectory, media, FileSystem, Label, QuickFormat,
+ ClusterSize, FormatExCallback);
+ if (Error) return -1;
+ ConResPuts(StdOut, STRING_FMT_COMPLETE);
+
+ //
+ // Enable compression if desired
+ //
+ if (CompressDrive)
+ {
+ if (!EnableVolumeCompression(RootDirectory, TRUE))
+ ConResPuts(StdOut, STRING_VOL_COMPRESS);
+ }
+
+ //
+ // Get the label if we don't have it
+ //
+ if (!GotALabel)
+ {
+ ConResPuts(StdOut, STRING_ENTER_LABEL);
+ fgetws(input, ARRAYSIZE(LabelString), stdin);
+
+ input[wcslen(input) - 1] = 0;
+ if (!SetVolumeLabelW(RootDirectory, input))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_LABEL, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+ }
+
+ if (!GetVolumeInformationW(RootDirectory,
+ volumeName, ARRAYSIZE(volumeName),
+ &serialNumber, &maxComponent, &flags,
+ fileSystem, ARRAYSIZE(fileSystem)))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+
+ //
+ // Print out some stuff including the formatted size
+ //
+ if (!GetDiskFreeSpaceExW(RootDirectory,
+ &freeBytesAvailableToCaller,
+ &totalNumberOfBytes,
+ &totalNumberOfFreeBytes))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME_SIZE, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+
+ ConResPrintf(StdOut, STRING_FREE_SPACE, totalNumberOfBytes.QuadPart,
+ totalNumberOfFreeBytes.QuadPart);
+
+ //
+ // Get the drive's serial number
+ //
+ if (!GetVolumeInformationW(RootDirectory,
+ volumeName, ARRAYSIZE(volumeName),
+ &serialNumber, &maxComponent, &flags,
+ fileSystem, ARRAYSIZE(fileSystem)))
+ {
+ K32LoadStringW(GetModuleHandle(NULL), STRING_NO_VOLUME, szMsg, ARRAYSIZE(szMsg));
+ PrintWin32Error(szMsg, GetLastError());
+ return -1;
+ }
+ ConResPrintf(StdOut, STRING_SERIAL_NUMBER,
+ (unsigned int)(serialNumber >> 16),
+ (unsigned int)(serialNumber & 0xFFFF));
+
+ return 0;