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, 10 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <fcntl.h>
    5: #include <sys/mman.h>
    6: #include <aitio.h>
    7: 
    8: 
    9: int
   10: main(int argc, char **argv)
   11: {
   12: 	FILE *f;
   13: 	char b[BUFSIZ];
   14: 	void *p = NULL;
   15: 
   16: 	f = io_fd2buf(io_dumbFile("xxx", 0644, 12345), "r+");
   17: 	fclose(f);
   18: 
   19: 	if (argc < 2)
   20: 		f = io_fmapopen(NULL, 0, 0, PROT_READ | PROT_WRITE, 0, 2000);
   21: 	else
   22: 		f = io_fmapopen(argv[1], O_RDWR, 0, PROT_READ | PROT_WRITE, MAP_SHARED, 0);
   23: 	if (!f) {
   24: 		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
   25: 		return 1;
   26: 	}
   27: 
   28: 	fwrite("123456789", 10, 1, f);
   29: 	fseek(f, 0, SEEK_SET);
   30: 	memset(b, 0, sizeof b);
   31: 	fread(b, sizeof b, 1, f);
   32: 	printf("b=%s\n", b);
   33: //	fread(b, sizeof b, 1, f);
   34: 	fwrite("***", 4, 1, f);
   35: 	fwrite("*|\n", 3, 1, f);
   36: 	fseek(f, 100, SEEK_CUR);
   37: 	fwrite("oOo", 3, 1, f);
   38: 	fseek(f, -10, SEEK_END);
   39: 	fwrite("123456789", 10, 1, f);
   40: 	fseek(f, -10, SEEK_END);
   41: 	memset(b, 0, sizeof b);
   42: 	fread(b, sizeof b, 1, f);
   43: 	printf("2.b=%s\n", b);
   44: 
   45: 	fclose(f);
   46: 
   47: 	// part 2
   48: 	
   49: 	f = io_fmemopen(&p, 100);
   50: 	if (!f) {
   51: 		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
   52: 		return 2;
   53: 	}
   54: 
   55: 	fwrite("123456789", 10, 1, f);
   56: 	fseek(f, 0, SEEK_SET);
   57: 	memset(b, 0, sizeof b);
   58: 	fread(b, sizeof b, 1, f);
   59: 	printf("b=%s\n", b);
   60: //	fread(b, sizeof b, 1, f);
   61: 	fwrite("***", 4, 1, f);
   62: 	fwrite("*|\n", 3, 1, f);
   63: 	fseek(f, 100, SEEK_CUR);
   64: 	fwrite("oOo", 3, 1, f);
   65: 	fseek(f, -10, SEEK_END);
   66: 	fwrite("123456789", 10, 1, f);
   67: 	fseek(f, -10, SEEK_END);
   68: 	memset(b, 0, sizeof b);
   69: 	fread(b, sizeof b, 1, f);
   70: 	printf("2.b=%s\n", b);
   71: 
   72: 	fclose(f);
   73: 
   74: 	// part 3
   75: 
   76: 	p = e_malloc(10000);
   77: 	f = io_fmemopen(&p, 10000);
   78: 	if (!f) {
   79: 		printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
   80: 		return 3;
   81: 	}
   82: 
   83: 	fwrite("123456789", 10, 1, f);
   84: 	fseek(f, 0, SEEK_SET);
   85: 	memset(b, 0, sizeof b);
   86: 	fread(b, sizeof b, 1, f);
   87: 	printf("b=%s\n", b);
   88: //	fread(b, sizeof b, 1, f);
   89: 	fwrite("***", 4, 1, f);
   90: 	fwrite("*|\n", 3, 1, f);
   91: 	fseek(f, 100, SEEK_CUR);
   92: 	fwrite("oOo", 3, 1, f);
   93: 	fseek(f, -10, SEEK_END);
   94: 	fwrite("123456789", 10, 1, f);
   95: 	fseek(f, -10, SEEK_END);
   96: 	memset(b, 0, sizeof b);
   97: 	fread(b, sizeof b, 1, f);
   98: 	printf("2.b=%s\n", b);
   99: 
  100: 	fclose(f);
  101: 	e_free(p);
  102: 
  103: 	return 0;
  104: }

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