Hi Frank,
The last bit of code I posted to you wasn't exactly perfect, I had
overlooked a couple of things. _This_time_ I think I have it! I have
appended the source here and will post a uuencoded zip file, which contains
both source and executable, to the international echo. This code just may
be able to help someone else with a similar problem.
As it turned out I don't require your routine for the original
project for which I needed the copier but it certainly got me going in the
right direction. The original project is a file manager, much like Norton
Comander, which will always supply a fully qualified filename as the
source. The destination, on the other hand, is determined from user input
and needs to cope with all possibilities. It has passed every test I have
given it so far.
Regards,
John
/*
Source written for and compiled with PCC.
This source file will contain much which will not compile
directly with another compiler. There are some library
functions which are not available even with PCC. There
shouldn't be too much work in adapting it however.
The printf and its relatives have been avoided because they
make the final EXE file about 5K bigger.
All constructive criticism, sugestions and comments are
welcome. Insults are best kept to yourself.
C_copy is an altered version of a routine supplied to me by
Frank Adam, in reply to my request for help.
Psplit was obtained from a snippets file, it is by
Bob Stout.
What is needed now is a much more intelligent move.
*/
#define NULL 0
#define TRUE 0
#define FALSE !TRUE
#define MAXPATH 160 /* Gives it plenty of room */
struct DTA
{
char reserved[21];
char attrib;
int time, date;
long size;
char name[12];
char extra[11];
};
/* The next three are part of a library routine to return
certain info about the drive allocation */
long drive_bytes_free();
extern int cluster_size;
extern long bytes_free;
/*=============================================================*/
main(argc, argv) /* need to start with source and dest */
int argc;
char *argv[];
{
char source[MAXPATH], dest[MAXPATH];
int mode=0;
strcpy(source, argv[1]);
strcpy(dest, argv[2]);
if (argc<3)
dest[0]=0; /* Null destination = current drive & dir */
if (argc<2)
source[0]=0; /* Null source should fail */
if(argc>3)
mode=atoi(argv[3]); /* 0 = Copy, 1 = Move, 2 = Rename */
c_copy(source,dest,mode);
}
c_copy(src, dst, mode)
char *src, *dst;
int mode;
{
struct DTA f;
char source_name[MAXPATH];
char sourcefile[MAXPATH];
char *ptr;
int done, cnt, vars=2;
char dest_name[MAXPATH];
long c_bytes;
char c_attrib;
/* Find the first matching directory entry */
if (done = read_dir_first(src,255,&f))
return FALSE;
if(! *dst)
vars=1;
/* Convert source & destination to upper case */
for(cnt=0; cntbytes_free)
{
puts("Insufficient space on target drive\n");
return 2;
}
/* Try to get as big a buffer as possible */
do
{
if(!(buffer=malloc(size)))
size/=2;
else
break;
}
while(size>500);
if(!buffer)
{
puts("Unable to allocate memory for buffer\n");
return(6);
}
/* In order to be able to handle hidden and system files
we need to clear all the source file attributes */
clr_attrib(src, -1);
if(!(infile=fopen(src, "r")))
{
puts("Error opening source file\n");
set_attrib(src, c_attrib); /* Reset the attributes */
return(4);
}
if(!(outfile=fopen(dst, "w")))
{
puts("Error opening destination file\n");
set_attrib(src, c_attrib);
return(5);
}
/* Now for the graphic display */
scr_rowcol(10, 10);
puts("00000000000000000000000000000000000000000000000000");
/* We finally get to do some file copying! */
while(1)
{
if(!(key=read(infile, buffer, size)))
break;
if(key<0)
{
puts("\nError reading from source\n");
break;
}
if((key=write(outfile,buffer,key))<0)
{
puts("\nError writing to destination\n");
break;
}
/* Calculate the new proportion for the graphic */
count=count+key;
frac=(int)((count*50)/c_bytes);
if(frac!=last_frac)
{
last_frac=frac;
scr_rowcol(10, 10);
for(key=0; key * Origin: Melbourne PC User Group +61-3-9699-6788 (3:632/309)
SEEN-BY: 50/99 78/0 620/243 623/630 632/0 107 271 304 309 328 348 360 393 525
SEEN-BY: 632/530 552 998 999 1000 633/371 634/384 388 396 635/301 502 503 544
SEEN-BY: 637/103 639/252 670/301 711/401 409 410 413 430 808 809 932 934
SEEN-BY: 712/515 713/888 714/906 800/1 7877/2809
@PATH: 632/309 998 635/503 50/99 711/808 809 934
|