home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 3 Comm
/
03-Comm.zip
/
RDQWKSRC.ZIP
/
READLIB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-15
|
19KB
|
873 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 <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "reader.h"
#include "qmail.h"
#include "system.h"
#include "ansi.h"
#include "readlib.h"
/*
Creates the Home Pathname.
Looks for the environment var :"READER" for systems which does not
pass the full pathname in argv[0];
If this variable is not found, assumes argv[0] contains the full
pathname and build the string from it.
*/
void MakeHomePath(path)
char *path;
{
int i;
char *pt;
if(( pt = (char *) getenv("READER")) == NULL )
strcpy(HomePath,path);
else
{
strcpy(HomePath,pt);
PrePath(HomePath);
}
i = strlen(HomePath);
while(i)
{
if(HomePath[i] == SEP || HomePath[i] == ':'||
HomePath[i] == SEPDOS ) break ;
HomePath[i] = 0;
i--;
}
pt = HomePath ;
while(*pt)
{
if( *pt == SEPDOS )
*pt = SEP ;
pt++;
}
}
/*----------------------------------------------------------------------*/
/*
Read the configuration file.
*/
int ReadConfig()
{
char Path[MAXPATHS],
Line[MAXPATHS],
var[50],
sign[50],
value[50],
value2[50];
int count = 0;
FILE *cnf;
printf("%s...\n",txt[53]); /* "REading config file" */
strcpy(Path,HomePath);
strcat(Path,CONFIG_FILE);
if(( cnf = fopen(Path,"r") ) == NULL )
{
printf("\n%s %s ! \n",txt[ 52], Path ); /* "Unable to open config file" */
return( ERROR );
}
while( fgets(Line,255,cnf) )
{
count++ ;
if(Line[0] == '#' || strlen(Line) < 2 )
continue;
sscanf(Line,"%s %s %s %s",var,sign,value,value2 );
if( affect(var,sign,value,value2) )
{
/* "error in config file in line..." */
printf("%s %s %s %d\n",txt[ 54 ],Path,txt[ 55],count);
printf("%s %d : %s\n", txt[ 56],count,Line);
return ( ERROR );
}
}
return( OK );
}
/*
Affects known configuration variables to their global values.
Returns ERROR on an invalid line.
Ignores unknown variables.
*/
int affect(var,sign,value,value2)
char *var,*sign,*value,*value2;
{
strlwr(var);
if(strcmp(sign,"=") )
return( ERROR );
if( ! strcmp (var, "editor") )
{
strcpy(Editor,value);
return( OK );
}
if( ! strcmp (var, "mail") )
{
strcpy(MailPath,value);
PrePath(MailPath);
return( OK );
}
if( ! strcmp (var, "reply") )
{
strcpy(ReplyPath,value);
PrePath(ReplyPath);
return( OK );
}
if( ! strcmp (var, "archiver") )
{
strcpy(Archiver,value);
return( OK );
}
if( ! strcmp (var, "unarchiver") )
{
strcpy(UnArchiver,value);
return( OK );
}
if( ! strcmp (var, "ansi") ) /* Starting in ansi mode or not... */
{
strlwr(value);
ansi = strcmp(value,"on") ? 0 : 1 ;
return( OK );
}
if( ! strcmp (var, "user") ) /* Get User First & Last Name */
{
strupr(value);
strupr(value2);
sprintf(UserName,"%s %s",value,value2);
return( OK );
}
return( OK );
}
/*
Adds the last separator to a pathname.
Must be room enough for that in the string !
*/
void PrePath( path )
char *path;
{
int l ;
l = strlen(path);
if( path [ l - 1 ] != SEP && path [ l - 1 ] != SEPDOS );
path[ l ] = SEP ;
path[ l+1] = EOS ;
}
/*
Builts the full work directory pathname, and
Deletes all files in the work directory.
( System dependants functions are in system.c )
*/
void Clean()
{
strcpy(WorkPath,HomePath);
strcat(WorkPath,WORK_DIR );
if( access(WorkPath,0) )
mkdir(WorkPath);
else
{
printf("%s %s...\n",txt[ 57],WorkPath); /* "Cleaning" */
Erase(WorkPath);
}
}
/*
FGET : Get a string like fgets, without any space or lf on the right.
*/
char *fget (s,n,fp)
char *s;
int n;
FILE *fp;
{
char *p ;
p = fgets(s,n,fp);
if( s[strlen(s) -1] == '\n' )
s[strlen(s) -1] = '\0' ;
while( s[strlen(s) -1] == ' ' )
s[strlen(s) -1] = '\0' ;
return( p ) ;
}
/*
Get conference, open global files etc...
*/
int GetConf(num)
int num;
{
char tmp[MAXPATHS];
struct stat stats;
sprintf(tmp,"%s%s/%d.idx",HomePath,CurBoard,num);
if( FilesOpen )
{
fclose(fidx);
fclose(fmsg);
FilesOpen = 0 ;
}
if( access(tmp)) /* File does not exists, no mail. */
{
{
red();
printf("%s %s\n",txt[ 44 ],tmp); /* "No mail on conf" */
return(ERROR);
}
}
else /* Ok file exists, read it . */
{
if(( fidx = fopen(tmp,"r+b") )== NULL )
{
printf("%s %s\n",txt[49],tmp); /* "unable to read file" */
return(ERROR);
}
if( setvbuf(fidx,NULL,_IOFBF,1024 ) )
printf("%sGetConf() [index]\n",txt[ 94 ]); /* "Setvbuf failed..." */
}
CurConf = num ;
stat(tmp,&stats );
TotMsg = (int) (stats.st_size / sizeof(struct MyIndex) );
sprintf(tmp,"%s%s/%d.cnf",HomePath,CurBoard,CurConf);
if ( (fmsg = fopen(tmp,"rb") ) == NULL )
{
printf("%s %s ! \n",txt[ 58 ],tmp ); /* "Error Reading file" */
return ERROR ;
}
if( setvbuf(fmsg,NULL,_IOFBF,VBUF ) )
printf("%sGetConf() [data]\n",txt[ 94 ]); /* "Setvbuf failed..." */
/* Go to Last message read */
fread((char *)&Index.LastRead,1,sizeof(struct MyIndex),fidx);
fseek(fidx,(long) Index.LastRead * (long) sizeof(struct MyIndex), 0 );
FilesOpen = 1 ;
return ( OK );
}
/* -----------------------------------------------------------------
Copy the current msg in file tmpname with quotes.
*/
void QuoteMsg(tmpname)
char *tmpname ;
{
FILE *fq;
struct MsgHeaderType *Msg ;
int i;
int col ;
byte *ptr ;
char quote[10];
char tmp1[MAXPATHS];
char tmp2[50];
char tmp[MAXPATHS];
char ch ;
Msg = ( struct MsgHeaderType *) rbuf ;
tmp2[0]=0;
if( ! strncmp( Msg->Author,"SYSOP",5) )
strcpy (quote,"Sys > ");
else
{
sscanf(Msg->Author,"%s %s",tmp1,tmp2); /* First & Last Name */
sprintf(quote,"%c%c > ",tmp1[0],tmp2[0] ? tmp2[0] : 32 ); /* Initials.. */
}
printf("%s %s ...\n",txt[59],quote ); /* "Quoting message with " */
if( ( fq = fopen(tmpname,"w")) == NULL )
{
printf("%s %s !\n",txt[50],tmpname ); /* "Unable to create file" */
return ;
}
if( setvbuf(fq,NULL,_IOFBF,VBUF ) )
printf("%sQuoteMsg() [temp file]\n",txt[ 94 ]); /* "Setvbuf failed..." */
RefMessage(fq,Msg); /* Dear User, in a message to... */
ptr = (byte *) (rbuf + sizeof(struct MsgHeaderType ) );
i = 0 ;
fprintf(fq,"%s",quote);
col = strlen(quote);
while (i < Index.Size)
{
if( *ptr == '\n' )
{
fprintf(fq,"\n%s",quote);
col = strlen(quote);
}
else
{
if( col < 78 ) /* Trunc quoted lines to 78 cols */
fprintf(fq,"%c",*ptr) ;
col ++ ;
}
i++;
ptr++;
}
fclose(fq);
}
/*
Réalise l'entête de la lettre...
*/
#define PUBLIC 1
#define MYSELF 2
void RefMessage(fp,Msg)
FILE *fp;
struct MsgHeaderType *Msg ;
{
char Destin[ 30 ];
char Ref [ 30 ];
int mon,day,mod ;
if( ! HeadLetter )
return ;
sscanf(Msg->MsgDate,"%d-%d",&mon,&day);
Initials(Msg->Author,Destin,25);
Initials(Msg->ForWho,Ref, 25 );
if( ! strncmp( Msg->ForWho,"ALL",3) )
mod = PUBLIC ;
else
if( ! strncmp( Msg->ForWho,UserName,strlen(UserName) ) )
mod = MYSELF ;
else
mod = 0 ;
fprintf(fp,"%s %s,\n",txt[96],Destin); /* "Cher" */
/* "Dear" */
fprintf(fp,"%s",txt[97]); /* "Dans un msg du" */
fprintf(fp," %d %s, ", day, Months[ mon - 1 ] ); /* "In a message of " */
switch( mod )
{
case PUBLIC :
fprintf(fp,"%s :\n\n",txt[98]); /* "vous écrivez" */
break ; /* "you wrote" */
case MYSELF :
fprintf(fp,"%s :\n\n",txt[99]); /* "vous m'écrivez" */
break ; /* "wrote me" */
default :
fprintf(fp,"%s",txt[ 100 ]); /* "destiné à" */
fprintf(fp," %s, ",Ref); /* "addressed to" */
fprintf(fp,"%s :\n\n",txt[98]); /* "vous écrivez" */
} /* "you wrote */
}
/*
F Formatte le nom en minuscules, avec les initiales Upcase,
F dans dst, null terminated.
E Format name in lowercase with initials in Uppercase in dst,
E null terminated
*/
void Initials(str,dst,len)
unsigned char *str,*dst ;
int len ;
{
int m,i;
m = 1 ;
for( i = 0 ; i < len ; i++ )
{
*dst = *str ;
if( *dst == '.' || *dst == '-' || *dst == ' ' )
m = 1 ; /* Next must be Upcase */
else
{
*dst = m ? toupper( *dst ) : tolower( *dst );
m = 0 ;
}
str++;
dst++ ;
}
*dst = 0 ; /* build a null-terminated string */
dst --;
while ( *dst == 32 )
{
*dst = 0 ;
dst--;
}
}
/*
more : prompts "more ?" and wait for yes, oui, ja .
returns 1 for yes, 0 for no.
'def ' manages the CR default answer.
*/
int more(def)
int def;
{
char tmp[10];
for(;;)
{
high(); yellow();
printf(" %s ? : ",txt[ 60 ]);
fget(tmp,5,stdin);
switch( (int) tmp[0])
{
case 'o' :
case 'y' :
case 'O' :
case 'Y' :
case 'j' :
case 'J' :
clear();
up(1);
printf("\r \r");
return ( 1 );
case 'N' :
case 'n' :
clear();
return( 0 );
case 0 : /* CR */
if( def == YES )
{
clear();
up(1);
printf("\r \r");
return ( 1 );
}
else
{
clear();
return( 0 );
}
}
}
}
/*
YesNo : prompts "Y/N : " and wait for yes, oui, ja .
returns 1 for yes, 0 for no.
*/
int YesNo(def)
int def;
{
char tmp[10];
for(;;)
{
printf(" %s : ",txt[ 61 ]);
fget(tmp,5,stdin);
switch( (int) tmp[0])
{
case 'o' :
case 'y' :
case 'O' :
case 'Y' :
case 'j' :
case 'J' :
printf("\r");
return ( 1 );
case 'N' :
case 'n' :
printf ("\r");
return( 0 );
case 0 : /* CR */
if( def == YES )
return ( 1 );
else
return( 0 );
}
}
}
void PackReply()
{
char RepFile[MAXPATHS];
char MsgFile[MAXPATHS];
char Command[MAXPATHS];
sprintf(RepFile,"%s%s.rep",ReplyPath,CurBoard);
if( !access(RepFile) )
{
red(); high();
/* "Warning ! file already exist " "delete it ?" */
printf("%s %s %s %s...!\n",txt[2],txt[62],RepFile,txt[63]);
printf(" %s ",txt[ 64 ]);
if(YesNo(YES))
unlink(RepFile);
}
sprintf(MsgFile,"%s/%s.msg",WorkPath,CurBoard);
sprintf(Command,"%s %s %s",Archiver,RepFile,MsgFile);
yellow();high();
printf("%s %s\n",txt[ 65 ],RepFile ); /* "packing replies in" */
green();
system(Command);
ReplyExist = 0 ;
unlink(MsgFile);
}
/*
UPDATECONF: Write only if necessary the last read pointer in the
first index structure of the file.
*/
void UpdateConf()
{
struct MyIndex Idx ;
if( fseek( fidx,0L,SEEK_SET ) )
{
/* printf("Seek error...\n"); */
return ;
}
fread((char *)&Idx.LastRead,1,sizeof(struct MyIndex),fidx);
if( Idx.LastRead < Index.MsgNum )
{
Idx.LastRead = Index.MsgNum ;
fseek( fidx,0L,SEEK_SET );
fwrite((char *)&Idx.LastRead,1,sizeof(struct MyIndex),fidx);
printf("%s.\n",txt[66]); /* "Last read pointer updated" */
}
}
/*------------------------------------------------------------------------*/
/*
NPRINT: Print a string not null-terminated on stdout.
*/
void nprint(str,len)
byte *str;
int len;
{
while(len--) putchar(*str++);
}
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/*
NFPRINT: Print a string not null-terminated in file fp
*/
void nfprint(fp,str,len)
FILE *fp;
byte *str;
int len;
{
while(len--) fputc(*str++,fp);
}
/*------------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* STR2MEM :
copies a string without the null character in order to fill
correctly the Qmail header.
*/
void str2mem(mem,str)
char *mem, *str;
{
while(*str)
{
*mem = *str;
str++;
mem++;
}
}
/*----------------------------------------------------------------------*/
/*
scpy : copies len bytes to src, deleting left spaces.
*/
void scpy(dest,src,len)
char *dest,*src;
int len;
{
register int i;
for( i = 0 ; i < len ; i ++)
*(dest +i )= *(src + i );
*(dest + i ) = 0 ;
i--;
while( *(dest + i ) == 32 )
{
*(dest + i ) = 0 ;
i--;
}
}
/*
FCOPY : fast copy of a file.
return : 0 on success
1 on error
*/
int fcopy(source,destin )
char *source;
char *destin;
{
int src,
dst;
int perm = S_IREAD | S_IWRITE ;
int acc = O_RDWR | O_BINARY | O_CREAT | O_TRUNC ;
int nb;
char *buf;
if(( buf = malloc ( 4096 ) ) == NULL )
{
printf("%s",txt[1] );
return ( 1 );
}
if( ( src = open (source,O_RDONLY | O_BINARY) ) == -1 )
{
printf("%s %s\n",txt[49],source ); /* "unable to read file" */
free( buf );
return( 1 );
}
if( ( dst = open (destin,acc,perm) ) == -1 )
{
printf("%s %s\n",txt[50],source ); /* "unable to create file" */
free( buf );
return( 1 );
}
while( (nb = read ( src, buf, 4096 )) )
{
write( dst,buf, nb );
}
close( src );
close( dst );
free( buf );
return( 0 );
}
/*
VIEW : View a file with more.
*/
void view(Path,File)
char *Path,*File;
{
FILE *fp;
char tmp[MAXPATHS];
char buf[513];
int line = 0 ;
sprintf(tmp,"%s/%s",Path,File );
if((fp = fopen(tmp,"r") )== NULL )
{
red();
printf("%s %s\n",txt[ 67 ], tmp ); /* "No file" */
return ;
}
clear();
cyan();
printf("\n\n");
while( fget(buf,512,fp ) )
{
line++ ;
if( line > SCREENLINES - 7 )
{
line = 0 ;
if( ! more(YES) )
break ;
cyan();
}
puts(buf) ;
}
fclose(fp);
}
/*
Si un rigolo tape ce mot dans le shell...
If a jerk were to type this word in a shell...
BTW the word merde = shit
*/
void merde()
{
magenta();high();
printf("\n");
printf("Espèce de petit connard, on t'a pas appris la politesse ???%c\n",7);
/* Stupid jerk, haven't you been shown proper maners??? */
printf("Va te faire foutre !!!\n\n");
/* Go to hell !!! */
}
/*
NUMERIC : Returns 1 if the string is a number, 0 if not.
*/
int Numeric( str )
char *str;
{
if( *str == 0 )
return( 0 );
while( *str )
{
if( *str < '0' || *str > '9' )
return ( 0 );
str++;
}
return ( 1 );
}
/*
IsQuoted(str) Returns 1 if the string contains c in the 15 left chars.
*/
int IsQuoted(c,str)
int c;
char *str ;
{
register int i;
for(i = 0 ; i < 15 ; i ++ )
{
if( *str == 0 ) return ( 0 );
if( *str++ == c ) return ( 1 );
}
return ( 0 );
}
/*
DATE : prints the date :-)
*/
void Date()
{
long t;
blue(); high();
time(&t);
printf(ctime(&t) );
}
/*
EmptyMsg : Tell that there is no BBs loaded.
*/
void EmptyMsg()
{
red(); high();
printf("%s ",txt[ 68 ] ); /* "Hey !" */
clear();magenta();
printf("%s\n",txt[69]); /* There is no bbs loaded !"*/
printf("%s...\n",txt[ 70 ]); /* use 'read' 'load' or 'help' command"*/
clear();
}
/*_-----------------------------------------------------------------------*/