/* #define DEBUG */
/*
 ΙΝΝ[ MS-DOS ! ]ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ»
 Ί                                                                           Ί
 Ί                 -  ADD2BBS.C R.Cougnenc 1990 - Public Domain -            Ί
 Ί                                                                           Ί
 ΗΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ¶
 Ί                                                                           Ί
 Ί    This program moves the newly downloaded files and their descriptions   Ί
 Ί    to the specified file directory and description file.                  Ί
 Ί                                                                           Ί
 Ί    Files are MOVED and do not remain in the source dir.                   Ί
 Ί                                                                           Ί
 Ί    Specially designed for Attila ALTAN, sysop of LI'LL BBS, Paris France. Ί
 Ί                                                                           Ί
 Ί    Compile: Microsoft-C or Turbo-C.                                       Ί
 Ί                                                                           Ί
 Ί                                                                           Ί
 ΘΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΌ

  The executable distribution file is compiled with MS-C, using Large Model.
  This allows the use of the total memory to store the filenames to process,
  and all screen outputs going to stdout.

  If you use Turbo-C, the #defines should make a program writing directly
  to the screen memory, this is a lot faster, but not a good solution for
  such a program.

  I took me just one day to write this program, it is certainly not
  perfect...
                      Feel free to correct all bugs  !
*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>

#include <errno.h>
#include <dos.h>
#include <stdlib.h>
#include <process.h>
#include <io.h>
#include <share.h>

#ifndef __TURBOC__
#include <sys/types.h>
#include <memory.h>
#else
#include <mem.h>
#include <dir.h>
#include <conio.h>
#define printf cprintf
#endif

#include <sys/stat.h>
#include "add2bbs.h"

/*-----------------------( Global variables )-----------------------------*/

struct Ftype *Fstru  ;
FILE *dfile ;

char ScanDir    [ 256 ] ,                 /* Ready for Posix :-)          */
     ScanPath   [ 256 ] ,
     FileDes    [ 256 ] ,
     DestDir    [ 256 ] ,
     FileList   [ 256 ] ,
     ConfPath   [ 256 ] ,
     TmpFile    [ 256 ] ,
     ExtCommand [ 256 ] ,
     CmTab [ MAX_C_LINES ][ MAX_C_COLS+1 ];

int nbfiles ;


/*----------------------( main program )------------------------------------*/

int main(int argc,char **argv)
{
        int result;
        Title();
        if( argc == 1 )      /* No argument, use configuration  file */
        {
                MakeConfPath(argv[0]);
                if( ! ReadConf() )
                        return(0xFF);
        }
        else
        {
                if( argc != 5 )
                {
                        Usage();
                        return( 0xFF);
                }
                strcpy( ScanDir  , argv[1]  );
                strcpy( FileDes  , argv[2]  );
                strcpy( DestDir  , argv[3]  );
                strcpy( FileList , argv[4]  );

        }

        if( ! VerifPath() )
                return(0xFF);

        if(! (nbfiles = FindFiles())  )
        {
                printf("%s : No file found.\n",ScanDir );
                return( 0xFF );
        }

        if ( (dfile = fopen( TmpFile,"a+")) == NULL )
        {
                printf("Unable to open file %s !\n",FileList);
                return (0xFF);
        }
        ListFiles(ALL);

        if( (result = ReadDesc() ) != nbfiles )
                ListFiles(NOTFOUND);

        fclose(dfile);
        if( result )
        {
            if( fappend( FileList,TmpFile ) )
            {
                    printf("Unable to update  file %s !\n",FileList );
                    printf("Comments remaining in %s.\n",TmpFile);
                    return(0xFF);
            }


            printf("%s",result == nbfiles ? "Update Sucessfull.\n"
                        : "All new files successfully added.\n");
        }
        else
        {
          unlink(TmpFile);
          printf("No file added.\n");
        }
        return( 0 );
}

