/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that flushes the buffer
after write operation. this program rename.c is executed by command
       lseek.c < lseek.in
   Input file lseek.in contains the following data:1 -10 
*/ 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define SIZE 128

int main() {
    char buf[SIZE+1], s[10], *name;
    int fd, sector;
  
    name = "lseek.c"; 
    buf[SIZE] = '\0'; // null terminate buffer 
    if((fd = open(name,O_RDONLY)) == -1) {
        printf("Cannot open file. \n");
        exit(1);
    }
    while(sector>=0){
        printf("\nBuffer: ");
        fgets(s,10, stdin);
        sector = atoi(s); // get the sector to read
        if(sector > 0) {
            if(lseek(fd,(long)sector*SIZE,SEEK_SET) == -1)
                printf("seek Error \n");
            if(read(fd,buf,SIZE)==0) {
                printf("Sector Out Of Range \n");
            }
            else 
               printf(buf);
         }
    } 
    close(fd);
}
