1

Is there any C API for validating user name ?

I am taking input of user_name which might not be present at that point in time. Since the user name needs to follow POSIX rules, is there any C API which can do the validation?

1

1 Answer 1

1

I presume you are trying to figure out whether a user name exists on the system you are running on. For that, you are looking for getwpent()/getpwnam() -- check the manual page for details.

Although these return more information than you want, getpwnam() will also (easily) tell you if there is or isn't a password file entry corresponding to a given user name.

(If you are simply looking to make sure a user name contains only valid characters, just make sure it only contains [A-Za-z0-9_]+)

Sign up to request clarification or add additional context in comments.

2 Comments

The user name should probably not be pure numeric; at the least, you can expect confusion if the user name looks like a number (doubly so if the UID is not the same number as the name — yes, you can do it; no, you should not do it!).
Yes, that's true. I suppose the right regexp is [A-Za-z_][A-Za-z_0-9]*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.