Check if user is root in C?

后端 未结 3 1479
臣服心动
臣服心动 2021-01-30 20:41

How can I verify if the user is root?

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 21:18

    better to use getuid or geteuid but it is in zconf.h header file and you must enter that like bellow :

    #include 
    #include 
    int main()
    {
    int a;
    a=getuid();
    //or you can use  a=geteuid();
    //euid is effective user id and uid is user id 
    // both euid and uid are zero when you are root user 
    if (a==0){
    printf("you are root user");
    //so you can do what`enter code here`ever `enter code here` you want as root user
    }
    else
    printf("please run the script as root user !");
    return 0;
    } 
    

提交回复
热议问题