/*-------------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³    FINDFILES : Creates a linked list with all the files found in the   ³
  ³                search directory.                                       ³
  ³                                                                        ³
  ³                Return :  Number of files found.                        ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

int FindFiles (void)
{
        int i = 0 ;
        static char *Pattern = "*.*" ;
        static char *ErrMsg  = "Findfiles(): Cannot allocate memory for %d bytes\n";
        struct Ftype *WorkStru;
        int echar ;

        struct DocFile
            {                                   /* My own struct since Borland  */
                char     Reserved[21];      /* and Microsoft use different  */
                char     attribut;          /* names...                     */
                unsigned Time;              /* Don't worry about warnings!  */
                unsigned Date;
                long     Taille;
                char     name[13];
        }
        SearchBlock;

        echar = (int) ScanDir[strlen(ScanDir) -1] ;
        if( echar && echar != '\\'  && echar != '/' && echar != ':' )
                strcat(ScanDir,"\\");
        strcpy(ScanPath,ScanDir);   /* Save it for later use...,   */
        strcat(ScanDir,Pattern);    /* and add the search pattern. */

        Fstru = (struct Ftype *) malloc ( sizeof (struct Ftype) );
        if( Fstru  == NULL )
        {
                printf(ErrMsg, sizeof( struct Ftype) );
                return( 0 );
        }
        WorkStru = Fstru;


#ifdef __TURBOC__
        if (findfirst (ScanDir,&SearchBlock,0)  == 0)
#else
                if (_dos_findfirst (ScanDir, _A_ARCH, &SearchBlock) == 0)
#endif
                {
                        do
                            {
                                WorkStru->Next =(struct Ftype * ) NULL;
                                WorkStru->HasComment  = 0 ;
                                strcpy( WorkStru->fname, SearchBlock.name );
                                strlwr( WorkStru->fname ); /* I prefer lower case !!! */
                                i++;
                                WorkStru->Next = (struct Ftype *) malloc ( sizeof (struct Ftype) );
                                if( WorkStru->Next  == NULL )
                                {
                                        printf(ErrMsg, sizeof( struct Ftype) );
                                        return(i);
                                }
                                WorkStru = WorkStru -> Next;
                                WorkStru ->Next = (struct Ftype * ) NULL;
                        }
#ifdef __TURBOC__
                        while(findnext (&SearchBlock) == 0 );
#else
                        while (_dos_findnext (&SearchBlock) == 0);
#endif
                }
                else return 0 ;
        return i ;


}
/*-------------------------------------------------------------------------*/



/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  LISTFILES:  Displays the files in the linked list.                    ³
  ³                                                                        ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

void ListFiles(int which)
{
        struct Ftype *WorkStru;
        int i   = 0 ;
        int nb  = 0 ;

        WorkStru = Fstru ;

        if( which == ALL )
                printf("ΔΔΔΔΔΔ> Files found in %s <ΔΔΔΔΔΔΔΔΔΔΔΔΔΔ\n\n",ScanDir);
        if( which == NOTFOUND )
                printf("ΔΔΔΔΔΔ> Files not found in %s <ΔΔΔΔΔΔΔΔΔΔΔΔΔΔ\n\n",FileDes);

        while( WorkStru -> Next != (struct Ftype *) NULL )
        {
                if( which == ALL ||(which == NOTFOUND && ! WorkStru -> HasComment ))
                {
                        printf("%-15s",WorkStru -> fname );
                        i++;
                        nb++;
                        if( i > 4 )
                        {
                                printf("\n");
                                i  = 0 ;
                        }
                }
                WorkStru = WorkStru -> Next ;
        }
        printf("\n\nΔΔΔΔΔ> %d Files.\n\n",nb);
}

/*-------------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  FLAGFILE :  Moves the file in the destination directory if it is      ³
  ³              found in the linked list,                                 ³
  ³              Copies its description to the destination file list,      ³
  ³              and flags the corresponding structure as commented ok.    ³
  ³                                                                        ³
  ³              Return :  1 if the file was in the list                   ³
  ³                        0 if not.                                       ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

int  FlagFile(char *line )
{
        struct Ftype *WorkStru;


        WorkStru = Fstru ;

        while( WorkStru -> Next != (struct Ftype *) NULL )
        {
                if ( ! strnicmp (line,WorkStru-> fname,strlen(WorkStru->fname) ) )
                {
                        if( MoveFile(WorkStru -> fname) )
                        {
                                printf("ΔΔΔΔΔ Found : %s ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ\n",WorkStru -> fname );
                                PrintTab();
                                WorkStru -> HasComment = 1 ;    /* Ok files is moved. */
                                printf("ΔΔΔΔΔώ File moved. ώΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ"
                                    "ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ\n\n");
                                printf("\nScanning %s..\r",FileDes);
                        }
                        return( 1 ); /*  Was in the list */
                }
                WorkStru = WorkStru -> Next ;
        }
        return( 0 );  /* Was'nt in the list */
}

