Getting user's uid with C

hi,

I need get current user uid with C. My program want root's permission to be execute. For exemple:
1
2
3
4
5
6
7
8
if (uid != 0){
    printf("You is not a root user!\n");
    exit;
    return 1;
} else {
    printf("Hi, root user!\n");
    return 0;
}


I'm using Ubuntu Linux 11.4.

Thanx.
thank you, man.

1
2
3
4
5
6
7
8
void isRoot(){
	if (getuid() != 0){
		printf("you is not a root!\n");
	} else {
		printf("you is a root!\n");
	}

}


but, I need return a value now. For exemplo:

1
2
3
4
5
6
if (getuid() != 0){
		printf("you is not a root!\n");
                return true;
	} else {
		printf("you is a root!\n");
                return false;


for to use with function. For example:

1
2
3
4
5
if (isRoot() == true){
     ...
} else {
   ...
}


thank you.
So just return it?

bool isRoot(void) {return getuid()==0;}
What's wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

bool isRoot(){
	if (getuid() != 0){
		return 1;
	} else {
		return 0;
	}

}

int main(){
	if (isRoot() == 0){
		printf("root");
	} else {
		printf("other");
	}
	return 0;
}


Thanks.
1) You didn't include <stdbool.h>
2) Your isRoot function doesn't look like the one I posted.
is this?

#include <stdio.h>
#include <stdbool.h>

bool isRoot(){
if (getuid() != 0){
return getuid()==0;
} else {
return getuid()==1;
}

}

int main(){
if (isRoot() == 0){
printf("root\n");
} else {
printf("other\n");
}
return 0;
}

??

is wrong :-(

andre@Andre:~$ gcc lib.c -o lib
andre@Andre:~$ ./lib
root
This is how it should look:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

bool isRoot(void) {return getuid()==0;}

int main(void)
{
  if (isRoot())printf("root\n");
  else printf("other\n");
  return 0;
}


Note that 0 is treated as false and all other values as true.
That's why if (isRoot() == 0) is not correct in this case.
Thank you, man. And sorry for my bad english, i'm from brazil.
Topic archived. No new replies allowed.