aboutsummaryrefslogtreecommitdiffstats
path: root/desktop.c
blob: d8f4f72c3c2af13ad0c71556aec27e28da782884 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * some code to handle desktop files
 * http://www.freedesktop.org/Standards/desktop-entry-spec
 *
 * This is really very simple and basic: next to no locale handling,
 * no caching, no other clever tricks ...
 * ida + fbi only use the Comment= entry of .directory files.
 *
 * (c) 2004 Gerd Hoffmann <kraxel@bytesex.org>
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <iconv.h>
#include <langinfo.h>

#include "list.h"
#include "desktop.h"

extern int debug;

/* ------------------------------------------------------------------------- */
/* desktop files are in utf-8                                                */

static int iconv_string(char *to, char *from,
			char *src, char *dst, size_t max)
{
    size_t ilen = strlen(src);
    size_t olen = max-1;
    iconv_t ic;

    ic = iconv_open(to,from);
    if (NULL == ic)
	return 0;

    while (ilen > 0) {
	if (-1 == iconv(ic,&src,&ilen,&dst,&olen)) {
	    /* skip + quote broken byte unless we are out of space */
	    if (E2BIG == errno)
		break;
	    if (olen < 4)
		break;
	    sprintf(dst,"\\x%02x",(int)(unsigned char)src[0]);
	    src  += 1;
	    dst  += 4;
	    ilen -= 1;
	    olen -= 4;
	}
    }
    dst[0] = 0;
    iconv_close(ic);
    return max-1 - olen;
}

int utf8_to_locale(char *src, char *dst, size_t max)
{
    char *codeset = nl_langinfo(CODESET);
    return iconv_string(codeset, "UTF-8", src, dst, max);
}

int locale_to_utf8(char *src, char *dst, size_t max)
{
    char *codeset = nl_langinfo(CODESET);
    return iconv_string("UTF-8", codeset, src, dst, max);
}

/* ------------------------------------------------------------------------- */
/* read/write desktop files                                                  */

struct desktop_line {
    struct list_head next;
    char line[1024];
};

static int read_file(char *filename, struct list_head *file)
{
    struct desktop_line *l;
    int len,count = 0;
    FILE *fp;

    INIT_LIST_HEAD(file);
    fp = fopen(filename,"r");
    if (NULL == fp) {
	if (debug)
	    fprintf(stderr,"open %s: %s\n",filename,strerror(errno));
	return 0;
    }
    for (;;) {
	l = malloc(sizeof(*l));
	memset(l,0,sizeof(*l));
	if (NULL == fgets(l->line,sizeof(l->line),fp)) {
	    free(l);
	    break;
	}
	len = strlen(l->line);
	if (l->line[len-1] == '\n')
	    l->line[len-1] = 0;
	list_add_tail(&l->next,file);
	count++;
    }
    fclose(fp);
    return count;
}

static int write_file(char *filename, struct list_head *file)
{
    struct desktop_line *l;
    struct list_head *item;
    FILE *fp;

    fp = fopen(filename,"w");
    if (NULL == fp) {
	fprintf(stderr,"open %s: %s\n",filename,strerror(errno));
	return 0;
    }
    list_for_each(item,file) {
	l = list_entry(item, struct desktop_line, next);
	fprintf(fp,"%s\n",l->line);
    }
    fclose(fp);
    return 0;
}

static int dump_file(struct list_head *file)
{
    struct desktop_line *l;
    struct list_head *item;

    fprintf(stderr,"\n");
    fprintf(stderr,"+--------------------\n");
    list_for_each(item,file) {
	l = list_entry(item, struct desktop_line, next);
	fprintf(stderr,"| %s\n",l->line);
    }
    return 0;
}

static int free_file(struct list_head *file)
{
    struct desktop_line *l;

    while (!list_empty(file)) {
	l = list_entry(file->next, struct desktop_line, next);
	list_del(&l->next);
	free(l);
    }
    return 0;
}

/* ------------------------------------------------------------------------- */

static char* get_entry(struct list_head *file, char *entry)
{
    struct desktop_line *l;
    struct list_head *item;
    int in_desktop_entry = 0;
    int len = strlen(entry);

    list_for_each(item,file) {
	l = list_entry(item, struct desktop_line, next);
	if (0 == strcmp(l->line,"[Desktop Entry]")) {
	    in_desktop_entry = 1;
	    continue;
	}
	if (0 == strncmp(l->line,"[",1)) {
	    in_desktop_entry = 0;
	    continue;
	}
	if (!in_desktop_entry)
	    continue;
	if (0 == strncmp(l->line,entry,len))
	    return l->line+len;
    }
    return NULL;
}

/* ------------------------------------------------------------------------- */

static int add_line(struct list_head *file, char *line)
{
    struct desktop_line *add;

    add = malloc(sizeof(*add));
    memset(add,0,sizeof(*add));
    snprintf(add->line,sizeof(add->line),"%s",line);
    list_add_tail(&add->next,file);
    return 0;
}

static int add_entry(struct list_head *file, char *entry, char *value)
{
    struct desktop_line *l,*add;
    struct list_head *item;

    list_for_each(item,file) {
	l = list_entry(item, struct desktop_line, next);
	if (0 != strcmp(l->line,"[Desktop Entry]"))
	    continue;
	add = malloc(sizeof(*add));
	memset(add,0,sizeof(*add));
	snprintf(add->line,sizeof(add->line),"%s%s",entry,value);
	list_add(&add->next,item);
	return 0;
    }
    return -1;
}

static int set_entry(struct list_head *file, char *type, char *entry, char *value)
{
    struct desktop_line *l;
    struct list_head *item;
    int in_desktop_entry = 0;
    int len = strlen(entry);

    list_for_each(item,file) {
	l = list_entry(item, struct desktop_line, next);
	if (0 == strcmp(l->line,"[Desktop Entry]")) {
	    in_desktop_entry = 1;
	    continue;
	}
	if (0 == strncmp(l->line,"[",1)) {
	    in_desktop_entry = 0;
	    continue;
	}
	if (!in_desktop_entry)
	    continue;
	if (0 == strncmp(l->line,entry,len)) {
	    snprintf(l->line,sizeof(l->line),"%s%s",entry,value);
	    return 0;
	}
    }
    if (0 != add_entry(file,entry,value)) {
	add_line(file,"[Desktop Entry]");
	add_entry(file,"Type=",type);
	add_entry(file,entry,value);
    }
    return 0;
}

/* ------------------------------------------------------------------------- */
/* public interface                                                          */

int desktop_read_entry(char *filename, char *entry, char *dest, size_t max)
{
    struct list_head file;
    char *value;
    int rc = 0;

    read_file(filename,&file);
    if (debug)
	dump_file(&file);
    value = get_entry(&file,entry);
    if (NULL != value) {
	rc = utf8_to_locale(value,dest,max);
	if (rc && debug)
	    fprintf(stderr,"# %s\n",dest);
    };
    free_file(&file);
    return rc;
}

int desktop_write_entry(char *filename, char *type, char *entry, char *value)
{
    struct list_head file;
    char utf8[1024];

    read_file(filename,&file);
    locale_to_utf8(value,utf8,sizeof(utf8));
    set_entry(&file,"Directory",entry,utf8);
    write_file(filename,&file);
    free_file(&file);
    return 0;
}