-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
95 lines (78 loc) · 1.87 KB
/
util.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
/*
*
* SwitchRes (C) 2010 Chris Kennedy <[email protected]>
*
* This program is Free Software under the GNU Public License
*
* SwitchRes is a modeline generator based off of Calamity's
* Methods of generating modelines
*
*/
#include "switchres.h"
#include <stdio.h>
#include <stdarg.h>
#include "math.h"
double Normalize(double a, double b) {
double c, e;
int d;
c = (double)a / (double)b;
d = a / b;
e = c - (double)d;
if (e > 0.0)
d++;
a = d * b;
return a;
}
int RunProgram(ConfigSettings *cs, char *temp) {
FILE *pi;
char c;
if (cs->verbose > 2)
sr_fprintf(stderr, "Running: %s\n", temp);
pi=popen(temp, "r");
if(pi != NULL) {
int i = 0;
c=fgetc(pi);
while(c != '\xff' && i < 64*1024) {
temp[i]=c;
c=fgetc(pi);
i++;
}
temp[i]='\x00';
pclose(pi);
} else
return -1;
if (cs->verbose > 4)
sr_fprintf(stderr, "Output from command is:\n '%s'\n", temp);
return 0;
}
int get_output(char *command, char *output)
{
FILE *pi;
pi = popen(command, "r");
if (pi != NULL)
{
char c;
int i = 0;
c = fgetc(pi);
while(c != '\n' && i < 255)
{
output[i++] = c;
c = fgetc(pi);
}
output[i] = '\0';
pclose(pi);
}
else
return -1;
return 0;
}
void sr_fprintf(FILE *fd, const char *fmt, ...) {
va_list vl;
va_start(vl, fmt);
if (logfd)
vfprintf(logfd, fmt, vl);
else
vfprintf(fd, fmt, vl);
va_end(vl);
return;
}