📄 vfdcmd.c
字号:
/*
vfdcmd.c
Virtual Floppy Disk drive control program (console version)
Copyright (C) 2003 Kenji Kato
*/
#define WIN32_LEAN_AND_MEAN
#define _CRTDBG_MAP_ALLOC
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "vfdctl.h"
#include "vfdutil.h"
#include "resource.h"
//
// prototypes for local functions
//
static int Install(char **args, DWORD current_state);
static int Remove(DWORD current_state);
static int Start(DWORD current_state);
static int Stop(DWORD current_state);
static int Mount(char **args, DWORD current_state);
static int Unmount(DWORD current_state);
static int Stat(DWORD current_state);
static int PrintHelp(const char *topic);
static BOOL Retry_Callback(DWORD param);
static BOOL Continue_Callback(DWORD err);
static void PrintMessage(UINT msg, ...);
static void PrintError(DWORD err);
//
// main
//
int main(int argc, char **argv)
{
DWORD driver_state;
char *cmd;
DWORD ret;
// Reports memory leaks at process termination
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
// At least one parameter (command) is required
if (argc < 2) {
PrintHelp(NULL);
return -1;
}
// Get Current Driver state
if ((ret = VfdGetDriverState(&driver_state)) != ERROR_SUCCESS) {
PrintMessage(IDS_GET_STAT_NG);
PrintError(ret);
return -1;
}
// Parse command line arguments
cmd = *(argv + 1);
if (stricmp(cmd, "install") == 0) {
if (argc > 4) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Install(argv + 2, driver_state);
}
else if (stricmp(cmd, "start") == 0) {
if (argc > 2) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Start(driver_state);
}
else if (stricmp(cmd, "mount") == 0) {
if (argc > 5) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Mount(argv + 2, driver_state);
}
else if (stricmp(cmd, "remove") == 0) {
if (argc > 2) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Remove(driver_state);
}
else if (stricmp(cmd, "stop") == 0) {
if (argc > 2) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Stop(driver_state);
}
else if (stricmp(cmd, "umount") == 0) {
if (argc > 2) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Unmount(driver_state);
}
else if (stricmp(cmd, "stat") == 0) {
if (argc > 2) {
PrintMessage(IDS_TOO_MANY_ARGS);
PrintHelp(cmd);
return -1;
}
return Stat(driver_state);
}
else if (stricmp(cmd, "help") == 0) {
PrintMessage(IDS_HELP_HEADER);
return PrintHelp(*(argv + 2));
}
else {
PrintMessage(IDS_INVALID_COMMAND, cmd);
PrintHelp(NULL);
return -1;
}
}
// Install Virtual Floppy Driver Dynamically
// Command Line Parameters:
// (optional) driver file path - default to executive's dir
// (optional) auto start switch - default to demand start
int Install(char **args, DWORD driver_state)
{
static const char *cmd = "install";
char *install_path = NULL;
BOOL auto_start = FALSE;
DWORD ret;
// process parameters
while (*args) {
if (_stricmp(*args, "/auto") == 0) {
if (auto_start) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
auto_start = TRUE;
}
}
else if (**args == '/') {
PrintMessage(IDS_INVALID_OPTION, *args);
PrintHelp(cmd);
return -1;
}
else {
if (install_path) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
install_path = *args;
}
}
args++;
}
// already installed?
if (driver_state != VFD_NOT_INSTALLED) {
PrintMessage(IDS_ALREADY_INSTALLED);
return -1;
}
// install the driver
if ((ret = VfdInstall(install_path, auto_start)) != ERROR_SUCCESS) {
PrintMessage(IDS_INSTALL_NG);
PrintError(ret);
return -1;
}
// operation successfull
PrintMessage(IDS_INSTALL_OK);
return 0;
}
// Remove Virtual Floppy Driver from system
// Command Line Parameters: None
int Remove(DWORD driver_state)
{
DWORD ret;
int i;
// ensure the driver is installed
if (driver_state == VFD_NOT_INSTALLED) {
PrintMessage(IDS_NOT_INSTALLED);
return -1;
}
// ensure the driver is stopped
if (driver_state == SERVICE_RUNNING) {
// unmount current image
ret = EnsureUnmount(Retry_Callback, Continue_Callback, 0);
if (ret == ERROR_SUCCESS) {
PrintMessage(IDS_UNMOUNT_OK);
}
else if (ret != ERROR_NOT_READY) {
PrintMessage(IDS_REMOVE_NG);
return -1;
}
// stop the driver
if ((ret = VfdStop(&driver_state)) != ERROR_SUCCESS) {
PrintMessage(IDS_STOP_NG);
PrintError(ret);
return -1;
}
if (driver_state == SERVICE_STOPPED) {
PrintMessage(IDS_STOP_OK);
}
else {
PrintMessage(IDS_STOP_PENDING);
}
}
// remove the driver
if ((ret = VfdRemove()) != ERROR_SUCCESS) {
PrintMessage(IDS_REMOVE_NG);
PrintError(ret);
return -1;
}
// Wait for the driver to be actually removed for 3 secs Max.
for (i = 0; i < 10; i++) {
ret = VfdGetDriverState(&driver_state);
if (ret != ERROR_SUCCESS) {
PrintMessage(IDS_GET_STAT_NG);
PrintError(ret);
return -1;
}
if (driver_state == VFD_NOT_INSTALLED) {
break;
}
Sleep(300);
}
if (driver_state == VFD_NOT_INSTALLED) {
PrintMessage(IDS_REMOVE_OK);
}
else {
PrintMessage(IDS_REMOVE_PENDING);
}
return 0;
}
// Start the Virtual FD Driver
// Command Line Parameters: None
int Start(DWORD driver_state)
{
DWORD ret;
// ensure that the driver is installed
if (driver_state == VFD_NOT_INSTALLED) {
if ((ret = VfdInstall(NULL, FALSE)) == ERROR_SUCCESS) {
PrintMessage(IDS_INSTALL_OK);
}
else {
PrintMessage(IDS_INSTALL_NG);
PrintError(ret);
return -1;
}
}
// ensure that the driver is not started yet
if (driver_state == SERVICE_RUNNING) {
PrintMessage(IDS_ALREADY_RUNNING);
return -1;
}
// start the driver
if ((ret = VfdStart(&driver_state)) != ERROR_SUCCESS) {
PrintMessage(IDS_START_NG);
PrintError(ret);
return -1;
}
// operation successfull
PrintMessage(IDS_START_OK);
return 0;
}
// Stop Virtual Floppy Driver
// Command Line Parameters: None
int Stop(DWORD driver_state)
{
DWORD ret;
// ensure that the driver is installed
if (driver_state == VFD_NOT_INSTALLED) {
PrintMessage(IDS_NOT_INSTALLED);
return -1;
}
// ensure that the driver is running
if (driver_state == SERVICE_STOPPED) {
PrintMessage(IDS_NOT_STARTED);
return -1;
}
// ensure that the image is unmounted
if (driver_state == SERVICE_RUNNING) {
ret = EnsureUnmount(Retry_Callback, Continue_Callback, 0);
if (ret == ERROR_SUCCESS) {
PrintMessage(IDS_UNMOUNT_OK);
}
else if (ret != ERROR_NOT_READY) {
PrintMessage(IDS_STOP_NG);
return -1;
}
}
// stop the driver
if ((ret = VfdStop(&driver_state)) != ERROR_SUCCESS) {
PrintMessage(IDS_STOP_NG);
PrintError(ret);
return -1;
}
if (driver_state == SERVICE_STOPPED) {
PrintMessage(IDS_STOP_OK);
}
else {
PrintMessage(IDS_STOP_PENDING);
PrintMessage(IDS_RESTART_WARN);
}
return 0;
}
// Mount an image file to Virtual Floppy Drive
// Command Line Parameters:
// (optional) image file path - default to most recently mounted image
// (optional) drive letter - default to first available letter
// (optional) read-only switch - default to image file's attribute
// (optional) image file size (used only to create a new image)
int Mount(char **args, DWORD driver_state)
{
static const char *cmd = "mount";
const char *file_name = NULL;
BOOL read_only = FALSE;
ULONG file_size = 0;
char drive_letter = '\0';
char actual_name[MAX_PATH];
DWORD ret;
// process parameters
while (*args) {
if (_stricmp(*args, "/ro") == 0) {
if (read_only) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
read_only = TRUE;
}
}
else if (strcmp(*args, "/720") == 0) {
if (file_size) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
file_size = VFD_FILESIZE_720KB;
}
}
else if (strcmp(*args, "/144") == 0) {
if (file_size) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
file_size = VFD_FILESIZE_1P44MB;
}
}
else if (strcmp(*args, "/288") == 0) {
if (file_size) {
PrintMessage(IDS_DUPLICATE_ARGS);
PrintHelp(cmd);
return -1;
}
else {
file_size = VFD_FILESIZE_2P88MB;
}
}
else if (strnicmp(*args, "/l:", 3) == 0) {
if (drive_letter) {
PrintMessage(IDS_DUPLICATE_ARGS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -