aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorTuukka Tikkanen <tuukka.tikkanen@linaro.org>2014-12-03 12:26:17 +0200
committerTuukka Tikkanen <tuukka.tikkanen@linaro.org>2014-12-05 09:52:39 +0200
commit27f2342b422a3284a07cc541d780192dfab483a1 (patch)
tree00fa28915060b30b6b47aea8463b34f9bbf0f801 /utils.c
parent4fea09fd20ec00a57bca4e686b30c17f506c2312 (diff)
idlestat: Make ptrerror return a non-NULL pointer
Some functions such as inter() and intersection() use NULL return value to indicate both errors (e.g. out of memory) and a result (empty intersection). Make ptrerror() return a unique non-NULL pointer value so that the error and NULL result can be told apart. Previously the parameter to ptrerror() was unconditionally interpreted as a pointer to string to be passed to perror(). Now the function checks the value and calls perror only if the parameter value is not NULL. A new function, is_err(), has been added to check if any pointer value is equal to ptrerror() return value. error(), ptrerror() and is_err() have been moved to utils.c. The functions that used ptrerror() before this patch have been modified to consistently return ptrerror() instead of NULL in cases of errors (affects the return statements where no call to perror() is desired). Likewise the callers now use is_err() instead of checking for NULL pointer. Signed-off-by: Tuukka Tikkanen <tuukka.tikkanen@linaro.org>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 73350b7..64220ba 100644
--- a/utils.c
+++ b/utils.c
@@ -37,6 +37,26 @@
#include "utils.h"
+static void * const ERROR_VALUE_PTR = (void * const)&ERROR_VALUE_PTR;
+
+int error(const char *str)
+{
+ perror(str);
+ return -1;
+}
+
+void *ptrerror(const char *str)
+{
+ if (str != NULL)
+ perror(str);
+ return ERROR_VALUE_PTR;
+}
+
+int is_err(const void *ptr)
+{
+ return ptr == ERROR_VALUE_PTR;
+}
+
int write_int(const char *path, int val)
{
FILE *f;