chiark / gitweb /
localectl: use automatic cleanup
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 7 Jan 2013 16:43:41 +0000 (11:43 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 7 Jan 2013 16:43:41 +0000 (11:43 -0500)
set_freep() is added to automatize set_free().

src/locale/localectl.c
src/shared/macro.h
src/shared/set.c
src/shared/set.h

index b3acb3ec456f4e97e4aa08b226ca76a3ca3875f3..290edcc103bbdbc2866f8972ad982ebc8448d4e5 100644 (file)
@@ -384,15 +384,14 @@ static int add_locales_from_archive(Set *locales) {
 }
 
 static int add_locales_from_libdir (Set *locales) {
-        DIR *dir;
+        DIR _cleanup_closedir_ *dir;
         struct dirent *entry;
         int r;
 
         dir = opendir("/usr/lib/locale");
         if (!dir) {
                 log_error("Failed to open locale directory: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         errno = 0;
@@ -406,10 +405,8 @@ static int add_locales_from_libdir (Set *locales) {
                         continue;
 
                 z = strdup(entry->d_name);
-                if (!z) {
-                        r = log_oom();
-                        goto finish;
-                }
+                if (!z)
+                        return log_oom();
 
                 r = set_put(locales, z);
                 if (r < 0) {
@@ -417,7 +414,7 @@ static int add_locales_from_libdir (Set *locales) {
 
                         if (r != -EEXIST) {
                                 log_error("Failed to add locale: %s", strerror(-r));
-                                goto finish;
+                                return r;
                         }
                 }
 
@@ -426,19 +423,14 @@ static int add_locales_from_libdir (Set *locales) {
 
         if (errno != 0) {
                 log_error("Failed to read locale directory: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
-        r = 0;
-
- finish:
-        closedir(dir);
-        return r;
+        return 0;
 }
 
 static int list_locales(DBusConnection *bus, char **args, unsigned n) {
-        Set *locales;
+        _cleanup_set_free_ Set *locales;
         _cleanup_strv_free_ char **l = NULL;
         char **j;
         int r;
@@ -449,17 +441,15 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) {
 
         r = add_locales_from_archive(locales);
         if (r < 0 && r != -ENOENT)
-                goto finish;
+                return r;
 
         r = add_locales_from_libdir(locales);
         if (r < 0)
-                goto finish;
+                return r;
 
         l = set_get_strv(locales);
-        if (!l) {
-                r = log_oom();
-                goto finish;
-        }
+        if (!l)
+                return log_oom();
 
         strv_sort(l);
 
@@ -468,12 +458,7 @@ static int list_locales(DBusConnection *bus, char **args, unsigned n) {
         STRV_FOREACH(j, l)
                 puts(*j);
 
-        r = 0;
-
-finish:
-        set_free(locales);
-
-        return r;
+        return 0;
 }
 
 static int set_vconsole_keymap(DBusConnection *bus, char **args, unsigned n) {
index 700171b8324e45bc2b4c48e1b01b5c8e486ff4ef..29d91392f8abc3848a3f454307ea9560f36a3a73 100644 (file)
@@ -199,6 +199,7 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
 #define _cleanup_close_ __attribute__((cleanup(closep)))
 #define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
 #define _cleanup_umask_ __attribute__((cleanup(umaskp)))
+#define _cleanup_set_free_ __attribute__((cleanup(set_freep)))
 #define _cleanup_strv_free_ __attribute__((cleanup(strv_freep)))
 
 #define VA_FORMAT_ADVANCE(format, ap)                                   \
index 5f83c50839ae78b831d6f3853eea7ce6c6eb89a4..53399b655b43daafe615489cdbadac8e9dfe17e9 100644 (file)
@@ -37,6 +37,14 @@ void set_free(Set* s) {
         hashmap_free(MAKE_HASHMAP(s));
 }
 
+void set_freep(Set **s) {
+        if (!s)
+                return;
+
+        set_free(*s);
+        *s = NULL;
+}
+
 void set_free_free(Set *s) {
         hashmap_free_free(MAKE_HASHMAP(s));
 }
index 9162e2ae8025699bea4e57edd59ffe39a55f2559..ed5b5a44d6456e372281f1f7522e345c0e3fc0ad 100644 (file)
@@ -33,6 +33,7 @@ typedef struct Set Set;
 
 Set *set_new(hash_func_t hash_func, compare_func_t compare_func);
 void set_free(Set* s);
+void set_freep(Set **s);
 void set_free_free(Set *s);
 Set* set_copy(Set *s);
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);