home *** CD-ROM | disk | FTP | other *** search
/ Los Alamos National Laboratory / LANL_CD.ISO / software / compres / src / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-30  |  2.5 KB  |  118 lines

  1. /****************************************************************
  2.  
  3. COPYRIGHT (C) 1992 UNIVERSITY OF CALIFORNIA
  4.  
  5. ***************************************************************/
  6.  
  7. #include <sys/types.h>
  8. #include <sys/uio.h>
  9. #include <malloc.h>
  10. #include <stdio.h>
  11. long lseek();
  12.  
  13. cread(name, x, nbyte, offset_byte)
  14. char *name, *x;
  15. int nbyte, offset_byte;
  16. {  int fd, status;
  17.  
  18.    fd = open(name, 0);
  19.    if(fd == -1)
  20.    {  printf("ERROR: Error opening %s for read\n", name);
  21.       exit(-1);
  22.    }
  23.  
  24.    lseek(fd, (long)offset_byte, 1);
  25.  
  26.    status = read(fd, x, nbyte);
  27.    if(status == -1)
  28.    {  printf("ERROR: Error reading file %s\n", name);
  29.       exit(-1);
  30.    }
  31.  
  32.    status = close(fd);
  33.    if(status == -1) 
  34.    {  printf("ERROR:  Error closing %s after read\n", name);
  35.       exit(-1);
  36.    }
  37. }
  38.  
  39. /**********************************************************************/
  40.  
  41. cwrite(name, x, nbyte, offset_byte)
  42. char *name, *x;
  43. int nbyte, offset_byte;
  44. {  int fd, status;
  45.  
  46.   if((fd = open(name, 1)) == -1)
  47.       fd = creat(name, 0644);
  48.  
  49.    lseek(fd, (long)offset_byte, 1);
  50.  
  51.    status = write(fd, x, nbyte);
  52.    if(status == -1)
  53.    {  printf("ERROR: Error writing file %s\n", name);
  54.       exit(-1);
  55.    }
  56.  
  57.    status = close(fd);
  58.    if(status == -1) 
  59.    {  printf("ERROR:  Error closing %s after write\n", name);
  60.       exit(-1);
  61.    }
  62.  }
  63.  
  64. /****************************************************************/
  65.  
  66. #define PAGE_SIZE 262144
  67.  
  68. int cread2(name, x)
  69. char *name, **x;
  70. {
  71.     int fd, status, nbyte, nbyte_tot = 0;
  72.     char *xx;
  73.  
  74.     fd = open(name, 0);
  75.     if(fd == -1) {
  76.         printf("ERROR: Error opening %s for read\n", name);
  77.         exit(-1);
  78.     }
  79.  
  80.     if(!(*x = malloc(PAGE_SIZE))) {
  81.         printf("ERROR: Not enough room for malloc\n");
  82.         exit(-1);
  83.     }
  84.     while(1) {
  85.  
  86.         xx = *x + nbyte_tot;
  87.         nbyte_tot += ( nbyte = read(fd, xx, PAGE_SIZE) );
  88.  
  89.         if(nbyte < 0) {
  90.             printf("ERROR: Error reading file %s\n", name);
  91.             exit(-1);
  92.     }
  93.  
  94.         else if( nbyte < PAGE_SIZE ) {
  95.             if(!(*x = realloc(*x, (unsigned)nbyte_tot))) {
  96.                 printf("ERROR: Not enough room for malloc\n");
  97.                 exit(-1);
  98.         }
  99.             break;
  100.     }
  101.  
  102.         else
  103.             if(!(*x = realloc(*x, (unsigned)(nbyte_tot + PAGE_SIZE)))) {
  104.                 printf("ERROR: Not enough room for malloc\n");
  105.                 exit(-1);
  106.         }
  107.     }
  108.  
  109.     status = close(fd);
  110.     if(status == -1) {
  111.         printf("ERROR:  Error closing %s after read\n", name);
  112.         exit(-1);
  113.     }
  114.  
  115.     return(nbyte_tot);
  116.  
  117. }
  118.