OpenCoverage

readutmp.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/readutmp.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* GNU's read utmp module.-
2-
3 Copyright (C) 1992-2001, 2003-2006, 2009-2018 Free Software Foundation, Inc.-
4-
5 This program is free software: you can redistribute it and/or modify-
6 it under the terms of the GNU General Public License as published by-
7 the Free Software Foundation; either version 3 of the License, or-
8 (at your option) any later version.-
9-
10 This program is distributed in the hope that it will be useful,-
11 but WITHOUT ANY WARRANTY; without even the implied warranty of-
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
13 GNU General Public License for more details.-
14-
15 You should have received a copy of the GNU General Public License-
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
17-
18/* Written by jla; revised by djm */-
19-
20#include <config.h>-
21-
22#include "readutmp.h"-
23-
24#include <errno.h>-
25#include <stdio.h>-
26-
27#include <sys/types.h>-
28#include <sys/stat.h>-
29#include <signal.h>-
30#include <stdbool.h>-
31#include <string.h>-
32#include <stdlib.h>-
33#include <stdint.h>-
34-
35#include "xalloc.h"-
36-
37#if USE_UNLOCKED_IO-
38# include "unlocked-io.h"-
39#endif-
40-
41/* Copy UT->ut_name into storage obtained from malloc. Then remove any-
42 trailing spaces from the copy, NUL terminate it, and return the copy. */-
43-
44char *-
45extract_trimmed_name (const STRUCT_UTMP *ut)-
46{-
47 char *p, *trimmed_name;-
48-
49 trimmed_name = xmalloc (sizeof (UT_USER (ut)) + 1);-
50 strncpy (trimmed_name, UT_USER (ut), sizeof (UT_USER (ut)));-
51 /* Append a trailing NUL. Some systems pad names shorter than the-
52 maximum with spaces, others pad with NULs. Remove any trailing-
53 spaces. */-
54 trimmed_name[sizeof (UT_USER (ut))] = '\0';-
55 for (p = trimmed_name + strlen (trimmed_name);-
56 trimmed_name < p && p[-1] == ' ';
trimmed_name < pDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • users
FALSEnever evaluated
p[-1] == ' 'Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • users
0-1
57 *--p = '\0')-
58 continue;
never executed: continue;
0
59 return trimmed_name;
executed 1 time by 1 test: return trimmed_name;
Executed by:
  • users
1
60}-
61-
62/* Is the utmp entry U desired by the user who asked for OPTIONS? */-
63-
64static bool-
65desirable_utmp_entry (STRUCT_UTMP const *u, int options)-
66{-
67 bool user_proc = IS_USER_PROCESS (u);
dead code: ((u)->ut_tv.tv_sec) != 0
((u)-> ut_user )[0]Description
TRUEevaluated 12 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
FALSEnever evaluated
((u)->ut_type == ( 7 ))Description
TRUEevaluated 3 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
FALSEevaluated 9 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
-
68 if ((options & READ_UTMP_USER_PROCESS) && !user_proc)
(options & REA..._USER_PROCESS)Description
TRUEnever evaluated
FALSEevaluated 12 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
!user_procDescription
TRUEnever evaluated
FALSEnever evaluated
0-12
69 return false;
never executed: return 0 ;
0
70 if ((options & READ_UTMP_CHECK_PIDS)
(options & REA...MP_CHECK_PIDS)Description
TRUEevaluated 8 times by 2 tests
Evaluated by:
  • users
  • who
FALSEevaluated 4 times by 1 test
Evaluated by:
  • pinky
4-8
71 && user_proc
user_procDescription
TRUEevaluated 2 times by 2 tests
Evaluated by:
  • users
  • who
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • users
  • who
2-6
72 && 0 < UT_PID (u)
0 < ((u)->ut_pid)Description
TRUEevaluated 2 times by 2 tests
Evaluated by:
  • users
  • who
FALSEnever evaluated
0-2
73 && (kill (UT_PID (u), 0) < 0 && errno == ESRCH))
kill (((u)->ut_pid), 0) < 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 2 tests
Evaluated by:
  • users
  • who
(*__errno_location ()) == 3Description
TRUEnever evaluated
FALSEnever evaluated
0-2
74 return false;
never executed: return 0 ;
0
75 return true;
executed 12 times by 3 tests: return 1 ;
Executed by:
  • pinky
  • users
  • who
12
76}-
77-
78/* Read the utmp entries corresponding to file FILE into freshly--
79 malloc'd storage, set *UTMP_BUF to that pointer, set *N_ENTRIES to-
80 the number of entries, and return zero. If there is any error,-
81 return -1, setting errno, and don't modify the parameters.-
82 If OPTIONS & READ_UTMP_CHECK_PIDS is nonzero, omit entries whose-
83 process-IDs do not currently exist. */-
84-
85#ifdef UTMP_NAME_FUNCTION-
86-
87int-
88read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,-
89 int options)-
90{-
91 size_t n_read = 0;-
92 size_t n_alloc = 0;-
93 STRUCT_UTMP *utmp = NULL;-
94 STRUCT_UTMP *u;-
95-
96 /* Ignore the return value for now.-
97 Solaris' utmpname returns 1 upon success -- which is contrary-
98 to what the GNU libc version does. In addition, older GNU libc-
99 versions are actually void. */-
100 UTMP_NAME_FUNCTION ((char *) file);-
101-
102 SET_UTMP_ENT ();-
103-
104 while ((u = GET_UTMP_ENT ()) != NULL)
(u = getutxent...!= ((void *)0)Description
TRUEevaluated 12 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
FALSEevaluated 3 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
3-12
105 if (desirable_utmp_entry (u, options))
desirable_utmp...y (u, options)Description
TRUEevaluated 12 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
FALSEnever evaluated
0-12
106 {-
107 if (n_read == n_alloc)
n_read == n_allocDescription
TRUEevaluated 9 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
FALSEevaluated 3 times by 3 tests
Evaluated by:
  • pinky
  • users
  • who
3-9
108 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
executed 9 times by 3 tests: utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);
Executed by:
  • pinky
  • users
  • who
9
109-
110 utmp[n_read++] = *u;-
111 }
executed 12 times by 3 tests: end of block
Executed by:
  • pinky
  • users
  • who
12
112-
113 END_UTMP_ENT ();-
114-
115 *n_entries = n_read;-
116 *utmp_buf = utmp;-
117-
118 return 0;
executed 3 times by 3 tests: return 0;
Executed by:
  • pinky
  • users
  • who
3
119}-
120-
121#else-
122-
123int-
124read_utmp (char const *file, size_t *n_entries, STRUCT_UTMP **utmp_buf,-
125 int options)-
126{-
127 size_t n_read = 0;-
128 size_t n_alloc = 0;-
129 STRUCT_UTMP *utmp = NULL;-
130 int saved_errno;-
131 FILE *f = fopen (file, "r");-
132-
133 if (! f)-
134 return -1;-
135-
136 for (;;)-
137 {-
138 if (n_read == n_alloc)-
139 utmp = x2nrealloc (utmp, &n_alloc, sizeof *utmp);-
140 if (fread (&utmp[n_read], sizeof utmp[n_read], 1, f) == 0)-
141 break;-
142 n_read += desirable_utmp_entry (&utmp[n_read], options);-
143 }-
144-
145 saved_errno = ferror (f) ? errno : 0;-
146 if (fclose (f) != 0)-
147 saved_errno = errno;-
148 if (saved_errno != 0)-
149 {-
150 free (utmp);-
151 errno = saved_errno;-
152 return -1;-
153 }-
154-
155 *n_entries = n_read;-
156 *utmp_buf = utmp;-
157-
158 return 0;-
159}-
160-
161#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2