From bc904476bdb5200ff09fda0797895f2ef8749fce Mon Sep 17 00:00:00 2001
From: Dmitri Pal <dpal@redhat.com>
Date: Sun, 23 Dec 2012 11:59:48 -0500
Subject: [PATCH 2/4] [INI] Use ENOMEM instead of errno

Reviewed all uses or errno. Cleaned places that needed cleaning.
Did not remove <errno.h> yet where it is not needed.
---
 ini/ini_comment.c   |    2 +-
 ini/ini_config_ut.c |    4 +---
 ini/ini_configobj.c |    4 ----
 ini/ini_fileobj.c   |   31 ++++++++++---------------------
 ini/ini_parse.c     |   18 ++++++------------
 5 files changed, 18 insertions(+), 41 deletions(-)

diff --git a/ini/ini_comment.c b/ini/ini_comment.c
index acf8b6674fa2fbc5012a01d74a9a0fc13c50b471..6b56d0282e956777a2c4e1edfc21aa64f4536a32 100644
--- a/ini/ini_comment.c
+++ b/ini/ini_comment.c
@@ -736,7 +736,7 @@ int ini_comment_serialize (struct ini_comment *ic,
 
         error = ini_comment_get_line(ic, i, &commentline, &len);
         if (error) {
-            TRACE_ERROR_NUMBER("Failed to get line", errno);
+            TRACE_ERROR_NUMBER("Failed to get line", error);
             return error;
         }
 
diff --git a/ini/ini_config_ut.c b/ini/ini_config_ut.c
index 362a941e0089b52c2224a9499709b85aa74e0587..57ffb8d5003cbdc03da69e67fcc57f583e706782 100644
--- a/ini/ini_config_ut.c
+++ b/ini/ini_config_ut.c
@@ -1555,12 +1555,10 @@ int main(int argc, char *argv[])
     srcdir = getenv("srcdir");
     if(srcdir) {
 
-        errno = 0;
         rundir = malloc(strlen(srcdir) + sizeof(inidir));
         if (!rundir) {
-            error = errno;
             printf("Failed to allocate memory to store path"
-                   " to the test files %d.\n", error);
+                   " to the test files %d.\n", ENOMEM);
             return -1;
         }
 
diff --git a/ini/ini_configobj.c b/ini/ini_configobj.c
index 7de57e415467dace920f4578995ed9916c41e9ae..e42d4d42d52187273d1b36201d2a8968333118bc 100644
--- a/ini/ini_configobj.c
+++ b/ini/ini_configobj.c
@@ -119,10 +119,8 @@ int ini_config_create(struct ini_cfgobj **ini_config)
         return EINVAL;
     }
 
-    errno = 0;
     new_co = malloc(sizeof(struct ini_cfgobj));
     if (!new_co) {
-        error = errno;
         TRACE_ERROR_NUMBER("Failed to allocate memory", ENOMEM);
         return ENOMEM;
     }
@@ -263,10 +261,8 @@ int ini_config_copy(struct ini_cfgobj *ini_config,
     }
 
     /* Create a new configuration object */
-    errno = 0;
     new_co = malloc(sizeof(struct ini_cfgobj));
     if (!new_co) {
-        error = errno;
         TRACE_ERROR_NUMBER("Failed to allocate memory", ENOMEM);
         return ENOMEM;
     }
diff --git a/ini/ini_fileobj.c b/ini/ini_fileobj.c
index d26add41ad5b80b80ef0732553eb66020586e9d4..1410a9008f1613d9e302e4302c2ea2d2e7bb7dbf 100644
--- a/ini/ini_fileobj.c
+++ b/ini/ini_fileobj.c
@@ -170,12 +170,10 @@ int ini_config_file_open(const char *filename,
     }
 
     /* Allocate structure */
-    errno = 0;
     new_ctx = malloc(sizeof(struct ini_cfgfile));
     if (!new_ctx) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to allocate file ctx.", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to allocate file ctx.", ENOMEM);
+        return ENOMEM;
     }
 
     new_ctx->filename = NULL;
@@ -189,13 +187,11 @@ int ini_config_file_open(const char *filename,
     new_ctx->count = 0;
 
     /* Construct the full file path */
-    errno = 0;
     new_ctx->filename = malloc(PATH_MAX + 1);
     if (!(new_ctx->filename)) {
-        error = errno;
         ini_config_file_destroy(new_ctx);
-        TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", ENOMEM);
+        return ENOMEM;
     }
 
     /* Construct path */
@@ -236,12 +232,10 @@ int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in,
     }
 
     /* Allocate structure */
-    errno = 0;
     new_ctx = malloc(sizeof(struct ini_cfgfile));
     if (!new_ctx) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to allocate file ctx.", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to allocate file ctx.", ENOMEM);
+        return ENOMEM;
     }
 
     new_ctx->file = NULL;
@@ -320,13 +314,10 @@ int ini_config_get_errors(struct ini_cfgfile *file_ctx,
         return EINVAL;
     }
 
-
-    errno = 0;
     errlist = calloc(file_ctx->count + 1, sizeof(char *));
     if (!errlist) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to allocate memory for errors.", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to allocate memory for errors.", ENOMEM);
+        return ENOMEM;
     }
 
     /* Bind iterator */
@@ -365,14 +356,12 @@ int ini_config_get_errors(struct ini_cfgfile *file_ctx,
              * are pretty short and will fir into the predefined
              * error length buffer.
              */
-            errno = 0;
             line = malloc(MAX_ERROR_LINE + 1);
             if (!line) {
-                error = errno;
-                TRACE_ERROR_NUMBER("Failed to get memory for error.", error);
+                TRACE_ERROR_NUMBER("Failed to get memory for error.", ENOMEM);
                 col_unbind_iterator(iterator);
                 ini_config_free_errors(errlist);
-                return error;
+                return ENOMEM;
             }
 
             snprintf(line, MAX_ERROR_LINE, LINE_FORMAT,
diff --git a/ini/ini_parse.c b/ini/ini_parse.c
index 5d393759a4407ce62d632261b1957b6ad61e10e5..9e81f2d2ecb49b56ac5501ad781f6fa96dab19cd 100644
--- a/ini/ini_parse.c
+++ b/ini/ini_parse.c
@@ -1012,12 +1012,10 @@ static int handle_kvp(struct parser_obj *po, uint32_t *action)
     }
 
     /* Dup the key name */
-    errno = 0;
     po->key = malloc(len + 1);
     if (!(po->key)) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to dup key", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to dup key", ENOMEM);
+        return ENOMEM;
     }
 
     memcpy(po->key, po->last_read, len);
@@ -1040,12 +1038,10 @@ static int handle_kvp(struct parser_obj *po, uint32_t *action)
     TRACE_INFO_NUMBER("LENGTH:", len);
 
     /* Dup the part of the value */
-    errno = 0;
     dupval = malloc(len + 1);
     if (!dupval) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to dup value", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to dup value", ENOMEM);
+        return ENOMEM;
     }
 
     memcpy(dupval, eq, len);
@@ -1152,12 +1148,10 @@ static int handle_section(struct parser_obj *po, uint32_t *action)
     }
 
     /* Dup the name */
-    errno = 0;
     dupval = malloc(len + 1);
     if (!dupval) {
-        error = errno;
-        TRACE_ERROR_NUMBER("Failed to dup section name", error);
-        return error;
+        TRACE_ERROR_NUMBER("Failed to dup section name", ENOMEM);
+        return ENOMEM;
     }
 
     memcpy(dupval, start, len);
-- 
1.7.1

