946
Chapter 46
#ifdef MSG_EXCEPT
fprintf(stderr, " -x Use MSG_EXCEPT flag\n");
#endif
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int msqid, flags, type;
ssize_t msgLen;
size_t maxBytes;
struct
mbuf msg; /* Message buffer for msgrcv() */
int opt; /* Option character from getopt() */
/* Parse command-line options and arguments */
flags = 0;
type = 0;
while ((opt = getopt(argc, argv, "ent:x")) != -1) {
switch (opt) {
case 'e': flags |= MSG_NOERROR; break;
case 'n': flags |= IPC_NOWAIT; break;
case 't': type = atoi(optarg); break;
#ifdef MSG_EXCEPT
case 'x': flags |= MSG_EXCEPT; break;
#endif
default: usageError(argv[0], NULL);
}
}
if (argc < optind + 1 || argc > optind + 2)
usageError(argv[0], "Wrong number of arguments\n");
msqid = getInt(argv[optind], 0, "msqid");
maxBytes = (argc > optind + 1) ?
getInt(argv[optind + 1], 0, "max-bytes") : MAX_MTEXT;
/* Get message and display on stdout */
msgLen = msgrcv(msqid, &msg, maxBytes, type, flags);
if (msgLen == -1)
errExit("msgrcv");
printf("Received: type=%ld; length=%ld", msg.mtype, (long) msgLen);
if (msgLen > 0)
printf("; body=%s", msg.mtext);
printf("\n");
exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––
Chia sẻ với bạn bè của bạn: