blob: f2c616e57ffb1f815a4ce39ebf74165fafd74215 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/**
* sysfs_path_is_file: Check if the path supplied points to a file
* @path: path to validate
* Returns 0 if path points to file, 1 otherwise
* Copied from sysfsutils-2.1.0 (which is LGPL2.1 or later), relicensed GPLv2 for use here.
*/
int sysfs_path_is_file(const char * path)
{
struct stat astats;
if (!path) {
errno = EINVAL;
return 1;
}
if ((lstat(path, &astats)) != 0) {
return 1;
}
if (S_ISREG(astats.st_mode))
return 0;
return 1;
}
int sysfs_read_file(const char * path, char **output)
{
int ret;
char *result = NULL;
int fd;
unsigned long resultsize = 0;
ssize_t length = 0;
resultsize = getpagesize();
result = malloc(resultsize);
if (!result)
return -ENOMEM;
memset(result, 0, resultsize);
fd = open(path, O_RDONLY);
if (fd < 0) {
ret = fd;
goto free_out;
}
length = read(fd, result, resultsize-1);
if (length < 0) {
close(fd);
ret = -1;
goto free_out;
}
result[length] = '\0';
if (result[length-1] == '\n')
result[length-1] = '\0';
*output = result;
ret = 0;
goto out;
free_out:
free(result);
out:
return ret;
}
|