local/fifoserver.c

///////////////////////////////////////////////////////////////////////////////
// Filename: fifoserver.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: together with fifoclient.c: demonstrates how to use FIFOs in UNIX
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/06 02:25:59 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */




// System headers /////////////////////////////////////////// System headers //

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream.h>
#include <string.h>


// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //

const char *fifo_file = "example_fifo";
const mode_t permission =
    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
// = 0666 = rw-rw-rw-


// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //
    /* NONE */



// Main /////////////////////////////////////////////////////////////// Main //

/* 
 * This program displays strings which are written to a FIFO by fifoclient.
 */  

int
main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    char buffer[80];

    // Create FIFO 

    umask(0);

    mkfifo(fifo_file, permission);

    // this would be equivalent:
    // mknod(fifo_file, S_IFIFO|permission, 0);

    while(true)
    {
        ifstream in(fifo_file);
        if(in)
        {
            while(in)
            {
                in.getline(buffer, 80);
                if(strlen(buffer))
                {       
                    cout << "Received: " << buffer << endl;
                    if(strncmp(buffer, "quit", 4) == 0)
                    {
                        unlink(fifo_file);
                        exit(EXIT_SUCCESS);
                    }
                }
            }
        }
        else
        {
            cerr << "can't open fifo file!" << endl;
            exit(EXIT_FAILURE);
        }
    }

    return(EXIT_SUCCESS);
}