Exemplo n.º 1
0
int main(void)
{
  ssize_t bytes_written;
  int fd;
  char *buf0 = "short string\n";
  char *buf1 = "This is a longer string\n";
  char *buf2 = "This is the longest string in this example";
  int iovcnt;
  struct iovec iov[3];

  fd = open("test.out", O_CREAT | O_RDWR | O_TRUNC, 00644);
  if (fd < 0) {
    std::cerr << "open failed." << std::endl;
    exit(1);
  }

  iov[0].iov_base = buf0;
  //std::cout << "len: " << strlen(buf0) << std::endl;
  iov[0].iov_len = strlen(buf0);
  iov[1].iov_base = buf1;
  //std::cout << "len: " << strlen(buf1) << std::endl;
  iov[1].iov_len = strlen(buf1);
  iov[2].iov_base = buf2;
  //std::cout << "len: " << strlen(buf2) << std::endl;
  iov[2].iov_len = strlen(buf2);
  iovcnt = sizeof(iov) / sizeof(struct iovec);

  bytes_written = my_writev(fd, iov, iovcnt);
  //std::cout << "bytes_written: " << bytes_written << std::endl;

  close(fd);

  return 0;
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
    int fd, fd1;
    struct iovec iov[3];
    struct stat myStruct;       /* First buffer */
    int x;                      /* Second buffer */
#define STR_SIZE 100
    char str[STR_SIZE];         /* Third buffer */
    ssize_t numRead, totRequired, numWrite;

    if (argc != 3 || strcmp(argv[1], "--help") == 0)
        usageErr("%s readFile writeFile\n", argv[0]);

    fd = open(argv[1], O_RDONLY);
    if (fd == -1)
        errExit("open");

    totRequired = 0;

    iov[0].iov_base = &myStruct;
    iov[0].iov_len = sizeof(struct stat);
    totRequired += iov[0].iov_len;

    iov[1].iov_base = &x;
    iov[1].iov_len = sizeof(x);
    totRequired += iov[1].iov_len;

    iov[2].iov_base = str;
    iov[2].iov_len = STR_SIZE;
    totRequired += iov[2].iov_len;

    numRead = my_readv(fd, iov, 3);
    if (numRead == -1)
        errExit("readv");

    if (numRead < totRequired)
        printf("Read fewer bytes than requested\n");

    printf("total bytes requested: %ld; bytes read: %ld\n",
            (long) totRequired, (long) numRead);

    fd1 = creat(argv[2], S_IRUSR | S_IWUSR);
    if (fd1 == -1)
        errExit("open");

    numWrite = my_writev(fd1, iov, 3);
    if (numWrite == -1)
        errExit("writev");

    printf("total bytes requested: %ld; bytes write: %ld\n",
            (long) totRequired, (long) numWrite);

    exit(EXIT_SUCCESS);
}
Exemplo n.º 3
0
Arquivo: 5-7.c Projeto: pru/tlpi-key
int main(int argc, char * argv[]) {
	int fd;
	int rv1,rv2,wv1,wv2;
	struct iovec iov[3];
	char buf[10]; // first buf
	int x; // second buf
	struct some_struct y; // third buf

	iov[0].iov_base = buf;
	iov[0].iov_len = 10;
	iov[1].iov_base = &x;
	iov[1].iov_len = sizeof(x);
	iov[2].iov_base = &y;
	iov[2].iov_len = sizeof(struct some_struct);

	if( argc < 2 )
		return -1;
	fd = open(argv[1], O_RDWR);
	if( fd == -1 )
		return -1;

	rv1 = readv(fd, iov, 3);
	print_contents(iov);

	lseek(fd, 0, SEEK_SET);

	rv2 = my_readv(fd, iov, 3);
	print_contents(iov);
	printf("readv: rv1=(%d) vs my_readv: rv2=(%d)\n STATUS: %s\n",rv1,rv2, rv1==rv2?"PASS":"******");
	
	lseek(fd, 0, SEEK_END);
	wv1 = writev(fd, iov, 3);

	wv2 = my_writev(fd, iov, 3);
	printf("writev: wv1=(%d) vs my_writev: wv2=(%d)\n STATUS: %s\n", wv1, wv2, wv1==wv2?"PASS":"******");
	
	close(fd);
	return 0;
}
Exemplo n.º 4
0
/*
 * call-seq:
 *
 *	io.kgio_trywritev(array)	-> nil, Array or :wait_writable
 *
 * Returns nil if the write was completed in full.
 *
 * Returns an Array of strings containing the unwritten portion
 * if EAGAIN was encountered, but some portion was successfully written.
 *
 * Returns :wait_writable if EAGAIN is encountered and nothing
 * was written.
 *
 * Note: it uses +Array()+ semantic for converting argument, so that
 * it will succeed if you pass something else.
 */
static VALUE kgio_trywritev(VALUE io, VALUE ary)
{
	return my_writev(io, ary, 0);
}