home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 3 Comm
/
03-Comm.zip
/
RDQWKSRC.ZIP
/
QLIB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-15
|
14KB
|
423 lines
/*
╔══════════════════════════════════════════════════════════════════════════╗
║ ║
║ Test routines to read, in Qmail Format - R.Cougnenc 90 - ║
║ ║
║ Public Domain ║
║ ║
╟──────────────────────────────────────────────────────────────────────────╢
║ ║
║ These modules correspond to different tests at reading in ║
║ Qmail Format. They were never planned to be released as a ║
║ package. ║
║ ║
║ Source code is not beautified or optimised as they are work ║
║ files and are distributed as is. ║
║ ║
║ The program, even at it's rustic stage is fully fonctional, ║
║ as I use it regularly... ║
║ ║
║ Feel free to use any or all of these modules, if you can sort ║
║ it out! :-) ║
║ ║
╚══════════════════════════════════════════════════════════════════════════╝
*/
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef __TURBOC__
#include <alloc.h>
#else
#include <malloc.h>
#endif
#include "reader.h"
#include "readlib.h"
#include "qmail.h"
#include "ansi.h"
struct MsgHeaderType Qmail ;
#define call goto /* :-)) */
/* Variables used to read Control.dat */
int LastConf = 0 ;
int Security = 0 ;
char BoardName[50],
UserName [50],
First [50],
City [50],
HomePhone[50],
DataPhone[50];
char ConfName[MAXCONF][NAMCONF];
int dd,
mm,
year,
hh,
mn,
sec;
int exp_dd,
exp_mm,
exp_yy;
long PrevOffset ;
int conf ;
int count;
int fd, fs ;
/*
Reads the file CONTROL.DAT pointed by Pathname.
This function is not finished but works well enought.
*/
int ReadControl(Path)
char *Path;
{
FILE *fp;
int i,numconf;
char tmp[MAXPATHS];
sprintf(tmp,"%s\\%s",Path,CNTRL_FILE);
if( (fp = fopen( tmp,"r") ) == NULL ) call bad_end ;
/*--- Get Board Name -----*/
if( fget(BoardName,50,fp) == NULL ) call bad_end ;
/*--- Skip 5 lines...-----*/
for (i = 0 ; i < 5 ; i++ )
if( fget(tmp, 126,fp) == NULL ) call bad_end ;
/* Get time & date */
sscanf(tmp,"%d-%d-%d,%d:%d:%d",&mm,&dd,&year,&hh,&mn,&sec);
/*--- Get User Name -----*/
if( fget(UserName,50,fp) == NULL ) call bad_end ;
/*--- Skip 3 lines...-----*/
for (i = 0 ; i < 3 ; i++ )
if( fget(tmp, 126,fp) == NULL ) call bad_end ;
/*--- Get Last conf #-----*/
if (( fget(tmp,126,fp ) == NULL) ) call bad_end ;
LastConf = atoi(tmp);
if(! LastConf || LastConf > MAXCONF ) call bad_end ;
/*--- Get Conf Names -----*/
for ( i = 0 ; i < MAXCONF ; i ++ )
{
if ( fget(tmp,126,fp) == NULL ) call bad_end ;
numconf = atoi(tmp);
if( numconf < 0 || numconf > LastConf ) call bad_end ;
if ( fget(ConfName[numconf],15,fp) == NULL ) call bad_end ;
if (numconf == LastConf ) break;
}
#ifdef MARKMAIL
/* Look for the 2 occurence of user Name */
while( 1 )
{
if( fget (tmp, 126, fp ) == NULL ) call bad_end ;
if(! strncmp(tmp,UserName ) ) break;
}
/* And get First Name. */
if( fget(First,50,fp) == NULL ) call bad_end ;
if( fget(City,50,fp) == NULL ) call bad_end ;
if( fget(HomePhone,50,fp) == NULL ) call bad_end ;
if( fget(DataPhone,50,fp) == NULL ) call bad_end ;
if( fget(tmp,50,fp) == NULL ) call bad_end ;
Security = atoi(tmp);
if( fget(tmp,50,fp) == NULL ) call bad_end ;
sscanf(tmp,"%d-%d-%d",&exp_mm,&exp_dd,&exp_yy);
exp_yy +=1900;
#endif
fclose(fp);
return ( OK );
bad_end:
fclose(fp);
return( ERROR );
}
/*
Scans the file MESSAGE.DAT, and calls WriteIndex for writing
index files.
*/
int MkIndex(SrcDir,DestDir)
char *SrcDir,*DestDir ;
{
FILE *fs,*fd,*fx;
unsigned char *ptr ;
int conf = 0 ;
int count = 0 ;
int oldcount = 0 ;
register int i ;
int Head = sizeof( struct MsgHeaderType );
unsigned MaxChars,
NbBlocs ;
char Src[MAXPATHS]; /* Source file Message.DAT */
char Dst[MAXPATHS]; /* Conference data file */
char Idx[MAXPATHS]; /* Conference index file */
long last ;
if( ReadControl(SrcDir) ) /* Load new CONTROL.DAT file. */
{
printf("%s !\n",txt[14]); /* "error in control.dat" */
return ( ERROR );
}
/* Update Control.dat in bbs dir */
sprintf(Src,"%s/%s",SrcDir ,CNTRL_FILE);
sprintf(Dst,"%s/%s",DestDir,CNTRL_FILE);
if( fcopy(Src,Dst ))
return( ERROR );
/* Update Newfiles.dat in bbs dir */
sprintf(Src,"%s/%s",SrcDir,NEWFILES);
if(! access(Src,0) )
{
sprintf(Dst,"%s/%s",DestDir,NEWFILES);
if( fcopy(Src,Dst ))
return( ERROR );
}
/* Update Welcome file in bbs dir */
sprintf(Src,"%s/%s",SrcDir,WELCOME); /* try ascii mode */
if(! access(Src,0) )
{
sprintf(Dst,"%s/%s",DestDir,WELCOME);
if( fcopy(Src,Dst ))
return( ERROR );
}
else
sprintf(Src,"%s/%s",SrcDir,WELCOMEG); /* or ansi mode */
if(! access(Src,0) )
{
sprintf(Dst,"%s/%s",DestDir,WELCOMEG);
if( fcopy(Src,Dst ))
return( ERROR );
}
/* Update News file in bbs dir */
sprintf(Src,"%s/%s",SrcDir,NEWS); /* try ascii mode */
if(! access(Src,0) )
{
sprintf(Dst,"%s/%s",DestDir,NEWS);
if( fcopy(Src,Dst ))
return( ERROR );
}
else
sprintf(Src,"%s/%s",SrcDir,NEWSG); /* or ansi mode */
if(! access(Src,0) )
{
sprintf(Dst,"%s/%s",DestDir,NEWSG);
if( fcopy(Src,Dst ))
return( ERROR );
}
yellow();
printf("%s...\n",txt[71]); /* "Adding msgs and creating indexes" */
cyan();
sprintf(Src,"%s/%s",SrcDir,MSG_FILE ); /* Open MESSAGES.DAT */
if(( fs = fopen(Src,"rb")) == NULL )
{
printf("%s %s...\n",txt[51],Src); /* "unable to open file" */
return (ERROR);
}
if( setvbuf(fs,NULL,_IOFBF,VBUF ) )
printf("%sMkIndex() [%s]\n",txt[ 94 ],Src); /* "Setvbuf failed..." */
/* Skip First Header */
if(! fread((char *) &Qmail.Status,1, Head ,fs) )
{
printf("%s %s\n",txt[58],Src ); /* "error reading file" */
fclose(fs);
return (ERROR );
}
sprintf(Dst,"%s/%d.cnf",DestDir,conf ); /*--< Open Conference file >--*/
if(( fd = fopen(Dst,"a+b")) == NULL )
{
printf("%s %s...\n",txt[51],Dst); /* "unable to open file" */
fclose(fs);
return (ERROR);
}
if( setvbuf(fd,NULL,_IOFBF,VBUF ) )
printf("%sMkIndex() [%s]\n",txt[ 94 ],Dst); /* "Setvbuf failed..." */
fseek(fd,0L,SEEK_END ); /* Go to end of file... */
last = (long) ftell(fd) ; /* Take size of existing messages */
/* printf("Last = %ld\n",last );*/
sprintf(Idx,"%s/%d.idx",DestDir,conf ); /*--< Open Conference Index >--*/
if( access(Idx,0) ) /* Create if not exist */
{
fx = fopen(Idx,"w");
fclose( fx );
}
if(( fx = fopen(Idx,"r+b")) == NULL )
{
printf("%s %s...\n",txt[ 51 ],Idx); /* "unable to open file" */
fclose(fs);
fclose(fd);
return (ERROR);
}
if( setvbuf(fx,NULL,_IOFBF,VBUF ) )
printf("%sMkIndex() [%s]\n",txt[ 94 ],Idx); /* "Setvbuf failed..." */
fseek(fx,0L,SEEK_END );
/* printf("Offset Index = %ld\n",ftell(fx) ); */
count = (int) ( ftell(fx) / sizeof( struct MyIndex ));
oldcount = count ;
/*----------------------------*/
while(1)
{
if( ! fread (&Qmail.Status,1, Head,fs) )
break; /* End of file */
if(Qmail.MsgTime[2] != 58 )
break; /* Dernier entete vide cree par les doors qmail probablement.*/
/* Faudrait tester un peu mieux que ça... enfin... */
memcpy((char *) rbuf, (char *) &Qmail.Status, Head ) ;
printf("%d : %d\r",conf,count - oldcount);
if( (int) Qmail.Conference != conf ) /* Another conference ? */
{
fclose(fx); /* Close index file */
fclose(fd); /* Close Conference file */
printf("Conference %d \t[ %10s ] : %2d New %s\n",
conf,ConfName[conf],count - oldcount ,
(count - oldcount) > 1 ? "Messages" : "Message");
count = 0 ;
oldcount = 0 ;
conf = Qmail.Conference ;
/* Open new files */
sprintf(Dst,"%s/%d.cnf",DestDir,conf ); /*--< Open Conference file >--*/
if(( fd = fopen(Dst,"a+b")) == NULL )
{
printf("%s %s...\n",txt[ 51 ],Dst); /* "unable to open file" */
fclose(fs);
return (ERROR);
}
if( setvbuf(fd,NULL,_IOFBF,VBUF ) )
printf("%sMkIndex() [%s]\n",txt[ 94 ],Dst); /* "Setvbuf failed..." */
fseek(fd,0L,SEEK_END ); /* Go to end of file... */
last = (long) ftell(fd) ; /* Take size of existing messages */
/* printf("Last = %ld\n",last ); */
sprintf(Idx,"%s/%d.idx",DestDir,conf ); /*--< Open Conference Index >--*/
if( access(Idx,0) ) /* Create if not exist */
{
fx = fopen(Idx,"w");
fclose( fx );
}
if(( fx = fopen(Idx,"r+b")) == NULL )
{
printf("%s %s...\n",txt[ 51 ],Idx); /* "unable to open file" */
fclose(fs);
fclose(fd);
return (ERROR);
}
if( setvbuf(fx,NULL,_IOFBF,VBUF ) )
printf("%sMkIndex() [%s]\n",txt[ 94 ],Idx); /* "Setvbuf failed..." */
fseek(fx,0L,SEEK_END );
/* printf("Offset Index = %ld\n",ftell(fx));*/
count = (int) ( ftell(fx) / sizeof( struct MyIndex ));
oldcount = count ;
}
NbBlocs = atoi(Qmail.SizeMsg) - 1;
MaxChars = NbBlocs * 128 ;
if( MaxChars > MYBUF )
{
printf("%s [ > %u bytes ]\n",txt[72],MYBUF); /* "msg too big" */
return (ERROR );
}
WriteIndex(fx,count,MaxChars,last); /* Write Index Structure */
count ++ ;
if(! fread((char *)( rbuf + Head) ,1,MaxChars,fs) ) /* Read the message..... */
{
printf("%s %s\n",txt[ 58 ],Src); /* "error reading file " */
fclose(fs);
fclose(fd);
fclose(fx);
return( ERROR );
}
ptr = (unsigned char * ) (rbuf + Head );
for( i = 0 ; i < MaxChars ; i ++ ) /* Translate to normal Line Feed */
{
if ( *ptr == 227 )
*ptr = '\n';
ptr++;
}
while( *(--ptr) == 0x20 ) *ptr = 0 ; /* delete padding spaces */
if(! fwrite(rbuf,1,MaxChars + Head,fd) ) /* And copy it. */
{
printf("%s %s\n",txt[ 73 ],Dst); /* "error writing file " */
fclose(fs);
fclose(fd);
fclose(fx);
return( ERROR );
}
last += (long ) MaxChars + Head ; /* INC counter... */
}
printf("Conference %d \t[ %10s ] : %2d New %s\n",
conf,ConfName[conf],count - oldcount,
(count - oldcount) > 1 ? "Messages" : "Message");
printf("\n");
fclose(fs);
fclose(fd);
fclose(fx);
}
/*
Add an index struct to the index file.
*/
void WriteIndex(fx,count,Size,Offset)
FILE *fx;
int count ;
int Size ;
long Offset ;
{
Index.MsgNum = count ;
Index.Size = Size ;
Index.Offset = Offset ;
Index.LastRead = 0 ;
if(! fwrite(&Index.LastRead,1,sizeof(struct MyIndex),fx ) )
{
printf("%s !\n",txt[74]); /* "error writing index file "*/
}
}