here comes the big header include rewrite
[monky] / src / smapi.c
index 7b97f13..e0ca9e1 100644 (file)
@@ -1,7 +1,7 @@
 /* smapi.c:  conky support for IBM Thinkpad smapi
  *
  * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
- * 
+ *
  * This library is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
  * USA.
  *
- * $Id$
- *
  */
-#include "conky.h"
+#define _GNU_SOURCE
+#include "conky.h"     /* text_buffer_size, PACKAGE_NAME, maybe more */
+#include "smapi.h"
+#include "logging.h"
 #include <stdio.h>
-#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
 
@@ -42,18 +45,18 @@ int smapi_bat_installed(int idx)
 
 }
 
-char *smapi_read_str(char *path)
+char *smapi_read_str(const char *path)
 {
        FILE *fp;
-       char *str = NULL;
+       char str[256] = "failed";
        if ((fp = fopen(path, "r")) != NULL) {
-               fscanf(fp, "%as\n", &str);
+               fscanf(fp, "%255s\n", str);
                fclose(fp);
        }
-       return str;
+       return strndup(str, text_buffer_size);
 }
 
-int smapi_read_int(char *path)
+int smapi_read_int(const char *path)
 {
        FILE *fp;
        int i = 0;
@@ -64,7 +67,7 @@ int smapi_read_int(char *path)
        return i;
 }
 
-char *smapi_get_str(char *fname)
+char *smapi_get_str(const char *fname)
 {
        char path[128];
        if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
@@ -73,7 +76,7 @@ char *smapi_get_str(char *fname)
        return smapi_read_str(path);
 }
 
-char *smapi_get_bat_str(int idx, char *fname)
+char *smapi_get_bat_str(int idx, const char *fname)
 {
        char path[128];
        if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
@@ -81,7 +84,7 @@ char *smapi_get_bat_str(int idx, char *fname)
        return smapi_read_str(path);
 }
 
-int smapi_get_bat_int(int idx, char *fname)
+int smapi_get_bat_int(int idx, const char *fname)
 {
        char path[128];
        if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
@@ -89,7 +92,7 @@ int smapi_get_bat_int(int idx, char *fname)
        return smapi_read_int(path);
 }
 
-char *smapi_get_bat_val(char *args)
+char *smapi_get_bat_val(const char *args)
 {
        char fname[128];
        int idx, cnt;
@@ -106,7 +109,7 @@ char *smapi_get_bat_val(char *args)
        return smapi_get_bat_str(idx, fname);
 }
 
-char *smapi_get_val(char *args)
+char *smapi_get_val(const char *args)
 {
        char str[128];