Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C function is used to find out if a user exists on the system?

Tags:

c

posix

Simple question; what is the function or library that i need to use to write a very simple program (in C) (linux, ubuntu) to find out whether a user exists on the system ?

In bash i'd do :

'ls -la /home | grep $user | wc -l'

I think it's posix for C (or python) ? Can anyone help me to get started ?

An example would be perfect like :

 $ doesUserExist John
 0

or

 $ doesUserExist John
 1

Thanks !

like image 787
Disco Avatar asked Jan 19 '23 16:01

Disco


1 Answers

getpwnam is used for all accesses to the passwd database. If it returns NULL, the user doesn't exist.

like image 67
thiton Avatar answered Feb 15 '23 10:02

thiton