]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/lslogins.c
misc: cosmetics, remove argument from usage(FILE*)
[thirdparty/util-linux.git] / login-utils / lslogins.c
index 76ada7fd2e789284ffe4533e4161b869e46e546e..c15b347f2df79dcca69036e79193b6ba8d776a6f 100644 (file)
@@ -31,7 +31,7 @@
 #include <shadow.h>
 #include <paths.h>
 #include <time.h>
-#include <utmp.h>
+#include <utmpx.h>
 #include <signal.h>
 #include <err.h>
 #include <limits.h>
@@ -243,10 +243,10 @@ static const struct lslogins_coldesc coldescs[] =
 };
 
 struct lslogins_control {
-       struct utmp *wtmp;
+       struct utmpx *wtmp;
        size_t wtmp_size;
 
-       struct utmp *btmp;
+       struct utmpx *btmp;
        size_t btmp_size;
 
        void *usertree;
@@ -410,19 +410,17 @@ again:
        return res;
 }
 
-static struct utmp *get_last_wtmp(struct lslogins_control *ctl, const char *username)
+static struct utmpx *get_last_wtmp(struct lslogins_control *ctl, const char *username)
 {
        size_t n = 0;
-       size_t len;
 
        if (!username)
                return NULL;
 
-       len = strlen(username);
        n = ctl->wtmp_size - 1;
        do {
                if (!strncmp(username, ctl->wtmp[n].ut_user,
-                   len < UT_NAMESIZE ? len : UT_NAMESIZE))
+                            sizeof(ctl->wtmp[0].ut_user)))
                        return ctl->wtmp + n;
        } while (n--);
        return NULL;
@@ -447,40 +445,38 @@ static int require_btmp(void)
        return 0;
 }
 
-static struct utmp *get_last_btmp(struct lslogins_control *ctl, const char *username)
+static struct utmpx *get_last_btmp(struct lslogins_control *ctl, const char *username)
 {
        size_t n = 0;
-       size_t len;
 
        if (!username)
                return NULL;
 
-       len = strlen(username);
        n = ctl->btmp_size - 1;
        do {
                if (!strncmp(username, ctl->btmp[n].ut_user,
-                   len < UT_NAMESIZE ? len : UT_NAMESIZE))
+                            sizeof(ctl->wtmp[0].ut_user)))
                        return ctl->btmp + n;
        }while (n--);
        return NULL;
 
 }
 
-static int read_utmp(char const *file, size_t *nents, struct utmp **res)
+static int read_utmp(char const *file, size_t *nents, struct utmpx **res)
 {
        size_t n_read = 0, n_alloc = 0;
-       struct utmp *utmp = NULL, *u;
+       struct utmpx *utmp = NULL, *u;
 
-       if (utmpname(file) < 0)
+       if (utmpxname(file) < 0)
                return -errno;
 
-       setutent();
+       setutxent();
        errno = 0;
 
-       while ((u = getutent()) != NULL) {
+       while ((u = getutxent()) != NULL) {
                if (n_read == n_alloc) {
                        n_alloc += 32;
-                       utmp = xrealloc(utmp, n_alloc * sizeof (struct utmp));
+                       utmp = xrealloc(utmp, n_alloc * sizeof (struct utmpx));
                }
                utmp[n_read++] = *u;
        }
@@ -489,7 +485,7 @@ static int read_utmp(char const *file, size_t *nents, struct utmp **res)
                return -errno;
        }
 
-       endutent();
+       endutxent();
 
        *nents = n_read;
        *res = utmp;
@@ -571,7 +567,7 @@ static int valid_pwd(const char *str)
        const char *p;
 
        for (p = str; p && *p; p++)
-               if (!isalnum((unsigned int) *p))
+               if (!isalnum((unsigned char) *p))
                        return 0;
        return p > str ? 1 : 0;
 }
@@ -582,7 +578,7 @@ static struct lslogins_user *get_user_info(struct lslogins_control *ctl, const c
        struct passwd *pwd;
        struct group *grp;
        struct spwd *shadow;
-       struct utmp *user_wtmp = NULL, *user_btmp = NULL;
+       struct utmpx *user_wtmp = NULL, *user_btmp = NULL;
        int n = 0;
        time_t time;
        uid_t uid;
@@ -919,7 +915,7 @@ static struct libscols_table *setup_table(struct lslogins_control *ctl)
        int n = 0;
 
        if (!table)
-               errx(EXIT_FAILURE, _("failed to initialize output table"));
+               err(EXIT_FAILURE, _("failed to allocate output table"));
        if (ctl->noheadings)
                scols_table_enable_noheadings(table, 1);
 
@@ -976,6 +972,9 @@ static void fill_table(const void *u, const VISIT which, const int depth __attri
                return;
 
        ln = scols_table_new_line(tb, NULL);
+       if (!ln)
+               err(EXIT_FAILURE, _("failed to allocate output line"));
+
        while (n < ncolumns) {
                int rc = 0;
 
@@ -1071,8 +1070,8 @@ static void fill_table(const void *u, const VISIT which, const int depth __attri
                        err(EXIT_FAILURE, _("internal error: unknown column"));
                }
 
-               if (rc != 0)
-                       err(EXIT_FAILURE, _("failed to set data"));
+               if (rc)
+                       err(EXIT_FAILURE, _("failed to add output data"));
                ++n;
        }
        return;
@@ -1220,8 +1219,9 @@ static int parse_time_mode(const char *s)
        errx(EXIT_FAILURE, _("unknown time format: %s"), s);
 }
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        size_t i;
 
        fputs(USAGE_HEADER, out);
@@ -1256,15 +1256,13 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        fputs(USAGE_HELP, out);
        fputs(USAGE_VERSION, out);
 
-       fprintf(out, _("\nAvailable columns:\n"));
-
+       fputs(USAGE_COLUMNS, out);
        for (i = 0; i < ARRAY_SIZE(coldescs); i++)
-               fprintf(out, " %14s  %s\n", coldescs[i].name,
-                               _(coldescs[i].help));
+               fprintf(out, " %14s  %s\n", coldescs[i].name, _(coldescs[i].help));
 
        fprintf(out, USAGE_MAN_TAIL("lslogins(1)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
@@ -1368,7 +1366,7 @@ int main(int argc, char *argv[])
                        groups = optarg;
                        break;
                case 'h':
-                       usage(stdout);
+                       usage();
                        break;
                case 'L':
                        add_column(columns, ncolumns++, COL_LAST_TTY);
@@ -1382,15 +1380,12 @@ int main(int argc, char *argv[])
                        outmode = OUT_NEWLINE;
                        break;
                case 'o':
-                       if (optarg) {
-                               if (*optarg == '=')
-                                       optarg++;
-                               ncolumns = string_to_idarray(optarg,
-                                               columns, ARRAY_SIZE(columns),
-                                               column_name_to_id);
-                               if (ncolumns < 0)
-                                       return EXIT_FAILURE;
-                       }
+                       if (*optarg == '=')
+                               optarg++;
+                       ncolumns = string_to_idarray(optarg, columns,
+                                       ARRAY_SIZE(columns), column_name_to_id);
+                       if (ncolumns < 0)
+                               return EXIT_FAILURE;
                        opt_o = 1;
                        break;
                case 'r':
@@ -1447,7 +1442,7 @@ int main(int argc, char *argv[])
                        break;
                }
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }