]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/hdaps.c
Fixed build with --enable-debug=full thanks to Evgeni Golov.
[xfce4-hdaps.git] / panel-plugin / hdaps.c
1 /*
2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
3 *
4 * Copyright Michael Orlitzky
5 *
6 * http://michael.orlitzky.com/
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * http://www.fsf.org/licensing/licenses/gpl.html
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <dirent.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include "hdaps.h"
33
34 /* The most space we expect to need when reading in
35 a file using these hdaps functions. To exceed 32
36 bytes worth of content in unload_heads you'd have
37 to throw your laptop hard as a motherfucker. */
38 #define MAX_FILE_CONTENTS_SIZE 32
39
40 /* Where the block device names are located. */
41 #define SYSFS_BLOCK_DEVICE_DIR "/sys/block/"
42
43
44
45 static int hdaps_device_exists(const char* device) {
46 /* Determine whether or not a device (file) exists and has an
47 unload_heads entry in sysfs. */
48 char path[FILENAME_MAX];
49 snprintf(path, FILENAME_MAX, UNLOAD_HEADS_FMT, device);
50 return (access(path, F_OK) == 0);
51 }
52
53
54
55 int get_hdaps_device_list(char list[MAX_HDAPS_DEVICES][FILENAME_MAX]) {
56 /* Gets a list of devices which support HDAPS.
57 * We start by getting every device in the sysfs
58 * block device directory, and then eliminate those
59 * which don't have an unload_heads entry.
60 */
61 int list_idx = 0;
62 DIR *dp;
63 struct dirent *ep;
64
65 dp = opendir(SYSFS_BLOCK_DEVICE_DIR);
66
67 if (dp != NULL) {
68 while ((ep = readdir(dp)) && list_idx < MAX_HDAPS_DEVICES) {
69 /* This next test covers "." and ".." too. */
70 if (hdaps_device_exists(ep->d_name)) {
71 strncpy(list[list_idx], ep->d_name, FILENAME_MAX);
72 list_idx++;
73 }
74 }
75
76 (void)closedir(dp); /* Explicitly ignore this. */
77 }
78
79 return list_idx;
80 }
81
82
83
84 int slurp_file(const char* filename, char* buf, int max_bytes) {
85 /* This function just reads the contents of filename
86 * into buf, and returns the number of bytes read.
87 */
88 if (filename == NULL || buf == NULL) {
89 /* HDAPS_SUPER_BAD_ERROR */
90 return HDAPS_ERROR;
91 }
92
93
94 /* Return an error value by default. */
95 int bytes_read = HDAPS_ERROR;
96
97 int fd = open(filename, O_RDONLY);
98
99 if (fd < 0) {
100 /* open() didn't work. Report the error, and bail. */
101 fprintf(stderr, "open(%s): %s\n", filename, strerror(errno));
102 return HDAPS_ERROR;
103 }
104
105 bytes_read = read(fd, buf, max_bytes-1);
106
107 if (bytes_read < 0) {
108 /* Why did we read fewer than 0 bytes? */
109 fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
110 }
111 else {
112 /* Null-terminate buf if read() worked. */
113 buf[bytes_read] = 0;
114 }
115
116
117 if (close(fd)) {
118 /* Oh hey we should be able to close the file, too. */
119 fprintf(stderr, "close(%s): %s\n", filename, strerror(errno));
120 }
121
122 return bytes_read;
123 }
124
125
126
127
128 int parse_int_from_file(const char* filename) {
129 /* Read an integer from a file. We expect the file
130 to contain an integer (although in string form). */
131
132 char buf[MAX_FILE_CONTENTS_SIZE];
133 int bytes_read = slurp_file(filename, buf, sizeof(buf));
134
135 if (bytes_read <= 0) {
136 /* We can't parse what doesn't exist. Note that
137 * reading "0" from a file should result in reading
138 * at least 1 byte.
139 */
140 return HDAPS_ERROR;
141 }
142 else {
143 /* If we read more than 0 bytes, hopefully we can
144 * count on atoi to succeed. */
145 return atoi(buf);
146 }
147 }