+ while ((ent = readdir(dir)) != NULL) {
+ if (ent->d_type == DT_REG) {
+ (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, ent->d_name);
+ if (unlink(pathbuf) == -1)
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ (void)closedir(dir);
+}
+
+/*
+ * Creates the specified file in wmii's /rbar directory with
+ * correct modes and initializes colors if colormode is enabled
+ *
+ */
+static void create_file(const char *name) {
+ char pathbuf[strlen(wmii_path)+256+1];
+ int fd;
+ int flags = O_CREAT | O_WRONLY;
+ struct stat statbuf;
+
+ (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
+
+ /* Overwrite file's contents if it exists */
+ if (stat(pathbuf, &statbuf) >= 0)
+ flags |= O_TRUNC;
+
+ if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
+ exit(EXIT_FAILURE);
+ if (use_colors) {
+ char *tmp = concat("#888888 ", wmii_normcolors);
+ if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
+ exit(EXIT_FAILURE);
+ }
+ (void)close(fd);
+}
+
+/*
+ * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
+ * cleans up all files and creates the needed files
+ *
+ */
+static void setup(void) {
+ unsigned int i;
+ struct stat statbuf;
+ char pathbuf[512];
+
+ /* Wait until wmii_path/rbar exists */
+ for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
+
+ cleanup_rbar_dir();
+ if (wlan_interface)
+ create_file(concat(order[ORDER_WLAN],"wlan"));
+ if (eth_interface)
+ create_file(concat(order[ORDER_ETH],"eth"));
+ if (battery_path)
+ create_file(concat(order[ORDER_BATTERY],"battery"));
+ create_file(concat(order[ORDER_LOAD],"load"));
+ if (time_format)
+ create_file(concat(order[ORDER_TIME],"time"));
+ for (i = 0; i < num_run_watches; i += 2) {
+ snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
+ create_file(pathbuf);
+ }
+}
+
+/*
+ * Writes the given message in the corresponding file in wmii's /rbar directory
+ *
+ */
+static void write_to_statusbar(const char *name, const char *message) {
+ char pathbuf[strlen(wmii_path)+256+1];
+ int fd;
+
+ (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
+ if ((fd = open(pathbuf, O_RDWR)) == -1) {
+ /* Try to re-setup stuff and just continue */
+ setup();
+ return;
+ }
+ if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
+ exit(EXIT_FAILURE);
+ (void)close(fd);
+}
+
+/*
+ * Writes an errormessage to statusbar
+ *
+ */
+static void write_error_to_statusbar(const char *message) {
+ cleanup_rbar_dir();
+ create_file("error");
+ write_to_statusbar("error", message);
+}
+
+/*
+ * Write errormessage to statusbar and exit
+ *
+ */
+void die(const char *fmt, ...) {
+ if (wmii_path != NULL) {
+ char buffer[512];
+ va_list ap;
+ va_start(ap, fmt);
+ (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
+ va_end(ap);
+
+ write_error_to_statusbar(buffer);
+ }
+ exit(EXIT_FAILURE);
+}
+
+/*
+ * Skip the given character for exactly 'amount' times, returns
+ * a pointer to the first non-'character' character in 'input'.
+ *
+ */