/* mt.c - Implementacion del comando "mt" de Unix en Win32 Copyright (C) 2000 Luis Carlos Castro Skertchly This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Inicio: 14 de Julio de 2000 Uso: mt -t tapename command [count] Versiones Corriendo: 1.0 - 21-jul-2000 1.0.1 - 12-ago-2008 - Se tradujeron los mensajes a ingles */ #define _CRT_SECURE_NO_DEPRECATE #define _WIN32_WINNT 0x0501 #define WIN32_LEAN_AND_MEAN #include #include #include "luislib.h" #define APPNAME_STRING "mt - Version 1.0.1\nCopyright (C) 2000-2008, Luis C. Castro Skertchly (lc_castro@yahoo.com)\n" #define TAPE_SF 1 #define TAPE_SR 2 #define TAPE_SS 4 #define TAPE_EOF 8 #define TAPE_REW 9 #define TAPE_OFFL 10 #define TAPE_EOD 11 #define TAPE_SMK 12 #define TAPE_INFO 16 #define TAPE_SBS 17 #define TAPE_GTP 18 #define TAPE_ERASE_S 19 #define TAPE_ERASE_L 20 int accion; int count; char szTapeName[256]; char szBuffer[2048]; char szErrorMsg[1024]; HANDLE hTape; DWORD error; BOOL silent; DWORD dwPartition; DWORD dwOffsetLow; DWORD dwOffsetHigh; DWORD dwInfosize; TAPE_GET_MEDIA_PARAMETERS tapeInfo; TAPE_SET_MEDIA_PARAMETERS tapeMedia; TAPE_GET_DRIVE_PARAMETERS driveInfo; int init(void) { return 0; } void usage(void) { fprintf(stderr, "Send commands to a magnetic tape device. Inspired by the Unix mt command.\n"); fprintf(stderr, "Usage: mt -v -t tapename command [n]\n" " -v - Verbose messages\n" " -t - Tape device name\n" " Commands:\n" " info - Show tape and drive information\n" " ers - Erase Short (Quickly)\n" " erl - Erase Long (Full erase)\n" " sbs - Set block size to n bytes\n" " gtp - Get tape position\n" " eof - Write n EOF marks\n" " fsf - Forward space n files\n" " fsr - Forward space n records\n" " bsf - Backward space n files\n" " bsr - Backward space n records\n" " rew - Rewind tape\n" " offl - Rewind tape and go offline\n" " eod - Seek to end of data\n" " smk - Write n setmarks\n" " fss - Forward space n setmarks\n" " bss - Backward space n setmarks\n" ); exit(1); } int parse_args(int argc, char **argv) { int i; count = 1; silent = TRUE; if (argc < 4) usage(); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-t")) { if (!argv[i + 1]) usage(); sprintf(szTapeName, "\\\\.\\%s", argv[++i]); _strupr(szTapeName); continue; } else if (!strcmp(argv[i], "-v")) { silent = FALSE; } else if (!strcmp(argv[i], "info")) { accion = TAPE_INFO; } else if (!strcmp(argv[i], "sbs")) { accion = TAPE_SBS; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "gtp")) { accion = TAPE_GTP; } else if (!strcmp(argv[i], "fsf")) { accion = TAPE_SF; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "bsf")) { accion = TAPE_SF; if (argv[i + 1]) count = -atoi(argv[++i]); else count = -1; continue; } else if (!strcmp(argv[i], "fsr")) { accion = TAPE_SR; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "bsr")) { accion = TAPE_SR; if (argv[i + 1]) count = -atoi(argv[++i]); else count = -1; continue; } else if (!strcmp(argv[i], "fss")) { accion = TAPE_SS; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "bss")) { accion = TAPE_SS; if (argv[i + 1]) count = -atoi(argv[++i]); else count = -1; continue; } else if (!strcmp(argv[i], "smk")) { accion = TAPE_SMK; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "eof")) { accion = TAPE_EOF; if (argv[i + 1]) count = atoi(argv[++i]); continue; } else if (!strcmp(argv[i], "rew")) { accion = TAPE_REW; } else if (!strcmp(argv[i], "eod")) { accion = TAPE_EOF; } else if (!strcmp(argv[i], "offl")) { accion = TAPE_OFFL; } else if (!strcmp(argv[i], "ers")) { accion = TAPE_ERASE_S; } else if (!strcmp(argv[i], "erl")) { accion = TAPE_ERASE_L; } else usage(); } return 0; } int print_drive_parameters(TAPE_GET_DRIVE_PARAMETERS *di) { fprintf(stderr, "ECC = %s\n", di->ECC ? "TRUE" : "FALSE"); fprintf(stderr, "Compression = %s\n", di->Compression ? "TRUE" : "FALSE"); fprintf(stderr, "DataPadding = %s\n", di->DataPadding ? "TRUE" : "FALSE"); fprintf(stderr, "ReportSetMarks = %s\n", di->ReportSetmarks ? "TRUE" : "FALSE"); fprintf(stderr, "Default Block Size = %ld\n", di->DefaultBlockSize); fprintf(stderr, "Minimum Block Size = %ld\n", di->MinimumBlockSize); fprintf(stderr, "Maximum Block Size = %ld\n", di->MaximumBlockSize); fprintf(stderr, "Maximum Partition Count = %ld\n", di->MaximumPartitionCount); fprintf(stderr, "EOT Warning Zone Size = %ld\n", di->EOTWarningZoneSize); fprintf(stderr, "\n"); return 0; } int print_tape_parameters(TAPE_GET_MEDIA_PARAMETERS *ti) { fprintf(stderr, "Capacity: %lu\n", ti->Capacity); fprintf(stderr, "Block Size: %d\n", ti->BlockSize); fprintf(stderr, "Partitions: %d\n", ti->PartitionCount); fprintf(stderr, "Write protected: %s\n", ti->WriteProtected ? "Yes" : "No"); return 0; } int main(int argc, char **argv) { init(); fprintf(stderr, APPNAME_STRING); parse_args(argc, argv); if (!silent) fprintf(stderr, "Opening device %s... ", szTapeName); hTape = CreateFile(szTapeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hTape == INVALID_HANDLE_VALUE) { fprintf(stderr, "mt - CreateFile('%s', GENERIC_READ | GENERIC_WRITE): %s", szTapeName, GetErrorMessage(GetLastError(), szErrorMsg, sizeof(szErrorMsg))); exit(1); } if (!silent) printf("Ok\n"); SetLastError(0); switch(accion) { case TAPE_OFFL: PrepareTape(hTape, TAPE_UNLOAD, FALSE); break; case TAPE_EOF: WriteTapemark(hTape, TAPE_FILEMARKS, count, FALSE); break; case TAPE_SMK: WriteTapemark(hTape, TAPE_SETMARKS, count, FALSE); break; case TAPE_REW: SetTapePosition(hTape, TAPE_REWIND, 0, 0, 0, FALSE); break; case TAPE_EOD: SetTapePosition(hTape, TAPE_SPACE_END_OF_DATA, 0, 0, 0, FALSE); break; case TAPE_SF: SetTapePosition(hTape, TAPE_SPACE_FILEMARKS, 0, count, 0, FALSE); break; case TAPE_SS: SetTapePosition(hTape, TAPE_SPACE_SETMARKS, 0, count, 0, FALSE); break; case TAPE_SR: SetTapePosition(hTape, TAPE_SPACE_RELATIVE_BLOCKS, 0, count, 0, FALSE); break; case TAPE_INFO: fprintf(stderr, "\nDrive parameters:\n"); dwInfosize = sizeof(driveInfo); error = GetTapeParameters(hTape, GET_TAPE_DRIVE_INFORMATION, &dwInfosize, &driveInfo); if (error != NO_ERROR) { GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg)); fprintf(stderr, "mt - GetTapeParameters(): %s", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } print_drive_parameters(&driveInfo); fprintf(stderr, "Tape parameters:\n"); dwInfosize = sizeof(tapeInfo); error = GetTapeParameters(hTape, GET_TAPE_MEDIA_INFORMATION, &dwInfosize, &tapeInfo); if (error != NO_ERROR) { fprintf(stderr, "mt - GetTapeParameters(): %s", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } print_tape_parameters(&tapeInfo); break; case TAPE_ERASE_L: printf("Erasing tape (long)..."); error = EraseTape(hTape, TAPE_ERASE_LONG, FALSE); if (error != NO_ERROR) { fprintf(stderr, "mt - EraseTape(): %s\n", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(0); } printf("Ok\n"); break; case TAPE_ERASE_S: printf("Erasing tape (short)..."); error = EraseTape(hTape, TAPE_ERASE_SHORT, FALSE); if (error != NO_ERROR) { fprintf(stderr, "mt - EraseTape(): %s\n", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(0); } printf("Ok\n"); break; case TAPE_SBS: dwInfosize = sizeof(tapeInfo); error = GetTapeParameters(hTape, GET_TAPE_MEDIA_INFORMATION, &dwInfosize, &tapeInfo); if (error != NO_ERROR) { fprintf(stderr, "mt - GetTapeParameters() - %s", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } tapeMedia.BlockSize = count; if (!silent) fprintf(stderr, "Setting blockSize = %d...", tapeMedia.BlockSize); error = SetTapeParameters(hTape, SET_TAPE_MEDIA_INFORMATION, &tapeMedia); if (error != NO_ERROR) { fprintf(stderr, "mt - SetTapeParameters(): %s: ", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } if (!silent) fprintf(stderr, "Ok\n"); break; case TAPE_GTP: error = GetTapePosition(hTape, TAPE_ABSOLUTE_POSITION, &dwPartition, &dwOffsetLow, &dwOffsetHigh); if (error != NO_ERROR) { fprintf(stderr, "mt - GetTapePosition(TAPE_ABSOLUTE_POSITION): %s\n", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } printf("Absolute Position:\n Block address: %lu\n\n", dwOffsetLow); error = GetTapePosition(hTape, TAPE_LOGICAL_POSITION, &dwPartition, &dwOffsetLow, &dwOffsetHigh); if (error != NO_ERROR) { fprintf(stderr, "mt - GetTapePosition(TAPE_LOGICAL_POSITION): %s\n", GetErrorMessage(error, szErrorMsg, sizeof(szErrorMsg))); exit(1); } printf("Logical Position:\n Partition: %lu\n Block address: %lu\n\n", dwPartition, dwOffsetLow); break; } CloseHandle(hTape); return 0; }