diff options
author | pjd <pjd@FreeBSD.org> | 2008-05-20 12:24:31 +0000 |
---|---|---|
committer | pjd <pjd@FreeBSD.org> | 2008-05-20 12:24:31 +0000 |
commit | 60f1019c3eed881a9253aa02d5e1dc7688970115 (patch) | |
tree | 515c62e9c67b6d288fceb126ee73a7de8dc13fb0 /sbin/swapon | |
parent | 0c5e80cf387d57ff83257ba8ff78ee9971f05118 (diff) | |
download | FreeBSD-src-60f1019c3eed881a9253aa02d5e1dc7688970115.zip FreeBSD-src-60f1019c3eed881a9253aa02d5e1dc7688970115.tar.gz |
- Change the meaning of -h flag from giving the output in megabytes to
giving the output in a human-readable form. This behaviour is consistent
with most of system tools.
- Add -m and -g options to give output in megabytes and gigabytes
respectively.
Diffstat (limited to 'sbin/swapon')
-rw-r--r-- | sbin/swapon/Makefile | 3 | ||||
-rw-r--r-- | sbin/swapon/swapon.8 | 10 | ||||
-rw-r--r-- | sbin/swapon/swapon.c | 75 |
3 files changed, 67 insertions, 21 deletions
diff --git a/sbin/swapon/Makefile b/sbin/swapon/Makefile index 85b0f0a..6f8e8df 100644 --- a/sbin/swapon/Makefile +++ b/sbin/swapon/Makefile @@ -8,4 +8,7 @@ LINKS+= ${BINDIR}/swapon ${BINDIR}/swapctl MLINKS= swapon.8 swapoff.8 MLINKS+=swapon.8 swapctl.8 +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> diff --git a/sbin/swapon/swapon.8 b/sbin/swapon/swapon.8 index f4ecd14..1af8d99 100644 --- a/sbin/swapon/swapon.8 +++ b/sbin/swapon/swapon.8 @@ -28,7 +28,7 @@ .\" @(#)swapon.8 8.1 (Berkeley) 6/5/93 .\" $FreeBSD$ .\" -.Dd December 28, 2002 +.Dd April 29, 2008 .Dt SWAPON 8 .Os .Sh NAME @@ -38,7 +38,7 @@ .Nm swapon Fl a | Ar .Nm swapoff Fl a | Ar .Nm swapctl -.Op Fl AhklsU +.Op Fl AghklmsU .Oo .Fl a Ar | @@ -130,9 +130,13 @@ The utility has the following options for listing swap: .Bl -tag -width indent .It Fl h -Output values in megabytes. +Output values in human-readable form. +.It Fl g +Output values in gigabytes. .It Fl k Output values in kilobytes. +.It Fl m +Output values in megabytes. .It Fl l List the devices making up system swap. .It Fl s diff --git a/sbin/swapon/swapon.c b/sbin/swapon/swapon.c index 13297f9..4fe3b0b 100644 --- a/sbin/swapon/swapon.c +++ b/sbin/swapon/swapon.c @@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$"); #include <string.h> #include <unistd.h> #include <fcntl.h> +#include <libutil.h> static void usage(void); static int swap_on_off(char *name, int ignoreebusy); @@ -79,7 +80,7 @@ main(int argc, char **argv) orig_prog = which_prog; doall = 0; - while ((ch = getopt(argc, argv, "AadlhksU")) != -1) { + while ((ch = getopt(argc, argv, "AadghklmsU")) != -1) { switch(ch) { case 'A': if (which_prog == SWAPCTL) { @@ -101,17 +102,23 @@ main(int argc, char **argv) else usage(); break; - case 's': - sflag = 1; + case 'g': + hflag = 'G'; + break; + case 'h': + hflag = 'H'; + break; + case 'k': + hflag = 'K'; break; case 'l': lflag = 1; break; - case 'h': + case 'm': hflag = 'M'; break; - case 'k': - hflag = 'K'; + case 's': + sflag = 1; break; case 'U': if (which_prog == SWAPCTL) { @@ -199,13 +206,29 @@ usage(void) fprintf(stderr, "-a | file ...\n"); break; case SWAPCTL: - fprintf(stderr, "[-AhklsU] [-a file ... | -d file ...]\n"); + fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n"); break; } exit(1); } static void +sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen, + long blocksize) +{ + + if (hflag == 'H') { + char tmp[16]; + + humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE, + HN_B | HN_NOSPACE | HN_DECIMAL); + snprintf(buf, bufsize, "%*s", hlen, tmp); + } else { + snprintf(buf, bufsize, "%*lld", hlen, val / blocksize); + } +} + +static void swaplist(int lflag, int sflag, int hflag) { size_t mibsize, size; @@ -216,19 +239,33 @@ swaplist(int lflag, int sflag, int hflag) long long used = 0; long long tmp_total; long long tmp_used; + char buf[32]; pagesize = getpagesize(); switch(hflag) { + case 'G': + blocksize = 1024 * 1024 * 1024; + strlcpy(buf, "1GB-blocks", sizeof(buf)); + hlen = 10; + break; + case 'H': + blocksize = -1; + strlcpy(buf, "Bytes", sizeof(buf)); + hlen = 10; + break; case 'K': blocksize = 1024; + strlcpy(buf, "1kB-blocks", sizeof(buf)); hlen = 10; break; case 'M': blocksize = 1024 * 1024; + strlcpy(buf, "1MB-blocks", sizeof(buf)); hlen = 10; break; default: getbsize(&hlen, &blocksize); + snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); break; } @@ -237,8 +274,6 @@ swaplist(int lflag, int sflag, int hflag) err(1, "sysctlnametomib()"); if (lflag) { - char buf[32]; - snprintf(buf, sizeof(buf), "%ld-blocks", blocksize); printf("%-13s %*s %*s\n", "Device:", hlen, buf, @@ -253,24 +288,28 @@ swaplist(int lflag, int sflag, int hflag) if (xsw.xsw_version != XSWDEV_VERSION) errx(1, "xswdev version mismatch"); - tmp_total = (long long)xsw.xsw_nblks * pagesize / blocksize; - tmp_used = (long long)xsw.xsw_used * pagesize / blocksize; + tmp_total = (long long)xsw.xsw_nblks * pagesize; + tmp_used = (long long)xsw.xsw_used * pagesize; total += tmp_total; used += tmp_used; if (lflag) { - printf("/dev/%-8s %*lld %*lld\n", - devname(xsw.xsw_dev, S_IFCHR), - hlen, tmp_total, - hlen, tmp_used); + sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen, + blocksize); + printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR), + buf); + sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen, + blocksize); + printf("%s\n", buf); } } if (errno != ENOENT) err(1, "sysctl()"); if (sflag) { - printf("Total: %*lld %*lld\n", - hlen, total, - hlen, used); + sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize); + printf("Total: %s ", buf); + sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize); + printf("%s\n", buf); } } |