#include <assert.h>
#include <limits.h>
#include <time.h>
-
-#include <libsystem/libsystem.h>
+#include <stdlib.h>
+#include <string.h>
#include "log.h"
#define FILEBUF 1024
+static inline void __cleanup_free_func(void *p) {
+ free(*(void**) p);
+}
+
+static inline bool isempty(const char *p) {
+ return !p || !p[0];
+}
+
+#define _cleanup_(x) __attribute__((cleanup(x)))
+
+#define _cleanup_free_ _cleanup_(__cleanup_free_func)
+
+#define new(t, n) ((t*) malloc(sizeof(t) * (n)))
+
+#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
+ for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
+
+#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
+
static FILE *log_f[LOG_TYPE_MAX];
static int stdout_bak = -1;
static int stderr_bak = -1;
return log_level;
}
+static bool __quote_complete(char *str, size_t l, char q) {
+ char *s, *s2;
+
+ assert(str);
+
+ if (!l)
+ return true;
+
+ s = strchr(str, q);
+ if (!s || (s - str) > l)
+ return true;
+
+ s = strchr(s + 1, q);
+ if (!s || (s - str) > l)
+ return false;
+
+ s2 = strchr(s + 1, q);
+ if (!s2 || (s2 - str) > l)
+ return true;
+
+ return __quote_complete(s + 1, l - (s + 1 - str), q);
+}
+
+#define QUOTES "\"\'"
+static bool quote_complete(char *str, size_t l) {
+ char quotes[] = QUOTES;
+ int i;
+
+ assert(str);
+
+ if (!l)
+ return true;
+
+ for (i = 0; quotes[i]; i++) {
+ if (!__quote_complete(str, l, quotes[i]))
+ return false;
+ }
+
+ return true;
+}
+
+static char *split(const char *c, size_t *l, const char *separator, char **state) {
+ bool separator_include_quotes;
+ char *current;
+ size_t s;
+
+ assert(c);
+ assert(l);
+ assert(separator);
+ assert(state);
+
+ current = *state ? *state : (char *) c;
+ if (!*current || *c == 0)
+ return NULL;
+
+ *l = 0;
+ separator_include_quotes = !!strspn(separator, QUOTES);
+ current += strspn(current, separator);
+
+ while ((s = strcspn(current + *l, separator))) {
+ *l += s;
+ if (separator_include_quotes ||
+ quote_complete(current, *l))
+ break;
+ (*l)++;
+ }
+
+ *state = current + *l;
+
+ return (char *) current;
+}
+
static int stdpath_dup_and_backup(FILE *f) {
int r, fd;