Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qAdd fan_speed.c #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ clock
battery
essid
volume
fan_speed
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC ?= gcc
LIBS = -lm -lasound
LIBS = -lm -lasound -lsensors
CFLAGS += -std=c99 -pedantic -Wall -Wextra -I$(PREFIX)/include
CFLAGS += -D_POSIX_C_SOURCE=200112L
LDFLAGS += -L$(PREFIX)/lib
Expand Down
1 change: 1 addition & 0 deletions Sourcedeps
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exist: exist.c
narg: narg.c
uq: uq.c
volume: volume.c
fan_speed: fan_speed.c
111 changes: 111 additions & 0 deletions fan_speed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sensors/sensors.h>
#include <sensors/error.h>
#include "common.h"

#define INTERVAL 3
#define FORMAT "%s %4.0f "

int put_infos(char *format)
{
int chip_nr = 0;

const sensors_chip_name *chip;

while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {

int i = 0;
const sensors_feature *feature;

while ((feature = sensors_get_features(chip, &i))) {

const sensors_subfeature *sf;

if (feature->type == SENSORS_FEATURE_FAN) {
double val;
char *label = NULL;

label = sensors_get_label(chip, feature);

sf = sensors_get_subfeature(chip, feature,
SENSORS_SUBFEATURE_FAN_FAULT);
if (sf && sensors_get_value(chip, sf->number, &val)) {
val = -1;
}
else {
sf = sensors_get_subfeature(chip, feature,
SENSORS_SUBFEATURE_FAN_INPUT);
if (sensors_get_value(chip, sf->number, &val)) {
val = -1;
}
}

if (strstr(format, "%s") == NULL) {
printf(format, val);
}
else if (strpbrk(format, "%f") == NULL) {
printf(format, label);
}
else {
printf(format, label, val);
}
free(label);
}
}
}

printf("\n");
fflush(stdout);

return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
char *format = FORMAT;
int interval = INTERVAL;
bool snoop = false;
int exit_code;

char opt;

while ((opt = getopt(argc, argv, "hsf:i:")) != -1) {
switch (opt) {
case 'h':
printf("fan_speed [-h|-s|-f FORMAT|-i INTERVAL]\n");
exit(EXIT_SUCCESS);
break;
case 's':
snoop = true;
break;
case 'f':
format = optarg;
break;
case 'i':
interval = atoi(optarg);
break;
}
}

exit_code = sensors_init(NULL);
if (exit_code) {
fprintf(stderr, "sensors_init: %s\n", sensors_strerror(exit_code));
return exit_code;
}

if (snoop) {
while ((exit_code = put_infos(format)) != EXIT_FAILURE) {
sleep(interval);
}
}
else {
exit_code = put_infos(format);
}

sensors_cleanup();
return exit_code;
}