How to get the username in C/C++ in Linux?

后端 未结 5 501
灰色年华
灰色年华 2020-12-05 10:18

How can I get the actual \"username\" without using the environment (getenv, ...) in a program?

5条回答
  •  佛祖请我去吃肉
    2020-12-05 10:37

    working a little bit through modern c++ specs

    static auto whoAmI = [](){ struct passwd *tmp = getpwuid (geteuid ());
      return tmp ? tmp->pw_name : "onlyGodKnows"; 
    }
    

提交回复
热议问题