Skip to content

Commit

Permalink
lslogins: merge read_utmp() code
Browse files Browse the repository at this point in the history
The code is used only in lslogins, so it does not make sense to
maintain it in libcommon.

Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Jul 16, 2015
1 parent 2b8a9bd commit a6bf40e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 111 deletions.
1 change: 0 additions & 1 deletion include/Makemodule.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ dist_noinst_HEADERS += \
include/pt-sgi.h \
include/pt-sun.h \
include/randutils.h \
include/readutmp.h \
include/rpmatch.h \
include/setproctitle.h \
include/statfs_magic.h \
Expand Down
28 changes: 0 additions & 28 deletions include/readutmp.h

This file was deleted.

3 changes: 1 addition & 2 deletions lib/Makemodule.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ libcommon_la_SOURCES = \
lib/timeutils.c \
lib/ttyutils.c \
lib/exec_shell.c \
lib/strv.c \
lib/readutmp.c
lib/strv.c

if LINUX
libcommon_la_SOURCES += \
Expand Down
78 changes: 0 additions & 78 deletions lib/readutmp.c

This file was deleted.

34 changes: 32 additions & 2 deletions login-utils/lslogins.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <shadow.h>
Expand All @@ -35,7 +36,6 @@
#include <signal.h>
#include <err.h>
#include <limits.h>

#include <search.h>

#include <libsmartcols.h>
Expand All @@ -56,7 +56,6 @@
#include "optutils.h"
#include "pathnames.h"
#include "logindefs.h"
#include "readutmp.h"
#include "procutils.h"

/*
Expand Down Expand Up @@ -476,6 +475,37 @@ static struct utmp *get_last_btmp(struct lslogins_control *ctl, const char *user

}

static int read_utmp(char const *file, size_t *nents, struct utmp **res)
{
size_t n_read = 0, n_alloc = 0;
struct utmp *utmp = NULL, *u;

if (utmpname(file) < 0)
return -errno;

setutent();
errno = 0;

while ((u = getutent()) != NULL) {
if (n_read == n_alloc) {
n_alloc += 32;
utmp = xrealloc(utmp, n_alloc * sizeof (struct utmp));
}
utmp[n_read++] = *u;
}
if (!u && errno) {
free(utmp);
return -errno;
}

endutent();

*nents = n_read;
*res = utmp;

return 0;
}

static int parse_wtmp(struct lslogins_control *ctl, char *path)
{
int rc = 0;
Expand Down

4 comments on commit a6bf40e

@sgn
Copy link
Contributor

@sgn sgn commented on a6bf40e Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

This change moves read_utmp from lib/readutmp.c (which is GPLv3+)tologin-utils/lslogins.c`, which is GPLv2+.

Is it fine to be done like this?

Sorry if this was clarified before.

@karelzak
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's not the perfect way, but I'm not sure if it makes sense to ask the original author of a few generic lines of code for permission.

We can rewrite it, but the result will be again while ((u = getutent()) { realloc() } ... because it's how libc utmp.h API is designed. Maybe I'll do it avoid a licence issue.

@karelzak
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now when I see the code, it's possible to consolidate it for lslogins.c. I'll do it.

@karelzak
Copy link
Collaborator Author

@karelzak karelzak commented on a6bf40e Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by commit 9138295.

@sgn thanks for your report.

Please sign in to comment.