main: Use new g_auto* to close file descriptors

This commit is contained in:
Bastien Nocera
2021-01-21 15:44:43 +01:00
parent b60d9d3556
commit e3594b879c
6 changed files with 23 additions and 31 deletions

View File

@@ -85,8 +85,9 @@ prepare_output (SensorDevice *sensor_device,
{
DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data;
g_auto(IioFd) fp = -1;
int fp, buf_len = 127;
int buf_len = 127;
data.data = g_malloc0(drv_data->buffer_data->scan_size * buf_len);
@@ -106,8 +107,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device);
}
close(fp);
bail:
g_free(data.data);
}

View File

@@ -9,6 +9,7 @@
#include "drivers.h"
#include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h>
#include <unistd.h>
@@ -68,8 +69,9 @@ prepare_output (SensorDevice *sensor_device,
{
DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data;
g_auto(IioFd) fp = -1;
int fp, buf_len = 127;
int buf_len = 127;
data.data = g_malloc (drv_data->buffer_data->scan_size * buf_len);
@@ -88,8 +90,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device);
}
close(fp);
bail:
g_free(data.data);
}

View File

@@ -9,6 +9,7 @@
#include "drivers.h"
#include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h>
#include <unistd.h>
@@ -73,8 +74,9 @@ prepare_output (SensorDevice *sensor_device,
{
DrvData *drv_data = (DrvData *) sensor_device->priv;
IIOSensorData data;
g_auto(IioFd) fp = 1;
int fp, buf_len = 127;
int buf_len = 127;
data.data = g_malloc(drv_data->buffer_data->scan_size * buf_len);
@@ -93,8 +95,6 @@ prepare_output (SensorDevice *sensor_device,
process_scan (data, sensor_device);
}
close(fp);
bail:
g_free(data.data);
}

View File

@@ -8,6 +8,7 @@
#include "drivers.h"
#include "accel-mount-matrix.h"
#include "utils.h"
#include <fcntl.h>
#include <unistd.h>
@@ -121,7 +122,8 @@ accelerometer_changed (gpointer user_data)
DrvData *drv_data = (DrvData *) sensor_device->priv;
struct input_absinfo abs_info;
int accel_x = 0, accel_y = 0, accel_z = 0;
int fd, r;
g_auto(IioFd) fd = -1;
int r;
AccelReadings readings;
AccelVec3 tmp;
@@ -136,8 +138,6 @@ accelerometer_changed (gpointer user_data)
READ_AXIS(ABS_Y, accel_y);
READ_AXIS(ABS_Z, accel_z);
close (fd);
/* Scale from 1G ~= 256 to a value in m/s² */
set_accel_scale (&readings.scale, 1.0 / 256 * 9.81);

View File

@@ -18,6 +18,7 @@
#include <gudev/gudev.h>
#include "uinput.h"
#include "utils.h"
#define ONEG 256
@@ -57,7 +58,7 @@ write_sysfs_string (char *filename,
char *val)
{
int ret = 0;
FILE *sysfsfp;
g_autoptr(FILE) sysfsfp = NULL;
char *temp;
temp = g_build_filename (basedir, filename, NULL);
@@ -67,7 +68,6 @@ write_sysfs_string (char *filename,
goto error_free;
}
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);
error_free:
g_free(temp);

View File

@@ -2,7 +2,7 @@
* Modified from industrialio buffer test code, and Lenovo Yoga (2 Pro) orientation helper
* Copyright (c) 2008 Jonathan Cameron
* Copyright (c) 2014 Peter F. Patel-Schneider
* Copyright (c) 2014 Bastien Nocera <hadess@hadess.net>
* Copyright (c) 2014, 2021 Bastien Nocera <hadess@hadess.net>
* Copyright (c) 2015 Elad Alfassa <elad@fedoraproject.org>
*
* This program is free software; you can redistribute it and/or modify it
@@ -11,6 +11,7 @@
*/
#include "iio-buffer-utils.h"
#include "utils.h"
#include <fcntl.h>
#include <string.h>
@@ -98,7 +99,7 @@ iioutils_get_type (unsigned *is_signed,
char *filename;
char signchar, endianchar;
unsigned padint;
FILE *sysfsfp;
g_autoptr(FILE) sysfsfp = NULL;
builtname = g_strdup_printf ("%s_type", name);
filename = g_build_filename (device_dir, "scan_elements", builtname, NULL);
@@ -127,11 +128,9 @@ iioutils_get_type (unsigned *is_signed,
if (ret < 0 || ret != 5) {
g_warning ("Failed to pass scan type description for %s", filename);
fclose (sysfsfp);
g_free (filename);
return FALSE;
}
fclose (sysfsfp);
*be = (endianchar == 'b');
*bytes = padint / 8;
@@ -156,7 +155,7 @@ iioutils_get_param_float (float *output,
const char *name,
const char *generic_name)
{
FILE *sysfsfp;
g_autoptr(FILE) sysfsfp = NULL;
char *builtname, *filename;
int ret = 0;
@@ -169,7 +168,6 @@ iioutils_get_param_float (float *output,
sysfsfp = fopen (filename, "r");
if (sysfsfp) {
ret = fscanf (sysfsfp, "%f", output);
fclose (sysfsfp);
g_free (filename);
if (ret == 1)
return 0;
@@ -191,7 +189,6 @@ iioutils_get_param_float (float *output,
g_debug ("Failed to read float from %s", filename);
ret = -EINVAL;
}
fclose (sysfsfp);
} else {
ret = -errno;
if (ret != -ENOENT)
@@ -228,7 +225,6 @@ build_channel_array (const char *device_dir,
int *counter)
{
GDir *dp;
FILE *sysfsfp;
int ret;
const char *name;
char *scan_el_dir;
@@ -251,6 +247,7 @@ build_channel_array (const char *device_dir,
while ((name = g_dir_read_name (dp)) != NULL) {
if (g_str_has_suffix (name, "_en")) {
g_autoptr(FILE) sysfsfp = NULL;
char *filename, *index_name;
iio_channel_info *current;
@@ -262,13 +259,12 @@ build_channel_array (const char *device_dir,
continue;
}
if (fscanf (sysfsfp, "%d", &ret) != 1) {
fclose (sysfsfp);
g_debug ("Could not read from scan_elements file '%s'", filename);
g_free (filename);
continue;
}
fclose (sysfsfp);
g_free (filename);
g_clear_pointer (&sysfsfp, fclose);
current = g_new0 (iio_channel_info, 1);
@@ -288,7 +284,6 @@ build_channel_array (const char *device_dir,
if (sysfsfp == NULL)
goto error;
ret = fscanf (sysfsfp, "%u", &current->index);
fclose (sysfsfp);
g_free (filename);
if (ret != 1)
goto error;
@@ -361,7 +356,7 @@ _write_sysfs_int (const char *filename,
int val2)
{
int ret = 0;
FILE *sysfsfp;
g_autoptr(FILE) sysfsfp = NULL;
int test;
char *temp;
temp = g_build_filename (basedir, filename, NULL);
@@ -375,8 +370,8 @@ _write_sysfs_int (const char *filename,
fprintf(sysfsfp, "%d %d", val, val2);
else
fprintf(sysfsfp, "%d", val);
g_clear_pointer (&sysfsfp, fclose);
fclose(sysfsfp);
if (verify) {
sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) {
@@ -390,7 +385,6 @@ _write_sysfs_int (const char *filename,
val, temp);
ret = -1;
}
fclose(sysfsfp);
}
error_free:
g_free (temp);
@@ -412,7 +406,7 @@ _write_sysfs_string (const char *filename,
int verify)
{
int ret = 0;
FILE *sysfsfp;
g_autoptr(FILE) sysfsfp = NULL;
char *temp;
temp = g_build_filename (basedir, filename, NULL);
@@ -422,7 +416,7 @@ _write_sysfs_string (const char *filename,
goto error_free;
}
fprintf(sysfsfp, "%s", val);
fclose(sysfsfp);
g_clear_pointer (&sysfsfp, fclose);
/* Verify? */
if (!verify)
@@ -438,7 +432,6 @@ _write_sysfs_string (const char *filename,
temp, val, basedir, filename);
ret = -1;
}
fclose(sysfsfp);
error_free:
g_free(temp);