/*
 * recursiveCp - copy a file to a directory (recursively entered)
 *
 * source_head  points to a buffer of size _MAX_PATH that ends with a ':'
 *              or a FILESEP.
 * source_tail  points to the null terminator of source_head.
 * source_wild  is the filename/pattern to append to source_head to get the
 *              names of the file(s) to copy.
 * dest_head    points to a buffer of size _MAX_PATH that ends with a ':'
 *              or a FILESEP
 * dest_tail    points to the null terminator of dest_head.
 *
 * Note that the buffers source_head/dest_head are passed down the
 * recursion to save stack space.
 */
static void recursiveCp( char *source_head, char *source_tail,
    char *source_wild, char *dest_head, char *dest_tail )
{

    DIR                 *directory;
    struct dirent       *nextdirentry;
    void                *crx = NULL;
    char                *new_source_tail;
    char                *new_dest_tail;

    pathCopy( source_head, source_tail, "*.*" );

    directory = opendir( source_head );
    if( directory == NULL ) {
        DropPrintALine( "file \"%s\" not found", source_head );
        return;
    }

    if( rxflag ) {
        char *err = FileMatchInit( &crx, source_wild );
        if( err != NULL ) {
            Die( "\"%s\": %s\n", err, source_wild );
        }
    }

    /*
     * loop through all files
     */
    while( ( nextdirentry = readdir( directory ) ) != NULL ) {
        /*
         * set up file name, then try to copy it
         */
        FNameLower( nextdirentry->d_name );
        if( rxflag ) {
            if( !FileMatch( crx, nextdirentry->d_name ) ) {
                continue;
            }
        } else {
            if( !FileMatchNoRx( nextdirentry->d_name, source_wild ) ) {
                continue;
            }
        }
        new_source_tail = pathCopy( source_head, source_tail,
            nextdirentry->d_name );
        new_dest_tail = pathCopy( dest_head, dest_tail, nextdirentry->d_name );

        if( nextdirentry->d_attr & _A_SUBDIR ) {

            if( !IsDotOrDotDot( nextdirentry->d_name ) && rflag ) {
                int     rc;

                rc = mkdir( dest_head );
                if( !rc ) {
                    DirCnt++;
                }
                if( !sflag ) {
                    if( rc ) {
                        PrintALineThenDrop( "directory %s already exists",
                            dest_head );
                    } else {
                        PrintALineThenDrop( "created new directory %s",
                            dest_head );
                    }
                }
                new_dest_tail = pathCopy( dest_head, new_dest_tail,
                    FILESEPSTR );
                new_source_tail = pathCopy( source_head, new_source_tail,
                    FILESEPSTR );
                recursiveCp( source_head, new_source_tail,
                    rxflag ? "*" : "*.*",
                    dest_head, new_dest_tail );
            }

        } else {

            CopyOneFile( dest_head, source_head );

        }

    }
    closedir( directory );
    if( rxflag ) {
        FileMatchFini( crx );
    }

} /* DoCP */
/*
 * CopyOneFile  - copy one file to another
 */
