Source program list
/*************************************************************/
/* Program name: BAT2COM.C 1.50 */
/* Author: Dong Zhanshan */
/* Completion date: 1993,1995 */
/* Purpose: Convert batch files (.bat) to command files (.COM) */
/* Compilation method: You can get BAT2COM.COM by compiling and linking with the following commands: */
/* tcc -mt bat2com */
/* tlink c:\tc\lib\c0t+bat2com,bat2com,,c:\tc\lib\cs\lib /t */
/*************************************************************/
#include <stdio.h>
#include <string.h>
/* Generate the header of the command file, including the machine instructions for calling 2EH */
char bat2comhead[81] = {
0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0x0E,0x1F,0x2E,
0x8B,0x0E,0x51,0x01,0xBE,0x51,0x01,0x8B,0xC6,0x50,
0x5B,0x51,0x83,0xC3,0x02,0x8B,0xF3,0x33,0xDB,0x8A,
0x1C,0x53,0x56,0x2E,0x8C,0x16,0x4D,0x01,0x2E,0x89,
0x26,0x4F,0x01,0xCD,0x2E,0x0E,0x1F,0x2E,0x8B,0x26,
0x4F,0x01,0x2E,0x8E,0x16,0x4D,0x01,0x58,0x5B,0x59,
0x03,0xC3,0x50,0x83,0xE9,0x01,0x83,0xF9,0x00,0x75,
0xCD,0xB8,0x00,0x4C,0xCD,0x21,0xC3,0x00,0x00,0x00,
0x00};
int totallength; /* Total length of the generated file */
char buffer[10000]; /* Buffer for generating command file */
/* Function to remove meaningless leading spaces in batch commands */
void removespace(str1)
char *str1;
{
unsigned int i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1 == ' ') && (i == 0))
{
++str1; /* str1 address plus 1 */
movmem(str1, str2, strlen(str1) + 1); /* Remove one leading space */
removespace(str2); /* Recursive call to this function */
}
}
/* Function to replace "%%" with "%" in batch commands */
void removedouble(str1)
char *str1;
{
unsigned int j, i = 0;
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if ((str1 == '%') && (str1[++i] == '%'))
{
--i;
for (j = 0; j < i; j++) {
++str1; ++str2;
}
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove one '%' character */
removedouble(str2); /* Recursive call to this function */
}
}
/* Function to remove "@" symbol in batch commands */
void removeatchar(str1)
char *str1;
{
char *str2;
str2 = str1; /* str2 points to the address of str1 */
if (str1[0] == '@')
{
++str1;
movmem(str1, str2, strlen(str1) + 1); /* Remove character '@' */
}
}
/* Convert all batch commands and write to buffer */
void transfer(flnm)
char *flnm;
{
unsigned int i, cmnum = 0;
char strlen1, ch, str1[256], *p, *cm;
FILE *txtfl;
if ((txtfl = fopen(flnm, "r")) == NULL) {
printf("Input file is not found !");
exit(0);
}
p = buffer; /* p points to the start address of the buffer */
for (i = 0; i < 81; ++i) *p++ = bat2comhead; /* Write command file header to buffer */
ch = 0x0d; /* Carriage return character */
totallength = 83; /* Total length of command file: header plus 2 bytes */
for (i = 0; i < 2; i++) p++; /* Move p pointer forward by 2 bytes */
do {
fgets(str1, 256, txtfl); /* Read a command from the batch file */
removespace(str1); /* Remove leading spaces */
removedouble(str1); /* Replace "%%" with "%" */
removeatchar(str1); /* Remove "@" character */
strlen1 = strlen(str1); /* Calculate command string length */
if (strlen1 > 2) { /* Is it a valid command? */
++cmnum; /* Increment command count */
*p++ = strlen1 - 1; /* Write command length to buffer */
++totallength; /* Increment total length of command file */
for (i = 0; i < strlen1 - 1; ++i) *p++ = str1; /* Write command string to buffer */
totallength += strlen1 - 1; /* Add command string length to total length of command file */
*p++ = ch; /* Write a carriage return character */
++totallength; /* Increment total length of command file */
strcpy(str1, ""); /* Clear command string content */
}
} while (!feof(txtfl)); /* Is it the end of the file? */
cm = &cmnum; /* Write command count */
buffer[81] = *cm++;
buffer[80] = *cm;
fclose(txtfl); /* Close file */
}
/* Write the constructed buffer content to the command file */
void writeBAT2COM(flnm)
char *flnm;
{
FILE *bfl;
unsigned int i;
char drive[3], dir[65], name[9], ext[5];
fnsplit(flnm, drive, dir, name, ext);
strcat(name, ".com");
if ((bfl = fopen(name, "wb")) == NULL) { /* Create and open a binary file */
printf("File does not opened !");
exit(1);
}
fwrite(buffer, totallength, 1, bfl); /* Write buffer content */
/* for (i=0;i<totallength;++i) fprintf(bfl,"%c",buffer); */
/* for (i=0;i<totallength;++i) fputc(buffer,bfl); */
fclose(bfl);
}
/* Help function */
void help()
{
printf("\nSyntex : B2C Batch_filename");
exit(0);
}
/* Main function */
main(argc, argv)
int argc;
char *argv[];
{
char flnm[80];
printf("\nB2C Version 1.0 Copyright (c) 1993,94 Dong Zhanshan");
switch (argc) {
case 1 : help();break;
case 2 : strcpy(flnm, argv[1]);break;
default: help();
}
transfer(flnm);
writeBAT2COM(flnm);
}
Everyone, take a look, is it