How to programmatically get the number of users registered on a Linux machine?

I was wondering if it is possible to programmatically get the number of users registered on a Linux machine in C? I studied and found out about utmp.h, but since not all programs use utmp logging, I did not think it would be accurate enough. Thanks in advance to everyone who wants to help.

EDIT: I apologize for the guys being not more specific, but when I talk about registered users, I mean anyone registered through the shell. Basically, what you get when you run the who command without command line arguments.

+3
source share
8 answers
#include <utmp.h>
#include <err.h>

#define NAME_WIDTH  8

    FILE *ufp;
    int numberOfUsers = 0;
    struct utmp usr;
    ufp = file(_PATH_UTMP);
    while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
    if (*usr.ut_name && *usr.ut_line && *usr.ut_line != '~') {
         numberOfUsers++;
        }
    }

    FILE *file(char *name)
    {
        FILE *ufp;

        if (!(ufp = fopen(name, "r"))) {
            err(1, "%s", name);
        }
        return(ufp);
    }

, utmp, . , !

+3

Linux, , , who. GPL, who, .

. , , , ? , , nm , nm `which who`. who (34 Mac OS X 10.6.4). , , . getutxent, utmpxname getpwuid. , .

-, apropos/man -k? "" users, . (: , BSDism, Linux. apropos - .) users ( 15), , , who, getutxent.

, try getutxent?

+3

getutent utmp . , api.

+1

utmp - .

man 5 utmp

+1

C, , , ?

who | awk -F' ' '{print $1}' | sort -u | wc -l
+1

, utmp .

, utmp, linux ( ) utmp.

, , " "?

0

bash:

$ ps aux | cut -d' ' -f1 | sort -d | uniq | wc -l
0

linux shell "" .

-1

Source: https://habr.com/ru/post/1775736/


All Articles