aboutsummaryrefslogtreecommitdiff
path: root/swu.c
diff options
context:
space:
mode:
Diffstat (limited to 'swu.c')
-rw-r--r--swu.c65
1 files changed, 52 insertions, 13 deletions
diff --git a/swu.c b/swu.c
index 0f3291a..5149879 100644
--- a/swu.c
+++ b/swu.c
@@ -1,3 +1,19 @@
+/* This file is part of swu
+ * Copyright (C) 2016 Sergey Poznyakoff
+ *
+ * Swu is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ *
+ * Swu is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with swu. If not, see <http://www.gnu.org/licenses/>.
+ */
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
@@ -10,7 +26,7 @@
char *pattern = "/proc/*/status";
int reverse;
-int human;
+int factor;
int show_all;
int show_total;
int sizewidth = 8;
@@ -187,7 +203,7 @@ cmp_size(struct swu const *a, struct swu const *b)
return 0;
}
-static int (*cmp)(struct swu const *, struct swu const *) = cmp_pid;
+static int (*cmp)(struct swu const *, struct swu const *) = cmp_size;
static int
compare(const void *a, const void *b)
@@ -209,7 +225,7 @@ collect(char **argv)
glob_t g;
size_t i;
- switch (glob (pattern, GLOB_NOSORT, globerrfunc, &g)) {
+ switch (glob(pattern, GLOB_NOSORT, globerrfunc, &g)) {
case 0:
break;
case GLOB_NOSPACE:
@@ -240,12 +256,12 @@ void
printsize(off_t size)
{
static char *suf[] = { "", "K", "M", "G", NULL };
- if (human) {
+ if (factor) {
int i;
unsigned fract = 0;
- for (i = 0; size > 1024 && suf[i+1]; i++) {
- fract = size % 1024;
- size /= 1024;
+ for (i = 0; size > factor && suf[i+1]; i++) {
+ fract = size % factor;
+ size /= factor;
}
if (fract)
printf("% *jd.%01u%s",
@@ -284,13 +300,30 @@ output(void)
void
help(void)
{
- //FIXME
+ printf("Usage: %s [OPTION]... [PROGNAME]...\n", progname);
+ puts("Summarize swap usage");
+ putchar('\n');
+ puts("-a, --all print all entries, including those with zero size");
+ puts("--help produce this help output");
+ puts("-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)");
+ puts("-H, --si likewise, but use powers of 1000 not 1024");
+ puts("-n, --name sort output by name");
+ puts("-p, --pid sort output by PID");
+ puts("-r, --reverse reverse sorting order");
+ puts("-s, --size sort output by size (default)");
+ puts("-c, -t, --total produce grand total");
+ puts("-v, --verbose produce additional diagnostics");
+ putchar('\n');
+ puts("Report bugs to <gray@gnu.org>");
}
+#define OPT_HELP 256
+
struct option opt[] = {
{ "all", 0, NULL, 'a' },
- { "human-readable", 0, NULL, 'H' },
- { "help", 0, NULL, 'h' },
+ { "help", 0, NULL, OPT_HELP },
+ { "human-readable", 0, NULL, 'h' },
+ { "si", 0, NULL, 'H' },
{ "name", 0, NULL, 'n' },
{ "pid", 0, NULL, 'p' },
{ "reverse", 0, NULL, 'r' },
@@ -316,11 +349,11 @@ main(int argc, char **argv)
show_all = 1;
break;
case 'H':
- human = 1;
+ factor = 1000;
break;
case 'h':
- help();
- exit(0);
+ factor = 1024;
+ break;
case 'n':
cmp = cmp_name;
break;
@@ -340,7 +373,13 @@ main(int argc, char **argv)
case 'v':
verbose++;
break;
+ case OPT_HELP:
+ help();
+ exit(0);
default:
+ fprintf(stderr,
+ "try '%s --help' for more information.\n",
+ progname);
exit(1);
}
}

Return to:

Send suggestions and report system problems to the System administrator.