using memcpy(), how to do it *PROPERLY* ?

In some *nix book I found this example of copying files:

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>

main (int argc, char *argv[]) {

int fd_src, fd_dst, rc;
caddr_t addr_src, addr_dst;
struct stat filestat;

fd_src = open( argv[1], O_RDONLY );
printf( “Source %d.\n”, fd_src );

fd_dst = open( argv[2], O_RDWR | O_CREAT );

fstat( fd_src, &filestat );

lseek( fd_dst, filestat.st_size -1, SEEK_SET );
write( fd_dst, " ", 1 );

addr_src = mmap( (caddr_t)0, filestat.st_size,
PROT_READ, MAP_SHARED, fd_src, 0 );
addr_dst = mmap( (caddr_t)0, filestat.st_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd_src, 0 );

memcpy( addr_dst, addr_src, filestat.st_size );

close( fd_src );
close( fd_dst );
exit( 0 );
}


It does compile with WC v10.6B cleanly, but “memcpy” fails. What is
different with memory management in QNX relative to unix’es?

Tony.

Tony wrote:

In some *nix book I found this example of copying files:

QNX4 does not support memory-mapped files.

On Sun, 13 Jun 2004 21:07:43 +1200, John Garvey <jgarvey@qnx.com> wrote:

QNX4 does not support memory-mapped files.

Aha!
Quite a surprize…

What is the work-around?

Tony.

Tony wrote:

On Sun, 13 Jun 2004 21:07:43 +1200, John Garvey <> jgarvey@qnx.com> > wrote:
QNX4 does not support memory-mapped files.
What is the work-around?

For your file copy example? read() and write().

“Tony” <mts.spb.suxx@mail.ru> wrote in message
news:opr9i5s3g1o93ri4@workstation.wst.quantum.ru

On Sun, 13 Jun 2004 21:07:43 +1200, John Garvey <> jgarvey@qnx.com> > wrote:

QNX4 does not support memory-mapped files.

Aha!
Quite a surprize…

What is the work-around?

As John said, a simple read()/write() combinaison will do.

Under unix is a file copy done through mmap the normal way of copying files.
I would expect such a method to be more expensive then read/write, hum maybe
not under monolitic kernel.

Tony.

Is there any way to ask OS to make a copy of a file for me? ( other then system(“cp file1 file2”); or the like )

Tony.

“Tony” <mts.spb.suxx@mail.ru> wrote in message
news:opr9mr1upco93ri4@mobile.wst.quantum.ru

Is there any way to ask OS to make a copy of a file for me? ( other then
system(“cp file1 file2”); or the like )

No.


Tony.