Short version: I want a way to run somefunction("username") and have it return the user ID associated with username. For example somefunction("root") would return 0.
I’m writing a server program that could potentially use low-numbered ports, so it has to start as root. Obviously, I don’t want it to run as root, so the plan is to let users specify what user the program should run as. The problem is that setuid() requires a user ID and I don’t know how to look up a user ID from a login name. I looked in unistd.h and it seems to only have functions for finding info about the current user.
I know I could just open /etc/passwd, but I’d rather not when there’s bound to be a function for this.
You want
getpwnam.Here’s a complete example I just wrote: