]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
treewide: use scols printf api where possible
authorRobin Jarry <robin@jarry.cc>
Thu, 31 Oct 2024 22:55:44 +0000 (23:55 +0100)
committerRobin Jarry <robin@jarry.cc>
Thu, 31 Oct 2024 23:40:37 +0000 (00:40 +0100)
Everywhere a string generated with xasprintf() is directly passed to
scols_line_refer_data(), use scols_line_sprintf() to remove the need for
an intermediate buffer.

Replace the (now redundant) private scols_line_asprintf() function.

Signed-off-by: Robin Jarry <robin@jarry.cc>
disk-utils/fdisk-list.c
libsmartcols/samples/continuous-json.c
libsmartcols/samples/continuous.c
lsfd-cmd/lsfd.c
misc-utils/lsclocks.c
sys-utils/irq-common.c
sys-utils/lscpu.c
sys-utils/lsipc.c

index fe2fe3e647c85c1890efe2472f5924019f6f1419..f1edeb7db031356e4576ad2a69cf2a1b113b1bdb 100644 (file)
@@ -309,9 +309,7 @@ int list_freespace_get_table(struct fdisk_context *cxt,
                        char *data = NULL;
 
                        if (tb0 && i == 0) {
-                               xasprintf(&data, "%d", ct + 1);
-
-                               if (scols_line_refer_data(ln, i, data)) {
+                               if (scols_line_sprintf(ln, i, "%d", ct + 1)) {
                                        fdisk_warn(cxt, _("failed to add output data"));
                                        rc = -ENOMEM;
                                        goto done;
index ae1f40597e9aaf24c7191de79b50de5e6ce01d0e..091cab2cf9bf307b42c223be5d786f8e1276eabe 100644 (file)
@@ -30,18 +30,15 @@ fail:
 
 static struct libscols_line *add_line(struct libscols_table *tb, int i)
 {
-       char *p;
        struct libscols_line *ln = scols_table_new_line(tb, NULL);
 
        if (!ln)
                err(EXIT_FAILURE, "failed to create output line");
 
-       xasprintf(&p, "%d", i);
-       if (scols_line_refer_data(ln, 0, p))
+       if (scols_line_sprintf(ln, 0, "%d", i))
                goto fail;
 
-       xasprintf(&p, "text%d", i);
-       if (scols_line_refer_data(ln, 1, p))
+       if (scols_line_sprintf(ln, 1, "text%d", i))
                goto fail;
 
        return ln;
index fb089c365a4610a8d66046b019773d8a600e5017..72ecdf22453f931eafb56d7606bae376bd8af70c 100644 (file)
@@ -45,18 +45,16 @@ fail:
 
 static struct libscols_line *add_line(struct libscols_table *tb, size_t i)
 {
-       char *p;
        struct libscols_line *ln = scols_table_new_line(tb, NULL);
 
        if (!ln)
                err(EXIT_FAILURE, "failed to create output line");
 
-       xasprintf(&p, "%zu", i);
-       if (scols_line_refer_data(ln, COL_NUM, p))
+       if (scols_line_sprintf(ln, COL_NUM, "%zu", i))
                goto fail;
 
-       xasprintf(&p, "data-%02zu-%02zu-%02zu-end", i + 1, i + 2, i + 3);
-       if (scols_line_refer_data(ln, COL_DATA, p))
+       if (scols_line_sprintf(ln, COL_DATA,  "data-%02zu-%02zu-%02zu-end",
+                              i + 1, i + 2, i + 3))
                goto fail;
 
        return ln;
index 49d31a16e2248ad03a12f4154573cee1d5ad844b..3f7144dbed7f0acfa401caf65b6dfaf22755ebb0 100644 (file)
@@ -2404,15 +2404,13 @@ static void emit_summary(struct lsfd_control *ctl)
 
                scols_reset_iter(itr, SCOLS_ITER_FORWARD);
                while (scols_filter_next_counter(*ct_fltr, itr, &ct) == 0) {
-                       char *str = NULL;
                        struct libscols_line *ln;
 
                        ln = scols_table_new_line(tb, NULL);
                        if (!ln)
                                err(EXIT_FAILURE, _("failed to allocate summary line"));
 
-                       xasprintf(&str, "%llu", scols_counter_get_result(ct));
-                       if (scols_line_refer_data(ln, 0, str))
+                       if (scols_line_sprintf(ln, 0, "%llu", scols_counter_get_result(ct)))
                                err(EXIT_FAILURE, _("failed to add summary data"));
                        if (scols_line_set_data(ln, 1, scols_counter_get_name(ct)))
                                err(EXIT_FAILURE, _("failed to add summary data"));
index 0799f65651263ddafacc83e800eea41a8c312b5f..66a2cd12478084090892cd79de7d8f9d4ba1a4b7 100644 (file)
@@ -211,22 +211,9 @@ static void __attribute__((__noreturn__)) usage(void)
        exit(EXIT_SUCCESS);
 }
 
-__attribute__ ((__format__ (__printf__, 3, 4)))
-static void scols_line_asprintf(struct libscols_line *ln, size_t n, const char *format, ...)
-{
-       char *data;
-       va_list args;
-
-       va_start(args, format);
-       xvasprintf(&data, format, args);
-       va_end(args);
-
-       scols_line_refer_data(ln, n, data);
-}
-
 static void scols_line_format_timespec(struct libscols_line *ln, size_t n, const struct timespec *ts)
 {
-       scols_line_asprintf(ln, n, "%ju.%09" PRId32, (uintmax_t) ts->tv_sec, (uint32_t) ts->tv_nsec);
+       scols_line_sprintf(ln, n, "%ju.%09" PRId32, (uintmax_t) ts->tv_sec, (uint32_t) ts->tv_nsec);
 }
 
 static clockid_t parse_clock(const char *name)
@@ -301,7 +288,7 @@ static void add_clock_line(struct libscols_table *tb, const int *columns,
                                break;
                        case COL_ID:
                                if (!clockinfo->no_id)
-                                       scols_line_asprintf(ln, i, "%ju", (uintmax_t) clockinfo->id);
+                                       scols_line_sprintf(ln, i, "%ju", (uintmax_t) clockinfo->id);
                                break;
                        case COL_CLOCK:
                                scols_line_set_data(ln, i, clockinfo->id_name);
@@ -350,8 +337,8 @@ static void add_clock_line(struct libscols_table *tb, const int *columns,
                                break;
                        case COL_NS_OFFSET:
                                if (clockinfo->ns_offset_name)
-                                       scols_line_asprintf(ln, i, "%"PRId64,
-                                                           get_namespace_offset(clockinfo->ns_offset_name));
+                                       scols_line_sprintf(ln, i, "%"PRId64,
+                                                          get_namespace_offset(clockinfo->ns_offset_name));
                                break;
                }
        }
index 82a7757600737b1306a3411ab3c1f6a3024c21d9..5c39193541730001653a058c3de1733dbd14f28d 100644 (file)
@@ -492,12 +492,13 @@ struct libscols_table *get_scols_cpus_table(struct irq_output *out,
 
        for (i = 0, j = 0; i < curr->nr_active_cpu; i++) {
                struct irq_cpu *cpu = &curr->cpus[i];
-               char *str;
+               double usage;
 
                if (!cpu_in_list(i, setsize, cpuset))
                        continue;
-               xasprintf(&str, "%0.1f", (double)((long double) cpu->total / (long double) curr->total_irq * 100.0));
-               if (str && scols_line_refer_data(ln, ++j, str) != 0)
+
+               usage = (long double) cpu->total / (long double) curr->total_irq * 100.0;
+               if (scols_line_sprintf(ln, ++j, "%0.1f", usage) != 0)
                        goto err;
        }
 
@@ -509,14 +510,14 @@ struct libscols_table *get_scols_cpus_table(struct irq_output *out,
 
        for (i = 0, j = 0; i < curr->nr_active_cpu; i++) {
                struct irq_cpu *cpu = &curr->cpus[i];
-               char *str;
+               double usage;
 
                if (!cpu_in_list(i, setsize, cpuset))
                        continue;
                if (!curr->delta_irq)
                        continue;
-               xasprintf(&str, "%0.1f", (double)((long double) cpu->delta / (long double) curr->delta_irq * 100.0));
-               if (str && scols_line_refer_data(ln, ++j, str) != 0)
+               usage = (long double) cpu->delta / (long double) curr->delta_irq * 100.0;
+               if (scols_line_sprintf(ln, ++j, "%0.1f", usage) != 0)
                        goto err;
        }
 
index 60a0912370a88b9f4e410f85b9ad76c166be3d26..c5b88e8cc9e012064b42e4444a7b6c6246c8d480 100644 (file)
@@ -823,12 +823,13 @@ static struct libscols_line *
 
        /* data column */
        if (fmt) {
-               char *data;
+               int ret;
+
                va_start(args, fmt);
-               xvasprintf(&data, fmt, args);
+               ret = scols_line_vprintf(ln, 1, fmt, args);
                va_end(args);
 
-               if (data && scols_line_refer_data(ln, 1, data))
+               if (ret < 0)
                        err(EXIT_FAILURE, _("failed to add output data"));
        }
 
index 179adbccfbd1dfaa828b32a408c9b6f949d0805c..85ebcafd3525626a0d5cdf33d9c31d090edbb073 100644 (file)
@@ -507,10 +507,10 @@ static void global_set_data(struct lsipc_control *ctl, struct libscols_table *tb
                                rc = scols_line_set_data(ln, n, "-");
                        break;
                case COL_USEPERC:
-                       if (usage) {
-                               xasprintf(&arg, "%2.2f%%", (double) used / limit * 100);
-                               rc = scols_line_refer_data(ln, n, arg);
-                       else
+                       if (usage)
+                               rc = scols_line_sprintf(ln, n, "%2.2f%%",
+                                       (double) used / limit * 100);
+                       else
                                rc = scols_line_set_data(ln, n, "-");
                        break;
                case COL_LIMIT:
@@ -570,12 +570,10 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                        int rc = 0;
                        switch (get_column_id(n)) {
                        case COL_KEY:
-                               xasprintf(&arg, "0x%08x", p->sem_perm.key);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "0x%08x", p->sem_perm.key);
                                break;
                        case COL_ID:
-                               xasprintf(&arg, "%d", p->sem_perm.id);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%d", p->sem_perm.id);
                                break;
                        case COL_OWNER:
                                arg = get_username(&pw, p->sem_perm.uid);
@@ -593,8 +591,7 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_CUID:
-                               xasprintf(&arg, "%u", p->sem_perm.cuid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->sem_perm.cuid);
                                break;
                        case COL_CUSER:
                                arg = get_username(&cpw, p->sem_perm.cuid);
@@ -602,8 +599,7 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_CGID:
-                               xasprintf(&arg, "%u", p->sem_perm.cgid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->sem_perm.cgid);
                                break;
                        case COL_CGROUP:
                                arg = get_groupname(&cgr, p->sem_perm.cgid);
@@ -611,8 +607,7 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_UID:
-                               xasprintf(&arg, "%u", p->sem_perm.uid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->sem_perm.uid);
                                break;
                        case COL_USER:
                                arg = get_username(&pw, p->sem_perm.uid);
@@ -620,8 +615,7 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_GID:
-                               xasprintf(&arg, "%u", p->sem_perm.gid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->sem_perm.gid);
                                break;
                        case COL_GROUP:
                                arg = get_groupname(&gr, p->sem_perm.gid);
@@ -636,8 +630,7 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                }
                                break;
                        case COL_NSEMS:
-                               xasprintf(&arg, "%ju", p->sem_nsems);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%ju", p->sem_nsems);
                                break;
                        case COL_OTIME:
                                if (p->sem_otime != 0) {
@@ -669,32 +662,27 @@ static void do_sem(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        err(EXIT_FAILURE, _("failed to allocate output line"));
 
                                /* SEMNUM */
-                               xasprintf(&arg, "%zu", i);
-                               rc = scols_line_refer_data(sln, 0, arg);
+                               rc = scols_line_sprintf(sln, 0, "%zu", i);
                                if (rc)
                                        break;
 
                                /* VALUE */
-                               xasprintf(&arg, "%d", e->semval);
-                               rc = scols_line_refer_data(sln, 1, arg);
+                               rc = scols_line_sprintf(sln, 1, "%d", e->semval);
                                if (rc)
                                        break;
 
                                /* NCOUNT */
-                               xasprintf(&arg, "%d", e->ncount);
-                               rc = scols_line_refer_data(sln, 2, arg);
+                               rc = scols_line_sprintf(sln, 2, "%d", e->ncount);
                                if (rc)
                                        break;
 
                                /* ZCOUNT */
-                               xasprintf(&arg, "%d", e->zcount);
-                               rc = scols_line_refer_data(sln, 3, arg);
+                               rc = scols_line_sprintf(sln, 3, "%d", e->zcount);
                                if (rc)
                                        break;
 
                                /* PID */
-                               xasprintf(&arg, "%d", e->pid);
-                               rc = scols_line_refer_data(sln, 4, arg);
+                               rc = scols_line_sprintf(sln, 4, "%d", e->pid);
                                if (rc)
                                        break;
 
@@ -775,12 +763,10 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
 
                        switch (get_column_id(n)) {
                        case COL_KEY:
-                               xasprintf(&arg, "0x%08x", p->msg_perm.key);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "0x%08x", p->msg_perm.key);
                                break;
                        case COL_ID:
-                               xasprintf(&arg, "%d", p->msg_perm.id);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%d", p->msg_perm.id);
                                break;
                        case COL_OWNER:
                                arg = get_username(&pw, p->msg_perm.uid);
@@ -798,8 +784,7 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                }
                                break;
                        case COL_CUID:
-                               xasprintf(&arg, "%u", p->msg_perm.cuid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->msg_perm.cuid);
                                break;
                        case COL_CUSER:
                                arg = get_username(&pw, p->msg_perm.cuid);
@@ -807,8 +792,7 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_CGID:
-                               xasprintf(&arg, "%u", p->msg_perm.cuid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->msg_perm.cuid);
                                break;
                        case COL_CGROUP:
                                arg = get_groupname(&gr, p->msg_perm.cgid);
@@ -816,8 +800,7 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_UID:
-                               xasprintf(&arg, "%u", p->msg_perm.uid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->msg_perm.uid);
                                break;
                        case COL_USER:
                                arg = get_username(&pw, p->msg_perm.uid);
@@ -825,8 +808,7 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_GID:
-                               xasprintf(&arg, "%u", p->msg_perm.gid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->msg_perm.gid);
                                break;
                        case COL_GROUP:
                                arg = get_groupname(&gr, p->msg_perm.gid);
@@ -848,8 +830,7 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_MSGS:
-                               xasprintf(&arg, "%ju", p->q_qnum);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%ju", p->q_qnum);
                                break;
                        case COL_SEND:
                                if (p->q_stime != 0)
@@ -864,12 +845,10 @@ static void do_msg(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                                          (time_t) p->q_rtime));
                                break;
                        case COL_LSPID:
-                               xasprintf(&arg, "%u", p->q_lspid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->q_lspid);
                                break;
                        case COL_LRPID:
-                               xasprintf(&arg, "%u", p->q_lrpid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->q_lrpid);
                                break;
                        }
                        if (rc != 0)
@@ -934,12 +913,10 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
 
                        switch (get_column_id(n)) {
                        case COL_KEY:
-                               xasprintf(&arg, "0x%08x", p->shm_perm.key);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "0x%08x", p->shm_perm.key);
                                break;
                        case COL_ID:
-                               xasprintf(&arg, "%d", p->shm_perm.id);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%d", p->shm_perm.id);
                                break;
                        case COL_OWNER:
                                arg = get_username(&pw, p->shm_perm.uid);
@@ -957,8 +934,7 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_CUID:
-                               xasprintf(&arg, "%u", p->shm_perm.cuid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_perm.cuid);
                                break;
                        case COL_CUSER:
                                arg = get_username(&pw, p->shm_perm.cuid);
@@ -966,8 +942,7 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_CGID:
-                               xasprintf(&arg, "%u", p->shm_perm.cuid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_perm.cuid);
                                break;
                        case COL_CGROUP:
                                arg = get_groupname(&gr, p->shm_perm.cgid);
@@ -975,8 +950,7 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_UID:
-                               xasprintf(&arg, "%u", p->shm_perm.uid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_perm.uid);
                                break;
                        case COL_USER:
                                arg = get_username(&pw, p->shm_perm.uid);
@@ -984,8 +958,7 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                        rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_GID:
-                               xasprintf(&arg, "%u", p->shm_perm.gid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_perm.gid);
                                break;
                        case COL_GROUP:
                                arg = get_groupname(&gr, p->shm_perm.gid);
@@ -1007,8 +980,7 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                rc = scols_line_refer_data(ln, n, arg);
                                break;
                        case COL_NATTCH:
-                               xasprintf(&arg, "%ju", p->shm_nattch);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%ju", p->shm_nattch);
                                break;
                        case COL_STATUS: {
                                        int comma = 0;
@@ -1062,12 +1034,10 @@ static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
                                                          (time_t) p->shm_dtim));
                                break;
                        case COL_CPID:
-                               xasprintf(&arg, "%u", p->shm_cprid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_cprid);
                                break;
                        case COL_LPID:
-                               xasprintf(&arg, "%u", p->shm_lprid);
-                               rc = scols_line_refer_data(ln, n, arg);
+                               rc = scols_line_sprintf(ln, n, "%u", p->shm_lprid);
                                break;
                        case COL_COMMAND:
                                arg = pid_get_cmdline(p->shm_cprid);