summaryrefslogtreecommitdiffstats
path: root/contrib/top/top.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/top/top.c')
-rw-r--r--contrib/top/top.c52
1 files changed, 39 insertions, 13 deletions
diff --git a/contrib/top/top.c b/contrib/top/top.c
index 55bee4e..aa119ea 100644
--- a/contrib/top/top.c
+++ b/contrib/top/top.c
@@ -9,7 +9,9 @@ char *copyright =
* but this entire comment MUST remain intact.
*
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
- * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
+ * Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
+ * Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
+ * Copyright (c) 1996, William LeFebvre, Group sys Consulting
*/
/*
@@ -53,9 +55,6 @@ char stdoutbuf[Buffersize];
/* build Signal masks */
#define Smask(s) (1 << ((s) - 1))
-/* for system errors */
-extern int errno;
-
/* for getopt: */
extern int optind;
extern char *optarg;
@@ -177,7 +176,7 @@ char *argv[];
/* FD_SET and friends are not present: fake it */
typedef int fd_set;
#define FD_ZERO(x) (*(x) = 0)
-#define FD_SET(f, x) (*(x) = f)
+#define FD_SET(f, x) (*(x) = 1<<f)
#endif
fd_set readfds;
@@ -209,6 +208,8 @@ char *argv[];
/* set the buffer for stdout */
#ifdef DEBUG
+ extern FILE *debug;
+ debug = fopen("debug.run", "w");
setbuffer(stdout, NULL, 0);
#else
setbuffer(stdout, stdoutbuf, Buffersize);
@@ -256,10 +257,16 @@ char *argv[];
optind = 1;
}
- while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != EOF)
+ while ((i = getopt(ac, av, "SIbinquvs:d:U:o:")) != EOF)
{
switch(i)
{
+ case 'v': /* show version number */
+ fprintf(stderr, "%s: version %s\n",
+ myname, version_string());
+ exit(1);
+ break;
+
case 'u': /* toggle uid/username display */
do_unames = !do_unames;
break;
@@ -304,10 +311,10 @@ char *argv[];
break;
case 's':
- if ((delay = atoi(optarg)) < 0)
+ if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0))
{
fprintf(stderr,
- "%s: warning: seconds delay should be non-negative -- using default\n",
+ "%s: warning: seconds delay should be positive -- using default\n",
myname);
delay = Default_DELAY;
warnings++;
@@ -579,7 +586,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
/* determine number of processes to actually display */
/* this number will be the smallest of: active processes,
number user requested, number current screen accomodates */
- active_procs = system_info.p_active;
+ active_procs = system_info.P_ACTIVE;
if (active_procs > topn)
{
active_procs = topn;
@@ -604,7 +611,13 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
u_endscreen(i);
/* now, flush the output buffer */
- fflush(stdout);
+ if (fflush(stdout) != 0)
+ {
+ new_message(MT_standout, " Write error on stdout");
+ putchar('\r');
+ quit(1);
+ /*NOTREACHED*/
+ }
/* only do the rest if we have more displays to show */
if (displays)
@@ -645,7 +658,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
/* set up arguments for select with timeout */
FD_ZERO(&readfds);
- FD_SET(1, &readfds); /* for standard input */
+ FD_SET(0, &readfds); /* for standard input */
timeout.tv_sec = delay;
timeout.tv_usec = 0;
@@ -660,7 +673,14 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
/* now read it and convert to command strchr */
/* (use "change" as a temporary to hold strchr) */
- (void) read(0, &ch, 1);
+ if (read(0, &ch, 1) != 1)
+ {
+ /* read error: either 0 or -1 */
+ new_message(MT_standout, " Read error on stdin");
+ putchar('\r');
+ quit(1);
+ /*NOTREACHED*/
+ }
if ((iptr = strchr(command_chars, ch)) == NULL)
{
/* illegal command */
@@ -763,7 +783,10 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
new_message(MT_standout, "Seconds to delay: ");
if ((i = readline(tempbuf1, 8, Yes)) > -1)
{
- delay = i;
+ if ((delay = i) == 0 && getuid() != 0)
+ {
+ delay = 1;
+ }
}
clear_message();
break;
@@ -893,6 +916,9 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n",
}
}
+#ifdef DEBUG
+ fclose(debug);
+#endif
quit(0);
/*NOTREACHED*/
}
OpenPOWER on IntegriCloud