/* sysV IPC message passing - sender 
   sendmsg_sysV.c
    
    meant to work with recvmsg_sysV:
      start rcvmesg_sysV in background,
      then start sendmsg_sysV
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    #define BUF 1024
    
    struct my_msg_st {
      long int my_msg_type;
      char some_text[BUFSIZ];
    };
    
    int main() {
      int run = 1;
      struct my_msg_st some_data;
      int msqid;
      char buffer[BUF];
      
      msqid = msgget( (key_t)1234, 0666 | IPC_CREAT);
      if (msqid == -1) {
        perror("msgget in sendmsg_sysV failed");
        exit(EXIT_FAILURE);
      }
      
      while (run) {
        printf("Enter some text:");
        fgets(buffer, BUF, stdin);
        some_data.my_msg_type = 1;
        strcpy(some_data.some_text, buffer);
        
        if (msgsnd(msqid, &some_data, BUF, 0) == -1) {
          perror("msgsnd in sendmsg_sysV failed");
          exit(EXIT_FAILURE);
        }
        
        if (strncmp(buffer, "end", 3) == 0) {
          run = 0;
        }
      }
      
      exit(EXIT_SUCCESS);
    }
