diff options
| author | Jason King <jason.brian.king@gmail.com> | 2017-06-13 04:16:45 -0500 |
|---|---|---|
| committer | Dan McDonald <danmcd@joyent.com> | 2017-09-04 20:42:57 -0400 |
| commit | 0a0551200ecbcd4f1b17560acaeeb7b6c8b0090e (patch) | |
| tree | 4e943314876a263be2f8209de2eea1a87428be80 /usr/src/cmd/fs.d | |
| parent | 30c75cb09b4d5e86a94a25a9a7ab7481510b57b0 (diff) | |
| download | illumos-joyent-0a0551200ecbcd4f1b17560acaeeb7b6c8b0090e.tar.gz | |
640 number_to_scaled_string is duplicated in several commands
Reviewed by: Sebastian Wiedenroth <wiedi@frubar.net>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Yuri Pankov <yuripv@gmx.com>
Approved by: Dan McDonald <danmcd@joyent.com>
Diffstat (limited to 'usr/src/cmd/fs.d')
| -rw-r--r-- | usr/src/cmd/fs.d/Makefile | 1 | ||||
| -rw-r--r-- | usr/src/cmd/fs.d/df.c | 101 |
2 files changed, 21 insertions, 81 deletions
diff --git a/usr/src/cmd/fs.d/Makefile b/usr/src/cmd/fs.d/Makefile index 3c36817494..2f5bd31ec7 100644 --- a/usr/src/cmd/fs.d/Makefile +++ b/usr/src/cmd/fs.d/Makefile @@ -87,6 +87,7 @@ CERRWARN += -_gcc=-Wno-unused-variable CERRWARN += -_gcc=-Wno-uninitialized CERRWARN += -_gcc=-Wno-unused-function +$(DFPROG) := LDLIBS += -lcmdutils $(SPPROG) := LDLIBS += -lkstat $(ROOTETCMNTTAB) := FILEMODE = 444 diff --git a/usr/src/cmd/fs.d/df.c b/usr/src/cmd/fs.d/df.c index 3ba4aeb832..ee7bf6e7d5 100644 --- a/usr/src/cmd/fs.d/df.c +++ b/usr/src/cmd/fs.d/df.c @@ -26,6 +26,9 @@ * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ +/* + * Copyright 2016 Jason King + */ #include <dlfcn.h> #include <stdio.h> @@ -48,7 +51,9 @@ #include <sys/mkdev.h> #include <sys/int_limits.h> #include <sys/zone.h> +#include <sys/debug.h> #include <libzfs.h> +#include <libcmdutils.h> #include "fslib.h" @@ -134,6 +139,7 @@ extern char *default_fstype(char *); /* message */ #define NUMBER_WIDTH 40 +CTASSERT(NUMBER_WIDTH >= NN_NUMBUF_SZ); /* * A numbuf_t is used when converting a number to a string representation @@ -219,7 +225,6 @@ static bool_int o_option; static bool_int tty_output; static bool_int use_scaling; -static int scale; static void usage(void); static void do_devnm(int, char **); @@ -591,7 +596,6 @@ parse_options(int argc, char *argv[]) SET_OPTION(g); } else if (arg == 'h') { use_scaling = TRUE; - scale = 1024; } else if (arg == 'k' && ! k_option) { SET_OPTION(k); } else if (arg == 'l' && ! l_option) { @@ -967,8 +971,7 @@ run_fs_specific_df(struct df_request request_list[], int entries) */ static int prune_list(struct df_request request_list[], - size_t n_requests, - size_t *valid_requests) + size_t n_requests, size_t *valid_requests) { size_t i; size_t n_valid = 0; @@ -1167,73 +1170,6 @@ number_to_string( } /* - * Convert an unsigned long long to a string representation and place the - * result in the caller-supplied buffer. - * The given number is in units of "unit_from" size, - * this will first be converted to a number in 1024 or 1000 byte size, - * depending on the scaling factor. - * Then the number is scaled down until it is small enough to be in a good - * human readable format i.e. in the range 0 thru scale-1. - * If it's smaller than 10 there's room enough to provide one decimal place. - * The value "(unsigned long long)-1" is a special case and is always - * converted to "-1". - * Returns a pointer to the caller-supplied buffer. - */ -static char * -number_to_scaled_string( - numbuf_t buf, /* put the result here */ - unsigned long long number, /* convert this number */ - int unit_from, - int scale) -{ - unsigned long long save = 0; - char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */ - char *uom = M; /* unit of measurement, initially 'K' (=M[0]) */ - - if ((long long)number == (long long)-1) { - (void) strcpy(buf, "-1"); - return (buf); - } - - /* - * Convert number from unit_from to given scale (1024 or 1000). - * This means multiply number by unit_from and divide by scale. - * - * Would like to multiply by unit_from and then divide by scale, - * but if the first multiplication would overflow, then need to - * divide by scale and then multiply by unit_from. - */ - if (number > (UINT64_MAX / (unsigned long long)unit_from)) { - number = (number / (unsigned long long)scale) * - (unsigned long long)unit_from; - } else { - number = (number * (unsigned long long)unit_from) / - (unsigned long long)scale; - } - - /* - * Now we have number as a count of scale units. - * Stop scaling when we reached exa bytes, then something is - * probably wrong with our number. - */ - - while ((number >= scale) && (*uom != 'E')) { - uom++; /* next unit of measurement */ - save = number; - number = (number + (scale / 2)) / scale; - } - /* check if we should output a decimal place after the point */ - if (save && ((save / scale) < 10)) { - /* sprintf() will round for us */ - float fnum = (float)save / scale; - (void) sprintf(buf, "%2.1f%c", fnum, *uom); - } else { - (void) sprintf(buf, "%4llu%c", number, *uom); - } - return (buf); -} - -/* * The statvfs() implementation allows us to return only two values, the total * number of blocks and the number of blocks free. The equation 'used = total - * free' will not work for ZFS filesystems, due to the nature of pooled storage. @@ -1488,16 +1424,19 @@ k_output(struct df_request *dfrp, struct statvfs64 *fsp) adjust_total_blocks(dfrp, &total_blocks, fsp->f_frsize); if (use_scaling) { /* comes from the -h option */ - (void) printf("%-*s %*s %*s %*s %-*s %-s\n", - FILESYSTEM_WIDTH, file_system, - SCALED_WIDTH, number_to_scaled_string(total_blocks_buf, - total_blocks, fsp->f_frsize, scale), - SCALED_WIDTH, number_to_scaled_string(used_blocks_buf, - used_blocks, fsp->f_frsize, scale), - AVAILABLE_WIDTH, number_to_scaled_string(available_blocks_buf, - available_blocks, fsp->f_frsize, scale), - CAPACITY_WIDTH, capacity_buf, - DFR_MOUNT_POINT(dfrp)); + nicenum_scale(total_blocks, fsp->f_frsize, + total_blocks_buf, sizeof (total_blocks_buf), 0); + nicenum_scale(used_blocks, fsp->f_frsize, + used_blocks_buf, sizeof (used_blocks_buf), 0); + nicenum_scale(available_blocks, fsp->f_frsize, + available_blocks_buf, sizeof (available_blocks_buf), 0); + + (void) printf("%-*s %*s %*s %*s %-*s %-s\n", + FILESYSTEM_WIDTH, file_system, + SCALED_WIDTH, total_blocks_buf, + SCALED_WIDTH, used_blocks_buf, + AVAILABLE_WIDTH, available_blocks_buf, + CAPACITY_WIDTH, capacity_buf, DFR_MOUNT_POINT(dfrp)); return; } |
