I have a DOS system and want to use its Ctrl+Break interrupt, and do file operations in its interrupt service routine, that is, read one file and write it to another file. But when operating on files, it always hangs, and I don't know why. I hope you experts won't hesitate to teach me! QQ:36014789
The program is as follows
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys b.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <IO.H>
#ifdef __cplusplus
#define __ARGU ...
#else
#define __ARGU
#endif
void interrupt (*oldhandler)(void); /*function declaration*/
void interrupt newint(__ARGU); /*function declaration*/
void install (void interrupt (*fadd)(__ARGU), int num);
main()
{
install (newint,0x1b); /*Ctrl+Break interrupt number: 1BH*/
keep(0,_SS+(_SP/16)-_psp); /*TSR program*/
return 0;
}
void interrupt newint(__ARGU)
{ int fd1=-1; //file number for reading, returned by the open function;
int fd2=-1; //file number for writing, returned by the open function;
int read_res=-1; //return value of the read function, -1 means error;
int write_res=-1; //return value of the write function, -1 means error;
char buf[10]; //buffer; //file number for reading, returned by the open function;
disable();
fd1=open("c:\\fr.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
fd2=open("c:\\fw.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
if(fd1==-1||fd2==-1)
puts("Open file failure!\n");
else
{
puts("Open files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
lseek(fd1,0,SEEK_SET);//move the file position pointer to the specified location;
//fread cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
read_res=read(fd1,buf,COUNT); //file reading function;
if(read_res==-1) //if file reading fails;
puts("Read file failure!\n");
else
{
puts("Read files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
lseek(fd2,0,SEEK_SET);//move the file position pointer to the specified location;
//fwrite cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
write_res=write(fd2,buf,COUNT); //file writing function;
if(write_res==-1) //if file writing fails;
puts("Write file failure!\n");
else
puts("Write files success!\n");
}
close(fd1); //close file;
close(fd2); //close file;
}
enable();
}
void install (void interrupt (*fadd)(__ARGU), int num)
{
disable(); //disable interrupts;
setvect(num,fadd); /*set interrupt*/
enable(); //enable interrupts;
}
The program is as follows
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys b.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <IO.H>
#ifdef __cplusplus
#define __ARGU ...
#else
#define __ARGU
#endif
void interrupt (*oldhandler)(void); /*function declaration*/
void interrupt newint(__ARGU); /*function declaration*/
void install (void interrupt (*fadd)(__ARGU), int num);
main()
{
install (newint,0x1b); /*Ctrl+Break interrupt number: 1BH*/
keep(0,_SS+(_SP/16)-_psp); /*TSR program*/
return 0;
}
void interrupt newint(__ARGU)
{ int fd1=-1; //file number for reading, returned by the open function;
int fd2=-1; //file number for writing, returned by the open function;
int read_res=-1; //return value of the read function, -1 means error;
int write_res=-1; //return value of the write function, -1 means error;
char buf[10]; //buffer; //file number for reading, returned by the open function;
disable();
fd1=open("c:\\fr.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
fd2=open("c:\\fw.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
if(fd1==-1||fd2==-1)
puts("Open file failure!\n");
else
{
puts("Open files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
lseek(fd1,0,SEEK_SET);//move the file position pointer to the specified location;
//fread cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
read_res=read(fd1,buf,COUNT); //file reading function;
if(read_res==-1) //if file reading fails;
puts("Read file failure!\n");
else
{
puts("Read files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
lseek(fd2,0,SEEK_SET);//move the file position pointer to the specified location;
//fwrite cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
write_res=write(fd2,buf,COUNT); //file writing function;
if(write_res==-1) //if file writing fails;
puts("Write file failure!\n");
else
puts("Write files success!\n");
}
close(fd1); //close file;
close(fd2); //close file;
}
enable();
}
void install (void interrupt (*fadd)(__ARGU), int num)
{
disable(); //disable interrupts;
setvect(num,fadd); /*set interrupt*/
enable(); //enable interrupts;
}
