test_lab_7.cc
void
write1(const char *d, const char *f, int start, int n, char c)
{
  int fd;
  char name[512];

  sleep(1);

  sprintf(name, "%s/%s", d, f);
  fd = open(name, O_WRONLY|O_CREAT, 0666);
  if (fd < 0 && errno == EEXIST)
    fd = open(name, O_WRONLY, 0666);
  if(fd < 0){
    fprintf(stderr, "test-lab-4-a: open(%s): %s\n",
            name, strerror(errno));
    exit(1);
  }
  if(lseek(fd, start, 0) != (off_t) start){
    fprintf(stderr, "test-lab-4-a: lseek(%s, %d): %s\n",
            name, start, strerror(errno));
    exit(1);
  }
  for(int i = 0; i < n; i++){
    if(write(fd, &c, 1) != 1){
      fprintf(stderr, "test-lab-4-a: write(%s): %s\n",
              name, strerror(errno));
      exit(1);
    }
    if(fsync(fd) != 0){
      fprintf(stderr, "test-lab-4-a: fsync(%s): %s\n",
              name, strerror(errno));
      exit(1);
    }
  }
  if(close(fd) != 0){
    fprintf(stderr, "test-lab-4-a: close(%s): %s\n",
            name, strerror(errno));
    exit(1);
  }
}
// write n characters starting at offset start,
// one at a time.
write n characters starting at offset start,
one at a time.
write n characters starting at offset start one at a time .
void
write1(const char *d, const char *f, int start, int n, char c)
{
  int fd;
  char name[512];
  sleep(1);
  sprintf(name, "%s/%s", d, f);
  fd = open(name, O_WRONLY|O_CREAT, 0666);
  if (fd < 0 && errno == EEXIST)
    fd = open(name, O_WRONLY, 0666);
  if(fd < 0){
    fprintf(stderr, "test-lab-4-a: open(%s): %s\n",
            name, strerror(errno));
    exit(1);
  }
  if(lseek(fd, start, 0) != (off_t) start){
    fprintf(stderr, "test-lab-4-a: lseek(%s, %d): %s\n",
            name, start, strerror(errno));
    exit(1);
  }
  for(int i = 0; i < n; i++){
    if(write(fd, &c, 1) != 1){
      fprintf(stderr, "test-lab-4-a: write(%s): %s\n",
              name, strerror(errno));
      exit(1);
    }
    if(fsync(fd) != 0){
      fprintf(stderr, "test-lab-4-a: fsync(%s): %s\n",
              name, strerror(errno));
      exit(1);
    }
  }
  if(close(fd) != 0){
    fprintf(stderr, "test-lab-4-a: close(%s): %s\n",
            name, strerror(errno));
    exit(1);
  }
}
void write1 ( const char * d , const char * f , int start , int n , char c ) { int fd ; char name [ 512 ] ; sleep ( 1 ) ; sprintf ( name , " " , d , f ) ; fd = open ( name , O_WRONLY | O_CREAT , 0666 ) ; if ( fd < 0 && errno == EEXIST ) fd = open ( name , O_WRONLY , 0666 ) ; if ( fd < 0 ) { fprintf ( stderr , " \n " , name , strerror ( errno ) ) ; exit ( 1 ) ; } if ( lseek ( fd , start , 0 ) != ( off_t ) start ) { fprintf ( stderr , " \n " , name , start , strerror ( errno ) ) ; exit ( 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( write ( fd , & c , 1 ) != 1 ) { fprintf ( stderr , " \n " , name , strerror ( errno ) ) ; exit ( 1 ) ; } if ( fsync ( fd ) != 0 ) { fprintf ( stderr , " \n " , name , strerror ( errno ) ) ; exit ( 1 ) ; } } if ( close ( fd ) != 0 ) { fprintf ( stderr , " \n " , name , strerror ( errno ) ) ; exit ( 1 ) ; } }
write n characters starting at offset start,
one at a time.
write n characters starting at offset start one at a time .