468,249 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

How to compare these variables by use operator 'AND'

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[i]=word;
printf("word==>%s\n",strsysc[i]);
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
Nov 13 '05 #1
5 2073
pattanawadee wrote:
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[i]=word;
printf("word==>%s\n",strsysc[i]);
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


--
Artie Gold -- Austin, Texas

Nov 13 '05 #2
On 21 Sep 2003 21:41:23 -0700, sp******@unicorn.eng.psu.ac.th
(pattanawadee) wrote:
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[i]=word;
printf("word==>%s\n",strsysc[i]);
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>>
Nov 13 '05 #3
Barry Schwarz <sc******@deloz.net> wrote in message news:<bk**********@216.39.135.208>...
On 21 Sep 2003 21:41:23 -0700, sp******@unicorn.eng.psu.ac.th
(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.
printf("bufft value -->%s\n",buff);
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


Please give me suggestion about this case

Regards,
Pattanawadee


<<Remove the del for email>>

Nov 13 '05 #4
On 22 Sep 2003 09:11:52 -0700, sp******@unicorn.eng.psu.ac.th
(pattanawadee) wrote:
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>>
Nov 13 '05 #5
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 <sc******@deloz.net> wrote in message news:<bk**********@216.39.135.80>...
On 22 Sep 2003 09:11:52 -0700, sp******@unicorn.eng.psu.ac.th
(pattanawadee) wrote:
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>>

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.