/*-----------------------------------------------------------------------*/


/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  READDESC:   Scans the source description file, and if a file of the   ³
  ³              linked list matches, calls FlagFile in order to process   ³
  ³              it.                                                       ³
  ³                                                                        ³
  ³              Return :    Number of files found                         ³
  ³                          0 on error.                                   ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/


int ReadDesc(void)
{
        FILE *fp;
        char tmp[256];
        int i,count = 0;

        if( (fp = fopen(FileDes,"r")) == NULL )
        {
                printf("Unable to open description file %s\n",FileDes);
                return( 0 );
        }
        setvbuf(fp,NULL,_IOFBF,MYBUFS);

        printf("\nReading %s..\r",FileDes);


        while(fgets(tmp,256,fp) )
        {
                Clean(tmp);
test_it:
                if( ! IsFilDes(tmp))  continue;   /* Search for the 1st comment line */
                else
                {
                        i = 0 ;
                        ClearTab();
                        strcpy(CmTab[i],tmp );
                        while(fgets(tmp,256,fp) )       /* read comments         */
                        {
                                i++;
                                Clean(tmp);
                                if ( ! IsComment(tmp))         /* until last one        */
                                {
#ifdef DEBUG
                                        PrintTab();
#endif
                                        if( FlagFile( CmTab[0] ) )
                                                count++;
                                        if( count == nbfiles) /* end of process*/
                                                return(count) ;
                                        goto test_it;
                                }
                                else    strcpy(CmTab[i],tmp );
                        }
                }
        }

        fclose( fp );
        return(count);
}
/*------------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³   ISFILDES   : Tries to determine if the line is the 1st description   ³
  ³                line of a file.                                         ³
  ³                                                                        ³
  ³                Return :  0 if it does not begin with a valid filename  ³
  ³                          1 if it does.                                 ³
  ³                                                                        ³
  ³                Assumes valid names beginning column 0, containing  an  ³
  ³                extension, and composed with ASCII characters.          ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

int IsFilDes( char *line)
{
        char name[15];
        char *ptr;

        strncpy(name,line,12);
        name[12] = 0 ;
        strupr( name);




        if( ! strchr(name, '.') ) return ( 0 );      /* If no extension, not */
                                                     /* a filename.          */

                                                     /* If invalid chars,    */
                                                     /* not a filename...    */
        ptr = name;
        while(*ptr)
        {
                if (*ptr < 32  || *ptr > 90  )  return ( 0 );
                ptr ++;
        }

        return ( 1 );
}
/*------------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³   ISCOMMENT  : Tries to determine if the line is an additionnal        ³
  ³                description line.                                       ³
  ³                                                                        ³
  ³                Return :  1 if it is one,                               ³
  ³                          0 if not.                                     ³
  ³                                                                        ³
  ³                Assumes valid additionnal description lines begin with  ³
  ³                a minimum of 30 space chars.                            ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

int IsComment(char *line)
{
        register int i;

        for(i=0; i < 30; i++)
        {
                if (! *line)        return( 0 );
                if (*line != 32)    return( 0 );
                line++;
        }

        return(1);
}
/*----------------------------------------------------------------------*/

/*
      ΪΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΏ
      ³   CLEAN :  Fixes the normalized bug of fgets() :-)   ³
      ΐΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΩ
*/

void Clean( char *line)
{
        int i;

        i = strlen(line) - 1;

        if(line[i] == '\n');
        line[i] = 0 ;
}
/*----------------------------------------------------------------------*/

/*
      ΪΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΏ
      ³   CLEARTAB: Cleans the comment buffer                ³
      ΐΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΩ
*/

