Implement username resolution.
authorAndre Noll <maan@systemlinux.org>
Sun, 1 Jun 2008 14:29:58 +0000 (16:29 +0200)
committerAndre Noll <maan@systemlinux.org>
Sun, 1 Jun 2008 14:29:58 +0000 (16:29 +0200)
adu.c

diff --git a/adu.c b/adu.c
index dad9d8c..c3a278e 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -1,5 +1,6 @@
 #include "adu.h"
 #include <dirent.h> /* readdir() */
+#include <pwd.h>
 
 #include "gcc-compat.h"
 #include "cmdline.h"
@@ -24,6 +25,7 @@ enum uid_info_flags {
 struct user_info {
        uint32_t uid;
        uint32_t flags;
+       char *pw_name;
        struct osl_table *table;
        uint64_t files;
        uint64_t bytes;
@@ -319,6 +321,7 @@ static uint32_t num_uids;
 static int open_user_table(struct user_info *ui, int create)
 {
        int ret;
+       struct passwd *pw;
 
        ui->desc = adu_malloc(sizeof(*ui->desc));
        ui->desc->num_columns = NUM_UT_COLUMNS;
@@ -326,6 +329,10 @@ static int open_user_table(struct user_info *ui, int create)
        ui->desc->column_descriptions = user_table_cols;
        ui->desc->dir = adu_strdup(conf.database_dir_arg);
        ui->desc->name = make_message("%u", (unsigned)ui->uid);
+       pw = getpwuid(ui->uid);
+       if (pw && pw->pw_name)
+               ui->pw_name = adu_strdup(pw->pw_name);
+
        INFO_LOG(".............................uid #%u: %u\n",
                (unsigned)num_uids, (unsigned)ui->uid);
        if (create) {
@@ -341,6 +348,7 @@ static int open_user_table(struct user_info *ui, int create)
 err:
        free((char *)ui->desc->name);
        free((char *)ui->desc->dir);
+       free(ui->pw_name);
        free(ui->desc);
        ui->desc->name = NULL;
        ui->desc->dir = NULL;
@@ -406,6 +414,8 @@ static void close_user_table(struct user_info *ui)
        ui->desc->name = NULL;
        free((char *)ui->desc->dir);
        ui->desc->dir = NULL;
+       free(ui->pw_name);
+       ui->pw_name = NULL;
        free(ui->desc);
        ui->desc = NULL;
        ui->table = NULL;
@@ -856,7 +866,7 @@ static void print_id_stats(void)
        struct user_info *ui;
 
        printf("User summary "
-               "(uid/dirs%s/files%s/size%s):\n",
+               "(pw_name/uid/dirs%s/files%s/size%s):\n",
                count_unit_buf, count_unit_buf, size_unit_buf);
        FOR_EACH_USER(ui) {
                char formated_dir_count[FORMATED_VALUE_SIZE],
@@ -873,7 +883,9 @@ static void print_id_stats(void)
                format_size_value(conf.size_unit_arg, ui->bytes,
                        conf.size_unit_arg == size_unit_arg_h,
                        formated_bytes);
-               printf("\t%u\t%s\t%s\t%s\n", (unsigned)ui->uid,
+               printf("\t%s\t%u\t%s\t%s\t%s\n",
+                       ui->pw_name? ui->pw_name : "?",
+                       (unsigned)ui->uid,
                        formated_dir_count,
                        formated_file_count,
                        formated_bytes
@@ -1008,14 +1020,16 @@ static int print_user_stats(void)
                if (!ui_used(ui) || !ui_admissible(ui))
                        continue;
                usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
-               printf("uid %u, by size%s:\n",
-                       (unsigned) ui->uid, size_unit_buf);
+               printf("%s (uid %u), by size%s:\n",
+                       ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
+                       size_unit_buf);
                ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
                        &usi.ret, &usi.osl_errno);
                if (ret < 0)
                        return ret;
-               printf("\nuid %u, by file count%s:\n",
-                       (unsigned) ui->uid, count_unit_buf);
+               printf("\n%s (uid %u), by file count%s:\n",
+                       ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
+                       count_unit_buf);
                usi.count = conf.limit_arg,
                usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
                ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,