static __thread int last_error;
+static void set_last_error(int errnum)
+{
+ last_error = errnum;
+}
+
+static void last_error_from_errno(void)
+{
+ last_error = errno;
+}
+
static void * zalloc(size_t size)
{
void *ptr;
ptr = malloc(size);
- if (ptr)
- memset(ptr, 0, size);
+ if (!ptr) {
+ set_last_error(ENOMEM);
+ return NULL;
+ }
+
+ memset(ptr, 0, size);
return ptr;
}
status = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, req);
if (status < 0) {
- last_error = errno;
+ last_error_from_errno();
return -1;
}
int status;
if (!gpiod_line_is_requested(line)) {
- last_error = -EPERM;
+ set_last_error(EPERM);
return -1;
}
status = ioctl(line->lreq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
if (status < 0) {
- last_error = errno;
+ last_error_from_errno();
return -1;
}
int status;
if (!gpiod_line_is_requested(line)) {
- last_error = -EPERM;
+ set_last_error(EPERM);
return -1;
}
status = ioctl(line->lreq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
if (status < 0) {
- last_error = errno;
+ last_error_from_errno();
return -1;
}
fd = open(path, O_RDWR);
if (fd < 0) {
- last_error = errno;
+ last_error_from_errno();
return NULL;
}
chip = zalloc(sizeof(*chip));
if (!chip) {
close(fd);
- last_error = ENOMEM;
+ set_last_error(ENOMEM);
return NULL;
}
if (status < 0) {
close(chip->fd);
free(chip);
- last_error = errno;
+ last_error_from_errno();
return NULL;
}
if (!chip->lines) {
close(chip->fd);
free(chip);
- last_error = ENOMEM;
+ set_last_error(ENOMEM);
return NULL;
}
status = asprintf(&path, "%s%s", dev_dir, name);
if (status < 0) {
- last_error = errno;
+ last_error_from_errno();
return NULL;
}
status = asprintf(&path, "%s%s%u", dev_dir, cdev_prefix, num);
if (!status) {
- last_error = errno;
+ last_error_from_errno();
return NULL;
}
int status;
if (offset >= chip->cinfo.lines) {
- last_error = EINVAL;
+ set_last_error(EINVAL);
return NULL;
}
status = ioctl(chip->fd, GPIO_GET_LINEINFO_IOCTL, &line->linfo);
if (status < 0) {
- last_error = errno;
+ last_error_from_errno();
return NULL;
}
new = zalloc(sizeof(*new));
if (!new) {
- last_error = ENOMEM;
+ set_last_error(ENOMEM);
return NULL;
}
new->dir = opendir(dev_dir);
if (!new->dir) {
- last_error = errno;
+ last_error_from_errno();
return NULL;
}