]> git.sur5r.net Git - i3/i3/blobdiff - src/util.c
Implement 'swap' command.
[i3/i3] / src / util.c
index 06fbea2a7ef0856aa6a2ac0d71c07bce5ea85dd5..32c3c57e15d5bd2f206bab5a1984a00e19dff06a 100644 (file)
@@ -458,3 +458,20 @@ void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it) {
      * waitpid() here. */
     waitpid(*nagbar_pid, NULL, 0);
 }
+
+/*
+ * Converts a string into a long using strtol().
+ * This is a convenience wrapper checking the parsing result. It returns true
+ * if the number could be parsed.
+ */
+bool parse_long(const char *str, long *out, int base) {
+    char *end;
+    long result = strtol(str, &end, base);
+    if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end != '\0')) {
+        *out = result;
+        return false;
+    }
+
+    *out = result;
+    return true;
+}