Copy data from one I/O vector to another
#include <string.h> size_t memcpyv( const struct iovec *dst, int dparts, size_t doff, const struct iovec *src, int sparts, size_t soff );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The memcpyv() function copies data pointed to by the src I/O vector, starting at offset soff, to the dst I/O vector, starting at offset doff. The number of parts in each I/O vector is specified by sparts and dparts.
The number of bytes copied.
This example copies data from a two-part I/O vector, starting at offset 5, into a three-part vector, starting at offset 3:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> void print_buffer (char * buffer, char * buffer_name, int buffer_len) { int i; printf (" %s: '", buffer_name); for (i = 0; i < buffer_len; i++) { if (buffer[i] == 0) printf ("_"); else printf ("%c", buffer[i]); } printf ("'\n"); } int main( void ) { char msg1a[10]="1234567890", msg1b[20]="abcdefghijklmnop", msg2a[10], msg2b[10], msg2c[20]; iov_t dest[3], source[2]; int dparts=3, sparts=2; size_t nbytes, doffset=3, soffset=5; SETIOV (source + 0, &msg1a, sizeof (msg1a)); SETIOV (source + 1, &msg1b, sizeof (msg1b)); printf ("Original data:\n"); print_buffer (msg1a, "msg1a", sizeof (msg1a)); print_buffer (msg1b, "msg1b", sizeof (msg1b)); SETIOV (dest + 0, &msg2a, sizeof (msg2a)); SETIOV (dest + 1, &msg2b, sizeof (msg2b)); SETIOV (dest + 2, &msg2c, sizeof (msg2c)); memset (msg2a, 0, sizeof (msg2a)); memset (msg2b, 0, sizeof (msg2b)); memset (msg2c, 0, sizeof (msg2c)); nbytes = memcpyv ( dest, dparts, doffset, source, sparts, soffset ); printf ( "\nCopied %d bytes:\n", nbytes ); print_buffer (msg2a, "msg2a", sizeof (msg2a)); print_buffer (msg2b, "msg2b", sizeof (msg2b)); print_buffer (msg2c, "msg2c", sizeof (msg2c)); return EXIT_SUCCESS; }
This program produces the following output:
Original data: msg1a: '1234567890' msg1b: 'abcdefghijklmnop____' Copied 25 bytes: msg2a: '___67890ab' msg2b: 'cdefghijkl' msg2c: 'mnop________________'
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |