]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
Added a utility function to acquire a list of events.
authorTim Gardner <tim.gardner@canonical.com>
Thu, 23 Jul 2009 15:34:27 +0000 (09:34 -0600)
committerTim Gardner <tim.gardner@canonical.com>
Thu, 23 Jul 2009 15:34:27 +0000 (09:34 -0600)
Construct a list of events for use by multiple functions,
such as block/unblock for specific rfkill types.

Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
rfkill.c

index 4d4b6b97f7edbd4bf73c5b6e54c761cd6f729e78..3a1206b7fd9165a9feb5a7b5b37fce5458c8f94c 100644 (file)
--- a/rfkill.c
+++ b/rfkill.c
@@ -110,22 +110,25 @@ static const char *type2string(enum rfkill_type type)
        return NULL;
 }
 
-static void rfkill_list(void)
+static struct rfkill_event *rfkill_get_event_list(int *num_events)
 {
        struct rfkill_event event;
-       const char *name;
+       struct rfkill_event *events = NULL;
        ssize_t len;
        int fd;
 
+       *num_events = 0;
+
        fd = open("/dev/rfkill", O_RDONLY);
        if (fd < 0) {
                perror("Can't open RFKILL control device");
-               return;
+               return NULL;
        }
 
        if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
                perror("Can't set RFKILL control device to non-blocking");
                close(fd);
+               return NULL;
        }
 
        while (1) {
@@ -145,15 +148,42 @@ static void rfkill_list(void)
                if (event.op != RFKILL_OP_ADD)
                        continue;
 
-               name = get_name(event.idx);
+               events = realloc(events,(*num_events+1)*sizeof(struct rfkill_event));
+               if (!events) {
+                       perror("Cannot realloc events");
+                       break;
+               }
 
-               printf("%u: %s: %s\n", event.idx, name,
-                                               type2string(event.type));
-               printf("\tSoft blocked: %s\n", event.soft ? "yes" : "no");
-               printf("\tHard blocked: %s\n", event.hard ? "yes" : "no");
+               events[*num_events] = event;
+               *num_events += 1;
        }
 
        close(fd);
+       return events;
+}
+
+static void rfkill_list(void)
+{
+       int num_events;
+       struct rfkill_event *events;
+       const char *name;
+       int i;
+
+       events = rfkill_get_event_list(&num_events);
+       if (!events)
+               return;
+
+       for (i = 0; i < num_events; i++) {
+
+               name = get_name(events[i].idx);
+
+               printf("%u: %s: %s\n", events[i].idx, name,
+                                               type2string(events[i].type));
+               printf("\tSoft blocked: %s\n", events[i].soft ? "yes" : "no");
+               printf("\tHard blocked: %s\n", events[i].hard ? "yes" : "no");
+       }
+
+       free(events);
 }
 
 static void rfkill_block(__u32 idx, __u8 block)