Qnx message prefix - c

I can not implement messaging between processes in QNX with the mechanism of the perfixes. I have tried many ways but all in vain.
#define PREFIX_POSITION "stat_pos"
char receiveBuffer = 0;
char replyBuffer[25];
name_attach_t *positionPrefix;
dispatch_t *dpp;
int rcvid;
dpp = dispatch_create();
if (positionPrefix = name_attach(NULL, PREFIX_POSITION, 0) == NULL){
printf("prefix - null\n");
return EXIT_FAILURE;
}
positionPrefix = name_attach(dpp, PREFIX_POSITION, 0);
while(true){
rcvid = MsgReceive(positionPrefix->chid, &receiveBuffer, sizeof(receiveBuffer), NULL);
switch(receiveBuffer){
case 'Q':
//Do something
break;
case 'H':
//Do something
break;
case 'D':
//Do something
break;
case 'S':
//Do something
break;
}
MsgReply(rcvid, REPLY_INDEX, &replyBuffer, sizeof(replyBuffer));
receiveBuffer = 0;
}
return 0;
}
name_attach () function always returns null. What could be the problem?

Related

mandatory and optional options with argc and argv [duplicate]

I have this piece of code in C
while((i = getopt(argc, argv, ":p:h:s:n:l:f:SLNF")) != -1)
switch(i){
case 'p': printf("Porta obbligatoria\n");
break;
case 'h': printf("hostname\n");
break;
case 's': printf("Surname\n");
break;
case 'n': printf("Name\n");
break;
case 'l': printf("Login\n");
break;
case 'f': printf("Faculty\n");
break;
case 'S': printf("Print Surname\n");
break;
case 'L': printf("Print Login\n");
break;
case 'N': printf("Print First name\n");
break;
case 'F': printf("Print Faculty\n");
break;
case '?': printf("USAGE\n");
break;
default: printf("USAGE default\n");
break;
}
return 0;
}
How can I have only one mandatory parameter? In my case is p.
For example:
./MyProgram -p 80 -h 127.0.0.1
Result ok.
./MyProgram -h 127.0.0.1
Error because missing -p
Only -p.
Thanks in advance.
Usually you use the while loop to store the values and then check the mandatory options after the loop:
int p = -1;
while((i = getopt(argc, argv, ":p:h:s:n:l:f:SLNF")) != -1)
switch(i){
case 'p': p = (int)atol(optarg);
break;
<skipped a few options >
default: printf("USAGE default\n");
break;
}
// Check mandatory parameters:
if (p == -1) {
printf("-p is mandatory!\n");
exit 1;
}
return 0;
}

What will be the reason for getting -1 as a return value for the getopt?