void CopyOneFile( char *dest, char *src )
{


    struct stat stat_s,stat_d;
    int         i;
    unsigned    srcattr;

    /*
     * first, check if source exists
     */
    if( stat( src,&stat_s ) == - 1 ) {
        DropPrintALine("file \"%s\" not found.",src );
        return;
    }
    _dos_getfileattr( src, &srcattr );

    /*
     * check if the archive bit is set; if not, go back
     */
    if( aflag ) {
        if( !(srcattr & _A_ARCH) ) {
            return;
        }
    }

    /*
     * if destination exists, make sure we can overwrite it
     */
    if( stat( dest,&stat_d ) != -1 ) {

#if !( defined( __OS2__ ) && defined( __386__ ) )
        if( sameFile( dest, &stat_d, src, &stat_s ) ) {
            DropPrintALine( "%s and %s the same file, copy failed",src,dest );
            return;
        }
#endif

        if( !(stat_d.st_mode & S_IWRITE) ) {
            if( !fflag ) {
                DropPrintALine( "destination file %s is read only - use cp -f", dest);
                return;
            } else {
                chmod( dest, S_IWRITE | S_IREAD );
            }
        }

        if( iflag ) {
            PrintALine( "overwrite %s (y\\n)", dest );
            i = 0;
            while( i != 'y' && i != 'n' ) {
                i=getch();
            }
            DropALine();
            if( i=='n' ) {
                return;
            }
        }

    }

    /*
     * copy the file, and if it works, reset archive flag
     * of source (if needed)
     */
    if( !GrabFile( src, &stat_s, dest, srcattr ) ) {
        return;
    }

    if( aflag ) {
        _dos_setfileattr( src, srcattr & (~_A_ARCH) );
    }

} /* CopyOneFile */
Exemple #3
0
/*
 * GrabFile - read in a specified file, dump it to destination
 */
int GrabFile( char *src, struct stat *stat_s, char *dest, unsigned srcattr )
{
#if defined( __OS2__ ) && defined( __386__ )
    int                 result;
#else
    ctrl_block          *cb;
#endif
#if !defined(__WATCOMC__) && ( defined( __NT__ ) )
    HANDLE              handle;
#else
    int                 handle;
#endif
    int                 okay=TRUE;
    timedate            td;
    unsigned            t = 0;
    unsigned            d = 0;

    /*
     * file handle
     */
    if( _dos_open( src, O_RDONLY, &handle ) ) {
        DropPrintALine( "Error opening file %s",src );
        IOError( errno );
    }

    /*
     * get time/date stamp of file
     */
    if( npflag || todflag ) {
        _dos_getftime( handle, &d, &t );
        if( todflag ) {
            td.yy = (((d & 0xFE00) >> 9) + 1980);
            td.mm = ((d & 0x01E0 ) >> 5);
            td.dd = (d & 0x001F);
            td.hr = ((t & 0xF800) >> 11);
            td.min = ((t & 0x07E0) >> 5);
            td.sec = ((t & 0x001F) << 1);
            /*
             * see if this file passes the date checks
             */
            if( tflag2 ) {
                if( td.hr <= after_t_d.hr ) {
                    if( td.hr < after_t_d.hr ) {
                        return( FALSE );
                    }
                    if( td.min <= after_t_d.min ) {
                        if( td.min < after_t_d.min ) {
                            return( FALSE );
                        }
                        if( td.sec < after_t_d.sec ) {
                            return( FALSE );
                        }
                    }
                }
            }
            if( Tflag1 ) {
                if( td.hr >= before_t_d.hr ) {
                    if( td.hr > before_t_d.hr ) {
                        return( FALSE );
                    }
                    if( td.min >= before_t_d.min ) {
                        if( td.min > before_t_d.min ) {
                            return( FALSE );
                        }
                        if( td.sec > before_t_d.sec ) {
                            return( FALSE );
                        }
                    }
                }
            }
            if( dflag2 ) {
                if( td.yy <= after_t_d.yy ) {
                    if( td.yy < after_t_d.yy ) {
                        return( FALSE );
                    }
                    if( td.mm <= after_t_d.mm ) {
                        if( td.mm < after_t_d.mm ) {
                            return( FALSE );
                        }
                        if( td.dd < after_t_d.dd ) {
                            return( FALSE );
                        }
                    }
                }
            }
            if( Dflag1 ) {
                if( td.yy >= before_t_d.yy ) {
                    if( td.yy > before_t_d.yy ) {
                        return( FALSE );
                    }
                    if( td.mm >= before_t_d.mm ) {
                        if( td.mm > before_t_d.mm ) {
                            return( FALSE );
                        }
                        if( td.dd > before_t_d.dd ) {
                            return( FALSE );
                        }
                    }
                }
            }
        }
    }