File: fireload_file.c

package info (click to toggle)
wmfire 0.0.3.8-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 308 kB
  • ctags: 90
  • sloc: ansic: 752; sh: 354; makefile: 50
file content (108 lines) | stat: -rw-r--r-- 1,999 bytes parent folder | download
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
/* fireload_file .. accesory to wmfire - Zinx Verituse */
/* e-mail @ zinx@linuxfreak.com */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

float getheight(char *scanstring, char *filename) {
	char buffer[1024], *num;
	FILE *f;
	int ch;
	float numf;

	if (!(f = fopen(filename, "r"))) return 100;

	for (; *scanstring && !feof(f); scanstring++) {
		switch (*scanstring) {
			case '%':
				if (scanstring[1] == 's') {
					while (!feof(f) && isspace(ch = fgetc(f)));
					while (!feof(f) && !isspace(ch = fgetc(f)));
					scanstring++;
				}
				break;
			default:
				while (!feof(f) && (ch = fgetc(f)) != *scanstring);
				break;
		}
	}

	if (feof(f)) {
		fclose(f);
		return 100;
	}
	/* Premature EOF */

	fgets(buffer, 1024, f);

	num = buffer + strspn(buffer, " \t\n");
	num[strcspn(num, " \t\n")] = 0;
	numf = atof(num);

	fclose(f);
	
	return numf;
}

void do_help(char *prgname) {
	printf("%s -F <file> -S <string> -m <minimum> -x <maximum>\n", prgname);
}

char *showfmt(float num) {
	static char show[64];
	char *ext[] = { "", "K", "M", "G" };
	int extn;

	for (extn = 0; (num >= 1024) && extn < 3; extn++, num /= 1024) ; 
	snprintf(show, 64, "%.1f%s", num, ext[extn]);	

	return show;
}

int main(int argc, char *argv[]) {
	int ch;

	float min = 0, max = 0, num;
	char *filename = NULL, *scanstr = NULL;

	while ((ch = getopt(argc, argv, "F:S:m:x:h")) != EOF) {
		switch (ch) {
			case 'F':
				filename = optarg;
				break;
			case 'S':
				scanstr = optarg;
				break;
			case 'm':
				min = atof(optarg);
				break;
			case 'x':
				max = atof(optarg);
				break;
			default:
				do_help(argv[0]);
				exit(0);
				break;
		}
	}

	if (!filename || !scanstr || !(max - min)) {
		do_help(argv[0]);		
		exit(-1);
	}

	printf("fireload\t%f\t%f\n", min, max); fflush(stdout);

	for (;;) {
		num = getheight(scanstr, filename);
		printf("%f\t%s\n", num, showfmt(num)); fflush(stdout);
		usleep(100000);
	}

	exit(0);
}