static inline int sys_read_buf(char *file, char *buf, int len)
{
- int fd, r;
+ int fd, r;
- if (!file || !buf || len < 0)
- return -EINVAL;
+ if (!file || !buf || len < 0)
+ return -EINVAL;
- fd = open(file, O_RDONLY);
- if (fd == -1)
- return -ENOENT;
+ fd = open(file, O_RDONLY);
+ if (fd == -1)
+ return -ENOENT;
- r = read(fd, buf, len);
- close(fd);
- if ((r >= 0) && (r < len))
- buf[r] = '\0';
- else
- return -EIO;
+ r = read(fd, buf, len);
+ close(fd);
+ if ((r >= 0) && (r < len))
+ buf[r] = '\0';
+ else
+ return -EIO;
- return 0;
+ return 0;
}
static inline int sys_write_buf(char *file, char *buf)
{
- int fd, r;
+ int fd, r;
- if (!file || !buf)
- return -EINVAL;
+ if (!file || !buf)
+ return -EINVAL;
- fd = open(file, O_WRONLY);
- if (fd == -1)
- return -EPERM;
+ fd = open(file, O_WRONLY);
+ if (fd == -1)
+ return -EPERM;
- r = write(fd, buf, strlen(buf));
- close(fd);
- if (r < 0)
- return -EIO;
+ r = write(fd, buf, strlen(buf));
+ close(fd);
+ if (r < 0)
+ return -EIO;
- return 0;
+ return 0;
}
static inline int sys_get_int(char *fname, int *val)
{
- char buf[SHARED_H_BUF_MAX];
- int r;
+ char buf[SHARED_H_BUF_MAX];
+ int r;
- if (!fname || !val)
- return -EINVAL;
+ if (!fname || !val)
+ return -EINVAL;
- r = sys_read_buf(fname, buf, sizeof(buf));
- if (r < 0)
- return r;
+ r = sys_read_buf(fname, buf, sizeof(buf));
+ if (r < 0)
+ return r;
- *val = atoi(buf);
- return 0;
+ *val = atoi(buf);
+ return 0;
}
static inline int sys_get_str(char *fname, char *str, int len)
{
- int r;
+ int r;
- if (!fname || !str || len < 0)
- return -EINVAL;
+ if (!fname || !str || len < 0)
+ return -EINVAL;
- r = sys_read_buf(fname, str, len);
- if (r < 0)
- return r;
+ r = sys_read_buf(fname, str, len);
+ if (r < 0)
+ return r;
- return 0;
+ return 0;
}
static inline int sys_set_int(char *fname, int val)
{
- char buf[SHARED_H_BUF_MAX];
- int r;
+ char buf[SHARED_H_BUF_MAX];
+ int r;
- if (!fname)
- return -EINVAL;
+ if (!fname)
+ return -EINVAL;
- snprintf(buf, sizeof(buf), "%d", val);
- r = sys_write_buf(fname, buf);
- if (r < 0)
- return r;
+ snprintf(buf, sizeof(buf), "%d", val);
+ r = sys_write_buf(fname, buf);
+ if (r < 0)
+ return r;
- return 0;
+ return 0;
}
static inline int sys_set_str(char *fname, char *val)
{
- int r;
+ int r;
- if (!fname || !val)
- return -EINVAL;
+ if (!fname || !val)
+ return -EINVAL;
- r = sys_write_buf(fname, val);
- if (r < 0)
- return r;
+ r = sys_write_buf(fname, val);
+ if (r < 0)
+ return r;
- return 0;
+ return 0;
}
#endif /* __LIBCOMMON_FILE_H__ */
#include <glib.h>
#define SYS_G_LIST_PREPEND(a, b) \
- a = g_list_prepend(a, (gpointer)b)
+ a = g_list_prepend(a, (gpointer)b)
#define SYS_G_LIST_APPEND(a, b) \
- a = g_list_append(a, (gpointer)b)
+ a = g_list_append(a, (gpointer)b)
#define SYS_G_LIST_REMOVE(a, b) \
- a = g_list_remove(a, (gpointer)b)
+ a = g_list_remove(a, (gpointer)b)
#define SYS_G_LIST_REMOVE_LIST(a, b) \
- a = g_list_delete_link(a, b)
+ a = g_list_delete_link(a, b)
#define SYS_G_LIST_LENGTH(a) \
- g_list_length(a)
+ g_list_length(a)
#define SYS_G_LIST_NTH(a, b) \
- g_list_nth_data(a, b)
+ g_list_nth_data(a, b)
#define SYS_G_LIST_FIND(a, b) \
- g_list_find(a, (gpointer)b)
+ g_list_find(a, (gpointer)b)
#define SYS_G_LIST_FREE_LIST(a) \
- g_list_free(a)
+ g_list_free(a)
#define SYS_G_LIST_FOREACH(head, elem, node) \
- for (elem = head, node = NULL; \
- elem && ((node = elem->data) != NULL); \
- elem = elem->next, node = NULL)
+ for (elem = head, node = NULL; \
+ elem && ((node = elem->data) != NULL); \
+ elem = elem->next, node = NULL)
#define SYS_G_LIST_FOREACH_SAFE(head, elem, elem_next, node) \
- for (elem = head, elem_next = g_list_next(elem), node = NULL; \
- elem && ((node = elem->data) != NULL); \
- elem = elem_next, elem_next = g_list_next(elem), node = NULL)
+ for (elem = head, elem_next = g_list_next(elem), node = NULL; \
+ elem && ((node = elem->data) != NULL); \
+ elem = elem_next, elem_next = g_list_next(elem), node = NULL)
#define SYS_G_LIST_REVERSE_FOREACH(head, elem, node) \
- for (elem = g_list_last(head), node = NULL; \
- elem && ((node = elem->data) != NULL); \
- elem = g_list_previous(elem), node = NULL)
+ for (elem = g_list_last(head), node = NULL; \
+ elem && ((node = elem->data) != NULL); \
+ elem = g_list_previous(elem), node = NULL)
#define SYS_G_LIST_REVERSE_FOREACH_SAFE(head, elem, elem_next, node) \
- for (elem = g_list_last(head), elem_next = g_list_previous(elem), node = NULL; \
- elem && ((node = elem->data) != NULL); \
- elem = elem_next, elem_next = g_list_previous(elem), node = NULL)
+ for (elem = g_list_last(head), elem_next = g_list_previous(elem), node = NULL; \
+ elem && ((node = elem->data) != NULL); \
+ elem = elem_next, elem_next = g_list_previous(elem), node = NULL)
#define SYS_G_LIST_NEXT(a) \
- g_list_next(a)
+ g_list_next(a)
#endif