Example #1
0
// Getpwents returns all entries in the user databases.
// This is aimed to be thread-safe, that is, if a goroutine is executing this
// function, another goroutine is blocked until it completes.
func Getpwents() []*Passwd {
	pwentMutex.Lock()
	defer pwentMutex.Unlock()
	C.setpwent()
	defer C.endpwent()
	ents := make([]*Passwd, 0, 10)
	for {
		cpw := C.getpwent()
		if cpw == nil {
			break
		}
		ents = append(ents, cpasswd2go(cpw))
	}
	return ents
}
Example #2
0
// Returns a list of all users on the system.
func ListAllUsers() ([]User, error) {
	users := []User{}
	for {
		pwent, err := C.getpwent()
		if pwent == nil {
			if err != nil {
				return nil, err
			} else {
				break
			}
		}
		users = append(users, pwentToUser(pwent))
	}
	C.endpwent()
	return users, nil
}