-
Notifications
You must be signed in to change notification settings - Fork 3
/
ncmatrix.c
1150 lines (872 loc) · 29 KB
/
ncmatrix.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
/**************************************************************************
* ncmatrix.c *
* Copyright (C) 2005 T Bloedorn *
* * built from CMatrix codebase by Chris Allegretta *
* *
* cmatrix.c *
* Copyright (C) 1999 Chris Allegretta *
* *
* This program 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 1, or (at your option) *
* any later version. *
* *
* This program 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 this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
* *
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include "config.h"
#ifdef HAVE_NCURSES_H
# include <ncurses.h>
#else /* Uh oh */
# include <curses.h>
#endif /* CURSES_H */
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif /* HAVE_SYS_IOCTL_H */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif /* HAVE_TERMIOS_H */
#ifdef HAVE_TERMIO_H
# include <termio.h>
#endif /* HAVE_TERMIO_H */
////////////////////////////////////////////////////////////////////////////////
///////////////////////// tmb for ncmatrix /////////////////////////////////////
// tmb - for syslog msgs in getnetload
#include <syslog.h>
/* Matrix typedef */
typedef struct cmatrix
{
int val;
int bold;
int color; // tmb - added color
} cmatrix;
char *AppName = "ncmatrix"; // tmb - changed name of app
// for -I option
#define NIC_DEV "/proc/net/dev" // linux only I guess?
#define MAX_NIC 12
#define MAX_STATS 16
#define NFILL_LIMIT 80 // max fillage to occur in matrix
#define THRESH_LIMIT 200
#define TXP 100 // tx packets
#define RXP 101
#define TXB 102
#define RXB 103
char Nic [MAX_NIC] = "eth0"; // holds the network interface to monitor eth0, eth1.. ppp0,,... etc
// tmb - returns value from field 'type' from /proc/net/dev buffer 'buf'
long getstats (int type, char *buf);
// tmb - fills longs for transmit packets, rx packets, trans. bytes and rx bytes
void getnetload (long *txp, long *rxp, long *txb, long *rxb);
// tmb - sets color of char based on nic load
void setcharcolor (int i, int j);
// tmb - return random number between min and max
int getrandom (int min, int max);
// tmb - fill matrix display radomly num times depending on type
void nfillmatrix (int type, long old, long new);
// tmb - returns color number from command line arg
int getstrcolor (char *arg);
// tmb - adjustable threshold limit for network streams
int RptThreshold = THRESH_LIMIT;
// tmb - the color for received packets
int RPColor = COLOR_MAGENTA;
// tmb - the color for transmitted packets
int TPColor = COLOR_RED;
///////////////////////// tmb for ncmatrix /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* Global variables, unfortunately */
int console = 0, xwindow = 0; /* Are we in the console? X? */
cmatrix **matrix = (cmatrix **) NULL; /* The matrix has you */
int *length = NULL; /* Length of cols in each line */
int *spaces = NULL; /* spaces left to fill */
int *updates = NULL; /* What does this do again? :) */
int va_system (char *str, ...)
{
va_list ap;
char foo[133];
va_start (ap, str);
vsnprintf (foo, 132, str, ap);
va_end (ap);
return system (foo);
}
/* What we do when we're all set to exit */
RETSIGTYPE
finish (int sigage)
{
curs_set (1);
clear ();
refresh ();
resetty ();
endwin ();
#ifdef HAVE_CONSOLECHARS
if (console)
va_system ("consolechars -d");
#elif defined(HAVE_SETFONT)
if (console)
va_system ("setfont");
#endif
exit (0);
}
/* What we do when we're all set to exit */
RETSIGTYPE
c_die (char *msg, ...)
{
va_list ap;
curs_set (1);
clear ();
refresh ();
resetty ();
endwin ();
#ifdef HAVE_CONSOLECHARS
if (console)
va_system ("consolechars -d");
#elif defined(HAVE_SETFONT)
if (console)
va_system ("setfont");
#endif
va_start (ap, msg);
vfprintf (stderr, "%s", ap);
va_end (ap);
exit (0);
}
void
usage (void)
{
printf (" Usage: %s -[abBfhlsVx] [-u delay] [-C color] [-I nic] [-H limit] [-R color] [-T color]\n", AppName);
printf (" -a: Asynchronous scroll\n");
printf (" -b: Bold characters on\n");
printf (" -B: All bold characters (overrides -b)\n");
printf (" -f: Force the linux $TERM type to be on\n");
printf (" -l: Linux mode (uses matrix console font)\n");
printf (" -o: Use old-style scrolling\n");
printf (" -h: Print usage and exit\n");
printf (" -n: No bold characters (overrides -b and -B, default)\n");
printf (" -s: \"Screensaver\" mode, exits on first keystroke\n");
printf (" -x: X window mode, use if your xterm is using mtx.pcf\n");
printf (" -V: Print version information and exit\n");
printf (" -u delay (0 - 10, default 4): Screen update delay\n");
printf (" -C [color]: Use this color for matrix (default green)\n");
printf (" -I [nic] : Use this network interface for data (/proc/net/dev)\n");
printf (" -H [limit]: threshold for reporting network traffic (loop trigger).\n");
printf (" -R [color]: Use this color for recieved packets.\n");
printf (" -T [color]: Use this color for transmitted packets.\n");
}
void
version (void)
{
printf
("-=[ %s ]=- version %s by Thomas Bloedorn 02/11/2005\noriginal cmatrix code by Chris Allegretta (compiled %s, %s)\n",
AppName, VERSION, __TIME__, __DATE__);
}
/* nmalloc from nano by Big Gaute */
void *nmalloc (size_t howmuch)
{
void *r;
/* Panic save? */
if (!(r = malloc (howmuch)))
c_die ("%s: malloc: out of memory!", AppName);
return r;
}
/* Initialize the global variables */
RETSIGTYPE
var_init (void)
{
int i, j;
if (matrix != NULL)
free (matrix);
matrix = nmalloc (sizeof (cmatrix) * (LINES + 1));
for (i = 0; i <= LINES; i++)
matrix[i] = nmalloc (sizeof (cmatrix) * COLS);
if (length != NULL)
free (length);
length = nmalloc (COLS * sizeof (int));
if (spaces != NULL)
free (spaces);
spaces = nmalloc (COLS * sizeof (int));
if (updates != NULL)
free (updates);
updates = nmalloc (COLS * sizeof (int));
/* Make the matrix */
for (i = 0; i <= LINES; i++)
for (j = 0; j <= COLS - 1; j += 2)
matrix[i][j].val = -1;
for (j = 0; j <= COLS - 1; j += 2)
{
/* Set up spaces[] array of how many spaces to skip */
spaces[j] = (int) rand () % LINES + 1;
/* And length of the stream */
length[j] = (int) rand () % (LINES - 3) + 3;
/* Sentinel value for creation of new objects */
matrix[1][j].val = ' ';
/* And set updates[] array for update speed. */
updates[j] = (int) rand () % 3 + 1;
}
}
void handle_sigwinch (int s)
{
char *tty = NULL;
int fd = 0;
int result = 0;
struct winsize win;
tty = ttyname (0);
if (!tty)
return;
fd = open (tty, O_RDWR);
if (fd == -1)
return;
result = ioctl (fd, TIOCGWINSZ, &win);
if (result == -1)
return;
COLS = win.ws_col;
LINES = win.ws_row;
#ifdef HAVE_RESIZETERM
resizeterm (LINES, COLS);
# ifdef HAVE_WRESIZE
if (wresize (stdscr, LINES, COLS) == ERR)
c_die ("Cannot resize window!");
# endif
/* HAVE_WRESIZE */
#endif /* HAVE_RESIZETERM */
var_init ();
/* Do these b/c width may have changed... */
clear ();
refresh ();
}
//
// tmb - returns color number from ascii string. checks limits and reports.
//
int getstrcolor (char *arg)
{
int colornum = COLOR_GREEN; // assume default
// compare *arg for good color match or else report as error
if (!strcasecmp (arg, "green"))
colornum = COLOR_GREEN;
else if (!strcasecmp (arg, "red"))
colornum = COLOR_RED;
else if (!strcasecmp (arg, "blue"))
colornum = COLOR_BLUE;
else if (!strcasecmp (arg, "white"))
colornum = COLOR_WHITE;
else if (!strcasecmp (arg, "yellow"))
colornum = COLOR_YELLOW;
else if (!strcasecmp (arg, "cyan"))
colornum = COLOR_CYAN;
else if (!strcasecmp (arg, "magenta"))
colornum = COLOR_MAGENTA;
else if (!strcasecmp (arg, "black"))
colornum = COLOR_BLACK;
else
{
printf (" Invalid color selection (%s)\n Valid "
"colors are green, red, blue, "
"white, yellow, cyan, magenta " "and black.\n", arg);
exit (1);
}
return (colornum);
}
int main (int argc, char *argv[])
{
int i, j = 0, count = 0, screensaver = 0, asynch = 0, bold =
-1, force = 0, y, z, firstcoldone = 0, oldstyle = 0, random =
0, update = 4, highnum = 0, mcolor = COLOR_GREEN, randnum = 0, randmin =
0;
char *oldtermname, *syscmd = NULL;
int optchr, keypress;
/* Many thanks to morph- ([email protected]) for this getopt patch */
opterr = 0;
while ((optchr = getopt (argc, argv, "abBfhlnosxVu:C:I:H:R:T:")) != EOF)
{
switch (optchr)
{
case 's':
screensaver = 1;
break;
case 'a':
asynch = 1;
break;
case 'b':
if (bold != 2 && bold != 0)
bold = 1;
break;
case 'B':
if (bold != 0)
bold = 2;
break;
case 'C': // get color for whole matrix
mcolor = getstrcolor (optarg);
break; // case C
case 'I': // network interface to monitor
snprintf (Nic, MAX_NIC - 1, "%s", optarg);
//printf ("optarg:%s\n", Nic);
break;
case 'H': // limit for network reporting. Like a timeout, but more of a loop counter
RptThreshold = atol (optarg);
break;
case 'R': // recieve packet color 1 - 7
RPColor = getstrcolor (optarg);
break;
case 'T': // transmit packet color 1 - 7
TPColor = getstrcolor (optarg);
break;
case 'f':
force = 1;
break;
case 'l':
console = 1;
break;
case 'n':
bold = 0;
break;
case 'h':
case '?':
usage ();
exit (0);
case 'o':
oldstyle = 1;
break;
case 'u':
update = atoi (optarg);
break;
case 'x':
xwindow = 1;
break;
case 'V':
version ();
exit (0);
} // switch optchar
} // while optchar
/* If bold hasn't been turned on or off yet, assume off */
if (bold == -1)
bold = 0;
oldtermname = getenv ("TERM");
if (force && strcmp ("linux", getenv ("TERM")))
{
/* Portability wins out here, apparently putenv is much more common on non-Linux than setenv */
putenv ("TERM=linux");
}
initscr ();
savetty ();
nonl ();
cbreak ();
noecho ();
timeout (0);
leaveok (stdscr, TRUE);
curs_set (0);
signal (SIGINT, finish);
signal (SIGWINCH, handle_sigwinch);
#ifdef HAVE_CONSOLECHARS
if (console)
if (va_system ("consolechars -f matrix") != 0)
{
c_die
(" There was an error running consolechars. Please make sure the\n"
" consolechars program is in your $PATH. Try running \"setfont matrix\" by hand.\n");
}
#elif defined(HAVE_SETFONT)
if (console)
if (va_system ("setfont matrix") != 0)
{
c_die (" There was an error running setfont. Please make sure the\n"
" setfont program is in your $PATH. Try running \"setfont matrix\" by hand.\n");
}
#endif
if (has_colors ())
{
start_color ();
/* Add in colors, if available */
#ifdef HAVE_USE_DEFAULT_COLORS
if (use_default_colors () != ERR)
{
init_pair (COLOR_BLACK, -1, -1);
init_pair (COLOR_GREEN, COLOR_GREEN, -1);
init_pair (COLOR_WHITE, COLOR_WHITE, -1);
init_pair (COLOR_RED, COLOR_RED, -1);
init_pair (COLOR_CYAN, COLOR_CYAN, -1);
init_pair (COLOR_MAGENTA, COLOR_MAGENTA, -1);
init_pair (COLOR_BLUE, COLOR_BLUE, -1);
init_pair (COLOR_YELLOW, COLOR_YELLOW, -1);
}
else
{
#else
{
#endif
init_pair (COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair (COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair (COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair (COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair (COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair (COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
}
}
srand (time (NULL));
/* Set up values for random number generation */
if (console || xwindow)
{
randnum = 51;
randmin = 166;
highnum = 217;
}
else
{
randnum = 93;
randmin = 33;
highnum = 123;
}
var_init ();
while (1)
{
count++;
if (count > 4)
count = 1;
if ((keypress = wgetch (stdscr)) != ERR)
{
if (screensaver == 1)
finish (0);
else
switch (keypress)
{
case 'q':
finish (0);
break;
case 'a':
asynch = 1 - asynch;
break;
case 'b':
bold = 1;
break;
case 'B':
bold = 2;
break;
case 'n':
bold = 0;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
update = keypress - 48;
break;
case '!':
mcolor = COLOR_RED;
break;
case '@':
mcolor = COLOR_GREEN;
break;
case '#':
mcolor = COLOR_YELLOW;
break;
case '$':
mcolor = COLOR_BLUE;
break;
case '%':
mcolor = COLOR_MAGENTA;
break;
case '^':
mcolor = COLOR_CYAN;
break;
case '&':
mcolor = COLOR_WHITE;
break;
}
}
for (j = 0; j <= COLS - 1; j += 2)
{
if (count > updates[j] || asynch == 0)
{
/* I dont like old-style scrolling, yuck */
if (oldstyle)
{
for (i = LINES - 1; i >= 1; i--)
matrix[i][j].val = matrix[i - 1][j].val;
random = (int) rand () % (randnum + 8) + randmin;
if (matrix[1][j].val == 0)
matrix[0][j].val = 1;
else if (matrix[1][j].val == ' ' || matrix[1][j].val == -1)
{
if (spaces[j] > 0)
{
matrix[0][j].val = ' ';
spaces[j]--;
}
else
{
/* Random number to determine whether head of next collumn
of chars has a white 'head' on it. */
if (((int) rand () % 3) == 1)
matrix[0][j].val = 0;
else
matrix[0][j].val = (int) rand () % randnum + randmin;
spaces[j] = (int) rand () % LINES + 1;
}
}
else if (random > highnum && matrix[1][j].val != 1)
{
matrix[0][j].val = '#';
}
else
matrix[0][j].val = (int) rand () % randnum + randmin;
}
else
{
/* New style scrolling (default) */
if (matrix[0][j].val == -1 && matrix[1][j].val == ' ' &&
spaces[j] > 0)
{
matrix[0][j].val = -1;
spaces[j]--;
}
else if (matrix[0][j].val == -1 && matrix[1][j].val == ' ')
{
length[j] = (int) rand () % (LINES - 3) + 3;
matrix[0][j].val = (int) rand () % randnum + randmin;
if ((int) rand () % 2 == 1)
matrix[0][j].bold = 2;
spaces[j] = (int) rand () % LINES + 1;
}
i = 0;
y = 0;
firstcoldone = 0;
while (i <= LINES)
{
// tmb
setcharcolor (i, j);
/* Skip over spaces */
while (i <= LINES &&
(matrix[i][j].val == ' ' || matrix[i][j].val == -1))
i++;
if (i > LINES)
break;
/* Go to the head of this collumn */
z = i;
y = 0;
while (i <= LINES && (matrix[i][j].val != ' ' && matrix[i][j].val != -1))
{
i++;
y++;
}
if (i > LINES)
{
matrix[z][j].val = ' ';
matrix[LINES][j].bold = 1;
continue;
}
matrix[i][j].val = (int) rand () % randnum + randmin;
//matrix[i][j].color = COLOR_GREEN;
matrix[i][j].color = mcolor;
if (matrix[i - 1][j].bold == 2)
{
matrix[i - 1][j].bold = 1;
matrix[i][j].bold = 2;
}
/* If we're at the top of the collumn and it's reached its
* full length (about to start moving down), we do this
* to get it moving. This is also how we keep segments not
* already growing from growing accidentally =>
*/
if (y > length[j] || firstcoldone)
{
matrix[z][j].val = ' ';
matrix[0][j].val = -1;
}
firstcoldone = 1;
i++;
}
}
}
/* Hack =P */
if (!oldstyle)
{
y = 1;
z = LINES;
}
else
{
y = 0;
z = LINES - 1;
}
for (i = y; i <= z; i++)
{
move (i - y, j);
if (matrix[i][j].val == 0 || matrix[i][j].bold == 2)
{
if (console || xwindow)
attron (A_ALTCHARSET);
attron (COLOR_PAIR (COLOR_WHITE));
if (bold)
attron (A_BOLD);
if (matrix[i][j].val == 0)
{
if (console || xwindow)
addch (183);
else
addch ('&');
}
else
addch (matrix[i][j].val);
attroff (COLOR_PAIR (COLOR_WHITE));
if (bold)
attroff (A_BOLD);
if (console || xwindow)
attroff (A_ALTCHARSET);
}
else
{
// tmb
//attron (COLOR_PAIR (mcolor));
attron (COLOR_PAIR (matrix[i][j].color));
if (matrix[i][j].val == 1)
{
if (bold)
attron (A_BOLD);
addch ('|');
if (bold)
attroff (A_BOLD);
}
else
{
if (console || xwindow)
attron (A_ALTCHARSET);
if (bold == 2 || (bold == 1 && matrix[i][j].val % 2 == 0))
attron (A_BOLD);
if (matrix[i][j].val == -1)
addch (' ');
else
addch (matrix[i][j].val);
if (bold == 2 || (bold == 1 && matrix[i][j].val % 2 == 0))
attroff (A_BOLD);
if (console || xwindow)
attroff (A_ALTCHARSET);
}
attroff (COLOR_PAIR (mcolor));
}
}
}
refresh ();
napms (update * 10);
}
syscmd = nmalloc (sizeof (char *) * (strlen (oldtermname) + 15));
sprintf (syscmd, "putenv TERM=%s", oldtermname);
system (syscmd);
finish (0);
}
//
// tmb - returns value from field 'type' from /proc/net/dev buffer 'buf'
//
long getstats (int type, char *buf)
{
long ret = 0;
int field = 0;
long stat [MAX_STATS];
// what type of info to return from buf. linux has specific fields
switch (type)
{
case TXP: // retunr transmit packets
field = 9;
break;
case RXP: // return recieve packets
field = 1;
break;
case TXB: // return transmit bytes
field = 8;
break;
case RXB: // return recieved bytes
field = 0;
break;
}
// get field data.. there is a better way to do this... ;)
sscanf (buf, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
&stat [0], &stat [1], &stat [2], &stat [3],
&stat [4], &stat [5], &stat [6], &stat [7],
&stat [8], &stat [9], &stat [10], &stat [11],
&stat [12], &stat [13], &stat [14], &stat [15]);
// only want realy fields
if (field < MAX_STATS)
ret = stat [field];
else
printf ("strange field value %d\n", field);
//printf ("field:%d data:%ld\n", field, ret);
return (ret);
}
//
// tmb - fills longs for transmit packets, rx packets, trans.
// bytes and rx bytes by reading /proc/net/dev(nic from command line)
//
// find load of network interface
// transmit packets
// recieve packetes
// tx bytes
// rx bytes
//
void getnetload (long *txp, long *rxp, long *txb, long *rxb)
{
FILE *fin;
char buf [1024];
static char msgsent = 0;
char *ptr;
int niclen = 0;
*txp = 0;
*rxp = 0;
*txb = 0;
*rxb = 0;
niclen = strlen (Nic);
if (niclen < 1)
return;
// open net dev
if ((fin = fopen (NIC_DEV, "rt")) == NULL)
return;
// and read it
while (!feof (fin))
{
memset (buf, 0x0, sizeof(buf));
fgets (buf, sizeof(buf), fin);
// too big then there is no newline
if (( buf [strlen(buf)-1] != 0x0A) && (msgsent == 0))
{
syslog (LOG_NOTICE, "fgets from getnetload() returned a string without a newline?");
msgsent = 1;
}
else
{
// find the Nic
if ((ptr = strstr (buf, Nic)))
{
// carefull
if ( niclen < MAX_NIC )
{
ptr += niclen;