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/swap | |
| 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/swap')
| -rw-r--r-- | usr/src/cmd/swap/Makefile.com | 2 | ||||
| -rw-r--r-- | usr/src/cmd/swap/swap.c | 177 |
2 files changed, 60 insertions, 119 deletions
diff --git a/usr/src/cmd/swap/Makefile.com b/usr/src/cmd/swap/Makefile.com index 3734d8ac58..0514c15e62 100644 --- a/usr/src/cmd/swap/Makefile.com +++ b/usr/src/cmd/swap/Makefile.com @@ -43,7 +43,7 @@ CLEANFILES += $(OBJS) all: $(PROG) -LDLIBS += -ldiskmgt +LDLIBS += -ldiskmgt -lcmdutils $(PROG): $(OBJS) $(LINK.c) $(OBJS) -o $@ $(LDLIBS) $(POST_PROCESS) diff --git a/usr/src/cmd/swap/swap.c b/usr/src/cmd/swap/swap.c index 91cecd5cb4..6b4522745d 100644 --- a/usr/src/cmd/swap/swap.c +++ b/usr/src/cmd/swap/swap.c @@ -36,7 +36,9 @@ * contributors. */ -#pragma ident "%Z%%M% %I% %E% SMI" +/* + * Copyright 2017 Jason King. + */ /* * Swap administrative interface @@ -64,6 +66,8 @@ #include <libintl.h> #include <libdiskmgt.h> #include <sys/fs/zfs.h> +#include <libcmdutils.h> +#include <sys/debug.h> #define LFLAG 0x01 /* swap -l (list swap devices) */ #define DFLAG 0x02 /* swap -d (delete swap device) */ @@ -75,6 +79,8 @@ #define KFLAG 0x80 /* swap -k (size in kilobytes) */ #define NUMBER_WIDTH 64 +/* make sure nicenum_scale() has enough space */ +CTASSERT(NUMBER_WIDTH >= NN_NUMBUF_SZ); typedef char numbuf_t[NUMBER_WIDTH]; static char *prognamep; @@ -85,9 +91,6 @@ static void usage(void); static int doswap(int flag); static int valid(char *, off_t, off_t); static int list(int flag); -static char *number_to_scaled_string(numbuf_t buf, unsigned long long number, - unsigned long long unit_from, unsigned long long scale); - int main(int argc, char **argv) @@ -253,7 +256,7 @@ main(int argc, char **argv) } if ((ret = valid(pathname, s_offset * 512, length * 512)) == 0) { - ret = add(pathname, s_offset, length, flag); + ret = add(pathname, s_offset, length, flag); } } if (!(flag & ~HFLAG & ~KFLAG)) { @@ -271,16 +274,16 @@ usage(void) (void) fprintf(stderr, gettext("Usage:\t%s -l\n"), prognamep); (void) fprintf(stderr, gettext("\tsub option :\n")); (void) fprintf(stderr, gettext("\t\t-h : displays size in human " - "readable format\n")); + "readable format\n")); (void) fprintf(stderr, gettext("\t\t-k : displays size in KB\n")); (void) fprintf(stderr, "\t%s -s\n", prognamep); (void) fprintf(stderr, gettext("\tsub option :\n")); (void) fprintf(stderr, gettext("\t\t-h : displays size in human " - "readable format rather than KB\n")); + "readable format rather than KB\n")); (void) fprintf(stderr, gettext("\t%s -d <file name> [low block]\n"), - prognamep); + prognamep); (void) fprintf(stderr, gettext("\t%s -a <file name> [low block]" - " [nbr of blocks]\n"), prognamep); + " [nbr of blocks]\n"), prognamep); } /* @@ -305,7 +308,6 @@ doswap(int flag) struct anoninfo ai; pgcnt_t allocated, reserved, available; numbuf_t numbuf; - unsigned long long scale = 1024L; /* * max = total amount of swap space including physical memory @@ -339,24 +341,25 @@ doswap(int flag) if (flag & HFLAG) { int factor = (int)(sysconf(_SC_PAGESIZE)); - (void) printf(gettext("total: %s allocated + "), - number_to_scaled_string(numbuf, allocated, - factor, scale)); - (void) printf(gettext("%s reserved = "), - number_to_scaled_string(numbuf, reserved, - factor, scale)); - (void) printf(gettext("%s used, "), - number_to_scaled_string(numbuf, - allocated + reserved, factor, scale)); - (void) printf(gettext("%s available\n"), - number_to_scaled_string(numbuf, available, - factor, scale)); + + nicenum_scale(allocated, factor, numbuf, sizeof (numbuf), 0); + (void) printf(gettext("total: %s allocated + "), numbuf); + + nicenum_scale(reserved, factor, numbuf, sizeof (numbuf), 0); + (void) printf(gettext("%s reserved = "), numbuf); + + nicenum_scale(allocated + reserved, factor, numbuf, + sizeof (numbuf), 0); + (void) printf(gettext("%s used, "), numbuf); + + nicenum_scale(available, factor, numbuf, sizeof (numbuf), 0); + (void) printf(gettext("%s available\n"), numbuf); } else { (void) printf(gettext("total: %luk bytes allocated + %luk" - " reserved = %luk used, %luk available\n"), - ctok(allocated), ctok(reserved), - ctok(reserved) + ctok(allocated), - ctok(available)); + " reserved = %luk used, %luk available\n"), + ctok(allocated), ctok(reserved), + ctok(reserved) + ctok(allocated), + ctok(available)); } return (0); @@ -373,7 +376,6 @@ list(int flag) char fullpath[MAXPATHLEN+1]; int num; numbuf_t numbuf; - unsigned long long scale = 1024L; if ((num = swapctl(SC_GETNSWP, NULL)) == -1) { perror(prognamep); @@ -387,13 +389,13 @@ list(int flag) if ((st = malloc(num * sizeof (swapent_t) + sizeof (int))) == NULL) { (void) fprintf(stderr, - gettext("Malloc failed. Please try later.\n")); + gettext("Malloc failed. Please try later.\n")); perror(prognamep); return (2); } if ((path = malloc(num * MAXPATHLEN)) == NULL) { (void) fprintf(stderr, - gettext("Malloc failed. Please try later.\n")); + gettext("Malloc failed. Please try later.\n")); perror(prognamep); return (2); } @@ -431,17 +433,17 @@ list(int flag) for (i = 0; i < num; i++, swapent++) { if (*swapent->ste_path != '/') (void) snprintf(fullpath, sizeof (fullpath), - "/dev/%s", swapent->ste_path); + "/dev/%s", swapent->ste_path); else (void) snprintf(fullpath, sizeof (fullpath), - "%s", swapent->ste_path); + "%s", swapent->ste_path); if (stat64(fullpath, &statbuf) < 0) if (*swapent->ste_path != '/') (void) printf(gettext("%-20s - "), - swapent->ste_path); + swapent->ste_path); else (void) printf(gettext("%-20s ?,? "), - fullpath); + fullpath); else { if (S_ISBLK(statbuf.st_mode) || S_ISCHR(statbuf.st_mode)) { @@ -455,34 +457,31 @@ list(int flag) } { int diskblks_per_page = - (int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT); + (int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT); if (flag & HFLAG) { - (void) printf(gettext(" %8s"), - number_to_scaled_string(numbuf, - swapent->ste_start, DEV_BSIZE, - scale)); - (void) printf(gettext(" %8s"), - number_to_scaled_string(numbuf, - swapent->ste_pages * - diskblks_per_page, - DEV_BSIZE, scale)); - (void) printf(gettext(" %8s"), - number_to_scaled_string(numbuf, - swapent->ste_free * - diskblks_per_page, - DEV_BSIZE, scale)); + nicenum_scale(swapent->ste_start, DEV_BSIZE, numbuf, + sizeof (numbuf), 0); + (void) printf(gettext(" %8s"), numbuf); + + nicenum_scale(swapent->ste_pages, DEV_BSIZE * + diskblks_per_page, numbuf, sizeof (numbuf), 0); + (void) printf(gettext(" %8s"), numbuf); + + nicenum_scale(swapent->ste_free, DEV_BSIZE * + diskblks_per_page, numbuf, sizeof (numbuf), 0); + (void) printf(gettext(" %8s"), numbuf); } else if (flag & KFLAG) { (void) printf(gettext(" %7luK %7luK %7luK"), - swapent->ste_start * DEV_BSIZE / 1024, - swapent->ste_pages * diskblks_per_page * - DEV_BSIZE / 1024, - swapent->ste_free * diskblks_per_page * - DEV_BSIZE / 1024); + swapent->ste_start * DEV_BSIZE / 1024, + swapent->ste_pages * diskblks_per_page * + DEV_BSIZE / 1024, + swapent->ste_free * diskblks_per_page * + DEV_BSIZE / 1024); } else { (void) printf(gettext(" %8lu %8lu %8lu"), - swapent->ste_start, - swapent->ste_pages * diskblks_per_page, - swapent->ste_free * diskblks_per_page); + swapent->ste_start, + swapent->ste_pages * diskblks_per_page, + swapent->ste_free * diskblks_per_page); } } if (swapent->ste_flags & ST_INDEL) @@ -493,64 +492,6 @@ list(int flag) return (0); } -/* Copied from du.c */ -static char * -number_to_scaled_string( - numbuf_t buf, /* put the result here */ - unsigned long long number, /* convert this number */ - unsigned long long unit_from, /* number of byes per input unit */ - unsigned long long scale) /* 1024 (-h) or 1000 (-H) */ -{ - 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 with unit_from and divide by scale. - * if number is large enough, we first divide and then multiply - * to avoid an overflow (large enough here means 100 (rather arbitrary - * value) times scale in order to reduce rounding errors) - * otherwise, we first multiply and then divide to avoid an underflow. - */ - if (number >= 100L * scale) { - number = number / scale; - number = number * unit_from; - } else { - number = number * unit_from; - number = number / 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, "%.1f%c", fnum, *uom); - } else { - (void) sprintf(buf, "%llu%c", number, *uom); - } - return (buf); -} - - - - static void dumpadm_err(const char *warning) { @@ -701,7 +642,7 @@ add(char *path, off_t offset, off_t cnt, int flags) statvfs("/etc", &fsb) == 0 && !(fsb.f_flag & ST_RDONLY)) { (void) printf( - gettext("operating system crash dump was previously " + gettext("operating system crash dump was previously " "disabled --\ninvoking dumpadm(1M) -d swap to select " "new dump device\n")); @@ -726,7 +667,7 @@ valid(char *pathname, off_t offset, off_t length) } if (!((S_ISREG(f.st_mode) && (f.st_mode & S_ISVTX) == S_ISVTX) || - S_ISBLK(f.st_mode))) { + S_ISBLK(f.st_mode))) { (void) fprintf(stderr, gettext("\"%s\" is not valid for swapping.\n" "It must be a block device or a regular file with the\n" @@ -753,8 +694,8 @@ valid(char *pathname, off_t offset, off_t length) if (offset < 0) { (void) fprintf(stderr, - gettext("%s: low block is invalid\n"), - pathname); + gettext("%s: low block is invalid\n"), + pathname); return (EINVAL); } |
