diff options
author | Thomas Haller <thaller@redhat.com> | 2022-02-28 19:20:37 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-03-02 22:52:08 +0100 |
commit | eb5ac5b5e7d7e35b93f8152c55bf76a1dc705df4 (patch) | |
tree | 65d1662b5604b75c0567f05158d60e0a6f3948f0 | |
parent | 114bb60b048d472b9cde91a15d79c26280c02ba7 (diff) |
cli: make APInfo parameter to fill_output_access_point() constth/nmcli-wifi-in-use
It's helpful to control when data/state gets mutated. In particular,
when passing on a pointer via several hops. C can help with that
at compile time via "const".
But the "index" field of APInfo is actually mutable, as it counts
the lines. So most of the data is immutable, but the index.
Make APInfo const. But to do that, the mutable part must be moved to a
separate place.
Also, start with the counter initialized to zero instead of one.
It is just nicer.
-rw-r--r-- | src/nmcli/devices.c | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/src/nmcli/devices.c b/src/nmcli/devices.c index f5519fe3fb..ded2a9eb8c 100644 --- a/src/nmcli/devices.c +++ b/src/nmcli/devices.c @@ -1250,18 +1250,16 @@ sort_access_points(const GPtrArray *aps) typedef struct { NmCli *nmc; - int index; - guint32 output_flags; NMAccessPoint *active_ap; const char *device; GPtrArray *output_data; + int *p_index; + guint32 output_flags; } APInfo; static void -fill_output_access_point(gpointer data, gpointer user_data) +fill_output_access_point(NMAccessPoint *ap, const APInfo *info) { - NMAccessPoint *ap = NM_ACCESS_POINT(data); - APInfo *info = user_data; NmcOutputField *arr; gboolean active; NM80211ApFlags flags; @@ -1344,7 +1342,7 @@ fill_output_access_point(gpointer data, gpointer user_data) arr = nmc_dup_fields_array((const NMMetaAbstractInfo *const *) nmc_fields_dev_wifi_list, info->output_flags); - ap_name = g_strdup_printf("AP[%d]", info->index++); /* AP */ + ap_name = g_strdup_printf("AP[%d]", ++(*info->p_index)); /* AP */ set_val_str(arr, 0, ap_name); set_val_str(arr, 1, ssid_str); set_val_str(arr, 2, ssid_hex_str); @@ -1377,6 +1375,12 @@ fill_output_access_point(gpointer data, gpointer user_data) g_ptr_array_add(info->output_data, arr); } +static void +fill_output_access_point_void(gpointer data, gpointer user_data) +{ + fill_output_access_point(data, user_data); +} + static char * bluetooth_caps_to_string(NMBluetoothCapabilities caps) { @@ -1695,10 +1699,11 @@ show_device_info(NMDevice *device, NmCli *nmc) g_ptr_array_add(out.output_data, arr); { - gs_unref_ptrarray GPtrArray *aps = NULL; - APInfo info = { + gs_unref_ptrarray GPtrArray *aps = NULL; + int info_index = 0; + const APInfo info = { .nmc = nmc, - .index = 1, + .p_index = &info_index, .output_flags = NMC_OF_FLAG_SECTION_PREFIX, .active_ap = active_ap, .device = nm_device_get_iface(device), @@ -1707,7 +1712,7 @@ show_device_info(NMDevice *device, NmCli *nmc) aps = sort_access_points( nm_device_wifi_get_access_points(NM_DEVICE_WIFI(device))); - g_ptr_array_foreach(aps, fill_output_access_point, &info); + g_ptr_array_foreach(aps, fill_output_access_point_void, (gpointer) &info); } print_data_prepare_width(out.output_data); @@ -2994,10 +2999,11 @@ show_access_point_info(NMDeviceWifi *wifi, NmCli *nmc, NmcOutputData *out) g_ptr_array_add(out->output_data, arr); { - gs_unref_ptrarray GPtrArray *aps = NULL; - APInfo info = { + gs_unref_ptrarray GPtrArray *aps = NULL; + int info_index = 0; + const APInfo info = { .nmc = nmc, - .index = 1, + .p_index = &info_index, .output_flags = 0, .active_ap = active_ap, .device = nm_device_get_iface(NM_DEVICE(wifi)), @@ -3005,7 +3011,7 @@ show_access_point_info(NMDeviceWifi *wifi, NmCli *nmc, NmcOutputData *out) }; aps = sort_access_points(nm_device_wifi_get_access_points(wifi)); - g_ptr_array_foreach(aps, fill_output_access_point, &info); + g_ptr_array_foreach(aps, fill_output_access_point_void, (gpointer) &info); } print_data_prepare_width(out->output_data); @@ -3049,12 +3055,13 @@ wifi_print_aps(NMDeviceWifi *wifi, ap = candidate_ap; } if (ap) { - APInfo info = { - .nmc = nmc, - .index = 1, - .output_flags = 0, - .device = nm_device_get_iface(NM_DEVICE(wifi)), - .output_data = out.output_data, + int info_index = 0; + const APInfo info = { + .nmc = nmc, + .p_index = &info_index, + .output_flags = 0, + .device = nm_device_get_iface(NM_DEVICE(wifi)), + .output_data = out.output_data, }; /* Add headers (field names) */ |