void ClearTab(void)
{
        memset (CmTab,0,MAX_C_LINES *( MAX_C_COLS + 1) );
}
/*---------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  PRINTTAB :  Prints the current file description on stdout, and in     ³
  ³              the destination file list.                                ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

void PrintTab(void)
{
        int i;

        for( i = 0 ; CmTab[i][0]; i++)
        {
                if( i ) CmTab[i][31] = '|' ;
                printf (      "%s\n",CmTab[i] );
                fprintf(dfile,"%s\n",CmTab[i] );
        }

}
/*--------------------------------------------------------------------------*/


/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  MAKECONFPATH: Creates the full path and name of the config file,      ³
  ³                from the argv[0] program arg.                           ³
  ³                                                                        ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

void MakeConfPath(char *path)
{
        int i;

        strcpy(ConfPath,path);
        i = strlen(ConfPath);
        while(i)
        {
                if(ConfPath[i] == '\\' || ConfPath[i] == ':') break ;
                ConfPath[i] = 0;
                i--;
        }
        strcpy(TmpFile,ConfPath);
        strcat( ConfPath,CONFIG_FILE );
        strcat( TmpFile,TMP_FILE );
}
/*----------------------------------------------------------------------*/



/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  READCONF:     Reads the config file.                                  ³
  ³                Return  1 on success                                    ³
  ³                        0 on error                                      ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/


int ReadConf(void)
{
        FILE *fp;
        char tmp[ 256 ];
        int count;

        if( (fp = fopen(ConfPath,"r")) == NULL )
        {
                printf("Unable to open Config File : %s\n",ConfPath);
                printf("You must create this file before using this program...\n\n");
                SampleConfig();
                printf("A sample one has been created for you.\n");
                printf("Look at the file : %s  with  your favourite text editor  !\n",
                ConfPath);
                return(0);
        }

        count = 0 ;
        printf("Reading Config...\r");
        while(fgets(tmp,255,fp ) )
        {
                Clean(tmp );
                strupr(tmp );
                if(strlen(tmp) < 2 )        continue ;        /* Empty lines not used   */
                if(tmp[0] == COMMENT_CHAR ) continue ;        /* Commented lines..      */

                switch( count )
                {
                case 0 :                       /* 1st line : Directory where files   */
                        strcpy( ScanDir, tmp );/* are downloaded.                    */
                        break  ;

                case 1 :
                        strcpy(FileDes,tmp );  /* 2nd line : Full name of the file   */
                        break  ;               /* containing descriptions.           */

                case 2 :
                        strcpy(DestDir, tmp ); /* 3rd line : Destination Directory   */
                        break  ;

                case 3 :                       /* 4th line : Full name of the BBS   */
                        strcpy(FileList,tmp);  /*description file to be updated     */
                        break ;


                case 4 :                       /* 5th line : Full name of the Batch  */
                        strcpy(ExtCommand,tmp);/*optionnal command                   */
                        break ;
                }

                count++ ;

                if( count > 5 )
                {
                        printf("Too many config lines !!! Invalid file...\n" );
                        fclose(fp);
                        return(0);
                }
        }
        fclose(fp);
        return( 1 );
}
/*--------------------------------------------------------------------------*/



/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  VERIFPATH:    Verifies filenames and pathnames.                       ³
  ³                Return  1 on success                                    ³
  ³                        0 on error                                      ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/


int VerifPath(void)
{
        int RetVal = 1 ;
        int echar ;

        if( access( ScanDir, 0 ) )
        {
                printf("Invalid Source Directory %s\n",ScanDir );
                RetVal = 0 ;
        }

        if( access( FileDes, 0 ) )
        {
                printf("Invalid Filename %s\n",FileDes );
                RetVal = 0 ;
        }

        if(ExtCommand[0] >32 )
                if( access( ExtCommand, 0 ) )
                {
                        printf("Invalid External Command File %s\n",ExtCommand );
                        RetVal = 0 ;
                }

        if( access( DestDir, 0 ) )
        {
                printf("Invalid Destination Directory %s\n",DestDir );
                RetVal = 0 ;
        }

         /* Prepares the Destination directory name for */
         /* correct concatenation                       */

        echar = (int) DestDir[strlen(DestDir) -1] ;
        if( echar && echar != '\\'  && echar != '/' && echar != ':' )
                strcat(DestDir,"\\");

         /* FileList not tested, it will be created if needed. */

        return( RetVal );
}
/*----------------------------------------------------------------------*/

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  MOVEFILE :  Renames the file in order to have it in the destination   ³
  ³              directory.                                                ³
  ³              Return :       1 on success                               ³
  ³                             0 on error                                 ³
  ³              Doesn't use rename() in order to be able to move files    ³
  ³              to different physical supports.                           ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

