-
Notifications
You must be signed in to change notification settings - Fork 15
/
display.c
1541 lines (1337 loc) · 30 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Top users/processes display for Unix
* Version 3
*
* This program may be freely redistributed,
* but this entire comment MUST remain intact.
*
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
*/
/*
* This file contains the routines that display information on the screen.
* Each section of the screen has two routines: one for initially writing
* all constant and dynamic text, and one for only updating the text that
* changes. The prefix "i_" is used on all the "initial" routines and the
* prefix "u_" is used for all the "updating" routines.
*
* ASSUMPTIONS:
* None of the "i_" routines use any of the termcap capabilities.
* In this way, those routines can be safely used on terminals that
* have minimal (or nonexistant) terminal capabilities.
*
* The routines are called in this order: *_loadave, i_timeofday,
* *_procstates, *_cpustates, *_memory, *_message, *_header,
* *_process, u_endscreen.
*/
#include "os.h"
#include <ctype.h>
#include <stdarg.h>
#include <unistd.h>
#include "pg_top.h"
#include "machine.h"
#include "screen.h" /* interface to screen package */
#include "layout.h" /* defines for screen position layout */
#include "display.h"
#include "boolean.h"
#include "utils.h"
#ifdef ENABLE_COLOR
#include "color.h"
#endif
#define CURSOR_COST 8
/* imported from screen.c */
extern int overstrike;
static int lmpid = -1;
static int display_width = MAX_COLS;
/* cursor positions of key points on the screen are maintained here */
/* layout.h has static definitions, but we may change our minds on some
of the positions as we make decisions about what needs to be displayed */
static int x_lastpid = X_LASTPID;
static int y_lastpid = Y_LASTPID;
static int x_loadave = X_LOADAVE;
static int y_loadave = Y_LOADAVE;
static int x_minibar = X_MINIBAR;
static int y_minibar = Y_MINIBAR;
static int x_uptime = X_UPTIME;
static int y_uptime = Y_UPTIME;
static int x_procstate = X_PROCSTATE;
static int y_procstate = Y_PROCSTATE;
static int x_cpustates = X_CPUSTATES;
static int y_cpustates = Y_CPUSTATES;
static int x_mem = X_MEM;
static int y_mem = Y_MEM;
static int x_swap = -1;
static int y_swap = -1;
static int y_message = Y_MESSAGE;
static int x_header = X_HEADER;
static int y_header = Y_HEADER;
static int x_idlecursor = X_IDLECURSOR;
static int y_idlecursor = Y_IDLECURSOR;
static int y_procs = Y_PROCS;
/* buffer and colormask that describes the content of the screen */
/* these are singly dimensioned arrays -- the row boundaries are
determined on the fly.
*/
static char *screenbuf = NULL;
static char *colorbuf = NULL;
static char scratchbuf[MAX_COLS];
static int bufsize = 0;
/* lineindex tells us where the beginning of a line is in the buffer */
#define lineindex(l) ((l)*MAX_COLS)
/* screen's cursor */
static int curr_x,
curr_y;
static int curr_color;
/* virtual cursor */
static int virt_x,
virt_y;
static char **procstate_names;
static char **cpustate_names;
static char **memory_names;
static char **swap_names;
static int num_procstates;
static int num_cpustates;
static int num_memory;
static int num_swap;
static int *lprocstates;
static int *lcpustates;
static int *cpustate_columns;
static int cpustate_total_length;
static enum
{
OFF, ON, ERASE
} header_status = ON;
#ifdef ENABLE_COLOR
static int load_cidx[3];
static int header_cidx;
static int *cpustate_cidx;
static int *memory_cidx;
static int *swap_cidx;
#endif
static int header_color = 0;
/* internal support routines */
/*
* static int string_count(char **pp)
*
* Pointer "pp" points to an array of string pointers, which is
* terminated by a NULL. Return the number of string pointers in
* this array.
*/
static int
string_count(char **pp)
{
register int cnt = 0;
if (pp != NULL)
{
while (*pp++ != NULL)
{
cnt++;
}
}
return (cnt);
}
void
display_clear()
{
#ifdef DEBUG
dprintf("display_clear\n");
#endif /* DEBUG */
clear();
memzero(screenbuf, bufsize);
memzero(colorbuf, bufsize);
curr_x = curr_y = 0;
}
/*
* void display_move(int x, int y)
*
* Efficiently move the cursor to x, y. This assumes the cursor is
* currently located at curr_x, curr_y, and will only use cursor
* addressing when it is less expensive than overstriking what's
* already on the screen.
*/
void
display_move(int x, int y)
{
char buff[128];
char *p;
char *bufp;
char *colorp;
int cnt = 0;
int color = curr_color;
#ifdef DEBUG
dprintf("display_move(%d, %d): curr_x %d, curr_y %d\n", x, y, curr_x, curr_y);
#endif /* DEBUG */
/* are we in a position to do this without cursor addressing? */
if (curr_y < y || (curr_y == y && curr_x <= x))
{
/*
* start buffering up what it would take to move there by rewriting
* what's on the screen
*/
cnt = CURSOR_COST;
p = buff;
/* one newline for every line */
while (cnt > 0 && curr_y < y)
{
if (color !=0)
{
p = strecpy(p, color_set(0));
color = 0;
cnt -= 5;
}
*p++ = '\n';
curr_y++;
curr_x = 0;
cnt--;
}
/* write whats in the screenbuf */
bufp = &screenbuf[lineindex(curr_y) + curr_x];
colorp = &colorbuf[lineindex(curr_y) + curr_x];
while (cnt > 0 && curr_x < x)
{
if (color !=*colorp)
{
color = *colorp;
p = strecpy(p, color_set(color));
cnt -= 5;
}
if ((*p = *bufp) == '\0')
{
/* somwhere on screen we haven't been before */
*p = *bufp = ' ';
}
p++;
bufp++;
colorp++;
curr_x++;
cnt--;
}
}
/* move the cursor */
if (cnt > 0)
{
/* screen rewrite is cheaper */
*p = '\0';
fputs(buff, stdout);
curr_color = color;
}
else
{
Move_to(x, y);
}
/* update our position */
curr_x = x;
curr_y = y;
}
/*
* display_write(int x, int y, int newcolor, int eol, char *new)
*
* Optimized write to the display. This writes characters to the
* screen in a way that optimizes the number of characters actually
* sent, by comparing what is being written to what is already on
* the screen (according to screenbuf and colorbuf). The string to
* write is "new", the first character of "new" should appear at
* screen position x, y. If x is -1 then "new" begins wherever the
* cursor is currently positioned. The string is written with color
* "newcolor". If "eol" is true then the remainder of the line is
* cleared. It is expected that "new" will have no newlines and no
* escape sequences.
*/
void
display_write(int x, int y, int newcolor, int eol, char *new)
{
char *bufp;
char *colorp;
int ch;
int diff;
#ifdef DEBUG
dprintf("display_write(%d, %d, %d, %d, \"%s\")\n",
x, y, newcolor, eol, new);
#endif /* DEBUG */
/* dumb terminal handling here */
if (!smart_terminal)
{
if (x != -1)
{
/* make sure we are on the right line */
while (curr_y < y)
{
putchar('\n');
curr_y++;
curr_x = 0;
}
/* make sure we are on the right column */
while (curr_x < x)
{
putchar(' ');
curr_x++;
}
}
/* write */
if (new != NULL)
{
fputs(new, stdout);
curr_x += strlen(new);
}
return;
}
/* adjust for "here" */
if (x == -1)
{
x = virt_x;
y = virt_y;
}
else
{
virt_x = x;
virt_y = y;
}
/* a pointer to where we start */
bufp = &screenbuf[lineindex(y) + x];
colorp = &colorbuf[lineindex(y) + x];
/* main loop */
while (new != NULL && (ch = *new++) != '\0')
{
/* if either character or color are different, an update is needed */
/* but only when the screen is wide enough */
if (x < display_width && (ch != *bufp || newcolor != *colorp))
{
/* check cursor */
if (y != curr_y || x != curr_x)
{
/* have to move the cursor */
display_move(x, y);
}
/* write character */
if (curr_color != newcolor)
{
fputs(color_set(newcolor), stdout);
curr_color = newcolor;
}
putchar(ch);
*bufp = ch;
*colorp = curr_color;
curr_x++;
}
/* move */
x++;
virt_x++;
bufp++;
colorp++;
}
/* eol handling */
if (eol && *bufp != '\0')
{
#ifdef DEBUG
dprintf("display_write: clear-eol (bufp = \"%s\")\n", bufp);
#endif /* DEBUG */
/* make sure we are color 0 */
if (curr_color != 0)
{
fputs(color_set(0), stdout);
curr_color = 0;
}
/* make sure we are at the end */
if (x != curr_x || y != curr_y)
{
Move_to(x, y);
curr_x = x;
curr_y = y;
}
/* clear to end */
clear_eol(strlen(bufp));
/* clear out whats left of this line's buffer */
diff = display_width - x;
if (diff > 0)
{
memzero(bufp, diff);
memzero(colorp, diff);
}
}
}
void
display_fmt(int x, int y, int newcolor, int eol, char *fmt,...)
{
va_list argp;
va_start(argp, fmt);
vsnprintf(scratchbuf, MAX_COLS, fmt, argp);
display_write(x, y, newcolor, eol, scratchbuf);
}
void
display_cte()
{
int len;
int y;
char *p;
int need_clear = 0;
/* is there anything out there that needs to be cleared? */
p = &screenbuf[lineindex(virt_y) + virt_x];
if (*p != '\0')
{
need_clear = 1;
}
else
{
/* this line is clear, what about the rest? */
y = virt_y;
while (++y < screen_length)
{
if (screenbuf[lineindex(y)] != '\0')
{
need_clear = 1;
break;
}
}
}
if (need_clear)
{
#ifdef DEBUG
dprintf("display_cte: clearing\n");
#endif /* DEBUG */
/* different method when there's no clear_to_end */
if (clear_to_end)
{
display_move(virt_x, virt_y);
putcap(clear_to_end);
}
else
{
if (++virt_y < screen_length)
{
display_move(0, virt_y);
virt_x = 0;
while (virt_y < screen_length)
{
p = &screenbuf[lineindex(virt_y)];
len = strlen(p);
if (len > 0)
{
clear_eol(len);
}
virt_y++;
}
}
}
/* clear the screenbuf */
len = lineindex(virt_y) + virt_x;
memzero(&screenbuf[len], bufsize - len);
memzero(&colorbuf[len], bufsize - len);
}
}
static void
summary_format(int x, int y, int *numbers, char **names)
{
register int num;
register char *thisname;
register char *lastname = NULL;
/* format each number followed by its string */
while ((thisname = *names++) != NULL)
{
/* get the number to format */
num = *numbers++;
/* display only non-zero numbers */
if (num != 0)
{
/* write the previous name */
if (lastname != NULL)
{
display_write(-1, -1, 0, 0, lastname);
}
/* write this number if positive */
if (num > 0)
{
display_write(x, y, 0, 0, itoa(num));
}
/* defer writing this name */
lastname = thisname;
/* next iteration will not start at x, y */
x = y = -1;
}
}
/*
* if the last string has a separator on the end, it has to be written
* with care
*/
if (lastname != NULL && (num = strlen(lastname)) > 1 &&
lastname[num - 2] == ',' && lastname[num - 1] == ' ')
{
display_fmt(-1, -1, 0, 1, "%.*s", num - 2, lastname);
}
else
{
display_write(-1, -1, 0, 1, lastname);
}
}
static void
summary_format_memory(int x, int y, long *numbers, char **names, int *cidx)
{
register long num;
register int color;
register char *thisname;
register char *lastname = NULL;
/* format each number followed by its string */
while ((thisname = *names++) != NULL)
{
/* get the number to format */
num = *numbers++;
color = 0;
/* display non-negative numbers */
if (num >= 0)
{
/* write the previous name */
if (lastname != NULL)
{
display_write(-1, -1, 0, 0, lastname);
}
/* defer writing this name */
lastname = thisname;
#ifdef ENABLE_COLOR
/* choose a color */
color = color_test(*cidx++, num);
#endif
/* is this number in kilobytes? */
if (thisname[0] == 'K')
{
display_write(x, y, color, 0, format_k(num));
lastname++;
}
else
{
display_write(x, y, color, 0, itoa((int) num));
}
/* next iteration will not start at x, y */
x = y = -1;
}
}
/*
* if the last string has a separator on the end, it has to be written
* with care
*/
if (lastname == NULL)
{
/*
* Don't show anything if the data doesn't exist.
*/
return;
}
else if ((num = strlen(lastname)) > 1 &&
lastname[num - 2] == ',' && lastname[num - 1] == ' ')
{
display_fmt(-1, -1, 0, 1, "%.*s", num - 2, lastname);
}
else
{
display_write(-1, -1, 0, 1, lastname);
}
}
/*
* int display_resize()
*
* Reallocate buffer space needed by the display package to accomodate
* a new screen size. Must be called whenever the screen's size has
* changed. Returns the number of lines available for displaying
* processes or -1 if there was a problem allocating space.
*/
int
display_resize()
{
register int lines;
register int newsize;
/* calculate the current dimensions */
/* if operating in "dumb" mode, we only need one line */
lines = smart_terminal ? screen_length : 1;
/*
* we don't want more than MAX_COLS columns, since the machine-dependent
* modules make static allocations based on MAX_COLS and we don't want to
* run off the end of their buffers
*/
display_width = screen_width;
if (display_width >= MAX_COLS)
{
display_width = MAX_COLS - 1;
}
/* see how much space we need */
newsize = lines * (MAX_COLS + 1);
/* reallocate only if we need more than we already have */
if (newsize > bufsize)
{
/* deallocate any previous buffer that may have been there */
if (screenbuf != NULL)
{
free(screenbuf);
}
if (colorbuf != NULL)
{
free(colorbuf);
}
/* allocate space for the screen and color buffers */
bufsize = newsize;
screenbuf = (char *) calloc(bufsize, sizeof(char));
colorbuf = (char *) calloc(bufsize, sizeof(char));
if (screenbuf == NULL || colorbuf == NULL)
{
/* oops! */
return (-1);
}
}
else
{
/* just clear them out */
memzero(screenbuf, bufsize);
memzero(colorbuf, bufsize);
}
/* adjust total lines on screen to lines available for procs */
lines -= y_procs;
/* return number of lines available */
/* for dumb terminals, pretend like we can show any amount */
return (smart_terminal ? lines : Largest);
}
/*
* int display_init(struct statics *statics)
*
* Initialize the display system based on information in the statics
* structure. Returns the number of lines available for displaying
* processes or -1 if there was an error.
*/
int
display_init(struct statics *statics)
{
register int lines;
register char **pp;
register char *p;
register int *ip;
register int i;
/*
* certain things may influence the screen layout, so look at those first
*/
/* a swap line shifts parts of the display down one */
swap_names = statics->swap_names;
if ((num_swap = string_count(swap_names)) > 0)
{
/* adjust screen placements */
y_message++;
y_header++;
y_idlecursor++;
y_procs++;
x_swap = X_SWAP;
y_swap = Y_SWAP;
}
/* call resize to do the dirty work */
lines = display_resize();
/* only do the rest if we need to */
if (lines > -1)
{
/* save pointers and allocate space for names */
procstate_names = statics->procstate_names;
num_procstates = string_count(procstate_names);
lprocstates = (int *) malloc(num_procstates * sizeof(int));
cpustate_names = statics->cpustate_names;
num_cpustates = string_count(cpustate_names);
lcpustates = (int *) malloc(num_cpustates * sizeof(int));
cpustate_columns = (int *) malloc(num_cpustates * sizeof(int));
memory_names = statics->memory_names;
num_memory = string_count(memory_names);
/* calculate starting columns where needed */
cpustate_total_length = 0;
pp = cpustate_names;
ip = cpustate_columns;
while (*pp != NULL)
{
*ip++ = cpustate_total_length;
if ((i = strlen(*pp++)) > 0)
{
cpustate_total_length += i + 8;
}
}
}
#ifdef ENABLE_COLOR
/* set up color tags for loadavg */
load_cidx[0] = color_tag("1min");
load_cidx[1] = color_tag("5min");
load_cidx[2] = color_tag("15min");
/* find header color */
header_cidx = color_tag("header");
header_color = color_test(header_cidx, 0);
/* color tags for cpu states */
cpustate_cidx = (int *) malloc(num_cpustates * sizeof(int));
i = 0;
p = strecpy(scratchbuf, "cpu.");
while (i < num_cpustates)
{
strcpy(p, cpustate_names[i]);
cpustate_cidx[i++] = color_tag(scratchbuf);
}
/* color tags for memory */
memory_cidx = (int *) malloc(num_memory * sizeof(int));
i = 0;
p = strecpy(scratchbuf, "memory.");
while (i < num_memory)
{
strcpy(p, homogenize(memory_names[i] + 1));
memory_cidx[i++] = color_tag(scratchbuf);
}
/* color tags for swap */
swap_cidx = (int *) malloc(num_swap * sizeof(int));
i = 0;
p = strecpy(scratchbuf, "swap.");
while (i < num_swap)
{
strcpy(p, homogenize(swap_names[i] + 1));
swap_cidx[i++] = color_tag(scratchbuf);
}
#endif
/* return number of lines available (or error) */
return (lines);
}
static void
pr_loadavg(double avg, int i)
{
int color = 0;
#ifdef ENABLE_COLOR
color = color_test(load_cidx[i], (int) (avg * 100));
#endif
display_fmt(x_loadave + X_LOADAVEWIDTH * i, y_loadave, color, 0,
avg < 10.0 ? " %5.2f" : " %5.1f", avg);
display_write(-1, -1, 0, 0, (i < 2 ? "," : ";"));
}
void
i_loadave(int mpid, double *avenrun)
{
register int i;
/* i_loadave also clears the screen, since it is first */
display_clear();
/* mpid == -1 implies this system doesn't have an _mpid */
if (mpid != -1)
{
display_fmt(0, 0, 0, 0,
"last pid: %5d; load avg:", mpid);
x_loadave = X_LOADAVE;
}
else
{
display_write(0, 0, 0, 0, "load averages:");
x_loadave = X_LOADAVE - X_LASTPIDWIDTH;
}
for (i = 0; i < 3; i++)
{
pr_loadavg(avenrun[i], i);
}
lmpid = mpid;
}
void
u_loadave(int mpid, double *avenrun)
{
register int i;
if (mpid != -1)
{
/* change screen only when value has really changed */
if (mpid != lmpid)
{
display_fmt(x_lastpid, y_lastpid, 0, 0,
"%5d", mpid);
lmpid = mpid;
}
}
/* display new load averages */
for (i = 0; i < 3; i++)
{
pr_loadavg(avenrun[i], i);
}
}
static char minibar_buffer[64];
#define MINIBAR_WIDTH 20
void
i_minibar(int (*formatter) (char *, int))
{
(void) ((*formatter) (minibar_buffer, MINIBAR_WIDTH));
display_write(x_minibar, y_minibar, 0, 0, minibar_buffer);
}
void
u_minibar(int (*formatter) (char *, int))
{
(void) ((*formatter) (minibar_buffer, MINIBAR_WIDTH));
display_write(x_minibar, y_minibar, 0, 0, minibar_buffer);
}
static int uptime_days;
static int uptime_hours;
static int uptime_mins;
static int uptime_secs;
void
i_uptime(time_t *bt, time_t *tod)
{
time_t uptime;
if (*bt != -1)
{
uptime = *tod - *bt;
uptime += 30;
uptime_days = uptime / 86400;
uptime %= 86400;
uptime_hours = uptime / 3600;
uptime %= 3600;
uptime_mins = uptime / 60;
uptime_secs = uptime % 60;
/*
* Display the uptime.
*/
display_fmt(x_uptime, y_uptime, 0, 0,
" up %d+%02d:%02d:%02d",
uptime_days, uptime_hours, uptime_mins, uptime_secs);
}
}
void
u_uptime(time_t *bt, time_t *tod)
{
i_uptime(bt, tod);
}
void
i_timeofday(time_t *tod)
{
/*
* Display the current time. "ctime" always returns a string that looks
* like this:
*
* Sun Sep 16 01:03:52 1973 012345678901234567890123 1 2
*
* We want indices 11 thru 18 (length 8).
*/
display_fmt((smart_terminal ? screen_width : 79) - 8, 0, 0, 1,
"%-8.8s", &(ctime(tod)[11]));
}
static int ltotal = 0;
/*
* *_procstates(total, brkdn, names) - print the process summary line
*/
void
i_procstates(int total, int *brkdn)
{
/* write current number of processes and remember the value */
display_fmt(0, y_procstate, 0, 0,
"%d processes: ", total);
ltotal = total;
/* remember where the summary starts */
x_procstate = virt_x;
/* format and print the process state summary */
summary_format(-1, -1, brkdn, procstate_names);
/* save the numbers for next time */
memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
}
void
u_procstates(int total, int *brkdn)
{
/* update number of processes only if it has changed */
if (ltotal != total)
{
display_fmt(0, y_procstate, 0, 0,
"%d", total);
/* if number of digits differs, rewrite the label */
if (digits(total) != digits(ltotal))
{
display_write(-1, -1, 0, 0, " processes: ");
x_procstate = virt_x;
}
/* save new total */
ltotal = total;
}
/* see if any of the state numbers has changed */
if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
{
/* format and update the line */
summary_format(x_procstate, y_procstate, brkdn, procstate_names);
memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
}
}
/*
* *_cpustates(states, names) - print the cpu state percentages
*/
/* cpustates_tag() calculates the correct tag to use to label the line */
char *
cpustates_tag()
{
register char *use;
static char *short_tag = "CPU: ";
static char *long_tag = "CPU states: ";
/*
* if length + strlen(long_tag) >= screen_width, then we have to use the
* shorter tag (we subtract 2 to account for ": ")
*/
if (cpustate_total_length + (int) strlen(long_tag) - 2 >= screen_width)
{
use = short_tag;
}
else
{
use = long_tag;
}
/* set x_cpustates accordingly then return result */
x_cpustates = strlen(use);