summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1a02ec9)
raw | patch | inline | side by side (parent: 1a02ec9)
author | Florian Forster <octo@collectd.org> | |
Sun, 17 Mar 2013 14:50:16 +0000 (15:50 +0100) | ||
committer | Florian Forster <octo@collectd.org> | |
Sun, 17 Mar 2013 14:57:14 +0000 (15:57 +0100) |
The entire business of dividing by sizeof(char) only made the code
unreadable, because by specification sizeof(char) evaluates to 1. The
defines were expanded and the parsing is now less sensitive to
formatting changes in the dmidecode output. Module global variables have
been moved to the top.
Fixes Github issue #267 in the process.
unreadable, because by specification sizeof(char) evaluates to 1. The
defines were expanded and the parsing is now less sensitive to
formatting changes in the dmidecode output. Module global variables have
been moved to the top.
Fixes Github issue #267 in the process.
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history | |
src/uuid.c | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index 82a4f017dc9c04a734e47160c10f84b632670b30..d617832ca48d6a29680ea7482df8b67e13255b5f 100644 (file)
--- a/src/common.c
+++ b/src/common.c
return (0);
} /* int strunescape */
+size_t strstripnewline (char *buffer)
+{
+ size_t buffer_len = strlen (buffer);
+
+ while (buffer_len > 0)
+ {
+ if ((buffer[buffer_len - 1] != '\n')
+ && (buffer[buffer_len - 1] != '\r'))
+ break;
+ buffer[buffer_len] = 0;
+ buffer_len--;
+ }
+
+ return (buffer_len);
+} /* size_t strstripnewline */
+
int escape_slashes (char *buf, int buf_len)
{
int i;
diff --git a/src/common.h b/src/common.h
index 2c74436318fecee1f9189bbfb6e658e28531d3f1..ae8e311f9983e8ff63441c0ab2936d287220ae67 100644 (file)
--- a/src/common.h
+++ b/src/common.h
*/
int strunescape (char *buf, size_t buf_len);
+/**
+ * Removed trailing newline characters (CR and LF) from buffer, which must be
+ * null terminated. Returns the length of the resulting string.
+ */
+__attribute__((nonnull (1)))
+size_t strstripnewline (char *buffer);
+
/*
* NAME
* timeval_cmp
diff --git a/src/uuid.c b/src/uuid.c
index cf23f5b37eea4e6505f62cb8ac37c65def408a07..2df6063984c04b09ebc6365a38144f33c61692f8 100644 (file)
--- a/src/uuid.c
+++ b/src/uuid.c
#define UUID_PRINTABLE_COMPACT_LENGTH (UUID_RAW_LENGTH * 2)
#define UUID_PRINTABLE_NORMAL_LENGTH (UUID_PRINTABLE_COMPACT_LENGTH + 4)
-#define HANDLE_PREFIX "Handle"
-#define SYSINFO_PREFIX "System Information"
-#define ALT_SYSINFO_PREFIX "\tSystem Information"
-#define UUID_PREFIX "\tUUID:"
-#define ALT_UUID_PREFIX "\t\tUUID:"
+static char *uuidfile = NULL;
+
+static const char *config_keys[] = {
+ "UUIDFile"
+};
static int
looks_like_a_uuid (const char *uuid)
uuid_parse_dmidecode(FILE *file)
{
char line[1024];
- int inSysInfo = 0;
- for (;;) {
- if (!fgets(line, sizeof(line)/sizeof(char), file)) {
- return NULL;
- }
- if (strncmp(line, HANDLE_PREFIX,
- (sizeof(HANDLE_PREFIX)/sizeof(char))-1) == 0) {
- /*printf("Got handle %s\n", line);*/
- inSysInfo = 0;
- } else if (strncmp(line, SYSINFO_PREFIX,
- (sizeof(SYSINFO_PREFIX)/sizeof(char))-1) == 0) {
- /*printf("Got system info %s\n", line);*/
- inSysInfo = 1;
- } else if (strncmp(line, ALT_SYSINFO_PREFIX,
- (sizeof(ALT_SYSINFO_PREFIX)/sizeof(char))-1) == 0) {
- /*printf("Got alt system info %s\n", line);*/
- inSysInfo = 1;
- }
-
- if (inSysInfo) {
- if (strncmp(line, UUID_PREFIX,
- (sizeof(UUID_PREFIX)/sizeof(char))-1) == 0) {
- char *uuid = line + (sizeof(UUID_PREFIX)/sizeof(char));
- /*printf("Got uuid [%s]\n", uuid);*/
- if (looks_like_a_uuid (uuid))
- return strdup (uuid);
- }
- if (strncmp(line, ALT_UUID_PREFIX,
- (sizeof(ALT_UUID_PREFIX)/sizeof(char))-1) == 0) {
- char *uuid = line + (sizeof(ALT_UUID_PREFIX)/sizeof(char));
- /*printf("Got alt uuid [%s]\n", uuid);*/
- if (looks_like_a_uuid (uuid))
- return strdup (uuid);
- }
- }
+ while (fgets (line, sizeof (line), file) != NULL)
+ {
+ char *fields[4];
+ int fields_num;
+
+ strstripnewline (line);
+
+ /* Look for a line reading:
+ * UUID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
+ */
+ fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields));
+ if (fields_num != 2)
+ continue;
+
+ if (strcmp("UUID:", fields[0]) != 0)
+ continue;
+
+ if (!looks_like_a_uuid (fields[1]))
+ continue;
+
+ return strdup (fields[1]);
}
return NULL;
}
uuid_get_from_file(const char *path)
{
FILE *file;
- char uuid[UUID_PRINTABLE_NORMAL_LENGTH+1];
+ char uuid[UUID_PRINTABLE_NORMAL_LENGTH + 1] = "";
- if (!(file = fopen(path, "r"))) {
+ file = fopen (path, "r");
+ if (file == NULL)
return NULL;
- }
if (!fgets(uuid, sizeof(uuid), file)) {
fclose(file);
return NULL;
}
fclose(file);
+ strstripnewline (uuid);
return strdup (uuid);
}
-static char *uuidfile = NULL;
-
static char *
uuid_get_local(void)
{
return NULL;
}
-static const char *config_keys[] = {
- "UUIDFile",
- NULL
-};
-#define NR_CONFIG_KEYS ((sizeof config_keys / sizeof config_keys[0]) - 1)
-
static int
uuid_config (const char *key, const char *value)
{
if (strcasecmp (key, "UUIDFile") == 0) {
- if (uuidfile) {
- ERROR ("UUIDFile given twice in configuration file");
- return 1;
- }
- uuidfile = strdup (value);
- return 0;
+ char *tmp = strdup (value);
+ if (tmp == NULL)
+ return -1;
+ sfree (uuidfile);
+ uuidfile = tmp;
+ } else {
+ return 1;
}
+
return 0;
}
void module_register (void)
{
- plugin_register_config ("uuid", uuid_config,
- config_keys, NR_CONFIG_KEYS);
- plugin_register_init ("uuid", uuid_init);
+ plugin_register_config ("uuid", uuid_config,
+ config_keys, STATIC_ARRAY_SIZE (config_keys));
+ plugin_register_init ("uuid", uuid_init);
}
/*