]>
cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ipc/msgtool.c
1 /*****************************************************************************
2 Excerpt from "Linux Programmer's Guide - Chapter 6"
3 (C)opyright 1994-1995, Scott Burkett
4 *****************************************************************************
6 *****************************************************************************
7 A command line tool for tinkering with SysV style Message Queues
8 *****************************************************************************/
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * See the COPYING file for license information.
40 #define MAX_SEND_SIZE 80
44 char mtext
[MAX_SEND_SIZE
];
47 void send_message(int qid
, struct mymsgbuf
*qbuf
, long type
, char *text
);
48 void read_message(int qid
, struct mymsgbuf
*qbuf
, long type
);
49 void remove_queue(int qid
);
50 void change_queue_mode(int qid
, char *mode
);
54 int main(int argc
, char *argv
[])
63 /* Create unique key via call to ftok() */
66 /* Open the queue - create if necessary */
67 if((msgqueue_id
= msgget(key
, IPC_CREAT
|0660)) == -1) {
72 switch(tolower(argv
[1][0]))
74 case 's': send_message(msgqueue_id
, (struct mymsgbuf
*)&qbuf
,
75 atol(argv
[2]), argv
[3]);
77 case 'r': read_message(msgqueue_id
, &qbuf
, atol(argv
[2]));
79 case 'd': remove_queue(msgqueue_id
);
81 case 'm': change_queue_mode(msgqueue_id
, argv
[2]);
91 void send_message(int qid
, struct mymsgbuf
*qbuf
, long type
, char *text
)
93 /* Send a message to the queue */
94 printf("Sending a message ...\n");
96 strcpy(qbuf
->mtext
, text
);
98 if((msgsnd(qid
, (struct msgbuf
*)qbuf
,
99 strlen(qbuf
->mtext
)+1, 0)) ==-1)
106 void read_message(int qid
, struct mymsgbuf
*qbuf
, long type
)
108 /* Read a message from the queue */
109 printf("Reading a message ...\n");
111 msgrcv(qid
, (struct msgbuf
*)qbuf
, MAX_SEND_SIZE
, type
, 0);
113 printf("Type: %ld Text: %s\n", qbuf
->mtype
, qbuf
->mtext
);
116 void remove_queue(int qid
)
118 /* Remove the queue */
119 msgctl(qid
, IPC_RMID
, 0);
122 void change_queue_mode(int qid
, char *mode
)
124 struct msqid_ds myqueue_ds
;
126 /* Get current info */
127 msgctl(qid
, IPC_STAT
, &myqueue_ds
);
129 /* Convert and load the mode */
130 sscanf(mode
, "%ho", &myqueue_ds
.msg_perm
.mode
);
132 /* Update the mode */
133 msgctl(qid
, IPC_SET
, &myqueue_ds
);
138 fprintf(stderr
, "msgtool - A utility for tinkering with msg queues\n");
139 fprintf(stderr
, "\nUSAGE: msgtool (s)end <type> <messagetext>\n");
140 fprintf(stderr
, " (r)ecv <type>\n");
141 fprintf(stderr
, " (d)elete\n");
142 fprintf(stderr
, " (m)ode <octal mode>\n");
This page took 0.042857 seconds and 5 git commands to generate.