summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorkn <kn@cvs.openbsd.org>2018-11-28 22:00:31 +0000
committerkn <kn@cvs.openbsd.org>2018-11-28 22:00:31 +0000
commit606b751053bd67b44741b3f1d328787297e8aaae (patch)
tree7e1723d3259478b44c63e791e5934c2fec4e558d /usr.bin
parent80e8dc680c8a080b8cc107f90f1bfc3a57855a07 (diff)
Allow reverse sort order
Prefixing the field with a dash will sort processes in reverse order. `o -pid' will therefore list PID 1 on top. "looks good" tedu, improvements and OK cheloha
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/top/display.c5
-rw-r--r--usr.bin/top/machine.c43
-rw-r--r--usr.bin/top/top.116
-rw-r--r--usr.bin/top/top.c44
4 files changed, 58 insertions, 50 deletions
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c
index 78d42793fed..0c4a1daeb12 100644
--- a/usr.bin/top/display.c
+++ b/usr.bin/top/display.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: display.c,v 1.57 2018/11/17 23:10:08 cheloha Exp $ */
+/* $OpenBSD: display.c,v 1.58 2018/11/28 22:00:30 kn Exp $ */
/*
* Top users/processes display for Unix
@@ -817,7 +817,8 @@ show_help(void)
"I | i - toggle the display of idle processes\n"
"k [-sig] pid - send signal `-sig' to process `pid'\n"
"n|# count - show `count' processes\n"
- "o field - specify sort order (size, res, cpu, time, pri, pid, command)\n"
+ "o [-]field - specify sort order (size, res, cpu, time, pri, pid, command)\n"
+ " (o -field sorts in reverse)\n"
"P pid - highlight process `pid' (P+ switches highlighting off)\n"
"p pid - display process by `pid' (p+ selects all processes)\n"
"q - quit\n"
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index 201e6d81674..80b10e64b1d 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: machine.c,v 1.95 2018/11/17 23:10:08 cheloha Exp $ */
+/* $OpenBSD: machine.c,v 1.96 2018/11/28 22:00:30 kn Exp $ */
/*-
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
@@ -602,6 +602,8 @@ static unsigned char sorted_state[] =
1 /* zombie */
};
+extern int rev_order;
+
/*
* proc_compares - comparison functions for "qsort"
*/
@@ -631,6 +633,17 @@ static unsigned char sorted_state[] =
#define ORDERKEY_CMD \
if ((result = strcmp(p1->p_comm, p2->p_comm)) == 0)
+/* remove one level of indirection and set sort order */
+#define SETORDER do { \
+ if (rev_order) { \
+ p1 = *(struct kinfo_proc **) pp2; \
+ p2 = *(struct kinfo_proc **) pp1; \
+ } else { \
+ p1 = *(struct kinfo_proc **) pp1; \
+ p2 = *(struct kinfo_proc **) pp2; \
+ } \
+ } while (0)
+
/* compare_cpu - the comparison function for sorting by cpu percentage */
static int
compare_cpu(const void *v1, const void *v2)
@@ -640,9 +653,7 @@ compare_cpu(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_PCTCPU
ORDERKEY_CPUTIME
@@ -663,9 +674,7 @@ compare_size(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_MEM
ORDERKEY_RSSIZE
@@ -686,9 +695,7 @@ compare_res(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_RSSIZE
ORDERKEY_MEM
@@ -709,9 +716,7 @@ compare_time(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_CPUTIME
ORDERKEY_PCTCPU
@@ -732,9 +737,7 @@ compare_prio(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_PRIO
ORDERKEY_PCTCPU
@@ -754,9 +757,7 @@ compare_pid(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_PID
ORDERKEY_PCTCPU
@@ -777,9 +778,7 @@ compare_cmd(const void *v1, const void *v2)
struct kinfo_proc *p1, *p2;
int result;
- /* remove one level of indirection */
- p1 = *(struct kinfo_proc **) pp1;
- p2 = *(struct kinfo_proc **) pp2;
+ SETORDER;
ORDERKEY_CMD
ORDERKEY_PCTCPU
diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index 675515c43dd..ba587864839 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: top.1,v 1.70 2018/11/02 12:46:10 kn Exp $
+.\" $OpenBSD: top.1,v 1.71 2018/11/28 22:00:30 kn Exp $
.\"
.\" Copyright (c) 1997, Jason Downs. All rights reserved.
.\"
@@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: November 2 2018 $
+.Dd $Mdocdate: November 28 2018 $
.Dt TOP 1
.Os
.Sh NAME
@@ -35,7 +35,7 @@
.Op Fl 1bCHIinqSu
.Op Fl d Ar count
.Op Fl g Ar string
-.Op Fl o Ar field
+.Op Fl o Oo - Oc Ns Ar field
.Op Fl p Ar pid
.Op Fl s Ar time
.Op Fl U Oo - Oc Ns Ar user
@@ -137,13 +137,16 @@ mode.
This is identical to
.Em batch
mode.
-.It Fl o Ar field
+.It Fl o Oo - Oc Ns Ar field
Sort the process display area using the specified
.Ar field
as the primary key.
The field name is the name of the column as seen in the output,
but in lower case.
The
+.Sq -
+prefix reverses the order.
+The
.Ox
version of
.Nm
@@ -327,10 +330,13 @@ This acts similarly to the command
Show
.Ar count
processes.
-.It o Ar field
+.It o Oo - Oc Ns Ar field
Sort the process display area using the specified
.Ar field
as the primary key.
+The
+.Sq -
+prefix reverses the order.
Values are the same as for the
.Fl o
flag, as detailed above.
diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c
index c63aa4d2003..87b7eaace58 100644
--- a/usr.bin/top/top.c
+++ b/usr.bin/top/top.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: top.c,v 1.97 2018/11/17 23:10:08 cheloha Exp $ */
+/* $OpenBSD: top.c,v 1.98 2018/11/28 22:00:30 kn Exp $ */
/*
* Top users/processes display for Unix
@@ -74,6 +74,7 @@ extern int ncpuonline;
extern int (*proc_compares[])(const void *, const void *);
int order_index;
+int rev_order;
int displays = 0; /* indicates unspecified */
char do_unames = Yes;
@@ -93,6 +94,9 @@ int combine_cpus = 0;
char topn_specified = No;
#endif
+struct system_info system_info;
+struct statics statics;
+
/*
* these defines enumerate the "strchr"s of the commands in
* command_chars
@@ -129,12 +133,24 @@ usage(void)
extern char *__progname;
fprintf(stderr,
- "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o field] "
+ "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o [-]field] "
"[-p pid] [-s time]\n\t[-U [-]user] [number]\n",
__progname);
}
static int
+getorder(char *field)
+{
+ int i, r = field[0] == '-';
+
+ i = string_index(r ? field + 1 : field, statics.order_names);
+ if (i != -1)
+ rev_order = r;
+
+ return i;
+}
+
+static int
filteruser(char buf[])
{
const char *errstr;
@@ -311,9 +327,6 @@ parseargs(int ac, char **av)
}
}
-struct system_info system_info;
-struct statics statics;
-
int
main(int argc, char *argv[])
{
@@ -381,20 +394,9 @@ main(int argc, char *argv[])
/* determine sorting order index, if necessary */
if (order_name != NULL) {
- if ((order_index = string_index(order_name,
- statics.order_names)) == -1) {
- char **pp, msg[512];
-
- snprintf(msg, sizeof(msg),
- "'%s' is not a recognized sorting order",
- order_name);
- strlcat(msg, ". Valid are:", sizeof(msg));
- pp = statics.order_names;
- while (*pp != NULL) {
- strlcat(msg, " ", sizeof(msg));
- strlcat(msg, *pp++, sizeof(msg));
- }
- new_message(MT_delayed, msg);
+ if ((order_index = getorder(order_name)) == -1) {
+ new_message(MT_delayed,
+ " %s: unrecognized sorting order", order_name);
order_index = 0;
}
}
@@ -879,10 +881,10 @@ rundisplay(void)
new_message(MT_standout,
"Order to sort: ");
if (readline(tempbuf, sizeof(tempbuf)) > 0) {
- if ((i = string_index(tempbuf,
- statics.order_names)) == -1) {
+ if ((i = getorder(tempbuf)) == -1) {
new_message(MT_standout,
" %s: unrecognized sorting order",
+ tempbuf[0] == '-' ? tempbuf + 1 :
tempbuf);
no_command = Yes;
} else