home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 January
/
VPR0101A.BIN
/
OLS
/
TAR32053
/
tar32053.exe
/
SRC
/
IOCTRL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-23
|
4KB
|
215 lines
/*ioctrl.c*/
#include "defconf.h"
#include "ioctrl.h"
#ifdef DLL
/* #define IO_TEST 2 */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define PRINT_BUF 10000
char dll_output_buffer_test[10]="";
int dll_output_buflen_test=10;
static char *dll_output_buffer;
static int dll_output_buflen=-1;
static char *dll_output_ptr;
/* for test play */
void ioctrl_output_init_test(void)
{
if(dll_output_buflen==-1 || dll_output_buffer==dll_output_buffer_test){
dll_output_buffer=dll_output_buffer_test;
dll_output_buflen=dll_output_buflen_test;
dll_output_ptr=dll_output_buffer;
}
}
void ioctrl_output_init(char *buf,int buflen)
{
dll_output_buffer=buf;
dll_output_buflen=buflen;
dll_output_ptr=dll_output_buffer;
}
void ioctrl_output_end(void)
{
#if IO_TEST >=2
orig_printf("#########ioctrl_output_end(begin)########%s###(end)########\n",dll_output_buffer);
#endif
}
int ioctrl_output_string(const char *string)
{
if(dll_output_ptr>=dll_output_buffer+dll_output_buflen){
/* nothing */
return -1;
}
while(dll_output_ptr<dll_output_buffer+dll_output_buflen-1 && *string!='\0'){
*dll_output_ptr=*string;
dll_output_ptr++;
string++;
}
*dll_output_ptr='\0';
return 0;
}
int ioctrl_output_n_string(const char *string,long n)
{
int c=0;
if(dll_output_ptr>=dll_output_buffer+dll_output_buflen){
/* nothing */
return -1;
}
while(dll_output_ptr<dll_output_buffer+dll_output_buflen-1 && c<n /* *string!='\0' */){
*dll_output_ptr=*string;
dll_output_ptr++;
string++;
c++;
}
*dll_output_ptr='\0';
return c;
}
int ioctrl_output_char(int c)
{
if(dll_output_ptr>=dll_output_buffer+dll_output_buflen){
return -1;
}
if(dll_output_ptr<dll_output_buffer+dll_output_buflen-1){
*dll_output_ptr=(char)c;
dll_output_ptr++;
/* string++; */
}
*dll_output_ptr='\0';
return 0;
}
int ioctrl_vprintf(const char *format,va_list arg)
{
char tmpbuf[PRINT_BUF];
int ret=0;
vsprintf(tmpbuf,format,arg);
#ifdef IO_TEST
ret=orig_printf("PRINTF:%s",tmpbuf);
#endif
#if !defined(IO_TEST) || IO_TEST>=2
ret=ioctrl_output_string(tmpbuf);
#endif
return ret;
}
int ioctrl_printf(const char *format,...)
{
va_list arg;
int ret;
va_start(arg,format);
ret=ioctrl_vprintf(format,arg);
va_end(arg);
return ret;
}
int ioctrl_fprintf(FILE *fp,const char *format,...)
{
va_list arg;
int ret;
va_start(arg,format);
if(fp==stdout || fp==stderr){
ret=ioctrl_vprintf(format,arg);
}else{
ret=vfprintf(fp,format,arg);
}
va_end(arg);
return ret;
}
int ioctrl_puts(const char *string)
{
int ret;
#ifdef IO_TEST
orig_printf("PUTS:");
ret=orig_puts(string);
#endif
#if !defined(IO_TEST) || IO_TEST>=2
ret=ioctrl_output_string(string);
ret=ioctrl_output_char('\n');
#endif
return ret;
}
int ioctrl_fputs(const char *string,FILE *fp)
{
int ret;
if(fp==stdout || fp==stderr){
ret=ioctrl_puts(string);
}else{
ret=orig_fputs(string,fp);
}
return ret;
}
int ioctrl_putchar(int c)
{
int ret;
/* assmume 0<=c<=255...*/
#ifdef IO_TEST
orig_printf("PUTCHAR:");
ret=orig_putchar(c);
#endif
#if !defined(IO_TEST) || IO_TEST>=2
ret=ioctrl_output_char(c);
#endif
return ret;
}
int ioctrl_putc(int c,FILE *fp)
{
int ret;
if(fp==stdout || fp==stderr){
ret=ioctrl_putchar(c);
}else{
ret=orig_putc(c,fp);
}
return ret;
}
void ioctrl_perror( const char *string )
{
if(0<=errno && errno<_sys_nerr){
fprintf(stderr,"%s: %s\n",string,_sys_errlist[errno]);
}
}
size_t ioctrl_fwrite( const void*buffer, size_t size, size_t count, FILE *stream )
{
if(stream==stdout || stream==stderr){
int ret;
#ifdef IO_TEST
orig_printf("FWRITE:");
ret=orig_fwrite(buffer,size,count,stdout);
#endif
#if !defined(IO_TEST) || IO_TEST>=2
ret=ioctrl_output_n_string(buffer,size*count);
#endif
return ret;
}else{
return orig_fwrite(buffer,size,count,stream);
}
}
int ioctrl_write( int handle, const void *buffer, unsigned int count )
{
if(handle==1 || handle==2){
int ret;
#ifdef IO_TEST
orig_printf("WRITE:");
ret=orig_write(1,buffer,count);
#endif
#if !defined(IO_TEST) || IO_TEST>=2
ret=ioctrl_output_n_string(buffer,count);
#endif
return ret;
}else{
return orig_write(handle,buffer,count);
}
}
#endif /* DLL */