wmtop: Use proper int types.
[dockapps.git] / wmifs / wmifs.c
blob49eb3d90632a1bd61b727db0752652378da1bc17
1 /* WMiFS - a complete network monitoring dock.app
2 Copyright (C) 1997, 1998 Martijn Pieterse <pieterse@xs4all.nl>
3 Copyright (C) 1997, 1998 Antoine Nulle <warp@xs4all.nl>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 Best viewed with vim5, using ts=4
21 This code was mainly put together by looking at the
22 following programs:
24 asclock
25 A neat piece of equip, used to display the date
26 and time on the screen.
27 Comes with every AfterStep installation.
29 Source used:
30 How do I create a not so solid window?
31 How do I open a window?
32 How do I use pixmaps?
34 pppstats
35 A program that prints the amount of data that
36 is transferred over a ppp-line.
38 Source used:
39 How do I read the ppp device?
40 ------------------------------------------------------------
42 Author: Martijn Pieterse (pieterse@xs4all.nl)
44 This program was hacked together between the 7th of March
45 and the 14th of March 1998.
47 This program might be Y2K resistant. We shall see. :)
49 This program is distributed under the GPL license.
50 (as were asclock and pppstats)
52 Known Features: (or in non M$ talk, BUGS)
53 * only ppp0 will be read
54 use wmifs if you want to read more than one ppp connection
55 not sure about this.
56 * the leds won't be reliable with
57 more than 1 ppp connection
58 * there is an iconwin, and win variable.
59 I have no clue why only win shouldn't
60 be enough. Will check it out later.
61 * The afterstep what seems the shift the
62 pixmap a bit. Don't know how and why.
63 It works in the WindowManager.
65 Things to do:
66 Split up main()
68 ----
69 Thanks
70 ----
72 Most of the ideas, and jumpstarting it:
74 #linuxnl, without this irc-channel wmppp would've never seen the light!
76 CCC (Constructive Code Criticism):
78 Marcelo E. Magallon
79 Thanks a LOT! It takes a while to get me convinced... :)
82 Minor bugs and ideas:
84 Marc De Scheemaecker / David Mihm / Chris Soghoian /
85 Alessandro Usseglio Viretta
87 and ofcourse numerous ppl who send us bug reports.
88 (numerous? hmm.. todo: rephrase this :) )
91 ----
92 Changes:
93 ---
94 02/29/2004 (Tom Marshall, tommy@home.tig-grr.com)
95 * Patch to add a special interface name "auto" for the -i
96 option. "wmifs -i auto" will automatically select the
97 first up interface.
98 01/08/2004 (Peter Samuelson, peter@samba-tng.org)
99 * Patch to make make sampling and scrolling intervals
100 customizable, adds new options -I and -s.
101 01/15/2002 (Matyas Koszik, koszik@debijan.lonyay.edu.hu)
102 * Patch that fixes segfaults on long interface names.
103 08/31/2001 (Davi Leal, davileal@terra.es)
104 * Patch that cuts long interface names, so they look
105 good in wmifs. For example, "dummy0" gets displayed
106 as "dumm0", "vmnet10" as "vmn10", etc.
107 06/16/2001 (Jorge GarcĂ­a, Jorge.Garcia@uv.es)
108 * Added the LockMode, so wmifs doesn't swap to another
109 interface if the one requested with "-i" isn't up.
110 05/06/2001 (Jordi Mallach, jordi@sindominio.net)
111 * Integrated many patches, fixing issues with suspended
112 wmifs.
113 07/21/1999 (Stephen Pitts, smpitts@midsouth.rr.com)
114 * Added new constant: BUFFER_SIZE to determine the size
115 of the buffer used in fgets() operations. Right now,
116 its at 512 bytes. Fixed crashing on my system when
117 one line of /proc/net/dev was longer than 128 bytes
118 04/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
119 * Changed the "middle of the waveform" line color
120 * Moved the RedrawWindow out of the main loop.
121 Lightens the system load
122 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
123 * Torn wmppp and wmifs apart.
124 This is gonna be wmifs
125 * Added parse_rcfile
126 * Added waitpid, to get rid of the zombies, spotteb by Alessandro Usseglio Viretta
127 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
128 * Used the DrawStats routine from wmifs in wmppp
129 * I decided to add a list in this source file
130 with name of ppl who helped me build this code better.
131 * I finally removed the /proc/net/route dependancy
132 All of the connections are taken from /proc/net/dev.
133 /proc/net/route is still used for checking if it is on-line.
134 27/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
135 * WMIFS: stats scrolled, while red led burning
136 * WMPPP: changed positions of line speed
137 25/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
138 * Changed the checknetdevs routine, a lot!
139 23/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
140 * Added line speed detection. via seperate exec. (this has to be suid root!)
141 Speed has to be no more than 99999
142 * Added speed and forcespeed in ~/.wmppprc and /etc/wmppprc
143 * wmifs: added on-line detection scheme, update the bitmap coordinates
144 * wmppp: the x-button now allways disconnects.
145 22/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
146 * Added /etc/wmppprc support, including "forced" mode.
147 * Added /etc/wmifsrc support, including "forced" mode.
148 21/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
149 * Moved the stats one pixel down.
150 * Added status led in wmifs.
151 * Changed RX/TX leds of wmifs to resemble wmppp
152 * Added the "dot" between eth.0 ppp.0 etc.
153 * Changed to wmifs stats to match wmppp stats (only pppX changed)
154 * Made sure that when specified -t 1, it stayed that way, even
155 when longer than 100 minutes online
156 * With -t 1, jump from 59:59 to 01:00 instead of 99:59 to 01:40
157 16/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
158 * Added "all" devices in wmifs
159 * Added "lo" support only if aked via -i
160 * Added on-line time detection (using /var/run/ppp0.pid)
161 * Added time-out for the orange led. (one minute)
162 15/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
163 * Another wmppp-master.xpm.
164 Line speed detection being the main problem here.. :(
165 * Moved START_COMMAND / STOP_COMMAND to ~/.wmppprc
166 Return 0, everything went ok.
167 Return 10, the command did not work.
168 Please note, these functions are ran in the background.
169 * Removed the ability to configure
170 * When "v" is clicked, an orange led will appear.
171 if the connect script fails (return value == 10)
172 it will become dark again. Else the on-line detection will
173 pick it up, and "green" it.
174 14/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
175 * Added "-timer"
176 * Added "-display" support
177 * Changed pixmap to a no-name pixmap.
178 + Changed LED positions
179 + Changed Timer position
180 + Changed Stats Size
181 05/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
182 * Added ~/.wmifsrc support.
183 * Put some code in DrawStats
184 * Check devices when pressing "device change"
185 03/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
186 * Added code for wmifs
187 28/03/1998 (Martijn Pieterse, pieterse@xs4all.nl)
188 * forgot what i did.. :)
189 27/03/1998 (Martijn Pieterse, pieterse@xs4all.nl)
190 * Added on-line detection
191 Scan through /proc/net/route and check everye line
192 for "ppp".
193 * A bit of code clean-up.
196 #define _DEFAULT_SOURCE
197 #include <X11/X.h> /* for ButtonPress, ButtonRelease, etc */
198 #include <X11/Xlib.h> /* for XEvent, XButtonEvent, etc */
199 #include <X11/xpm.h> /* for XpmColorSymbol, Pixel, etc */
200 #include <ctype.h> /* for toupper */
201 #include <linux/ppp_defs.h> /* for ppp_stats, pppstat */
202 #include <net/if_ppp.h> /* for ifpppstatsreq, ifr__name, etc */
203 #include <stddef.h> /* for size_t */
204 #include <stdio.h> /* for fprintf, NULL, stderr, etc */
205 #include <stdlib.h> /* for exit, atof, atoi, getenv */
206 #include <string.h> /* for strcmp, strcpy, strlen, etc */
207 #include <sys/ioctl.h> /* for ioctl */
208 #include <sys/socket.h> /* for socket, AF_INET */
209 #include <sys/time.h> /* for timeval, gettimeofday */
210 #include <sys/wait.h> /* for waitpid, WNOHANG */
211 #include <time.h> /* for timespec, nanosleep */
212 #include <libdockapp/misc.h> /* for execCommand */
213 #include <libdockapp/wmgeneral.h> /* for copyXPMArea, display, etc */
214 #include "wmifs-mask.xbm" /* for wmifs_mask_bits, etc */
215 #include "wmifs-master.xpm" /* for wmifs_master_xpm */
217 /* How often to check for new network interface, in ms */
218 #define CHECK_INTERFACE_INTERVAL 5000
220 /* How often to query the interfaces, in ms */
221 #define DEFAULT_SAMPLE_INTERVAL 50
223 /***********/
224 /* Defines */
225 /***********/
227 #ifndef ifr__name
228 #define ifr__name ifr_name
229 #endif
231 #ifndef stats_ptr
232 #define stats_ptr stats.p.FIXME
233 #endif
235 /* Fill in and uncomment the hardcoded actions */
236 /* #define LEFT_ACTION (NULL) */
237 /* #define MIDDLE_ACTION (NULL) */
238 /* #define RIGHT_ACTION (NULL) */
240 /* Defines voor alle coordinate */
242 #define LED_NET_RX (4)
243 #define LED_NET_TX (5)
244 #define LED_NET_POWER (6)
246 #define WMIFS_VERSION "1.6"
248 /* the size of the buffer read from /proc/net/ */
249 #define BUFFER_SIZE 512
250 /**********************/
251 /* External Variables */
252 /**********************/
254 extern char **environ;
256 /********************/
257 /* Global Variables */
258 /********************/
260 char *active_interface = NULL;
261 int TimerDivisor = 60;
262 int WaveForm = 0;
263 int LockMode = 0;
264 int SampleInt = DEFAULT_SAMPLE_INTERVAL;
265 int ScrollSpeed = CHECK_INTERFACE_INTERVAL;
266 XpmIcon wmgen;
267 char color[256];
269 /*****************/
270 /* PPP variables */
271 /*****************/
273 #define PPP_UNIT 0
274 int ppp_h = -1;
276 #define PPP_STATS_HIS 54
278 /***********************/
279 /* Function Prototypes */
280 /***********************/
282 void usage(void);
283 void printversion(void);
284 void DrawTime(int, int);
285 void DrawStats(int *, int, int, int, int);
287 void SetOnLED(int);
288 void SetErrLED(int);
289 void SetWaitLED(int);
290 void SetOffLED(int);
292 void ButtonUp(int);
293 void ButtonDown(int);
295 void wmifs_routine(int, char **);
297 void get_ppp_stats(struct ppp_stats *cur);
300 /********/
301 /* Main */
302 /********/
304 int main(int argc, char *argv[])
307 int i;
309 color[0] = 0;
311 /* Parse Command Line */
313 for (i = 1; i < argc; i++) {
314 char *arg = argv[i];
316 if (*arg == '-') {
317 switch (arg[1]) {
318 case 'c' :
319 if (argc > i+1) {
320 strcpy(color, argv[i+1]);
321 i++;
323 break;
324 case 'd':
325 if (strcmp(arg+1, "display")) {
326 usage();
327 exit(1);
329 break;
330 case 'g':
331 if (strcmp(arg+1, "geometry")) {
332 usage();
333 exit(1);
335 break;
336 case 'i':
337 active_interface = argv[i+1];
338 i++;
339 break;
340 case 'I':
341 SampleInt = atof(argv[i+1]) * 1000;
342 i++;
343 break;
344 case 'l':
345 LockMode = 1;
346 break;
347 case 's':
348 ScrollSpeed = atof(argv[i+1]) * 1000;
349 i++;
350 break;
351 case 'v':
352 printversion();
353 exit(0);
354 break;
355 case 'w':
356 WaveForm = 1;
357 break;
358 default:
359 if (strcmp(argv[i-1], "-geometry")) {
360 usage();
361 exit(0);
363 break;
368 wmifs_routine(argc, argv);
369 return 0;
372 Pixel scale_pixel(Pixel pixel, float scale)
374 int red, green, blue;
376 red = pixel / ( 1 << 16 );
377 green = pixel % (1 << 16) / (1 << 8);
378 blue = pixel % (1 << 8);
380 red *= scale;
381 green *= scale;
382 blue *= scale;
384 return red * (1 << 16) + green * (1 << 8) + blue;
387 /*******************************************************************************\
388 |* wmifs_routine *|
389 \*******************************************************************************/
391 #define MAX_STAT_DEVICES 16
393 typedef struct {
395 char name[8];
396 int his[55][2];
397 long istatlast;
398 long ostatlast;
400 } stat_dev;
402 stat_dev stat_devices[MAX_STAT_DEVICES];
404 char *left_action = NULL;
405 char *right_action = NULL;
406 char *middle_action = NULL;
408 int checknetdevs(void);
409 int get_statistics(char *, long *, long *, long *, long *);
410 int stillonline(char *);
411 void DrawActiveIFS(char *);
413 void wmifs_routine(int argc, char **argv)
416 rckeys wmifs_keys[] = {
417 { "left", &left_action },
418 { "middle", &middle_action },
419 { "right", &right_action },
420 { NULL, NULL }
424 int i, j;
425 XEvent Event;
426 int but_stat = -1;
428 int stat_online;
429 int stat_current;
430 int first_time = 1;
432 unsigned int curtime;
433 unsigned int nexttime;
434 struct timeval tv, tv2;
436 long ipacket, opacket, istat, ostat;
438 char temp[BUFFER_SIZE];
439 char *p;
441 for (i = 0; i < MAX_STAT_DEVICES; i++) {
442 stat_devices[i].name[0] = 0;
443 for (j = 0; j < 48; j++) {
444 stat_devices[i].his[j][0] = 0;
445 stat_devices[i].his[j][1] = 0;
449 stat_online = checknetdevs();
451 stat_current = 0;
452 if (active_interface) {
453 int isauto = !strcmp(active_interface, "auto");
454 for (i = 0; i < stat_online; i++) {
455 if ((isauto && stillonline(stat_devices[i].name)) ||
456 !strcmp(stat_devices[i].name, active_interface)) {
457 stat_current = i;
458 break;
463 #ifdef LEFT_ACTION
464 left_action = strdup(LEFT_ACTION);
465 #endif
466 #ifdef MIDDLE_ACTION
467 middle_action = strdup(MIDDLE_ACTION);
468 #endif
469 #ifdef RIGHT_ACTION
470 right_action = strdup(RIGHT_ACTION);
471 #endif
473 /* Scan throught the .rc files */
474 parse_rcfile(CONF"/wmifsrc", wmifs_keys);
476 p = getenv("HOME");
477 if (p == NULL || *p == 0) {
478 fprintf(stderr, "Unknown $HOME directory, please check your environment\n");
479 return;
481 strcpy(temp, p);
482 strcat(temp, "/.wmifsrc");
483 parse_rcfile(temp, wmifs_keys);
485 parse_rcfile(CONF"/wmifsrc.fixed", wmifs_keys);
487 /* set user-defined colors */
488 if (color[0] != 0) {
489 Window Root;
490 XColor col;
491 XWindowAttributes attributes;
492 int screen;
493 Pixel pixel;
494 #define NUMSYMBOLS 4
495 XpmColorSymbol user_color[NUMSYMBOLS] = {
496 {NULL, "#2081B2CAAEBA", 0}, /* + */
497 {NULL, "#28A23CF338E3", 0}, /* O */
498 {NULL, "#000049244103", 0}, /* @ */
499 {NULL, "#18618A288617", 0}, /* # */
503 /* code based on GetColor() from wmgeneral.c */
504 /* we need a temporary display to parse the color */
505 display = XOpenDisplay(NULL);
506 screen = DefaultScreen(display);
507 Root = RootWindow(display, screen);
508 XGetWindowAttributes(display, Root, &attributes);
510 col.pixel = 0;
511 if (!XParseColor(display, attributes.colormap, color, &col)) {
512 fprintf(stderr, "wmtime: can't parse %s.\n", color);
513 goto draw_window;
514 } else if (!XAllocColor(display, attributes.colormap, &col)) {
515 fprintf(stderr, "wmtime: can't allocate %s.\n", color);
516 goto draw_window;
519 pixel = col.pixel;
521 /* replace colors from wmtime-master.xpm */
522 user_color[0].pixel = pixel;
523 user_color[1].pixel = scale_pixel(pixel, .3);
524 user_color[2].pixel = scale_pixel(pixel, .4);
525 user_color[3].pixel = scale_pixel(pixel, .8);
527 wmgen.attributes.valuemask |= XpmColorSymbols;
528 wmgen.attributes.numsymbols = NUMSYMBOLS;
529 wmgen.attributes.colorsymbols = user_color;
531 XCloseDisplay(display);
534 draw_window:
535 openXwindow(argc, argv, wmifs_master_xpm, (char*)wmifs_mask_bits, wmifs_mask_width, wmifs_mask_height);
537 /* > Button */
538 AddMouseRegion(0, 5, 5, 35, 15);
539 AddMouseRegion(1, 5, 20, 58, 58);
541 gettimeofday(&tv2, NULL);
542 nexttime = ScrollSpeed;
544 DrawActiveIFS(stat_devices[stat_current].name);
546 while (1) {
547 struct timespec ts;
549 gettimeofday(&tv, NULL);
550 curtime = (tv.tv_sec - tv2.tv_sec) * 1000
551 + (tv.tv_usec - tv2.tv_usec) / 1000;
553 waitpid(0, NULL, WNOHANG);
555 for (i = 0; i < stat_online; i++) {
556 get_statistics(stat_devices[i].name, &ipacket, &opacket, &istat, &ostat);
558 if (first_time) {
559 first_time = 0;
560 } else {
561 stat_devices[i].his[53][0] += istat - stat_devices[i].istatlast;
562 stat_devices[i].his[53][1] += ostat - stat_devices[i].ostatlast;
565 if (i == stat_current) {
566 if (!stillonline(stat_devices[i].name))
567 SetErrLED(LED_NET_POWER);
568 else
569 SetOnLED(LED_NET_POWER);
571 if (stat_devices[i].istatlast == istat)
572 SetOffLED(LED_NET_RX);
573 else
574 SetOnLED(LED_NET_RX);
576 if (stat_devices[i].ostatlast == ostat)
577 SetOffLED(LED_NET_TX);
578 else
579 SetOnLED(LED_NET_TX);
582 stat_devices[i].istatlast = istat;
583 stat_devices[i].ostatlast = ostat;
585 RedrawWindow();
587 if (curtime >= nexttime) {
588 nexttime = curtime + ScrollSpeed;
590 DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
591 for (i = 0; i < stat_online; i++) {
592 if (stillonline(stat_devices[i].name)) {
593 for (j = 1; j < 54; j++) {
594 stat_devices[i].his[j-1][0] = stat_devices[i].his[j][0];
595 stat_devices[i].his[j-1][1] = stat_devices[i].his[j][1];
597 stat_devices[i].his[53][0] = 0;
598 stat_devices[i].his[53][1] = 0;
601 RedrawWindow();
604 while (XPending(display)) {
605 XNextEvent(display, &Event);
606 switch (Event.type) {
607 case Expose:
608 RedrawWindow();
609 break;
610 case DestroyNotify:
611 XCloseDisplay(display);
612 exit(0);
613 break;
614 case ButtonPress:
615 but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
616 break;
617 case ButtonRelease:
618 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
620 if (but_stat == i && but_stat >= 0) {
621 switch (but_stat) {
622 case 0:
623 /* re-read the table */
624 strcpy(temp, stat_devices[stat_current].name);
625 stat_online = checknetdevs();
626 stat_current = 0;
627 for (i = 0; i < stat_online; i++) {
628 if (!strcmp(temp, stat_devices[i].name))
629 stat_current = i;
632 stat_current++;
633 if (stat_current == stat_online)
634 stat_current = 0;
636 DrawActiveIFS(stat_devices[stat_current].name);
638 DrawStats(&stat_devices[stat_current].his[0][0], 54, 40, 5, 58);
639 break;
640 case 1:
641 switch (Event.xbutton.button) {
642 case 1:
643 if (left_action)
644 execCommand(left_action);
645 break;
646 case 2:
647 if (middle_action)
648 execCommand(middle_action);
649 break;
650 case 3:
651 if (right_action)
652 execCommand(right_action);
653 break;
655 break;
659 but_stat = -1;
660 RedrawWindow();
661 break;
664 ts.tv_sec = 0;
665 ts.tv_nsec = SampleInt * 1000000;
666 nanosleep(&ts, NULL);
670 /*******************************************************************************\
671 |* void DrawActiveIFS(char *) *|
672 \*******************************************************************************/
674 void DrawActiveIFS(char *real_name)
677 /* Cijfers op: 0,65
678 Letters op: 0,75
679 Alles 9 hoog, 6 breedt
681 Destinatie: 5,5
684 size_t i;
685 int k;
686 size_t len;
687 char name[256];
690 copyXPMArea(5, 84, 30, 10, 5, 5);
693 strcpy(name, real_name);
694 len = strlen(name);
695 if (len > 5) {
696 for (i = len-5; i < len && !(name[i] >= '0' && name[i] <= '9'); i++)
698 for (; i <= len; i++) /* '=' to get the '\0' character moved too \*/
699 name[i-(len-5)] = name[i];
702 k = 5;
703 for (i = 0; name[i]; i++) {
704 int c;
706 if (i == strlen(name)-1 && strlen(name) <= 4 && name[strlen(name)-1] >= '0' &&
707 name[strlen(name)-1] <= '9') {
708 copyXPMArea(61, 64, 4, 9, k, 5);
709 k += 4;
711 c = toupper(name[i]);
712 if (c >= 'A' && c <= 'Z') {
713 c -= 'A';
714 copyXPMArea(c * 6, 74, 6, 9, k, 5);
715 k += 6;
716 } else {
717 c -= '0';
718 copyXPMArea(c * 6, 64, 6, 9, k, 5);
719 k += 6;
725 /*******************************************************************************\
726 |* get_statistics *|
727 \*******************************************************************************/
729 int get_statistics(char *devname, long *ip, long *op, long *is, long *os)
732 FILE *fp;
733 char temp[BUFFER_SIZE];
734 char *p, *saveptr;
735 char *tokens = " |:\n";
736 int input, output;
737 int i;
738 int found;
739 struct ppp_stats ppp_cur;
742 if (!strncmp(devname, "ppp", 3)) {
743 static int ppp_opened;
745 if (!ppp_opened) {
746 /* Open the ppp device. */
747 memset(&ppp_cur, 0, sizeof(ppp_cur));
748 ppp_h = socket(AF_INET, SOCK_DGRAM, 0);
749 if (ppp_h < 0)
750 return -1;
751 get_ppp_stats(&ppp_cur);
752 ppp_opened = 1;
755 get_ppp_stats(&ppp_cur);
757 *op = ppp_cur.p.ppp_opackets;
758 *ip = ppp_cur.p.ppp_ipackets;
760 *is = ppp_cur.p.ppp_ibytes;
761 *os = ppp_cur.p.ppp_obytes;
763 return 0;
766 /* Read from /proc/net/dev the stats! */
767 fp = fopen("/proc/net/dev", "r");
768 if (!fgets(temp, BUFFER_SIZE, fp)) {
769 fclose(fp);
770 return -1;
772 if (!fgets(temp, BUFFER_SIZE, fp)) {
773 fclose(fp);
774 return -1;
777 input = -1;
778 output = -1;
779 i = 0;
780 found = -1;
782 p = strtok_r(temp, tokens, &saveptr);
783 do {
784 if (!(strcmp(p, "packets"))) {
785 if (input == -1)
786 input = i;
787 else
788 output = i;
790 i++;
791 p = strtok_r(NULL, tokens, &saveptr);
792 } while (input == -1 || output == -1);
794 while (fgets(temp, BUFFER_SIZE, fp)) {
795 if (strstr(temp, devname)) {
796 found = 0;
797 p = strtok_r(temp, tokens, &saveptr);
798 i = 0;
799 do {
800 if (i == input) {
801 *ip = *is = atoi(p);
802 input = -1;
804 if (i == output) {
805 *op = *os = atoi(p);
806 output = -1;
808 i++;
809 p = strtok_r(NULL, tokens, &saveptr);
810 } while (input != -1 || output != -1);
813 fclose(fp);
815 return found;
818 /*******************************************************************************\
819 |* stillonline *|
820 \*******************************************************************************/
822 int stillonline(char *ifs)
825 FILE *fp;
826 int i;
828 i = 0;
829 fp = fopen("/proc/net/route", "r");
830 if (fp) {
831 char temp[BUFFER_SIZE];
833 while (fgets(temp, BUFFER_SIZE, fp)) {
834 if (strstr(temp, ifs)) {
835 i = 1; /* Line is alive */
836 break;
839 fclose(fp);
841 return i;
844 /*******************************************************************************\
845 |* checknetdevs *|
846 \*******************************************************************************/
848 int checknetdevs(void)
851 FILE *fd;
852 int i = 0, j;
853 int k;
854 int devsfound = 0;
855 char foundbuffer[MAX_STAT_DEVICES][8];
857 for (i = 0; i < MAX_STAT_DEVICES; i++)
858 foundbuffer[i][0] = 0;
860 /* foundbuffer vullen met info uit /proc/net/dev */
862 fd = fopen("/proc/net/dev", "r");
863 if (fd) {
864 char temp[BUFFER_SIZE];
866 /* Skip the first 2 lines */
867 if (!fgets(temp, BUFFER_SIZE, fd)) {
868 fclose(fd);
869 return -1;
871 if (!fgets(temp, BUFFER_SIZE, fd)) {
872 fclose(fd);
873 return -1;
875 while (fgets(temp, BUFFER_SIZE, fd)) {
876 char *p, *saveptr;
877 char *tokens = " :\t\n";
879 p = strtok_r(temp, tokens, &saveptr);
880 if (p == NULL) {
881 printf("Barfed on: %s", temp);
882 break;
884 /* Skip dummy code */
886 if (!strncmp(p, "dummy", 5))
887 continue;
889 /* If p == "lo", and active_interface (as given on the cmd line) != "lo",
890 skip it! */
892 if (strcmp(p, "lo") || (active_interface && !strcmp(active_interface, "lo"))) {
893 strcpy(foundbuffer[devsfound], p);
894 devsfound++;
896 if (devsfound >= MAX_STAT_DEVICES)
897 break;
899 fclose(fd);
902 /* Nu foundbuffer naar stat_devices[].name kopieeren */
904 for (i = 0; i < MAX_STAT_DEVICES; i++) {
905 /* Loop stat_devices na, als die naam niet voorkomt in foundbuffer, kill! */
907 if (stat_devices[i].name[0]) {
908 k = 0;
909 for (j = 0; j < MAX_STAT_DEVICES; j++) {
910 if (!strcmp(stat_devices[i].name, foundbuffer[j])) {
911 k = 1;
912 foundbuffer[j][0] = 0;
915 if (!k)
916 stat_devices[i].name[0] = 0;
920 for (i = 0, j = 0; j < MAX_STAT_DEVICES; i++, j++) {
922 while (!stat_devices[j].name[0] && j < MAX_STAT_DEVICES)
923 j++;
925 if (j < MAX_STAT_DEVICES && i != j)
926 stat_devices[i] = stat_devices[j];
928 i--;
930 for (j = 0; j < MAX_STAT_DEVICES; j++) {
931 if (foundbuffer[j][0]) {
933 strcpy(stat_devices[i].name, foundbuffer[j]);
935 for (k = 0; k < 48; k++) {
936 stat_devices[i].his[k][0] = 0;
937 stat_devices[i].his[k][1] = 0;
940 i++;
943 if (LockMode && active_interface != NULL) {
944 k = 0;
945 for (j = 0; j < i; j++)
946 if (!strcmp(stat_devices[j].name, active_interface)) {
947 k = 1;
948 break;
950 if (!k) {
951 strcpy(stat_devices[i].name, active_interface);
952 for (k = 0; k < 48; k++) {
953 stat_devices[i].his[k][0] = 0;
954 stat_devices[i].his[k][1] = 0;
956 devsfound++;
960 return devsfound;
963 /*******************************************************************************\
964 |* DrawStats *|
965 \*******************************************************************************/
967 void DrawStats(int *his, int num, int size, int x_left, int y_bottom)
970 int pixels_per_byte;
971 int j, k;
972 int *p;
973 int p2, p3;
975 pixels_per_byte = size;
976 p = his;
977 for (j = 0; j < num; j++) {
978 if (p[0] + p[1] > pixels_per_byte)
979 pixels_per_byte = p[0] + p[1];
980 p += 2;
983 pixels_per_byte /= size;
984 p = his;
986 for (k = 0; k < num; k++) {
987 int p0, p1;
989 p0 = p[0];
990 p1 = p[1];
993 if (WaveForm) {
994 p2 = 0;
995 p3 = 1;
996 for (j = 0; j < size; j++) {
997 if (j < p0 / pixels_per_byte)
998 copyXPMArea(100+2, 68, 1, 1, k+x_left, y_bottom-size/2+p2/2);
999 else if (j < (p0 + p1) / pixels_per_byte)
1000 copyXPMArea(100+1, 68, 1, 1, k+x_left, y_bottom-size/2+p2/2);
1001 else
1002 copyXPMArea(100+0, 68, 1, 1, k+x_left, y_bottom-size/2+p2/2);
1004 p2 = (p2 + p3);
1005 p3 *= -1;
1006 p2 *= -1;
1008 copyXPMArea(100+3, 68, 1, 1, k+x_left, y_bottom-size/2);
1009 } else {
1010 for (j = 0; j < size; j++) {
1011 if (j < p0 / pixels_per_byte)
1012 copyXPMArea(100+2, 68, 1, 1, k+x_left, y_bottom-j);
1013 else if (j < (p0 + p1) / pixels_per_byte)
1014 copyXPMArea(100+1, 68, 1, 1, k+x_left, y_bottom-j);
1015 else
1016 copyXPMArea(100+0, 68, 1, 1, k+x_left, y_bottom-j);
1019 p += 2;
1023 /*******************************************************************************\
1024 |* usage *|
1025 \*******************************************************************************/
1027 void usage(void)
1030 fprintf(stderr, "\nwmifs - programming: tijno, (de)bugging & design: warpstah, webhosting: bobby\n\n");
1031 fprintf(stderr, "usage:\n");
1032 fprintf(stderr, "\t-c <color>\t\tset color\n");
1033 fprintf(stderr, "\t-display <display name>\tset display\n");
1034 fprintf(stderr, "\t-geometry +x+y\t\tset window position\n");
1035 fprintf(stderr, "\t-h\t\t\tthis help screen\n");
1036 fprintf(stderr, "\t-i <interface name>\tdefault (as it appears in /proc/net/route)\n");
1037 fprintf(stderr, "\t-I <interval>\t\tsampling interval, in seconds (default: 0.05)\n");
1038 fprintf(stderr, "\t-l\t\t\tstarts in lock mode\n");
1039 fprintf(stderr, "\t-s <interval>\t\tscrolling interval, in seconds (default: 5)\n");
1040 fprintf(stderr, "\t-v\t\t\tprint the version number\n");
1041 fprintf(stderr, "\t-w\t\t\twaveform load\n");
1042 fprintf(stderr, "\n");
1045 /*******************************************************************************\
1046 |* printversion *|
1047 \*******************************************************************************/
1049 void printversion(void)
1052 fprintf(stderr, "%s\n", WMIFS_VERSION);
1055 /*******************************************************************************\
1056 |* get_ppp_stats *|
1057 \*******************************************************************************/
1059 void get_ppp_stats(struct ppp_stats *cur)
1062 struct ifpppstatsreq req;
1064 memset(&req, 0, sizeof(req));
1066 req.stats_ptr = (void *) &req.stats;
1068 sprintf(req.ifr__name, "ppp%d", PPP_UNIT);
1070 if (ioctl(ppp_h, SIOCGPPPSTATS, &req) < 0) {
1071 /* fprintf(stderr, "heyho!\n") */;
1073 *cur = req.stats;
1076 #define LED_SZE_X (4)
1077 #define LED_SZE_Y (4)
1079 #define LED_ON_NET_X (87)
1080 #define LED_ON_NET_Y (66)
1081 #define LED_OFF_NET_X (93)
1082 #define LED_OFF_NET_Y (66)
1083 #define LED_ERR_NET_X (81)
1084 #define LED_ERR_NET_Y (66)
1085 #define LED_ON_SW_NET_X (49)
1086 #define LED_ON_SW_NET_Y (85)
1087 #define LED_OFF_SW_NET_X (44)
1088 #define LED_OFF_SW_NET_Y (85)
1090 #define LED_PWR_X (53)
1091 #define LED_PWR_Y (7)
1092 #define LED_SND_X (47)
1093 #define LED_SND_Y (7)
1094 #define LED_RCV_X (41)
1095 #define LED_RCV_Y (7)
1097 #define LED_SW_X (38)
1098 #define LED_SW_Y (14)
1100 /*******************************************************************************\
1101 |* SetOnLED *|
1102 \*******************************************************************************/
1103 void SetOnLED(int led)
1106 switch (led) {
1108 case LED_NET_RX:
1109 copyXPMArea(LED_ON_NET_X, LED_ON_NET_Y, LED_SZE_X, LED_SZE_Y, LED_RCV_X, LED_RCV_Y);
1110 break;
1111 case LED_NET_TX:
1112 copyXPMArea(LED_ON_NET_X, LED_ON_NET_Y, LED_SZE_X, LED_SZE_Y, LED_SND_X, LED_SND_Y);
1113 break;
1114 case LED_NET_POWER:
1115 copyXPMArea(LED_ON_NET_X, LED_ON_NET_Y, LED_SZE_X, LED_SZE_Y, LED_PWR_X, LED_PWR_Y);
1116 break;
1120 /*******************************************************************************\
1121 |* SetOffLED *|
1122 \*******************************************************************************/
1123 void SetOffLED(int led)
1126 switch (led) {
1128 case LED_NET_RX:
1129 copyXPMArea(LED_OFF_NET_X, LED_OFF_NET_Y, LED_SZE_X, LED_SZE_Y, LED_RCV_X, LED_RCV_Y);
1130 break;
1131 case LED_NET_TX:
1132 copyXPMArea(LED_OFF_NET_X, LED_OFF_NET_Y, LED_SZE_X, LED_SZE_Y, LED_SND_X, LED_SND_Y);
1133 break;
1134 case LED_NET_POWER:
1135 copyXPMArea(LED_OFF_NET_X, LED_OFF_NET_Y, LED_SZE_X, LED_SZE_Y, LED_PWR_X, LED_PWR_Y);
1136 break;
1140 /*******************************************************************************\
1141 |* SetErrLED *|
1142 \*******************************************************************************/
1143 void SetErrLED(int led)
1146 switch (led) {
1147 case LED_NET_POWER:
1148 copyXPMArea(LED_ERR_NET_X, LED_ERR_NET_Y, LED_SZE_X, LED_SZE_Y, LED_PWR_X, LED_PWR_Y);
1149 break;