int MoveFile(char *name)
{
        char tmp[256];
        char fullpath[256];


        strcpy(fullpath,ScanPath);
        strcat(fullpath,name   );

        if( ExtCommand[0] > 32 )
        {
                sprintf(tmp,"%s %s",ExtCommand,fullpath);
                printf("Calling External Command for %s...\n",fullpath);
                /* spawnlp(P_WAIT,ExtCommand,ExtCommand,fullpath,NULL);*/
                system(tmp);
        }
        if (access(fullpath,0 ) )
                return ( 0 ) ;

                              /* Explanation about the above silly lines... */
                              /* The User may need to process the file and  */
                              /* eventually remove it or something else.    */
                              /* It is impossible to use correctly a child  */
                              /* process and get his return value under this*/
                              /* !$&‡…"‚ MS-DOS !!!                         */
                              /* (spawn could have done it but does not work*/
                              /* with batch files... )                      */
                              /* So the only thing we will do is to verify  */
                              /* the acces to the file , and pray... :-)    */

        RealStat(fullpath,name);  /* Date or size may have been modified... */
        strcpy( tmp,DestDir );
        strcat(tmp,name);

        if( fmove(fullpath,tmp) )
        {
                printf("Unable to move %s to %s !\n", fullpath,tmp );
                return( 0 );
        }
        return ( 1 );
}
/*------------------------------------------------------------------------*/


/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  FMOVE    :  Moves source File to dest File, using a fast copy routine ³
  ³                                                                        ³
  ³              Return :       0 on success                               ³
  ³                             1 on error                                 ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

fmove(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[MYBUFS];

#ifdef __TURBOC__
        struct ftime df;
#else
        unsigned df,
        tf;
#endif


        if(  ( src  = open (source,O_RDONLY | O_BINARY) ) == -1 )
        {
                printf("Unable to read file %s\n",source );
                return( 1 );
        }
#ifdef __TURBOC__
        getftime(src,&df);            /* Get the original file date !   */
#else
        _dos_getftime(src,&df,&tf);
#endif



        if(  ( dst  = open (destin,acc,perm) ) == -1 )
        {
                printf("Unable to create file %s\n",source );
                Perror("System Report");
                return( 1 );
        }

        while( (nb  = read ( src, buf, MYBUFS )) )
                write( dst,buf,nb );

#ifdef __TURBOC__
        setftime(dst,&df);            /* Set the original file date !  */
#else
        _dos_setftime(dst,df,tf);
#endif

        close( src );
        close( dst );

        if( unlink(source) == -1 )
        {
                printf("Unable to remove %s !\n", source );
                Perror("System Report");
                /* Not a real error,return OK. */
        }
        return( 0 );
}
/*-----------------------------------------------------------------------*/


/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  FAPPEND  :  Appends source File to dest File, trying to manage with   ³
  ³              sharing errors,I don't understand why this doesn't work   ³
  ³              like in  **IX systems whith C-language and DOS!           ³
  ³              Here is a quick & dirty method... But I do not want to    ³
  ³              waste time learning  those specific stuff which I'll      ³
  ³              never use... :-)                                          ³
  ³                                                                        ³
  ³              Removes Source file on succes.                            ³
  ³                                                                        ³
  ³              Return :       0 on success                               ³
  ³                             1 on error                                 ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/


