File: usersmeter.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (42 lines) | stat: -rw-r--r-- 814 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <sys/types.h>
#include <time.h> // nust be included before utmp.h for FreeBSD
#include <utmp.h>


#include "usersmeter.h"


Users getUsers()
{
  Users users;
  users.number = 0;

#ifdef __FreeBSD__
  FILE *ut;
  if ((ut = fopen(_PATH_UTMP, "r")) != NULL)
  {
    struct utmp usr;
    /* only entries with both name and line fields */
    while (fread((char *)&usr, sizeof(usr), 1, ut) == 1)
      if (*usr.ut_name && *usr.ut_line)
        users.number++;
    fclose(ut);
  }
#else
  // __linux__ and __sun__

  // ideas out of the program "uptime"
  setutent();
  struct utmp *utmpstruct;
  while ((utmpstruct = getutent()))
  {
    if ((utmpstruct->ut_type == USER_PROCESS) &&
        (utmpstruct->ut_name[0] != '\0'))
      users.number++;
  }
  endutent();
#endif

  return users;
}