aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/util.c
blob: 6a5806eb1b8118b68c3a503bf61212d99373193c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "util.h"

gchar* find_existing_file2(gchar *, const gchar *);

const XDG_Var XDG[] = {
    { "XDG_CONFIG_HOME", "~/.config" },
    { "XDG_DATA_HOME",   "~/.local/share" },
    { "XDG_CACHE_HOME",  "~/.cache" },
    { "XDG_CONFIG_DIRS", "/etc/xdg" },
    { "XDG_DATA_DIRS",   "/usr/local/share/:/usr/share/" },
};

/*@null@*/ gchar*
get_xdg_var (XDG_Var xdg) {
    const gchar *actual_value = getenv(xdg.environmental);
    const gchar *home         = getenv("HOME");

    if (!actual_value || !actual_value[0])
        actual_value = xdg.default_value;

    if (!actual_value)
        return NULL;

    return str_replace("~", home, actual_value);
}

/*@null@*/ gchar*
find_xdg_file (int xdg_type, const char* basename) {
    /* xdg_type = 0 => config
       xdg_type = 1 => data
       xdg_type = 2 => cache  */

    gchar *xdgv = get_xdg_var(XDG[xdg_type]);
    gchar *path = g_strconcat (xdgv, basename, NULL);
    g_free (xdgv);

    if (file_exists(path))
        return path;

    if (xdg_type == 2)
        return NULL;

    /* the file doesn't exist in the expected directory.
     * check if it exists in one of the system-wide directories. */
    char *system_dirs = get_xdg_var(XDG[3 + xdg_type]);
    path = find_existing_file2(system_dirs, basename);
    g_free(system_dirs);

    return path;
}

gboolean
file_exists (const char * filename) {
    return (access(filename, F_OK) == 0);
}

char *
str_replace (const char* search, const char* replace, const char* string) {
    gchar **buf;
    char *ret;

    if(!string)
        return NULL;

    buf = g_strsplit (string, search, -1);
    ret = g_strjoinv (replace, buf);
    g_strfreev(buf);

    return ret;
}

gboolean
for_each_line_in_file(const gchar *path, void (*callback)(const gchar *l, void *c), void *user_data) {
    gchar *line = NULL;
    gsize len;

    GIOChannel *chan = g_io_channel_new_file(path, "r", NULL);

    if (!chan)
        return FALSE;

    while (g_io_channel_read_line(chan, &line, &len, NULL, NULL) == G_IO_STATUS_NORMAL) {
        callback(line, user_data);
        g_free(line);
    }

    g_io_channel_unref (chan);

    return TRUE;
}

/* This function searches the directories in the : separated ($PATH style)
 * string 'dirs' for a file named 'basename'. It returns the path of the first
 * file found, or NULL if none could be found.
 * NOTE: this function modifies the 'dirs' argument. */
gchar*
find_existing_file2(gchar *dirs, const gchar *basename) {
    char *saveptr = NULL;

    /* iterate through the : separated elements until we find our file. */
    char *tok  = strtok_r(dirs, ":", &saveptr);
    char *path = g_strconcat (tok, "/", basename, NULL);

    while (!file_exists(path)) {
        g_free(path);

        tok = strtok_r(NULL, ":", &saveptr);
        if (!tok)
            return NULL; /* we've hit the end of the string */

        path = g_strconcat (tok, "/", basename, NULL);
    }

    return path;
}

/* search a PATH style string for an existing file+path combination.
 * everything after the last ':' is assumed to be the name of the file.
 * e.g. "/tmp:/home:a/file" will look for /tmp/a/file and /home/a/file.
 *
 * if there are no :s then the entire thing is taken to be the path. */
gchar*
find_existing_file(const gchar* path_list) {
    if(!path_list)
        return NULL;

    char *path_list_dup = g_strdup(path_list);

    char *basename = strrchr(path_list_dup, ':');
    if(!basename)
      return file_exists(path_list_dup) ? path_list_dup : NULL;

    basename[0] = '\0';
    basename++;

    char *result = find_existing_file2(path_list_dup, basename);
    g_free(path_list_dup);
    return result;
}

gchar*
argv_idx(const GArray *a, const guint idx) {
    return g_array_index(a, gchar*, idx);
}

GString *
append_escaped (GString *dest, const gchar *src) {
    g_assert(dest);
    g_assert(src);

    // Hint that we are going to append another string.
    int oldlen = dest->len;
    g_string_set_size (dest, dest->len + strlen(src) * 2);
    g_string_truncate (dest, oldlen);

    // Append src char by char with baddies escaped
    for (const gchar *p = src; *p; p++) {
        switch (*p) {
        case '\\':
            g_string_append (dest, "\\\\");
            break;
        case '\'':
            g_string_append (dest, "\\'");
            break;
        case '\n':
            g_string_append (dest, "\\n");
            break;
        default:
            g_string_append_c (dest, *p);
            break;
        }
    }
    return dest;
}