I'm using getopt function as follows,
char c;
void function(int len,char **data_buff)
{
while ((c = getopt(len, data_buff,
"M:i:h:b:p:s:a:m:c:H:P:f:x:y:l:u:V:D:U:W:O:E:F:R:N:n:B")) != EOF)
{
printf("c==>%c\n", c);
switch (c)
{
case 'M': /** InputMode **/
printf("in case M");
break;
case 'i': / serial input device /
printf("in case i");
break;
case 'B': / bind to incoming UDP stream /
printf("in case B");
break;
case 'V': / Sisnet data server version number /
printf("in case V");
break;
case 'b': / serial input baud rate /
printf("in case b");
break;
case 'a': / Destination caster address /
printf("in case a");
break;
case 'p': / Destination caster port /
printf("in case p");
break;
case 'm': / Destination caster mountpoint for stream upload /
printf("in case m");
break;
case 's': / File name for input data simulation from file /
printf("in case s");
break;
case 'f': / name of an initialization file /
printf("in case f");
break;
case 'x': / user ID to access incoming stream /
printf("in case x");
break;
case 'y': / password to access incoming stream /
printf("in case y");
break;
case 'u': / Sisnet data server user ID /
printf("in case u");
break;
case 'l': / Sisnet data server password /
printf("in case l");
break;
case 'c': / DestinationCaster password for stream upload to mountpoint /
printf("in case c");
break;
case 'H': /* Input host address*/
printf("in case H");
break;
case 'P': / Input port /
printf("in case P");
break;
case 'D': / Source caster mountpoint for stream input /
printf("in case D");
break;
case 'U': / Source caster user ID for input stream access /
printf("in case U");
break;
case 'W': / Source caster password for input stream access /
printf("in case W]");
break;
case 'E': / Proxy Server /
printf("in case E");
break;
case 'F': / Proxy port /
printf("in case F");
break;
case 'R': / maximum delay between reconnect attempts in seconds /
printf("in case R");
break;
case 'O': / OutputMode /
printf("in case O");
break;
case 'n': / Destination caster user ID for stream upload to mountpoint /
printf("in case n");
break;
case 'N': / Ntrip-STR, optional for Ntrip Version 2.0 /
printf("in case N");
break;
case 'h': / print help screen /
printf("in case h");
break;
case '?':
printf("in case ?");
break;
default:
printf("in case default");
break;
}
}
}
I'm passing array of char pointer(as mentioned below) as an argument for the function where the above while is running.
char *ntrip_buff1[19];
ntrip_buff1[0] = "./ntripserver";
ntrip_buff1[1] = "-M";
ntrip_buff1[2] = "1";
ntrip_buff1[3] = "-i";
ntrip_buff1[4] = "/dev/ttymxc5";
ntrip_buff1[5] = "-b";
ntrip_buff1[6] = "115200";
ntrip_buff1[7] = "-O";
ntrip_buff1[8] = "1";
ntrip_buff1[9] = "-a";
ntrip_buff1[10] = "abc.com";
ntrip_buff1[11] = "-p";
ntrip_buff1[12] = "2101";
ntrip_buff1[13] = "-m";
ntrip_buff1[14] = "Mount2";
ntrip_buff1[15] = "-n";
ntrip_buff1[16] = "BroadlySoughtCub";
ntrip_buff1[17] = "-c";
ntrip_buff1[18] = "sauhduhasd";
So I observed that initially it will work fine but, whenever I change the above buffer and pass it to the function as an argument then in that case it is returning -1 always.
For example-
ntrip_buff1[18] = "sauhduhasd";
changed to
ntrip_buff1[18] = "abcd";
So what will be the reason for getting -1 as the return value for the getopt function?
If you are trying to use getopt() to analyze more than one set of arguments in a single run, you have to work out how your particular version of getopt() allows you to do that — if, indeed, it does allow you to do it.
On macOS Big Sur 11.7.1, for example, the manual page documents:
extern int optreset;
…
In order to use getopt() to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, the variable optreset must be set to 1 before the second and each additional set of calls to getopt(), and the variable optind must be initialized […to 1].
Adapting your code a bit and creating an MCVE (Minimal, Complete, Verifiable Example
— or MRE or whatever name SO now uses)
or an
SSCCE (Short, Self-Contained, Correct Example)
— the same idea by a different name — I produced the following code. Note that it has a null pointer at the end of the argument list.
/* SO 7447-0255 */
#include <stdio.h>
#include <unistd.h>
static void function(int len, char **data_buff)
{
int c;
/* Dropped colon after h in option list */
while ((c = getopt(len, data_buff,
"M:i:hb:p:s:a:m:c:H:P:f:x:y:l:u:V:D:U:W:O:E:F:R:N:n:B")) != -1)
{
switch (c)
{
case 'M': /** InputMode **/
printf("in case M [%s]\n", optarg);
break;
case 'i':
/* serial input device */
printf("in case i [%s]\n", optarg);
break;
case 'B':
/* bind to incoming UDP stream */
printf("in case B\n");
break;
case 'V':
/* Sisnet data server version number */
printf("in case V [%s]\n", optarg);
break;
case 'b':
/* serial input baud rate */
printf("in case b [%s]\n", optarg);
break;
case 'a':
/* Destination caster address */
printf("in case a [%s]\n", optarg);
break;
case 'p':
/* Destination caster port */
printf("in case p [%s]\n", optarg);
break;
case 'm':
/* Destination caster mountpoint for stream upload */
printf("in case m [%s]\n", optarg);
break;
case 's':
/* File name for input data simulation from file */
printf("in case s [%s]\n", optarg);
break;
case 'f':
/* name of an initialization file */
printf("in case f [%s]\n", optarg);
break;
case 'x':
/* user ID to access incoming stream */
printf("in case x [%s]\n", optarg);
break;
case 'y':
/* password to access incoming stream */
printf("in case y [%s]\n", optarg);
break;
case 'u':
/* Sisnet data server user ID */
printf("in case u [%s]\n", optarg);
break;
case 'l':
/* Sisnet data server password */
printf("in case l [%s]\n", optarg);
break;
case 'c':
/* DestinationCaster password for stream upload to mountpoint */
printf("in case c [%s]\n", optarg);
break;
case 'H': /* Input host address*/
printf("in case H [%s]\n", optarg);
break;
case 'P':
/* Input port */
printf("in case P [%s]\n", optarg);
break;
case 'D':
/* Source caster mountpoint for stream input */
printf("in case D [%s]\n", optarg);
break;
case 'U':
/* Source caster user ID for input stream access */
printf("in case U [%s]\n", optarg);
break;
case 'W':
/* Source caster password for input stream access */
printf("in case W] [%s]\n", optarg);
break;
case 'E':
/* Proxy Server */
printf("in case E [%s]\n", optarg);
break;
case 'F':
/* Proxy port */
printf("in case F [%s]\n", optarg);
break;
case 'R':
/* maximum delay between reconnect attempts in seconds */
printf("in case R [%s]\n", optarg);
break;
case 'O':
/* OutputMode */
printf("in case O [%s]\n", optarg);
break;
case 'n':
/* Destination caster user ID for stream upload to mountpoint */
printf("in case n [%s]\n", optarg);
break;
case 'N':
/* Ntrip - STR, optional for Ntrip Version 2.0 */
printf("in case N [%s]\n", optarg);
break;
case 'h':
/* print help screen */
printf("in case h\n");
break;
case '?':
printf("in case ?\n");
break;
default:
printf("in case default (option %c)\n", c);
break;
}
}
for (int i = optind; i < len; i++)
printf("Non-option argument %d: [%s]\n", i, data_buff[i]);
}
int main(void)
{
char *ntrip[] =
{
[0] = "./ntripserver",
[1] = "-M",
[2] = "1",
[3] = "-i",
[4] = "/dev/ttymxc5",
[5] = "-b",
[6] = "115200",
[7] = "-O",
[8] = "1",
[9] = "-a",
[10] = "abc.com",
[11] = "-p",
[12] = "2101",
[13] = "-m",
[14] = "Mount2",
[15] = "-n",
[16] = "BroadlySoughtCub",
[17] = "-c",
[18] = "sauhduhasd",
[19] = NULL,
};
enum { NUM_NTRIP = sizeof(ntrip) / sizeof(ntrip[0]) };
printf("Pass 1:\n");
function(NUM_NTRIP-1, ntrip);
printf("Pass 2:\n");
function(NUM_NTRIP-1, ntrip);
/* macOS / *BSD getopt() */
optreset = 1; // Delete this line with GNU getopt
optind = 1;
printf("Pass 3:\n");
function(NUM_NTRIP-1, ntrip);
return 0;
}
When run, it produced:
Pass 1:
in case M [1]
in case i [/dev/ttymxc5]
in case b [115200]
in case O [1]
in case a [abc.com]
in case p [2101]
in case m [Mount2]
in case n [BroadlySoughtCub]
in case c [sauhduhasd]
Pass 2:
Pass 3:
in case M [1]
in case i [/dev/ttymxc5]
in case b [115200]
in case O [1]
in case a [abc.com]
in case p [2101]
in case m [Mount2]
in case n [BroadlySoughtCub]
in case c [sauhduhasd]
Note that Phase 2 without any reset didn't produce any results.
The GNU getopt() manual doesn't document how to reset the argument handling (and doesn't provide the explicit optreset mechanism), but empirically, just resetting optind = 1; works OK. The only thing to be leery of is if you stop the first cycle of parsing part way through the processing of argv[1] and argv[1] is something like -abc (three single-letter options ganged up). You should be OK if you process to the end of the arguments on each cycle.

