#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

/**
 * 打印错误信息，并且退出
 **/
void failed(char* s);

/**
 * 移动读取文件的位置
 * */
void printpoc(char* string,int fildes);

int main(void)
{
    int fd;
    int pid;
    char buf[10];
    if ((fd = open("test.c",O_RDONLY)) < 0) {
        failed("open failed");
    }
    read(fd,buf,10);/* */
    printpoc("Before fork",fd);
    if ((pid = fork()) < 0) {
        failed("fork failed");
    }else if(!pid){
        printpoc("child before read",fd);
        read(fd,buf,10);
        printpoc("child after read",fd);
    }else{
        wait(NULL);
        printpoc("parent after read",fd);
    }
    return 0;
}
void failed(char* s){
    perror(s);
    exit(1);
}

void printpoc(char * string,int fildes){
    long pos;
    if ((pos = lseek(fildes,0L,1)) < 0L) {
        failed("lseek failed");
    }
    printf("%s:%ld\n",string,pos);
}
