]>
gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
13 /* The most space we expect to need when reading in
14 a file using these hdaps functions. To exceed 32
15 bytes worth of content in unload_heads you'd have
16 to throw your laptop hard as a motherfucker. */
17 #define MAX_FILE_CONTENTS_SIZE 32
19 /* Where the block device names are located. */
20 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
23 static int hdaps_device_exists(const char* device
) {
24 /* Determine whether or not a device (file) exists and has an
25 unload_heads entry in sysfs. */
26 char path
[FILENAME_MAX
];
27 snprintf(path
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, device
);
28 return (access(path
, F_OK
) == 0);
32 int get_hdaps_device_list(char list
[MAX_HDAPS_DEVICES
][FILENAME_MAX
]) {
37 dp
= opendir(SYSFS_BLOCK_DEVICE_DIR
);
40 while ((ep
= readdir(dp
)) && list_idx
< MAX_HDAPS_DEVICES
) {
41 /* This next test covers "." and ".." too. */
42 if (hdaps_device_exists(ep
->d_name
)) {
43 strncpy(list
[list_idx
], ep
->d_name
, FILENAME_MAX
);
48 (void)closedir(dp
); /* Explicitly ignore this. */
55 int slurp_file(const char* filename
, char* buf
, int max_bytes
) {
56 /* This function just reads the contents of filename
57 * into buf. It is slightly stolen from the hdapsd project.
60 /* Return an error value by default. */
61 int ret
= HDAPS_ERROR
;
62 int fd
= open(filename
, O_RDONLY
);
64 if (filename
== NULL
|| buf
== NULL
) {
69 fprintf(stderr
, "open(%s): %s\n", filename
, strerror(errno
));
73 ret
= read(fd
, buf
, max_bytes
-1);
76 fprintf(stderr
, "read(%s): %s\n", filename
, strerror(errno
));
79 buf
[ret
] = 0; /* Null-terminate buf. */
83 fprintf(stderr
, "close(%s): %s\n", filename
, strerror(errno
));
92 int parse_int_from_file(const char* filename
) {
93 /* Read an integer from a file. We expect the file
94 to contain an integer (although in string form). */
96 char buf
[MAX_FILE_CONTENTS_SIZE
];
97 int ret
= slurp_file(filename
, buf
, sizeof(buf
));
100 /* Why did we read fewer than 0 bytes? */
104 /* If we read more than 0 bytes, hopefully we can
105 count on atoi to succeed. */