How do I change a user password in C using Linux system calls?

不问归期 提交于 2019-12-08 18:17:33

You can try to use putpwent for this. As Jonathan Leffler said in the comments, you need putspent if you want to update the shadow file.

But the easiest and probably the most portable way to do this would be to just call passwd via system(3) or popen(3).

The first thing I learned when I started writing software on Linux after coming from a background in Windows and OS X development is that command line utilities are to be embraced, not shunned. Even for things that have the option of using a C api or a command line utility, it's often smarter and more reliable to fork and exec (do NOT popen unless you don't care to check whether or not the return code is 0!) than to use the C api.

In fact, calling a command line utility is no different than calling a C api unless you need to eke out every last bit of performance in your application. It's practically an API where the function name is the utility name and the parameters are the command line arguments.

So in answer to your question: try exec passwd and it'll take care of all your issues.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!