summaryrefslogtreecommitdiffstats
path: root/login-utils/lslogins.c
diff options
context:
space:
mode:
authorKarel Zak2014-05-29 11:37:15 +0200
committerKarel Zak2014-05-29 11:46:56 +0200
commitd8b2ddb7bbe5bd818c1f8946873da9a35f1bd275 (patch)
tree25036e3c1f2f1eb3107c69a55a1f51e9b7438ad6 /login-utils/lslogins.c
parentlslogins: improve nologins and shadow usage (diff)
downloadkernel-qcow2-util-linux-d8b2ddb7bbe5bd818c1f8946873da9a35f1bd275.tar.gz
kernel-qcow2-util-linux-d8b2ddb7bbe5bd818c1f8946873da9a35f1bd275.tar.xz
kernel-qcow2-util-linux-d8b2ddb7bbe5bd818c1f8946873da9a35f1bd275.zip
lslogins: remove --sort-by-name
If we really need a sort functionality hardcoded into lslogins(1) then we need a generic sort options (like for lsblk,--sort <column>). Note that it seems that "lslogins | sort --key <col>" is good enough for now as lslogins(1) does not convert any data to human readable non-precise format (like for example lsblk SIZE column etc.). Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'login-utils/lslogins.c')
-rw-r--r--login-utils/lslogins.c77
1 files changed, 21 insertions, 56 deletions
diff --git a/login-utils/lslogins.c b/login-utils/lslogins.c
index a11358eb6..64c10bcc6 100644
--- a/login-utils/lslogins.c
+++ b/login-utils/lslogins.c
@@ -154,11 +154,10 @@ enum {
F_NOPWD = (1 << 2),
F_SYSAC = (1 << 3),
F_USRAC = (1 << 4),
- F_SORT = (1 << 5),
- F_EXTRA = (1 << 6),
- F_FAIL = (1 << 7),
- F_LAST = (1 << 8),
- F_SELINUX = (1 << 9),
+ F_EXTRA = (1 << 5),
+ F_FAIL = (1 << 6),
+ F_LAST = (1 << 7),
+ F_SELINUX = (1 << 8),
};
/*
@@ -257,8 +256,6 @@ struct lslogins_control {
uid_t SYS_UID_MIN;
uid_t SYS_UID_MAX;
- int (*cmp_fn) (const void *a, const void *b);
-
char **ulist;
size_t ulsiz;
@@ -515,7 +512,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
}
} else if ((lslogins_flag & F_SYSAC) &&
- uid < ctl->SYS_UID_MIN || uid > ctl->SYS_UID_MAX) {
+ (uid < ctl->SYS_UID_MIN || uid > ctl->SYS_UID_MAX)) {
errno = EAGAIN;
return NULL;
}
@@ -535,6 +532,9 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
shadow = getspnam(pwd->pw_name);
ulckpwdf();
+ /* required by tseach() stuff */
+ user->uid = pwd->pw_uid;
+
while (n < ncolumns) {
switch (columns[n++]) {
case COL_LOGIN:
@@ -602,7 +602,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
case COL_NOLOGIN:
if (strstr(pwd->pw_shell, "nologin"))
user->nologin = 1;
- else if (pwd->uid) {
+ else if (pwd->pw_uid) {
user->nologin = access("/etc/nologin", F_OK) ||
access("/var/run/nologin", F_OK);
}
@@ -666,12 +666,6 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
break;
}
}
- /* check if we have the info needed to sort */
- if (lslogins_flag & F_SORT) { /* sorting by username */
- if (!user->login)
- user->login = xstrdup(pwd->pw_name);
- } else /* sorting by UID */
- user->uid = pwd->pw_uid;
return user;
}
@@ -682,17 +676,6 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
#define IS_REAL_ERRNO(e) !((e) == ENOENT || (e) == ESRCH || \
(e) == EBADF || (e) == EPERM || (e) == EAGAIN)
-/*
-static void *user_in_tree(void **rootp, struct lslogins_user *u)
-{
- void *rc;
- rc = tfind(u, rootp, ctl->cmp_fn);
- if (!rc)
- tdelete(u, rootp, ctl->cmp_fn);
- return rc;
-}
-*/
-
/* get a definitive list of users we want info about... */
static int str_to_uint(char *s, unsigned int *ul)
@@ -801,6 +784,13 @@ static int get_user(struct lslogins_control *ctl, struct lslogins_user **user,
return 0;
}
+static int cmp_uid(const void *a, const void *b)
+{
+ uid_t x = ((struct lslogins_user *)a)->uid;
+ uid_t z = ((struct lslogins_user *)b)->uid;
+ return x > z ? 1 : (x < z ? -1 : 0);
+}
+
static int create_usertree(struct lslogins_control *ctl)
{
struct lslogins_user *user = NULL;
@@ -811,29 +801,16 @@ static int create_usertree(struct lslogins_control *ctl)
if (get_user(ctl, &user, ctl->ulist[n]))
return -1;
if (user) /* otherwise an invalid user name has probably been given */
- tsearch(user, &ctl->usertree, ctl->cmp_fn);
+ tsearch(user, &ctl->usertree, cmp_uid);
++n;
}
} else {
while ((user = get_next_user(ctl)))
- tsearch(user, &ctl->usertree, ctl->cmp_fn);
+ tsearch(user, &ctl->usertree, cmp_uid);
}
return 0;
}
-static int cmp_uname(const void *a, const void *b)
-{
- return strcmp(((struct lslogins_user *)a)->login,
- ((struct lslogins_user *)b)->login);
-}
-
-static int cmp_uid(const void *a, const void *b)
-{
- uid_t x = ((struct lslogins_user *)a)->uid;
- uid_t z = ((struct lslogins_user *)b)->uid;
- return x > z ? 1 : (x < z ? -1 : 0);
-}
-
static struct libscols_table *setup_table(void)
{
struct libscols_table *tb = scols_new_table();
@@ -1144,7 +1121,6 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
fputs(_(" -o, --output[=<list>] Define the columns to output\n"), out);
fputs(_(" -r, --raw Display the raw table\n"), out);
fputs(_(" -s, --system-accs Display system accounts\n"), out);
- fputs(_(" -t, --sort-by-name Sort output by login instead of UID\n"), out);
fputs(_(" --time-format=<type> Display dates in short, full or iso format\n"), out);
fputs(_(" -u, --user-accs Display user accounts\n"), out);
fputs(_(" -x, --extra Display extra information\n"), out);
@@ -1200,7 +1176,6 @@ int main(int argc, char *argv[])
{ "last", no_argument, 0, OPT_LAST },
{ "raw", no_argument, 0, 'r' },
{ "system-accs", no_argument, 0, 's' },
- { "sort-by-name", no_argument, 0, 't' },
{ "time-format", required_argument, 0, OPT_TIME_FMT },
{ "user-accs", no_argument, 0, 'u' },
{ "version", no_argument, 0, OPT_VER },
@@ -1229,10 +1204,9 @@ int main(int argc, char *argv[])
textdomain(PACKAGE);
atexit(close_stdout);
- ctl->cmp_fn = cmp_uid;
ctl->time_mode = TIME_SHORT_RELATIVE;
- while ((c = getopt_long(argc, argv, "acefg:hl:mno:rstuxzZ",
+ while ((c = getopt_long(argc, argv, "acefg:hl:mno:rsuxzZ",
longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@@ -1287,10 +1261,6 @@ int main(int argc, char *argv[])
ctl->SYS_UID_MAX = getlogindefs_num("SYS_UID_MAX", UL_SYS_UID_MAX);
lslogins_flag |= F_SYSAC;
break;
- case 't':
- ctl->cmp_fn = cmp_uname;
- lslogins_flag |= F_SORT;
- break;
case 'u':
ctl->UID_MIN = getlogindefs_num("UID_MIN", UL_UID_MIN);
ctl->UID_MAX = getlogindefs_num("UID_MAX", UL_UID_MAX);
@@ -1366,13 +1336,8 @@ int main(int argc, char *argv[])
columns[ncolumns++] = i;
} else if (!ncolumns) {
- if (lslogins_flag & F_SORT) {
- columns[ncolumns++] = COL_LOGIN;
- columns[ncolumns++] = COL_UID;
- } else {
- columns[ncolumns++] = COL_UID;
- columns[ncolumns++] = COL_LOGIN;
- }
+ columns[ncolumns++] = COL_UID;
+ columns[ncolumns++] = COL_LOGIN;
columns[ncolumns++] = COL_PGRP;
columns[ncolumns++] = COL_PGID;
columns[ncolumns++] = COL_LAST_LOGIN;