File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / example / test_bufio.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:06:31 2016 UTC (7 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <aitio.h>


int
main(int argc, char **argv)
{
	FILE *f;
	char b[BUFSIZ];
	void *p = NULL;

	f = io_fd2buf(io_dumbFile("xxx", 0644, 12345), "r+");
	fclose(f);

	if (argc < 2)
		f = io_fmapopen(NULL, 0, 0, PROT_READ | PROT_WRITE, 0, 2000);
	else
		f = io_fmapopen(argv[1], O_RDWR, 0, PROT_READ | PROT_WRITE, MAP_SHARED, 0);
	if (!f) {
		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
		return 1;
	}

	fwrite("123456789", 10, 1, f);
	fseek(f, 0, SEEK_SET);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("b=%s\n", b);
//	fread(b, sizeof b, 1, f);
	fwrite("***", 4, 1, f);
	fwrite("*|\n", 3, 1, f);
	fseek(f, 100, SEEK_CUR);
	fwrite("oOo", 3, 1, f);
	fseek(f, -10, SEEK_END);
	fwrite("123456789", 10, 1, f);
	fseek(f, -10, SEEK_END);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("2.b=%s\n", b);

	fclose(f);

	// part 2
	
	f = io_fmemopen(&p, 100);
	if (!f) {
		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
		return 2;
	}

	fwrite("123456789", 10, 1, f);
	fseek(f, 0, SEEK_SET);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("b=%s\n", b);
//	fread(b, sizeof b, 1, f);
	fwrite("***", 4, 1, f);
	fwrite("*|\n", 3, 1, f);
	fseek(f, 100, SEEK_CUR);
	fwrite("oOo", 3, 1, f);
	fseek(f, -10, SEEK_END);
	fwrite("123456789", 10, 1, f);
	fseek(f, -10, SEEK_END);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("2.b=%s\n", b);

	fclose(f);

	// part 3

	p = e_malloc(10000);
	f = io_fmemopen(&p, 10000);
	if (!f) {
		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
		return 3;
	}

	fwrite("123456789", 10, 1, f);
	fseek(f, 0, SEEK_SET);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("b=%s\n", b);
//	fread(b, sizeof b, 1, f);
	fwrite("***", 4, 1, f);
	fwrite("*|\n", 3, 1, f);
	fseek(f, 100, SEEK_CUR);
	fwrite("oOo", 3, 1, f);
	fseek(f, -10, SEEK_END);
	fwrite("123456789", 10, 1, f);
	fseek(f, -10, SEEK_END);
	memset(b, 0, sizeof b);
	fread(b, sizeof b, 1, f);
	printf("2.b=%s\n", b);

	fclose(f);
	e_free(p);

	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>