gabriel rosenkoetter on Fri, 14 Jun 2002 17:20:22 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] unix cp function call


On Fri, Jun 14, 2002 at 10:20:25AM -0400, Fred K Ollinger wrote:
> Does anyone know how to achieve a cp in a c program? I have two pointers
> to 2 char arrays, one is the target the other is the dest.

memcpy(3).

memmove(3) if you've got overlapping bytes.

(memcpy(3) is an ISO C99 thing; earlier standards say bcopy(3), but
that's deprecated now.)

If you're really copying text strings, then use strcpy(3) to get a
\0-filled tail in the destination.

> How do I get this to copy?

memcpy(dst, src, sizeof(src));

Or, if src is user input, then:

#define MAX_BUF 1024 /* or whatever's sane */
memcpy(dst, src, MAX_BUF);

> What does the function prototype look like?

     void *
     memcpy(void * restrict dst, const void * restrict src, size_t len);

> Where's the include file?

/usr/include/string.h on Linux and NetBSD, /usr/include/memory.h on
Solaris.

man -k string and ten minute's research would have told you all
this, btw.

-- 
gabriel rosenkoetter
gr@eclipsed.net

Attachment: pgpCE8E1f7UNA.pgp
Description: PGP signature