]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ipc/msgtool.c
Add col; regenerate autofiles
[cygwin-apps/cygutils.git] / src / ipc / msgtool.c
1 /*****************************************************************************
2 Excerpt from "Linux Programmer's Guide - Chapter 6"
3 (C)opyright 1994-1995, Scott Burkett
4 *****************************************************************************
5 MODULE: msgtool.c
6 *****************************************************************************
7 A command line tool for tinkering with SysV style Message Queues
8 *****************************************************************************/
9 /*
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.
14 *
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.
19 *
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.
23 *
24 * See the COPYING file for license information.
25 */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "common.h"
32
33 #if HAVE_SYS_IPC_H
34 # include <sys/ipc.h>
35 #endif
36 #if HAVE_SYS_MSG_H
37 # include <sys/msg.h>
38 #endif
39
40 #define MAX_SEND_SIZE 80
41
42 struct mymsgbuf {
43 long mtype;
44 char mtext[MAX_SEND_SIZE];
45 };
46
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);
51 void usage(void);
52
53
54 int main(int argc, char *argv[])
55 {
56 key_t key;
57 int msgqueue_id;
58 struct mymsgbuf qbuf;
59
60 if(argc == 1)
61 usage();
62
63 /* Create unique key via call to ftok() */
64 key = ftok(".", 'm');
65
66 /* Open the queue - create if necessary */
67 if((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
68 perror("msgget");
69 exit(1);
70 }
71
72 switch(tolower(argv[1][0]))
73 {
74 case 's': send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,
75 atol(argv[2]), argv[3]);
76 break;
77 case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2]));
78 break;
79 case 'd': remove_queue(msgqueue_id);
80 break;
81 case 'm': change_queue_mode(msgqueue_id, argv[2]);
82 break;
83
84 default: usage();
85
86 }
87
88 return(0);
89 }
90
91 void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
92 {
93 /* Send a message to the queue */
94 printf("Sending a message ...\n");
95 qbuf->mtype = type;
96 strcpy(qbuf->mtext, text);
97
98 if((msgsnd(qid, (struct msgbuf *)qbuf,
99 strlen(qbuf->mtext)+1, 0)) ==-1)
100 {
101 perror("msgsnd");
102 exit(1);
103 }
104 }
105
106 void read_message(int qid, struct mymsgbuf *qbuf, long type)
107 {
108 /* Read a message from the queue */
109 printf("Reading a message ...\n");
110 qbuf->mtype = type;
111 msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
112
113 printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
114 }
115
116 void remove_queue(int qid)
117 {
118 /* Remove the queue */
119 msgctl(qid, IPC_RMID, 0);
120 }
121
122 void change_queue_mode(int qid, char *mode)
123 {
124 struct msqid_ds myqueue_ds;
125
126 /* Get current info */
127 msgctl(qid, IPC_STAT, &myqueue_ds);
128
129 /* Convert and load the mode */
130 sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
131
132 /* Update the mode */
133 msgctl(qid, IPC_SET, &myqueue_ds);
134 }
135
136 void usage(void)
137 {
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");
143 exit(1);
144 }
This page took 0.042857 seconds and 5 git commands to generate.