home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_11 / 8n11055a < prev    next >
Text File  |  1990-09-18  |  863b  |  44 lines

  1.  
  2.  
  3. func()
  4. {
  5.     int msqid, msglen;
  6.     long msgtype;
  7.     char string[80];
  8.     struct mssg  {
  9.         long mtype;
  10.         char mtext[BUFSIZ];
  11.     } msgbuf;
  12.  
  13.     /* Ask the user if their looking
  14.      * for a particular message type.
  15.     */
  16.     printf("Enter message type or hit <RETURN> for the \
  17. first message on queue: ");
  18.  
  19.     gets(string);
  20.     if (*string == NULL)
  21.         msgtype=0;
  22.     else
  23.         sscanf(string,"%ld",&msgtype));
  24.  
  25.     /* Attempt to receive a message.  The kernel 
  26.      * will put the process to sleep until
  27.      * a message arrives.
  28.     */
  29.     if ((msglen=msgrcv(msqid, &msgbuf, BUFSIZ, msgtype, 0)) == -1)  {
  30.         /* The perror(3C) function prints the 
  31.          * text of the error number contained 
  32.          * in the external integer errno.
  33.         */
  34.             perror("msgget() failed: ");
  35.             return(-1);
  36.     }
  37.  
  38.     /* NULL terminate the message */
  39.     msgbuf.mtext[msglen]=NULL;
  40.  
  41.     return(0);
  42. }
  43.  
  44.