/*
 * ipcsfs2 - Using the file system for inter-process communication
 *           Fixes the file position pointer problem in ipcfs1.c
 *           by using lseek to reset the pointer to the beginning
 *           Shows the frustration with using the file system for
 *           IPC by needing to move the file position pointer around
 */

#include <fcntl.h>     // needed for open system call
#include <stdio.h>     // needed for printf
#include <stdlib.h>    // needed for exit
#include <string.h>    // nneded for strlen
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h>  // needed for wait system call
#include <unistd.h>    // needed for fork, read, write, close system calls

int main()
{
    // Open a file for reading and writing
    // Create it if it doesn't exist
    int myfile = open("MYFILE", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

    pid_t pid = fork(); // fork into 2 processes

    if (pid < 0) // error
    {
        printf("ERROR: COULD NOT FORK\n");
        exit(EXIT_FAILURE);
    }
    else if (pid == 0) // child
    {
        /* Write to parent */
        char *data = "HELLO";
        write(myfile, data, strlen(data));
        printf("Child wrote %s\n", data);

        /* Close file */
        close(myfile);

        return 0; // Return success
    }
    else // parent
    {
        /* Wait for child */
        wait(0);

        /* Reset file position pointer */
        lseek(myfile, 0, SEEK_SET);

        /* Read data */
        char data[32];
        read(myfile, data, sizeof(data));
        printf("Parent received %s from child\n", data);

        /* Close file */
        close(myfile);
    }
    return 0; // Return success
}