int fappend(char *destin, char *source )
{
        int src,dst,
        try = 10,
        nb,result;
        char buf[ MYBUFS ];

        printf("Updating file %s...\n",destin);

        if(  ( src = open( source, O_RDONLY | O_BINARY ) ) == -1 )
        {
                printf("Error opening file %s !\n",source);
                return ( 1 );
        }

        while( try-- )
        {
                dst = sopen( destin,
                             O_RDWR | O_APPEND | O_CREAT | O_BINARY,
                             SH_DENYWR , S_IWRITE );
                if( dst == - 1 )
                {
                        switch( errno )
                        {
                        case EMFILE  :
                                printf(" Too many files opened !!!!!!! "
                                    " Check your system configuration !!!\n");
                                return( 1 );

                        case EACCES  :
                                if( try < 1 ) return (1);
                                printf("File busy...?  Try %d\r",10 - try );
                                sleep( 2 );   /* Wait 2 seconds */
                                break ;

                        default :
                                Perror("System Report") ;
                                return ( 1 );
                        }
                }
                else break ;        /* File sucessfully opened */
        }


        while( (nb = read ( src, buf, MYBUFS ) ) )
        {
                result = write( dst,buf,nb );
                if( result == -1 )
                {
                        printf("Error Writing file %s !\n",destin);
                        close  ( src );
                        close  ( dst );
                        return (  1  );
                }
        }

        close ( src );
        close ( dst );
        if ( unlink(source) != 0 )
        {
                printf("Unable to remove %s : ",source);
                Perror("");
                return( 1 );
        }
        return ( 0 ) ;
}


/*
   ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
   ³  REALSTAT :  Puts the real date & size of the filename in  the comment.³
   ³                                                                        ³
   ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

#define C_OFFSET 33      /* Comment text seems to begin at column 33 ... */

void RealStat(char *fullpath,char *name)
{
        struct stat  st  ;
        struct tm   *tim ;
        char comment[256];
        int i;

        for( i = 0; CmTab[0][i + C_OFFSET] ; i++ )
                comment[i] = CmTab[0][i+ C_OFFSET];
        comment[i] = 0;

        /* Comment text seems to begin at column C_OFFSET ... */
        /* Save it, and take the real file stats              */

        stat(fullpath ,&st);
        tim = gmtime( &st.st_atime );
        sprintf(CmTab[0],"%-12s  %7ld  %02d-%02d-%02d  %s",
        strupr(name),
        st.st_size,
        tim->tm_mon +1,
        tim->tm_mday,
        tim->tm_year,
        comment);

        /* I don't test the COUNTRY variable in **environ, this is */
        /* The US date format...                                   */
}
/*--------------------------------------------------------------------------*/


/*
 ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
 ³  PERROR :   Perror() is a replacement for perror(),writing on stdout   ³
 ³             instead of stderr.                                         ³
 ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/
void Perror(char *s)
{
        printf("%s :    %s\n",s,sys_errlist[errno] );
}
/*------------------------------------------------------------------------*/

#ifndef __TURBOC__
/*
 ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
 ³  SLEEP  :   quick implementation of function sleep(), non existent     ³
 ³             in Microsoft - C... !!!!                                   ³
 ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/
sleep(unsigned  sec)
{
        long t1,t2;

        time (&t1);
        t2 = t1;
        t1+= (long) sec;
        while( t2 < t1 )
                time (&t2 );
}
/*------------------------------------------------------------------------*/
#endif

/*
  ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
  ³  SAMPLECONFIG: Creates a sample configuration file if possible.        ³
  ³                                                                        ³
  ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ
*/

