1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <mqueue.h>
#include <errno.h>
typedef struct {
char *f_mqname;
int f_verbose;
int f_helper;
} mqueue_params;
mqueue_params g_params;
void helper(char *progname)
{
printf("Usage: %s [OPTS] VALUE\n\n"
"Version: 0.0.1 \n"
"-m mqueue name\n"
"-v verbose output\n"
"-h help options\n"
"\n"
, progname);
}
#define full_mqpath_len 1024
int main(int argc, char **argv)
{
//stack params
int ret=0;
int c,err;
int i;
int mq_fd;
char full_mqpath[full_mqpath_len];
int prio;
struct mq_attr attr;
char *pop_val=NULL;
int pop_size;
memset(&g_params,0,sizeof(g_params));
//process arguments
while ((c = getopt(argc, argv, "m:vh")) != -1)
{
switch (c)
{
case 'm':
g_params.f_mqname = optarg;
break;
case 'v':
g_params.f_verbose = 1;
break;
case 'h':
default:
helper(argv[0]);
exit(1);
}
}
if (g_params.f_helper)
{
helper(argv[0]);
return 0;
}
//set mqueue name
if (g_params.f_mqname == NULL)
{
g_params.f_mqname = "/stack-0";
}
//compose string
memset(&full_mqpath[0], 0, full_mqpath_len);
snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
if (g_params.f_verbose)
{
printf("Full path: %s\n",&full_mqpath);
}
mq_fd = mq_open(g_params.f_mqname,O_RDWR | O_CREAT, 0666, NULL);
err = errno;
if (mq_fd == -1)
{
if (g_params.f_verbose)
{
printf("ERROR:%d,%s\n",err,strerror(err));
}
return -1;
}
//printf("%s\n",g_params.f_mqname);
mq_getattr(mq_fd, &attr);
pop_val = malloc(attr.mq_msgsize+1);
memset(pop_val, 0, attr.mq_msgsize+1);
if (attr.mq_curmsgs > 0)
{
pop_size = mq_receive(mq_fd, pop_val, attr.mq_msgsize, &prio);
printf("%s\n",pop_val);
} else {
ret = -1;
}
close(mq_fd);
free(pop_val);
return ret;
}
|