#include #include #include #include #include #include #include #include #include #include "err.h" #include "mesg.h" using namespace std; int msg_qid; void exit_server(int sig) { if (msgctl(msg_qid, IPC_RMID, 0) == -1) syserr("msgctl RMID"); exit(0); } int main() { Mesg mesg; int n, filefd; if (signal(SIGINT, exit_server) == SIG_ERR) syserr("signal"); if ((msg_qid = msgget(MKEY, 0666 | IPC_CREAT | IPC_EXCL)) == -1) syserr("msgget"); for(;;) { if ((n = msgrcv(msg_qid, &mesg, MAXMESGDATA, 1L, 0)) < 0) syserr("msgrcv"); mesg.mesg_data[n] = '\0'; /* null terminate filename */ mesg.mesg_type = 2L; /* send messages of this type */ if ( (filefd = open(mesg.mesg_data, 0)) < 0) { cerr << "Can't open: `" << mesg.mesg_data << "'" << endl; } else { cerr << "Sending: `" << mesg.mesg_data << "' "; while ( (n = read(filefd, mesg.mesg_data, MAXMESGDATA)) > 0) { cout << '#'; if (msgsnd(msg_qid, (char *) &mesg, n, 0) != 0) syserr("msgsnd"); } cout << endl; close(filefd); if (n < 0) syserr("read"); } /* Send a message with a length of 0 to signalize the end. */ if (msgsnd(msg_qid, (char *) &mesg, 0, 0) != 0) syserr("msgsnd"); } }