Skip to content

Commit f1bdb61

Browse files
committed
Merge branch 'andrewgaul-reopen-fan'
2 parents b39ed3a + b2cd984 commit f1bdb61

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/daemon.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ static void cleanup_and_exit(int exit_code)
8282
struct s_fans *next_fan;
8383
while (fans != NULL) {
8484
next_fan = fans->next;
85+
if (fans->file != NULL) {
86+
fclose(fans->file);
87+
}
8588
free(fans->fan_output_path);
8689
free(fans->fan_manual_path);
8790
free(fans);
@@ -91,6 +94,9 @@ static void cleanup_and_exit(int exit_code)
9194
struct s_sensors *next_sensor;
9295
while (sensors != NULL) {
9396
next_sensor = sensors->next;
97+
if (sensors->file != NULL) {
98+
fclose(sensors->file);
99+
}
94100
free(sensors->path);
95101
free(sensors);
96102
sensors = next_sensor;

src/global.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ extern const char* PROGRAM_NAME;
88
extern const char* PROGRAM_PID;
99

1010
struct s_sensors {
11+
FILE* file;
1112
char* path;
1213
unsigned int temperature;
1314
struct s_sensors *next;
1415
};
1516

1617
struct s_fans {
17-
char* path;
18+
FILE* file;
19+
char* path; // TODO: unused
1820
char* fan_output_path;
1921
char* fan_manual_path;
2022
struct s_fans *next;

src/mbpfan.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ t_sensors *retrieve_sensors()
206206
tmp->next->next = NULL;
207207
}
208208

209-
fclose(file);
209+
s->file = file;
210210
sensors_found++;
211211
}
212212

@@ -273,7 +273,7 @@ t_fans *retrieve_fans()
273273
strncat( path_manual, path_man_end, strlen(path_begin) );
274274

275275

276-
FILE *file = fopen(path_output, "r");
276+
FILE *file = fopen(path_output, "w");
277277

278278
if(file != NULL) {
279279
fan = (t_fans *) malloc( sizeof( t_fans ) );
@@ -297,7 +297,7 @@ t_fans *retrieve_fans()
297297
tmp->next->next = NULL;
298298
}
299299

300-
fclose(file);
300+
fan->file = file;
301301
fans_found++;
302302
}
303303

@@ -362,11 +362,11 @@ t_sensors *refresh_sensors(t_sensors *sensors)
362362
t_sensors *tmp = sensors;
363363

364364
while(tmp != NULL) {
365-
FILE *file = fopen(tmp->path, "r");
366-
367-
if(file != NULL) {
368-
fscanf(file, "%d", &tmp->temperature);
369-
fclose(file);
365+
if(tmp->file != NULL) {
366+
char buf[16];
367+
int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0);
368+
buf[len] = '\0';
369+
sscanf(buf, "%d", &tmp->temperature);
370370
}
371371

372372
tmp = tmp->next;
@@ -380,14 +380,12 @@ t_sensors *refresh_sensors(t_sensors *sensors)
380380
void set_fan_speed(t_fans* fans, int speed)
381381
{
382382
t_fans *tmp = fans;
383-
FILE *file;
384383

385384
while(tmp != NULL) {
386-
file = fopen(tmp->fan_output_path, "rw+");
387-
388-
if(file != NULL) {
389-
fprintf(file, "%d", speed);
390-
fclose(file);
385+
if(tmp->file != NULL) {
386+
char buf[16];
387+
int len = snprintf(buf, sizeof(buf), "%d", speed);
388+
pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0);
391389
}
392390

393391
tmp = tmp->next;

0 commit comments

Comments
 (0)