1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
14 #include <sys/types.h>
19 void string_replace (char **dest
, char *src
) {
20 if (*dest
) g_free(*dest
);
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
));
35 static gboolean
verify_path_ex (char *path
, int include_last
, GError
**err
) {
37 len
= reallen
= (int)strlen(path
);
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
) {
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 */
54 for (; i
< len
; ++i
) {
55 if (path
[i
] == G_DIR_SEPARATOR
) {
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
;
68 static void _xfree_ptr (void *p
) {
69 void **pp
= (void **)p
;
74 gboolean
verify_path (const char *path
, int include_last
, GError
**err
) {
76 __attribute__((cleanup(_xfree_ptr
))) char *p
= strdup(path
);
77 return verify_path_ex(p
, include_last
, err
);
83 void xml_escape (char **text
) {
85 if (!text
|| !*text
) return;
86 esc
= g_markup_escape_text(*text
, -1);