How to compare these variables by use operator 'AND'

P

pattanawadee

Dear all,

I have a problem about compare the 2 variables by use operator
'AND' while one variable is the int constant 0x00000002 and the other
one is string '0x202'

for the constant 0x00000002 is the flag of systemcall OPEN following
fcntl.h

#define O_RDWR 0x00000002 /* open for reading and
writing */

and the string '0x202' is the variable which I got from use command
strtok cut string from output of ktruss such as I got
bufft value-->open("testprg.txt", 0x202, 0x8049bb0)

The below is my code whic use to cut string buff


char test[80], **strsysc=NULL;
char *sep = "()\",";
char *word, *brkt;
int i=0;
int aa;

strsysc=(char**)calloc(4,sizeof(char*));
printf("bufft value -->%s\n",buff);
strcpy(test,buff);
printf("test value %s\n",test);
for (word = strtok_r(test, sep, &brkt);
word;
word = strtok_r(NULL, sep, &brkt))
{
strsysc=word;
printf("word==>%s\n",strsysc);
i=i+1;
}

from code I got strsysc[2] = '0x202'
and I'd like to use 'AND' to compare it

like this if(0x00000002 & strsysc[2])
I cannot use because strsysc[2] is string and I try to use
atoi[strsysc[2]] the runtime error said Memory fault.

Please give me suggestion about this case

Regards,
Pattanawadee
 
A

Artie Gold

pattanawadee said:
Dear all,

I have a problem about compare the 2 variables by use operator
'AND' while one variable is the int constant 0x00000002 and the other
one is string '0x202'

for the constant 0x00000002 is the flag of systemcall OPEN following
fcntl.h

#define O_RDWR 0x00000002 /* open for reading and
writing */

and the string '0x202' is the variable which I got from use command
strtok cut string from output of ktruss such as I got
bufft value-->open("testprg.txt", 0x202, 0x8049bb0)

The below is my code whic use to cut string buff


char test[80], **strsysc=NULL;
char *sep = "()\",";
char *word, *brkt;
int i=0;
int aa;

strsysc=(char**)calloc(4,sizeof(char*));
printf("bufft value -->%s\n",buff);
strcpy(test,buff);
printf("test value %s\n",test);
for (word = strtok_r(test, sep, &brkt);
word;
word = strtok_r(NULL, sep, &brkt))
{
strsysc=word;
printf("word==>%s\n",strsysc);
i=i+1;
}

from code I got strsysc[2] = '0x202'
and I'd like to use 'AND' to compare it

like this if(0x00000002 & strsysc[2])
I cannot use because strsysc[2] is string and I try to use
atoi[strsysc[2]] the runtime error said Memory fault.

Please give me suggestion about this case

Use `strtol' instead. It will deal with numbers rendered in hex
properly.

HTH,
--ag
 
B

Barry Schwarz

Dear all,

I have a problem about compare the 2 variables by use operator
'AND' while one variable is the int constant 0x00000002 and the other
one is string '0x202'

There is no AND operator in C. There are, however, two operators
which have the word "and" in their titles. It is a shame you don't
tell us which one you mean until some 40 lines later.
for the constant 0x00000002 is the flag of systemcall OPEN following
fcntl.h

#define O_RDWR 0x00000002 /* open for reading and
writing */

and the string '0x202' is the variable which I got from use command
strtok cut string from output of ktruss such as I got
bufft value-->open("testprg.txt", 0x202, 0x8049bb0)

The below is my code whic use to cut string buff


char test[80], **strsysc=NULL;
char *sep = "()\",";
char *word, *brkt;
int i=0;
int aa;

strsysc=(char**)calloc(4,sizeof(char*));

You did not include stdlib.h which causes the call to calloc to invoke
undefined behavior. If you hadn't included the unnecessary and
undesirable cast, the compiler would have issued a diagnostic warning
you about this.
printf("bufft value -->%s\n",buff);

Give us a clue, what is buff?
strcpy(test,buff);
printf("test value %s\n",test);
for (word = strtok_r(test, sep, &brkt);

Where is the prototype for strtok_r? What is its return type.
word;
word = strtok_r(NULL, sep, &brkt))
{
strsysc=word;
printf("word==>%s\n",strsysc);
i=i+1;
}

from code I got strsysc[2] = '0x202'


Did you mean "0x202"? If the string actually starts with 0x, atoi
will always return a 0 value since x is not a decimal digit.
and I'd like to use 'AND' to compare it

like this if(0x00000002 & strsysc[2])

& is the bit-wise and operator. It will not tell you if the two
values are equal. It will only tell you if their binary
representations have at least one bit in common on or not.
I cannot use because strsysc[2] is string and I try to use
atoi[strsysc[2]] the runtime error said Memory fault.

Did you mean atoi(strsysc[2])? The statement
if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
should test the bits of the value represented by strsysc[2].
Please give me suggestion about this case

Regards,
Pattanawadee



<<Remove the del for email>>
 
P

pattanawadee

Barry Schwarz said:
On 21 Sep 2003 21:41:23 -0700, (e-mail address removed)
(pattanawadee) wrote:
You did not include stdlib.h which causes the call to calloc to invoke
undefined behavior. If you hadn't included the unnecessary and
undesirable cast, the compiler would have issued a diagnostic warning
you about this.

sorry !! I cut only some of my code I alredy include stdlib.h.
Give us a clue, what is buff?

bufft value-->open("testprg.txt", 0x202, 0x8049bb0)
if up to what the systemcall is used in this case is case I want to
catch the flag of system call OPEN from buff value it is '0x202'

Did you mean atoi(strsysc[2])? The statement
if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
should test the bits of the value represented by strsysc[2].

I try test this line follow your suggestion but it still tell me
on runtime error that 'Memory Falut'


What I am doing is I try to modified the ktruss command (file dump.c)
on netbsd
my problem is I cannot find where in dump.c I can get the flag value
of system call OPEN I only can find how to get buff (such as
open("testprg.txt", 0x202, 0x8049bb0) so, I have to cut this for get
the flag value (0x202) and I would like to check if it is the O_RDWR
flag which in fcntl.h define to 0x00000002
so I use & operator.
I test if (0x00000002 & 0x202) it true.
So that means it flag O_RDWR but what I get is string '0x202' so I
cannot..use (0x00000002 & strsysc[2])
I try if (0x00000002 & (int)strtol(strsysc[2], NULL, 16)) it still
tell me runtime error Memory fault
 
B

Barry Schwarz

Did you mean atoi(strsysc[2])? The statement
if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
should test the bits of the value represented by strsysc[2].

I try test this line follow your suggestion but it still tell me
on runtime error that 'Memory Falut'

You need to provide a reasonably small (less than 500 lines)
compilable example that demonstrates the problem you are having.


<<Remove the del for email>>
 
P

pattanawadee

Dear Sir,

Now I can solve my problem. It's my fault I don't think that
sometime the value of strsysc[2] is NULL so, it can make error like
that.
Thank you so much for your good suggestion
if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
that help me so much for my problem

Best Regards,
Pattanawadee

Barry Schwarz said:
Did you mean atoi(strsysc[2])? The statement
if (0x00000002 & (int)strtol(strsysc[2], NULL, 16))
should test the bits of the value represented by strsysc[2].

I try test this line follow your suggestion but it still tell me
on runtime error that 'Memory Falut'

You need to provide a reasonably small (less than 500 lines)
compilable example that demonstrates the problem you are having.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top