home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 13
/
mediashare_13.zip
/
mediashare_13
/
ZIPPED
/
PROGRAM
/
SNPD9404.ZIP
/
WB_FCOPY.C
< prev
next >
Wrap
Text File
|
1994-04-03
|
2KB
|
64 lines
.I 11 3
**
** To compile file_copy(), define Afilecopy=1 on the command line
** To compile file_append(), define Afileappe=1 on the command line
.I 24 17
int file_copy(char *from, char *to)
#else
int file_append(char *from, char *to)
#endif
{
int fdfrom,fdto;
int bufsiz;
fdfrom = open(from,O_RDONLY|O_BINARY,0);
if (fdfrom < 0)
return 1;
#if Afileappe
/* Open R/W by owner, R by everyone else */
fdto=open(to,O_BINARY|O_CREAT|O_APPEND|O_RDWR,S_IREAD|S_IWRITE);
if (fdto < 0)
.D 25 15
.I 41 2
fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
if (fdto < 0)
.D 42 2
.I 46 5
/* Use the largest buffer we can get */
for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
{
register char *buffer;
.D 47 3
.I 52 27
{
while (1)
{
register int n;
n = read(fdfrom,buffer,bufsiz);
if (n == -1) /* if error */
break;
if (n == 0) /* if end of file */
{
free(buffer);
close(fdto);
close(fdfrom);
return 0; /* success */
}
if (n != write(fdto,buffer,(unsigned) n))
break;
}
free(buffer);
break;
}
}
err2: close(fdto);
remove(to); /* delete any partial file */
err: close(fdfrom);
return 1;
}
.D 53 24