network*: cosmetix
[k8lowj.git] / src / util.c
blobc9722ed4dbcecc30b25113fdead7b96b1ae1eb12
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include "glib-all.h"
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <time.h>
12 #include <stdlib.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <errno.h>
16 #include <string.h>
18 #include "util.h"
20 void
21 string_replace(char **dest, char *src) {
22 if (*dest) g_free(*dest);
23 *dest = src;
26 gboolean
27 verify_dir(const char *path, GError **err) {
28 /* mode 0700 so other people can't peek at passwords! */
29 if (mkdir(path, 0700) < 0 && errno != EEXIST) {
30 g_set_error(err, 0, 0, /* FIXME domain */
31 _("Failed to create directory '%s': %s"),
32 path, g_strerror(errno));
33 return FALSE;
35 return TRUE;
38 gboolean
39 verify_path(char *path, int include_last, GError **err) {
40 int i, len, reallen;
42 len = reallen = (int)strlen(path);
43 if (!include_last) {
44 for (i = len-1; i > 0; i--)
45 if (path[i] == G_DIR_SEPARATOR)
46 break;
47 if (i > 0) {
48 len = i;
49 path[len] = 0;
52 /* the common case is that the path already exists. */
53 if (!verify_dir(path, NULL)) {
54 /* otherwise, start creating parent directories until we succeed. */
55 for (i = len-1; i > 0; i--) {
56 if (path[i] == G_DIR_SEPARATOR) {
57 path[i] = 0;
58 if (verify_dir(path, NULL)) {
59 path[i] = G_DIR_SEPARATOR;
60 break;
62 path[i] = G_DIR_SEPARATOR;
65 /* once a parent dir succeeded, create the subdirectories we needed. */
66 i++;
67 for ( ; i < len; i++) {
68 if (path[i] == G_DIR_SEPARATOR) {
69 path[i] = 0;
70 if (!verify_dir(path, err))
71 return FALSE;
72 path[i] = G_DIR_SEPARATOR;
75 if (!verify_dir(path, err))
76 return FALSE;
78 if (!include_last)
79 path[len] = G_DIR_SEPARATOR;
80 return TRUE;
83 void
84 xml_escape(char **text) {
85 char *esc;
86 if (!text || !*text)
87 return;
88 esc = g_markup_escape_text(*text, -1);
89 g_free(*text);
90 *text = esc;