How to use nested Switch with commandline arguments?

I have my switch case and statements inside my main function as follows:
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
Now I want to make use of nested switch when user passes an argument -S inside the case A can i do it as follows? Is the following procedure correct?
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
switch(c)
{
case 'S':
size = 100;
break;
}
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
In the above code can I use same switch(c) for the nested case also , is the usage of nested switch correct?Thanks in advance.
No, within the outer swich case 'A', variable c can be thought of as constant, therefore your nested switch will not find a case to match and does not have a default case.
If you know your arguments are in a particular order, you can address them directly e.g. argv[1] and argv[2] for the first and second arguments.

Parsing options having a common flag in C

I have a C program where I accept multiple arguments. Here, I have a common flag d for both data-store and disk. Is there a way that I can check for the flags in-order and get the value of store before I check with case d. I've tried various ways like adding a while loop before this to check for s and then enter this loop etc.
static void
ParseOptions(int argc, char* argv[])
{
int c, option_index;
int ind = 0;
while((c = getopt_long(argc, argv, "s:d:",
long_options, &option_index))!= -1) {
ind = optind;
switch(c) {
case 's':
optionStore = true;
store = strdup(optarg);
break;
case 'd':
if(strcmp(store,"datastore") == 0){
printf("In datastore\n");
datastore = strdup(optarg);
}
else if(strcmp(store,"disk") == 0){
printf("In disk\n");
disk = strdup(optarg);
}
break;
default:
exit(-1);
}
}
}
Not sure how to go about this.
You need to store optarg returned for flag d in a temporary variable, and use it after the loop exits to set either disk or datastore:
char *temp_disk_or_datastore;
while((c = getopt_long(argc, argv, "s:d:",
long_options, &option_index))!= -1) {
ind = optind;
switch(c) {
case 's':
optionStore = true;
store = strdup(optarg);
break;
case 'd':
temp_disk_or_datastore = strdup(optarg);
break;
default:
exit(-1);
}
}
if (store == NULL) {
printf("Missing storage option");
exit(-1);
}
if(strcmp(store,"datastore") == 0){
printf("In datastore\n");
datastore = temp_disk_or_datastore;
}
else if(strcmp(store,"disk") == 0){
printf("In disk\n");
disk = temp_disk_or_datastore;
}

