]>
gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
a2962e40ffaaff47ccea819dd15afe9c46799ae5
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/"
24 static int hdaps_device_exists(const char* device
) {
25 /* Determine whether or not a device (file) exists and has an
26 unload_heads entry in sysfs. */
27 char path
[FILENAME_MAX
];
28 snprintf(path
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, device
);
29 return (access(path
, F_OK
) == 0);
34 int get_hdaps_device_list(char list
[MAX_HDAPS_DEVICES
][FILENAME_MAX
]) {
35 /* Gets a list of devices which support HDAPS.
36 * We start by getting every device in the sysfs
37 * block device directory, and then eliminate those
38 * which don't have an unload_heads entry.
44 dp
= opendir(SYSFS_BLOCK_DEVICE_DIR
);
47 while ((ep
= readdir(dp
)) && list_idx
< MAX_HDAPS_DEVICES
) {
48 /* This next test covers "." and ".." too. */
49 if (hdaps_device_exists(ep
->d_name
)) {
50 strncpy(list
[list_idx
], ep
->d_name
, FILENAME_MAX
);
55 (void)closedir(dp
); /* Explicitly ignore this. */
63 int slurp_file(const char* filename
, char* buf
, int max_bytes
) {
64 /* This function just reads the contents of filename
65 * into buf, and returns the number of bytes read.
67 if (filename
== NULL
|| buf
== NULL
) {
68 /* HDAPS_SUPER_BAD_ERROR */
73 /* Return an error value by default. */
74 int bytes_read
= HDAPS_ERROR
;
76 int fd
= open(filename
, O_RDONLY
);
79 /* open() didn't work. Report the error, and bail. */
80 fprintf(stderr
, "open(%s): %s\n", filename
, strerror(errno
));
84 bytes_read
= read(fd
, buf
, max_bytes
-1);
87 /* Why did we read fewer than 0 bytes? */
88 fprintf(stderr
, "read(%s): %s\n", filename
, strerror(errno
));
91 /* Null-terminate buf if read() worked. */
97 /* Oh hey we should be able to close the file, too. */
98 fprintf(stderr
, "close(%s): %s\n", filename
, strerror(errno
));
107 int parse_int_from_file(const char* filename
) {
108 /* Read an integer from a file. We expect the file
109 to contain an integer (although in string form). */
111 char buf
[MAX_FILE_CONTENTS_SIZE
];
112 int bytes_read
= slurp_file(filename
, buf
, sizeof(buf
));
114 if (bytes_read
<= 0) {
115 /* We can't parse what doesn't exist. Note that
116 * reading "0" from a file should result in reading
122 /* If we read more than 0 bytes, hopefully we can
123 * count on atoi to succeed. */