home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************
-
- COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
-
- ***************************************************************/
-
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <malloc.h>
- #include <stdio.h>
- long lseek();
-
- cread(name, x, nbyte, offset_byte)
- char *name, *x;
- int nbyte, offset_byte;
- { int fd, status;
-
- fd = open(name, 0);
- if(fd == -1)
- { printf("ERROR: Error opening %s for read\n", name);
- exit(-1);
- }
-
- lseek(fd, (long)offset_byte, 1);
-
- status = read(fd, x, nbyte);
- if(status == -1)
- { printf("ERROR: Error reading file %s\n", name);
- exit(-1);
- }
-
- status = close(fd);
- if(status == -1)
- { printf("ERROR: Error closing %s after read\n", name);
- exit(-1);
- }
- }
-
- /**********************************************************************/
-
- cwrite(name, x, nbyte, offset_byte)
- char *name, *x;
- int nbyte, offset_byte;
- { int fd, status;
-
- if((fd = open(name, 1)) == -1)
- fd = creat(name, 0644);
-
- lseek(fd, (long)offset_byte, 1);
-
- status = write(fd, x, nbyte);
- if(status == -1)
- { printf("ERROR: Error writing file %s\n", name);
- exit(-1);
- }
-
- status = close(fd);
- if(status == -1)
- { printf("ERROR: Error closing %s after write\n", name);
- exit(-1);
- }
- }
-
- /****************************************************************/
-
- #define PAGE_SIZE 262144
-
- int cread2(name, x)
- char *name, **x;
- {
- int fd, status, nbyte, nbyte_tot = 0;
- char *xx;
-
- fd = open(name, 0);
- if(fd == -1) {
- printf("ERROR: Error opening %s for read\n", name);
- exit(-1);
- }
-
- if(!(*x = malloc(PAGE_SIZE))) {
- printf("ERROR: Not enough room for malloc\n");
- exit(-1);
- }
- while(1) {
-
- xx = *x + nbyte_tot;
- nbyte_tot += ( nbyte = read(fd, xx, PAGE_SIZE) );
-
- if(nbyte < 0) {
- printf("ERROR: Error reading file %s\n", name);
- exit(-1);
- }
-
- else if( nbyte < PAGE_SIZE ) {
- if(!(*x = realloc(*x, (unsigned)nbyte_tot))) {
- printf("ERROR: Not enough room for malloc\n");
- exit(-1);
- }
- break;
- }
-
- else
- if(!(*x = realloc(*x, (unsigned)(nbyte_tot + PAGE_SIZE)))) {
- printf("ERROR: Not enough room for malloc\n");
- exit(-1);
- }
- }
-
- status = close(fd);
- if(status == -1) {
- printf("ERROR: Error closing %s after read\n", name);
- exit(-1);
- }
-
- return(nbyte_tot);
-
- }
-