+/*
+ * This function just concats two strings in place, it should only be used
+ * for concatting order to the name of a file or concatting color codes.
+ * Otherwise, the buffer size would have to be increased.
+ *
+ */
+static char *concat(const char *str1, const char *str2) {
+ static char concatbuf[32];
+ (void)snprintf(concatbuf, sizeof(concatbuf), "%s%s", str1, str2);
+ return concatbuf;
+}
+
+/*
+ * Cleans wmii's /rbar directory by deleting all regular files
+ *
+ */
+static void cleanup_rbar_dir() {
+ struct dirent *ent;
+ DIR *dir;
+ char pathbuf[strlen(wmii_path)+256+1];
+
+ if ((dir = opendir(wmii_path)) == NULL)
+ exit(EXIT_FAILURE);
+
+ 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;
+
+ (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
+ if ((fd = open(pathbuf, O_CREAT | O_WRONLY | O_TRUNC, 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);
+}
+
+/*
+ * 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)
+ exit(EXIT_FAILURE);
+ 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);