Mandatory parameter getopt in C

I have this piece of code in C
while((i = getopt(argc, argv, ":p:h:s:n:l:f:SLNF")) != -1)
switch(i){
case 'p': printf("Porta obbligatoria\n");
break;
case 'h': printf("hostname\n");
break;
case 's': printf("Surname\n");
break;
case 'n': printf("Name\n");
break;
case 'l': printf("Login\n");
break;
case 'f': printf("Faculty\n");
break;
case 'S': printf("Print Surname\n");
break;
case 'L': printf("Print Login\n");
break;
case 'N': printf("Print First name\n");
break;
case 'F': printf("Print Faculty\n");
break;
case '?': printf("USAGE\n");
break;
default: printf("USAGE default\n");
break;
}
return 0;
}
How can I have only one mandatory parameter? In my case is p.
For example:
./MyProgram -p 80 -h 127.0.0.1
Result ok.
./MyProgram -h 127.0.0.1
Error because missing -p
Only -p.
Thanks in advance.
Usually you use the while loop to store the values and then check the mandatory options after the loop:
int p = -1;
while((i = getopt(argc, argv, ":p:h:s:n:l:f:SLNF")) != -1)
switch(i){
case 'p': p = (int)atol(optarg);
break;
<skipped a few options >
default: printf("USAGE default\n");
break;
}
// Check mandatory parameters:
if (p == -1) {
printf("-p is mandatory!\n");
exit 1;
}
return 0;
}

Resources