void SampleConfig(void)
{
        FILE *fp;


        if( (fp = fopen(ConfPath,"w")) == NULL ) return ;

        fprintf(fp,"#----------------------------------------------------------\n");
        fprintf(fp,"# This is a sample configuration file for ADD2BBS.EXE.     \n");
        fprintf(fp,"# Replace the pathnames & directories by your own values.  \n");

        fprintf(fp,"#----------------------------------------------------------\n");
        fprintf(fp,"# Below is the name of the directory where the program     \n");
        fprintf(fp,"# will look for the files you want to move :               \n");
        fprintf(fp,"#\n\n");
        fprintf(fp,"C:\\FILE2MOV\n\n");
        fprintf(fp,"#\n");

        fprintf(fp,"#----------------------------------------------------------\n");
        fprintf(fp,"# Below is the Full Name and Pathname of the file          \n");
        fprintf(fp,"# containing the descriptions corresponding to the files : \n");
        fprintf(fp,"#\n\n");
        fprintf(fp,"C:\\FIL2MOVE\\INPUT.DSC\n\n");
        fprintf(fp,"#\n");

        fprintf(fp,"#----------------------------------------------------------\n");
        fprintf(fp,"# Now, the directory where you want to move the files to:  \n");
        fprintf(fp,"#\n\n");
        fprintf(fp,"C:\\FILMOVED\n\n");
        fprintf(fp,"#\n");

        fprintf(fp,"#----------------------------------------------------------\n");
        fprintf(fp,"# Here, the full Name and Pathname of the description      \n");
        fprintf(fp,"# file you want to update or create :\n");
        fprintf(fp,"#\n\n");
        fprintf(fp,"C:\\FIL2MOVE\\OUTPUT.DSC\n\n");
        fprintf(fp,"#\n");
        fprintf(fp,"#----------------------------------------------------------\n");

        fprintf(fp,"# Finally the full pathname of an optionnal batch file,\n");
        fprintf(fp,"#\n");
        fprintf(fp,"# It will be called by the program with the current\n");
        fprintf(fp,"# filename as argument ( e.g ' TESTFIL.BAT 2BEMOVED.ZIP')\n");
        fprintf(fp,"# just BEFORE moving the file.\n");
        fprintf(fp,"#\n");
        fprintf(fp,"# (This can be used to SCAN a ZIP file for virii,        \n");
        fprintf(fp,"# remove a ZIP comment, add a new one, etc ...)          \n");
        fprintf(fp,"#\n");
        fprintf(fp,"# Leave this field blank if no special processing needed.\n");
        fprintf(fp,"#\n\n");
        fprintf(fp,"TESTFILE.BAT\n\n");
        fprintf(fp,"#\n");
        fprintf(fp,"#----------------------------------------------------------\n");

        fclose(fp);
}
/*------------------------------------------------------------------------*/


void Usage(void)
{
        printf("Usage : ADD2BBS source_dir source_list dest_dir dest_list\n\n");
        printf("        Configuration file is used if no argument.\n");
}
/*------------------------------------------------------------------------*/

void Title(void )
{
        printf(
            "\t\tΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ\n"
            "\t\t³ Add2bbs: Public Domain PcBoard Utility  ³\n"
            "\t\t³    by Ren‚ COUGNENC & Attila ALTAN      ³\n"
            "\t\t³            France - 1990                ³\n"
            "\t\tΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ[%3s]ΝΎ\n"
            "\n", ___VERSION___
            );
}
/*-------------------------------------------------------------------------*/


/*=====================( End of add2bbs.C )================================*/


#ifdef NOT_USED_PERHAPS_ONE_DAY

 /*
   ΥΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΈ
   ³  LASTFILES:  Scans the linked list for the undescribed files.          ³
   ³              Moves them to the destination directory, and write an     ³
   ³              empty comment to the destination description file.        ³
   ³                                                                        ³
   ΤΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΎ

*/


void LastFiles(void)
{
        struct Ftype *WorkStru;
        struct stat  st  ;
        struct tm   *tim ;

        WorkStru = Fstru ;

        while( WorkStru -> Next != (struct Ftype *) NULL )
        {
                if (! WorkStru->HasComment )
                {
                        stat(WorkStru -> fname ,&st);
                        tim = gmtime( &st.st_atime );
                        sprintf(CmTab[0],"%-12s  %7ld  %02d-%02d-%02d  %s",
                        strupr(WorkStru->fname),
                        st.st_size,
                        tim->tm_mon +1,
                        tim->tm_mday,
                        tim->tm_year,
                        NullComment);
                        CmTab[1][0] = 0;
                        if( MoveFile( WorkStru -> fname ) )
                        {
                                printf("ΔΔΔΔΔ Not Found : %s ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ\n",WorkStru -> fname );
                                PrintTab();
                                printf("----- File moved.---------------------------\n");
                        }
                }
                WorkStru = WorkStru -> Next ;
        }
}
/*--------------------------------------------------------------------------*/
#endif
