cosmetix
[k8lowj.git] / src / util.c
blobbaec7013f0dae7c4f2606a96f938e980b4c517b6
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #include "glib-all.h"
6 #include <ctype.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
16 #include "util.h"
19 void string_replace (char **dest, char *src) {
20 if (*dest) g_free(*dest);
21 *dest = src;
25 gboolean verify_dir (const char *path, GError **err) {
26 /* mode 0700 so other people can't peek at passwords! */
27 if (mkdir(path, 0700) < 0 && errno != EEXIST) {
28 g_set_error(err, 0, 0, /* FIXME domain */ _("Failed to create directory '%s': %s"), path, g_strerror(errno));
29 return FALSE;
31 return TRUE;
35 static gboolean verify_path_ex (char *path, int include_last, GError **err) {
36 int i, len, reallen;
37 len = reallen = (int)strlen(path);
38 if (!include_last) {
39 for (i = len-1; i > 0; --i) if (path[i] == G_DIR_SEPARATOR) break;
40 if (i > 0) { len = i; path[len] = 0; }
42 /* the common case is that the path already exists */
43 if (!verify_dir(path, NULL)) {
44 /* otherwise, start creating parent directories until we succeed */
45 for (i = len-1; i > 0; --i) {
46 if (path[i] == G_DIR_SEPARATOR) {
47 path[i] = 0;
48 if (verify_dir(path, NULL)) { path[i] = G_DIR_SEPARATOR; break; }
49 path[i] = G_DIR_SEPARATOR;
52 /* once a parent dir succeeded, create the subdirectories we needed */
53 ++i;
54 for (; i < len; ++i) {
55 if (path[i] == G_DIR_SEPARATOR) {
56 path[i] = 0;
57 if (!verify_dir(path, err)) return FALSE;
58 path[i] = G_DIR_SEPARATOR;
61 if (!verify_dir(path, err)) return FALSE;
63 if (!include_last) path[len] = G_DIR_SEPARATOR;
64 return TRUE;
68 static void _xfree_ptr (void *p) {
69 void **pp = (void **)p;
70 if (*pp) free(*pp);
74 gboolean verify_path (const char *path, int include_last, GError **err) {
75 if (path != NULL) {
76 __attribute__((cleanup(_xfree_ptr))) char *p = strdup(path);
77 return verify_path_ex(p, include_last, err);
79 return FALSE;
83 void xml_escape (char **text) {
84 char *esc;
85 if (!text || !*text) return;
86 esc = g_markup_escape_text(*text, -1);
87 g_free(*text);
88 *text = esc;