aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <dbphillipsnz@gmail.com>2015-09-01 17:23:34 +1200
committerDavid Phillips <dbphillipsnz@gmail.com>2015-09-01 17:23:34 +1200
commit956a6261ebfdf8fb3346b8935d0ec59c1c26df7c (patch)
tree55bedcb77c2066352e107f564062d8fff61008b4
parent266cb3f209e752856b832af008cf7aba40be3cba (diff)
downloadparamano-956a6261ebfdf8fb3346b8935d0ec59c1c26df7c.tar.xz
Cleaning up common functions
-rw-r--r--bat_tray.c20
-rw-r--r--common.c43
-rw-r--r--common.h11
-rw-r--r--getgov.c4
4 files changed, 34 insertions, 44 deletions
diff --git a/bat_tray.c b/bat_tray.c
index e70d47d..36b08ae 100644
--- a/bat_tray.c
+++ b/bat_tray.c
@@ -261,19 +261,30 @@ void bat_tray_hide()
**********************************************************************/
int get_battery_state()
{
- if (file_has_line(CHARGE_STATE_PATH, "Discharging"))
+ char state[1024];
+ FILE* fd;
+
+ if (!(fd = fopen(CHARGE_STATE_PATH, "r")) ||
+ !fgets(state, sizeof(state), fd))
+ return STATE_UNKNOWN;
+
+ fclose(fd);
+
+ chomp(state);
+
+ if (strcmp(state, "Discharging") == 0)
{
debug("Battery discharging\n");
return STATE_DISCHARGING;
}
- if (file_has_line(CHARGE_STATE_PATH, "Full"))
+ if (strcmp(state, "Full") == 0)
{
debug("Battery full\n");
return STATE_CHARGED;
}
- if (file_has_line(CHARGE_STATE_PATH, "Charging"))
+ if (strcmp(state, "Charging") == 0)
{
debug("Battery charging\n");
return STATE_CHARGING;
@@ -295,7 +306,7 @@ int get_bat_num()
for(i = 0; i < 10; i++)
{
asprintf(&file, "/sys/class/power_supply/BAT%d/present", i);
- if( (fd = check_for_file(file)) )
+ if( (fd = fopen(file, "r")) )
{
debug("Found battery %d\n",i);
if (fgetc(fd) == '1')
@@ -305,6 +316,7 @@ int get_bat_num()
free(file);
return i;
}
+ fclose(fd);
}
}
debug("Fallthrough: couldn't find battery\n");
diff --git a/common.c b/common.c
index 46a3105..ecff03c 100644
--- a/common.c
+++ b/common.c
@@ -21,6 +21,9 @@
#include <stdarg.h>
+/***********************************************************************
+ * Return integer value from first line in file (formatted filename)
+ **********************************************************************/
int get_int_value_from_filef(const char* format, ...)
{
int value = 0;
@@ -32,6 +35,10 @@ int get_int_value_from_filef(const char* format, ...)
return value;
}
+
+/***********************************************************************
+ * va_list wrapper function for get_int_value_from_filef()
+ **********************************************************************/
int vget_int_value_from_filef(const char* format, va_list args)
{
@@ -62,44 +69,16 @@ int get_int_value_from_file(const char* filename)
}
-/***********************************************************************
- * Return true/false if a file has specified line of text
- **********************************************************************/
-bool file_has_line(const char *filename, const char *line)
-{
- FILE* fd;
- char buffer[4096];
-
- if (!(fd = fopen(filename, "r")))
- return false;
-
- while (fgets(buffer, sizeof(buffer), fd) != NULL)
- {
- if(strstr(buffer, line) != NULL)
- {
- fclose(fd);
- return true;
- }
- }
- fclose(fd);
- return false;
-}
-
/***********************************************************************
- * Calls fopen on the specified file, giving debug error message on
- * failure. Returns the file descriptor in all cases.
+ * Truncates a string at the first '\r' or '\n'
**********************************************************************/
-FILE* check_for_file(char* path)
+void chomp(char *string)
{
- FILE* fd;
- if(!(fd = fopen(path, "r")))
- {
- debug("Couldn't open '%s'\n",path);
- }
- return fd;
+ string[strcspn(string, "\r\n")] = '\0';
}
+
/***********************************************************************
* Fetches first number (ie the 35 of "35 123") from a
* string
diff --git a/common.h b/common.h
index 056eddd..10deb14 100644
--- a/common.h
+++ b/common.h
@@ -21,12 +21,11 @@
#include <stdarg.h>
-int get_int_value_from_filef(const char* format, ...);
-int vget_int_value_from_filef(const char* format, va_list args);
-int get_int_value_from_file(const char* filename);
-int get_int(const char* string);
-bool file_has_line(const char *filename, const char *line);
-FILE *check_for_file(char* path);
+int get_int_value_from_filef(const char* format, ...);
+int vget_int_value_from_filef(const char* format, va_list args);
+int get_int_value_from_file(const char* filename);
+void chomp(char *string);
+int get_int(const char* string);
// <ew> Stringification of line number
#define STRING2(x) #x
diff --git a/getgov.c b/getgov.c
index cff7329..df648a4 100644
--- a/getgov.c
+++ b/getgov.c
@@ -60,7 +60,7 @@ bool gg_current(int core, char* out, int size)
char *path;
asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core);
- if (!(fd = check_for_file(path)))
+ if (!(fd = fopen(path, "r")))
{
free(path);
return false;
@@ -89,7 +89,7 @@ bool gg_available(int core, char* out, int size)
FILE *fd;
asprintf(&path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors", core);
- if (!(fd = check_for_file(path)))
+ if (!(fd = fopen(path, "r")))
{
free(path);
return false;