summaryrefslogtreecommitdiffstats
path: root/sys-utils/ipcutils.c
diff options
context:
space:
mode:
authorSami Kerola2012-12-16 11:43:51 +0100
committerKarel Zak2012-12-19 11:04:53 +0100
commit56692a6701c4847d0cd80457c5adb98e5eaa6ced (patch)
tree41c51e5c4c825322263c028c29cbf9c52325a4a3 /sys-utils/ipcutils.c
parentipcs: assist debugging (diff)
downloadkernel-qcow2-util-linux-56692a6701c4847d0cd80457c5adb98e5eaa6ced.tar.gz
kernel-qcow2-util-linux-56692a6701c4847d0cd80457c5adb98e5eaa6ced.tar.xz
kernel-qcow2-util-linux-56692a6701c4847d0cd80457c5adb98e5eaa6ced.zip
ipcs: add --human readable size conversion option
Introduces new function ipc_print_size() which will call size_to_human_string(), and handles the occasional '([k]bytes)' printing if default size format is requested. Reviewed-by: Karel Zak <kzak@redhat.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'sys-utils/ipcutils.c')
-rw-r--r--sys-utils/ipcutils.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c
index 7d1f0d1ef..d81ca2063 100644
--- a/sys-utils/ipcutils.c
+++ b/sys-utils/ipcutils.c
@@ -7,6 +7,7 @@
#include "path.h"
#include "pathnames.h"
#include "ipcutils.h"
+#include "strutils.h"
#ifndef SEMVMX
# define SEMVMX 32767 /* <= 32767 semaphore maximum value */
@@ -504,3 +505,42 @@ void ipc_print_perms(FILE *f, struct ipc_stat *is)
else
fprintf(f, " %-10u\n", is->gid);
}
+
+void ipc_print_size(int unit, char *msg, size_t size, const char *end,
+ int width)
+{
+ char format[16];
+
+ if (!msg)
+ /* NULL */ ;
+ else if (msg[strlen(msg) - 1] == '=')
+ printf("%s", msg);
+ else if (unit == IPC_UNIT_BYTES)
+ printf(_("%s (bytes) = "), msg);
+ else if (unit == IPC_UNIT_KB)
+ printf(_("%s (kbytes) = "), msg);
+ else
+ printf("%s = ", msg);
+
+ switch (unit) {
+ case IPC_UNIT_DEFAULT:
+ case IPC_UNIT_BYTES:
+ sprintf(format, "%%%dzu", width);
+ printf(format, size);
+ break;
+ case IPC_UNIT_KB:
+ sprintf(format, "%%%dzu", width);
+ printf(format, size / 1024);
+ break;
+ case IPC_UNIT_HUMAN:
+ sprintf(format, "%%%ds", width);
+ printf(format, size_to_human_string(SIZE_SUFFIX_1LETTER, size));
+ break;
+ default:
+ /* impossible occurred */
+ abort();
+ }
+
+ if (end)
+ printf("%s", end);
+}