iio: Simplify _write_sysfs_string()

Use g_autofree to avoid a goto on error.
This commit is contained in:
Bastien Nocera
2022-03-25 12:45:39 +01:00
parent 62bb35960e
commit 7ba893a6c9

View File

@@ -400,38 +400,32 @@ _write_sysfs_string (const char *filename,
const char *val, const char *val,
int verify) int verify)
{ {
int ret = 0;
g_autoptr(FILE) sysfsfp = NULL; g_autoptr(FILE) sysfsfp = NULL;
char *temp; g_autofree char *temp = NULL;
temp = g_build_filename (basedir, filename, NULL); temp = g_build_filename (basedir, filename, NULL);
sysfsfp = fopen (temp, "w"); sysfsfp = fopen (temp, "w");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; return -errno;
goto error_free;
} }
fprintf(sysfsfp, "%s", val); fprintf(sysfsfp, "%s", val);
g_clear_pointer (&sysfsfp, fclose); g_clear_pointer (&sysfsfp, fclose);
/* Verify? */ /* Verify? */
if (!verify) if (!verify)
goto error_free; return 0;
sysfsfp = fopen(temp, "r"); sysfsfp = fopen(temp, "r");
if (sysfsfp == NULL) { if (sysfsfp == NULL) {
ret = -errno; return -errno;
goto error_free;
} }
if (fscanf(sysfsfp, "%s", temp) != 1 || if (fscanf(sysfsfp, "%s", temp) != 1 ||
strcmp(temp, val) != 0) { strcmp(temp, val) != 0) {
g_warning ("Possible failure in string write of %s Should be %s written to %s\\%s\n", g_warning ("Possible failure in string write of %s Should be %s written to %s\\%s\n",
temp, val, basedir, filename); temp, val, basedir, filename);
ret = -1; return -1;
} }
error_free: return 0;
g_free(temp);
return ret;
} }
/** /**