aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Makefile44
-rw-r--r--README13
-rw-r--r--swu.1103
-rw-r--r--swu.c65
5 files changed, 216 insertions, 14 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..027df83
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.emacs*
+*~
+core
+swu
+*.tar.gz
diff --git a/Makefile b/Makefile
index 0730f58..8c3fb6d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,46 @@
-CFLAGS=-ggdb
+# 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/>.
+
+CFLAGS=
+PREFIX=/usr/local
+MANDIR=$(PREFIX)/share/man
+BINDIR=$(PREFIX)/bin
+MAN1DIR=$(MANDIR)/man1
+
+PACKAGE=swu
+VERSION=1.0
+distdir=$(PACKAGE)-$(VERSION)
+
+DISTFILES=Makefile swu.c swu.1 README
swu: swu.c
cc -oswu $(CFLAGS) $(CPPFLAGS) swu.c
+
+clean:
+ rm -f *.o swu
+
+install: swu
+ install -D swu $(DESTDIR)$(BINDIR)/swu
+ install -D -m 644 swu.1 $(DESTDIR)$(MAN1DIR)/swu.1
+
+distdir:
+ rm -rf $(distdir) && \
+ mkdir $(distdir) && \
+ cp $(DISTFILES) $(distdir)
+
+dist: distdir
+ tar cfz $(distdir).tar.gz $(distdir)
+ rm -rf $(distdir)
diff --git a/README b/README
new file mode 100644
index 0000000..1f939ad
--- /dev/null
+++ b/README
@@ -0,0 +1,13 @@
+The swu utility displays the amount of swap space used by the programs.
+The program is Linux-specific. To compile it, just run make in its
+source directory.
+
+To install, run 'make install'. This will install swu binary in
+/usr/local/bin, and its manpage in /usr/local/share/man/man1. These
+defaults can be overridden using the PREFIX variable. E.g. to install
+the binary in /usr/bin and the manpage in /usr/share/man/man1, run
+'make install PREFIX=/usr'. See Makefile for more info.
+
+Refer to the manpage (swu.1), for info about the program itself.
+
+Report bugs and suggestions to <gray@gnu.org>.
diff --git a/swu.1 b/swu.1
new file mode 100644
index 0000000..d60f478
--- /dev/null
+++ b/swu.1
@@ -0,0 +1,103 @@
+.\" This file is part of swu -*- nroff -*-
+.\" 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/>.
+.TH SWU 1 "March 12, 2016" "SWU" "User Commands"
+.SH NAME
+swu \- summarize swap usage
+.SH SYNOPSIS
+\fBswu\fR
+[\fB\-acHhnprstv\fR]
+[\fB\-\-all\fR]
+[\fB\-\-help\fR]
+[\fB\-\-human-readable\fR]
+[\fB\-\-si\fR]
+[\fB\-\-name\fR]
+[\fB\-\-pid\fR]
+[\fB\-\-reverse\fR]
+[\fB\-\-size\fR]
+[\fB\-\-total\fR]
+[\fB\-\-verbose\fR]
+[\fIPROGRAM\fR]...
+.sp
+\fBswu \-\-help\fR
+.SH DESCRIPTION
+Displays the amount of swap space used by each
+.I PROGRAM
+argument. Without arguments, displays information about programs that
+use non-zero amount of swap space. With \fB\-a\fR (\fB\-\-all\fR)
+option, displays information about all programs, including those that
+use no swap space.
+.PP
+The output consists of three columns: PID, swap space used, and
+program name. By default, it is sorted by the used swap space in
+ascending order.
+.SH OPTIONS
+.TP
+.BR \-a ", " \-\-all
+Print all entries, including those with zero size.
+.TP
+.B \-\-help
+Produce help output.
+.TP
+.BR \-h ", " \-\-human\-readable
+Print sizes in human readable format (e.g., 1K 234M 2G).
+.TP
+.BR \-H ", " \-\-si
+Likewise, but use powers of 1000 not 1024.
+.TP
+.BR \-n ", " \-\-name
+Sort output by name.
+.TP
+.BR \-p ", " \-\-pid
+Sort output by PID.
+.TP
+.BR \-r ", " \-\-reverse
+Reverse sorting order.
+.TP
+.BR \-s ", " \-\-size
+Sort output by size (default).
+.TP
+.BR \-c ", " \-t ", " \-\-total
+Produce grand total.
+.TP
+.BR \-v ", " \-\-verbose
+Produce additional diagnostics.
+.SH BUGS
+The program is Linux-specific.
+.PP
+The produced grand total (see the \fB\-t\fR option) can differ
+somewhat from what is reported by
+.BR free (1).
+.SH AUTHORS
+Sergey Poznyakoff
+.SH "BUG REPORTS"
+Report bugs to <gray@gnu.org.ua>.
+.SH COPYRIGHT
+Copyright \(co 2016 Sergey Poznyakoff
+.br
+.na
+License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
+.br
+.ad
+This is free software: you are free to change and redistribute it.
+There is NO WARRANTY, to the extent permitted by law.
+.\" Local variables:
+.\" eval: (add-hook 'write-file-hooks 'time-stamp)
+.\" time-stamp-start: ".TH [A-Z_][A-Z0-9_.\\-]* [0-9] \""
+.\" time-stamp-format: "%:B %:d, %:y"
+.\" time-stamp-end: "\""
+.\" time-stamp-line-limit: 20
+.\" end:
+
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.