1
|
1 /*****************************************************************************/
|
|
2 /* config_file.c - config file routines */
|
122
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
1
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp.h"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
122
|
23 void
|
|
24 gftp_add_bookmark (gftp_bookmarks_var * newentry)
|
1
|
25 {
|
122
|
26 gftp_bookmarks_var * preventry, * folderentry, * endentry;
|
|
27 char *curpos;
|
1
|
28
|
122
|
29 if (!newentry->protocol)
|
124
|
30 newentry->protocol = g_strdup ("FTP");
|
1
|
31
|
122
|
32 /* We have to create the folders. For example, if we have
|
|
33 Debian Sites/Debian, we have to create a Debian Sites entry */
|
|
34 preventry = gftp_bookmarks;
|
|
35 if (preventry->children != NULL)
|
1
|
36 {
|
122
|
37 endentry = preventry->children;
|
|
38 while (endentry->next != NULL)
|
|
39 endentry = endentry->next;
|
|
40 }
|
|
41 else
|
|
42 endentry = NULL;
|
|
43 curpos = newentry->path;
|
|
44 while ((curpos = strchr (curpos, '/')) != NULL)
|
|
45 {
|
|
46 *curpos = '\0';
|
|
47 /* See if we already made this folder */
|
|
48 if ((folderentry = (gftp_bookmarks_var *)
|
|
49 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path)) == NULL)
|
1
|
50 {
|
122
|
51 /* Allocate the individual folder. We have to do this for the edit
|
|
52 bookmarks feature */
|
|
53 folderentry = g_malloc0 (sizeof (*folderentry));
|
124
|
54 folderentry->path = g_strdup (newentry->path);
|
122
|
55 folderentry->prev = preventry;
|
|
56 folderentry->isfolder = 1;
|
|
57 g_hash_table_insert (gftp_bookmarks_htable, folderentry->path,
|
|
58 folderentry);
|
|
59 if (preventry->children == NULL)
|
|
60 preventry->children = folderentry;
|
1
|
61 else
|
122
|
62 endentry->next = folderentry;
|
|
63 preventry = folderentry;
|
|
64 endentry = NULL;
|
1
|
65 }
|
|
66 else
|
|
67 {
|
122
|
68 preventry = folderentry;
|
|
69 if (preventry->children != NULL)
|
|
70 {
|
|
71 endentry = preventry->children;
|
|
72 while (endentry->next != NULL)
|
|
73 endentry = endentry->next;
|
|
74 }
|
|
75 else
|
|
76 endentry = NULL;
|
1
|
77 }
|
122
|
78 *curpos = '/';
|
|
79 curpos++;
|
1
|
80 }
|
|
81
|
122
|
82 /* Get the parent node */
|
|
83 if ((curpos = strrchr (newentry->path, '/')) == NULL)
|
|
84 preventry = gftp_bookmarks;
|
|
85 else
|
1
|
86 {
|
122
|
87 *curpos = '\0';
|
|
88 preventry = (gftp_bookmarks_var *)
|
|
89 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path);
|
|
90 *curpos = '/';
|
1
|
91 }
|
|
92
|
122
|
93 if (preventry->children != NULL)
|
1
|
94 {
|
122
|
95 endentry = preventry->children;
|
|
96 while (endentry->next != NULL)
|
|
97 endentry = endentry->next;
|
|
98 endentry->next = newentry;
|
1
|
99 }
|
122
|
100 else
|
|
101 preventry->children = newentry;
|
|
102 newentry->prev = preventry;
|
|
103 newentry->next = NULL;
|
|
104 g_hash_table_insert (gftp_bookmarks_htable, newentry->path, newentry);
|
1
|
105 }
|
|
106
|
|
107
|
320
|
108 static int
|
|
109 copyfile (char *source, char *dest)
|
|
110 {
|
|
111 int srcfd, destfd;
|
|
112 char buf[8192];
|
|
113 ssize_t n;
|
|
114
|
|
115 if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1)
|
|
116 {
|
|
117 printf (_("Error: Cannot open local file %s: %s\n"),
|
|
118 source, g_strerror (errno));
|
|
119 exit (1);
|
|
120 }
|
|
121
|
|
122 if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
|
|
123 {
|
|
124 printf (_("Error: Cannot open local file %s: %s\n"),
|
|
125 dest, g_strerror (errno));
|
|
126 close (srcfd);
|
|
127 exit (1);
|
|
128 }
|
|
129
|
|
130 while ((n = read (srcfd, buf, sizeof (buf))) > 0)
|
|
131 {
|
|
132 if (write (destfd, buf, n) == -1)
|
|
133 {
|
|
134 printf (_("Error: Could not write to socket: %s\n"),
|
|
135 g_strerror (errno));
|
|
136 exit (1);
|
|
137 }
|
|
138 }
|
|
139
|
|
140 if (n == -1)
|
|
141 {
|
|
142 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno));
|
|
143 exit (1);
|
|
144 }
|
|
145
|
|
146 if (close (srcfd) == -1)
|
|
147 {
|
|
148 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno));
|
|
149 exit (1);
|
|
150 }
|
|
151
|
|
152 if (close (destfd) == -1)
|
|
153 {
|
|
154 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno));
|
|
155 exit (1);
|
|
156 }
|
|
157
|
|
158 return (1);
|
|
159 }
|
|
160
|
|
161
|
122
|
162 static void
|
124
|
163 gftp_read_bookmarks (char *global_data_path)
|
1
|
164 {
|
|
165 char *tempstr, *temp1str, buf[255], *curpos;
|
199
|
166 gftp_config_vars * global_entry;
|
122
|
167 gftp_bookmarks_var * newentry;
|
1
|
168 FILE *bmfile;
|
|
169 size_t len;
|
|
170 int line;
|
|
171
|
|
172 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
173 {
|
|
174 printf (_("gFTP Error: Bad bookmarks file name %s\n"), BOOKMARKS_FILE);
|
87
|
175 exit (1);
|
1
|
176 }
|
|
177
|
|
178 if (access (tempstr, F_OK) == -1)
|
|
179 {
|
124
|
180 temp1str = g_strdup_printf ("%s/bookmarks", global_data_path);
|
1
|
181 if (access (temp1str, F_OK) == -1)
|
|
182 {
|
|
183 printf (_("Warning: Cannot find master bookmark file %s\n"),
|
|
184 temp1str);
|
|
185 g_free (temp1str);
|
|
186 return;
|
|
187 }
|
|
188 copyfile (temp1str, tempstr);
|
|
189 g_free (temp1str);
|
|
190 }
|
58
|
191
|
1
|
192 if ((bmfile = fopen (tempstr, "r")) == NULL)
|
|
193 {
|
|
194 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"), tempstr,
|
|
195 g_strerror (errno));
|
87
|
196 exit (1);
|
1
|
197 }
|
|
198 g_free (tempstr);
|
|
199
|
|
200 line = 0;
|
|
201 newentry = NULL;
|
|
202 while (fgets (buf, sizeof (buf), bmfile))
|
|
203 {
|
|
204 len = strlen (buf);
|
107
|
205 if (len > 0 && buf[len - 1] == '\n')
|
1
|
206 buf[--len] = '\0';
|
107
|
207 if (len > 0 && buf[len - 1] == '\r')
|
1
|
208 buf[--len] = '\0';
|
|
209 line++;
|
|
210
|
|
211 if (*buf == '[')
|
|
212 {
|
|
213 newentry = g_malloc0 (sizeof (*newentry));
|
|
214 for (; buf[len - 1] == ' ' || buf[len - 1] == ']'; buf[--len] = '\0');
|
124
|
215 newentry->path = g_strdup (buf + 1);
|
1
|
216 newentry->isfolder = 0;
|
122
|
217 gftp_add_bookmark (newentry);
|
1
|
218 }
|
|
219 else if (strncmp (buf, "hostname", 8) == 0 && newentry)
|
|
220 {
|
|
221 curpos = buf + 9;
|
|
222 if (newentry->hostname)
|
|
223 g_free (newentry->hostname);
|
124
|
224 newentry->hostname = g_strdup (curpos);
|
1
|
225 }
|
|
226 else if (strncmp (buf, "port", 4) == 0 && newentry)
|
|
227 newentry->port = strtol (buf + 5, NULL, 10);
|
|
228 else if (strncmp (buf, "protocol", 8) == 0 && newentry)
|
|
229 {
|
|
230 curpos = buf + 9;
|
|
231 if (newentry->protocol)
|
|
232 g_free (newentry->protocol);
|
124
|
233 newentry->protocol = g_strdup (curpos);
|
1
|
234 }
|
|
235 else if (strncmp (buf, "remote directory", 16) == 0 && newentry)
|
|
236 {
|
|
237 curpos = buf + 17;
|
|
238 if (newentry->remote_dir)
|
|
239 g_free (newentry->remote_dir);
|
124
|
240 newentry->remote_dir = g_strdup (curpos);
|
1
|
241 }
|
|
242 else if (strncmp (buf, "local directory", 15) == 0 && newentry)
|
|
243 {
|
|
244 curpos = buf + 16;
|
|
245 if (newentry->local_dir)
|
|
246 g_free (newentry->local_dir);
|
124
|
247 newentry->local_dir = g_strdup (curpos);
|
1
|
248 }
|
|
249 else if (strncmp (buf, "username", 8) == 0 && newentry)
|
|
250 {
|
|
251 curpos = buf + 9;
|
|
252 if (newentry->user)
|
|
253 g_free (newentry->user);
|
124
|
254 newentry->user = g_strdup (curpos);
|
1
|
255 }
|
|
256 else if (strncmp (buf, "password", 8) == 0 && newentry)
|
|
257 {
|
|
258 curpos = buf + 9;
|
|
259 if (newentry->pass)
|
|
260 g_free (newentry->pass);
|
124
|
261 newentry->pass = g_strdup (curpos);
|
1
|
262 newentry->save_password = *newentry->pass != '\0';
|
|
263 }
|
|
264 else if (strncmp (buf, "account", 7) == 0 && newentry)
|
|
265 {
|
|
266 curpos = buf + 8;
|
|
267 if (newentry->acct)
|
|
268 g_free (newentry->acct);
|
124
|
269 newentry->acct = g_strdup (curpos);
|
1
|
270 }
|
199
|
271 else if (*buf == '#' || *buf == '\0')
|
|
272 continue;
|
|
273 else
|
1
|
274 {
|
199
|
275 if ((curpos = strchr (buf, '=')) == NULL)
|
|
276 continue;
|
|
277 *curpos = '\0';
|
|
278
|
|
279 if ((global_entry = g_hash_table_lookup (gftp_global_options_htable,
|
|
280 buf)) == NULL ||
|
|
281 gftp_option_types[global_entry->otype].read_function == NULL)
|
|
282 {
|
|
283 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"),
|
|
284 line, buf);
|
|
285 continue;
|
|
286 }
|
|
287
|
|
288 if (newentry->local_options_hash == NULL)
|
|
289 newentry->local_options_hash = g_hash_table_new (string_hash_function,
|
|
290 string_hash_compare);
|
|
291
|
|
292 newentry->num_local_options_vars++;
|
|
293 newentry->local_options_vars = g_realloc (newentry->local_options_vars,
|
|
294 sizeof (gftp_config_vars) * newentry->num_local_options_vars);
|
|
295
|
|
296 memcpy (&newentry->local_options_vars[newentry->num_local_options_vars - 1], global_entry,
|
|
297 sizeof (newentry->local_options_vars[newentry->num_local_options_vars - 1]));
|
|
298
|
|
299 newentry->local_options_vars[newentry->num_local_options_vars - 1].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
300 newentry->local_options_vars[newentry->num_local_options_vars - 1].value = NULL;
|
|
301
|
|
302 if (gftp_option_types[global_entry->otype].read_function (curpos + 1,
|
|
303 &newentry->local_options_vars[newentry->num_local_options_vars - 1], line) != 0)
|
|
304 {
|
|
305 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"),
|
|
306 line, buf);
|
|
307 continue;
|
|
308 }
|
|
309
|
|
310 g_hash_table_insert (newentry->local_options_hash, newentry->local_options_vars[newentry->num_local_options_vars - 1].key, &newentry->local_options_vars[newentry->num_local_options_vars - 1]);
|
1
|
311 }
|
|
312 }
|
|
313 }
|
|
314
|
|
315
|
198
|
316 int
|
|
317 gftp_config_parse_args (char *str, int numargs, int lineno, char **first, ...)
|
1
|
318 {
|
|
319 char *curpos, *endpos, *pos, **dest, tempchar;
|
|
320 int ret, has_colon;
|
|
321 va_list argp;
|
|
322
|
|
323 ret = 1;
|
|
324 va_start (argp, first);
|
|
325 curpos = str;
|
|
326 dest = first;
|
|
327 *dest = NULL;
|
|
328 while (numargs > 0)
|
|
329 {
|
|
330 has_colon = 0;
|
|
331 if (numargs > 1)
|
|
332 {
|
|
333 if ((endpos = strchr (curpos, ':')) == NULL)
|
|
334 {
|
|
335 printf (_("gFTP Warning: Line %d doesn't have enough arguments\n"),
|
|
336 lineno);
|
|
337 ret = 0;
|
|
338 endpos = curpos + strlen (curpos);
|
|
339 }
|
|
340 else
|
|
341 {
|
|
342 /* Allow colons inside the fields. If you want a colon inside a
|
|
343 field, just put 2 colons in there */
|
|
344 while (endpos != NULL && *(endpos - 1) == '\\')
|
|
345 {
|
|
346 endpos = strchr (endpos + 1, ':');
|
|
347 has_colon = 1;
|
|
348 }
|
|
349 }
|
|
350 }
|
|
351 else
|
|
352 endpos = curpos + strlen (curpos);
|
|
353
|
|
354 *dest = g_malloc (endpos - curpos + 1);
|
|
355 tempchar = *endpos;
|
|
356 *endpos = '\0';
|
|
357 strcpy (*dest, curpos);
|
|
358 *endpos = tempchar;
|
|
359 if (has_colon)
|
|
360 {
|
|
361 pos = *dest;
|
|
362 curpos = *dest;
|
|
363 while (*pos != '\0')
|
|
364 {
|
|
365 if (*pos != '\\' && *(pos + 1) != ':')
|
|
366 *curpos++ = *pos++;
|
|
367 else
|
|
368 pos++;
|
|
369 }
|
|
370 *curpos = '\0';
|
|
371 }
|
|
372 if (*endpos == '\0')
|
|
373 break;
|
|
374 curpos = endpos + 1;
|
|
375 if (numargs > 1)
|
|
376 {
|
|
377 dest = va_arg (argp, char **);
|
|
378 *dest = NULL;
|
|
379 }
|
|
380 numargs--;
|
|
381 }
|
|
382
|
|
383 while (numargs > 1)
|
|
384 {
|
|
385 dest = va_arg (argp, char **);
|
|
386 *dest = g_malloc (1);
|
|
387 **dest = '\0';
|
|
388 numargs--;
|
|
389 }
|
|
390 va_end (argp);
|
|
391 return (1);
|
|
392 }
|
|
393
|
|
394
|
122
|
395 static void *
|
|
396 gftp_config_read_str (char *buf, int line)
|
|
397 {
|
|
398 char *ret;
|
|
399
|
|
400 ret = g_strdup (buf);
|
|
401 return (ret);
|
|
402 }
|
|
403
|
|
404
|
|
405 static void
|
|
406 gftp_config_write_str (FILE *fd, void *data)
|
|
407 {
|
|
408 fprintf (fd, "%s", (char *) data);
|
|
409 }
|
|
410
|
|
411
|
|
412 static void *
|
|
413 gftp_config_read_proxy (char *buf, int line)
|
|
414 {
|
|
415 gftp_proxy_hosts * host;
|
|
416 unsigned int nums[4];
|
|
417 char *pos;
|
|
418
|
|
419 host = g_malloc0 (sizeof (*host));
|
|
420 if ((pos = strchr (buf, '/')) == NULL)
|
|
421 host->domain = g_strdup (buf);
|
|
422 else
|
|
423 {
|
|
424 *pos = '\0';
|
|
425 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]);
|
|
426 host->ipv4_network_address =
|
|
427 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
428
|
|
429 if (strchr (pos + 1, '.') == NULL)
|
|
430 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10));
|
|
431 else
|
|
432 {
|
|
433 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2],
|
|
434 &nums[3]);
|
|
435 host->ipv4_netmask =
|
|
436 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3];
|
|
437 }
|
|
438 }
|
|
439
|
|
440 return (host);
|
|
441 }
|
|
442
|
|
443
|
|
444 static void
|
|
445 gftp_config_write_proxy (FILE *fd, void *data)
|
|
446 {
|
|
447 gftp_proxy_hosts * host;
|
|
448
|
|
449 host = data;
|
|
450
|
|
451 if (host->domain)
|
|
452 fprintf (fd, "%s", host->domain);
|
|
453 else
|
|
454 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d",
|
|
455 host->ipv4_network_address >> 24 & 0xff,
|
|
456 host->ipv4_network_address >> 16 & 0xff,
|
|
457 host->ipv4_network_address >> 8 & 0xff,
|
|
458 host->ipv4_network_address & 0xff,
|
|
459 host->ipv4_netmask >> 24 & 0xff,
|
|
460 host->ipv4_netmask >> 16 & 0xff,
|
|
461 host->ipv4_netmask >> 8 & 0xff,
|
|
462 host->ipv4_netmask & 0xff);
|
|
463 }
|
|
464
|
|
465
|
|
466 static void *
|
|
467 gftp_config_read_ext (char *buf, int line)
|
|
468 {
|
|
469 gftp_file_extensions * tempext;
|
|
470
|
|
471 tempext = g_malloc (sizeof (*tempext));
|
198
|
472 gftp_config_parse_args (buf, 4, line, &tempext->ext, &tempext->filename,
|
|
473 &tempext->ascii_binary, &tempext->view_program);
|
122
|
474
|
|
475 tempext->stlen = strlen (tempext->ext);
|
|
476
|
|
477 return (tempext);
|
|
478 }
|
|
479
|
|
480
|
|
481 static void
|
|
482 gftp_config_write_ext (FILE *fd, void *data)
|
|
483 {
|
|
484 gftp_file_extensions * tempext;
|
|
485
|
|
486 tempext = data;
|
|
487 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename,
|
|
488 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary,
|
|
489 tempext->view_program);
|
|
490 }
|
|
491
|
|
492
|
|
493 gftp_config_list_vars gftp_config_list[] = {
|
|
494 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy,
|
|
495 NULL, 0,
|
|
496 N_("This section specifies which hosts are on the local subnet and won't need to go out the proxy server (if available). Syntax: dont_use_proxy=.domain or dont_use_proxy=network number/netmask")},
|
|
497 {"ext", gftp_config_read_ext, gftp_config_write_ext,
|
|
498 NULL, 0,
|
|
499 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")},
|
|
500 {"localhistory", gftp_config_read_str, gftp_config_write_str,
|
|
501 NULL, 0, NULL},
|
|
502 {"remotehistory", gftp_config_read_str, gftp_config_write_str,
|
|
503 NULL, 0, NULL},
|
|
504 {"hosthistory", gftp_config_read_str, gftp_config_write_str,
|
|
505 NULL, 0, NULL},
|
|
506 {"porthistory", gftp_config_read_str, gftp_config_write_str,
|
|
507 NULL, 0, NULL},
|
|
508 {"userhistory", gftp_config_read_str, gftp_config_write_str,
|
|
509 NULL, 0, NULL},
|
|
510 {NULL, NULL, NULL,
|
|
511 NULL, 0, NULL}
|
|
512 };
|
|
513
|
|
514
|
|
515 static void
|
|
516 gftp_setup_global_options (gftp_config_vars * cvars)
|
|
517 {
|
|
518 int i;
|
|
519
|
|
520 for (i=0; cvars[i].key != NULL; i++)
|
|
521 {
|
136
|
522 if (cvars[i].key != NULL && *cvars[i].key != '\0')
|
122
|
523 g_hash_table_insert (gftp_global_options_htable,
|
|
524 cvars[i].key, &cvars[i]);
|
|
525 }
|
|
526 }
|
|
527
|
|
528
|
|
529 void
|
124
|
530 gftp_read_config_file (char *global_data_path)
|
122
|
531 {
|
|
532 char *tempstr, *temp1str, *curpos, buf[255];
|
|
533 gftp_config_list_vars * tmplistvar;
|
|
534 gftp_config_vars * tmpconfigvar;
|
125
|
535 char **protocol_list;
|
122
|
536 FILE *conffile;
|
|
537 int line, i;
|
|
538 size_t len;
|
|
539
|
|
540 gftp_global_options_htable = g_hash_table_new (string_hash_function,
|
|
541 string_hash_compare);
|
|
542
|
|
543 gftp_register_config_vars (gftp_global_config_vars);
|
|
544
|
125
|
545 protocol_list = NULL;
|
122
|
546 for (i=0; gftp_protocols[i].register_options != NULL; i++)
|
|
547 {
|
125
|
548 if (gftp_protocols[i].shown)
|
|
549 {
|
|
550 protocol_list = g_realloc (protocol_list, sizeof (char *) * (i + 2));
|
|
551 protocol_list[i] = gftp_protocols[i].name;
|
|
552 protocol_list[i + 1] = NULL;
|
|
553 }
|
|
554
|
122
|
555 if (gftp_protocols[i].register_options != NULL)
|
|
556 gftp_protocols[i].register_options ();
|
|
557 }
|
|
558
|
125
|
559 if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
560 "default_protocol")) != NULL)
|
201
|
561 {
|
|
562 tmpconfigvar->listdata = protocol_list;
|
|
563 tmpconfigvar->flags |= GFTP_CVARS_FLAGS_DYNLISTMEM;
|
|
564 }
|
125
|
565
|
122
|
566 gftp_config_list_htable = g_hash_table_new (string_hash_function,
|
|
567 string_hash_compare);
|
|
568
|
|
569 for (i=0; gftp_config_list[i].key != NULL; i++)
|
|
570 {
|
|
571 g_hash_table_insert (gftp_config_list_htable, gftp_config_list[i].key,
|
|
572 &gftp_config_list[i]);
|
|
573 }
|
|
574
|
|
575 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
576 {
|
|
577 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
578 exit (1);
|
|
579 }
|
|
580
|
|
581 if (access (tempstr, F_OK) == -1)
|
|
582 {
|
|
583 temp1str = expand_path (BASE_CONF_DIR);
|
|
584 if (access (temp1str, F_OK) == -1)
|
|
585 {
|
|
586 if (mkdir (temp1str, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
|
|
587 {
|
|
588 printf (_("gFTP Error: Could not make directory %s: %s\n"),
|
|
589 temp1str, g_strerror (errno));
|
|
590 exit (1);
|
|
591 }
|
|
592 }
|
|
593 g_free (temp1str);
|
|
594
|
124
|
595 temp1str = g_strdup_printf ("%s/gftprc", global_data_path);
|
122
|
596 if (access (temp1str, F_OK) == -1)
|
|
597 {
|
|
598 printf (_("gFTP Error: Cannot find master config file %s\n"),
|
|
599 temp1str);
|
|
600 printf (_("Did you do a make install?\n"));
|
|
601 exit (1);
|
|
602 }
|
|
603 copyfile (temp1str, tempstr);
|
|
604 g_free (temp1str);
|
|
605 }
|
|
606
|
|
607 if ((conffile = fopen (tempstr, "r")) == NULL)
|
|
608 {
|
|
609 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
610 g_strerror (errno));
|
|
611 exit (1);
|
|
612 }
|
|
613 g_free (tempstr);
|
|
614
|
|
615 line = 0;
|
|
616 while (fgets (buf, sizeof (buf), conffile))
|
|
617 {
|
|
618 len = strlen (buf);
|
|
619 if (len > 0 && buf[len - 1] == '\n')
|
|
620 buf[--len] = '\0';
|
|
621 if (len > 0 && buf[len - 1] == '\r')
|
|
622 buf[--len] = '\0';
|
|
623 line++;
|
|
624
|
|
625 if (*buf == '#' || *buf == '\0')
|
|
626 continue;
|
|
627
|
|
628 if ((curpos = strchr (buf, '=')) == NULL)
|
|
629 continue;
|
|
630
|
|
631 *curpos = '\0';
|
|
632
|
|
633 if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
634 buf)) != NULL)
|
|
635 {
|
|
636 tmplistvar->list = g_list_append (tmplistvar->list,
|
|
637 tmplistvar->read_func (curpos + 1,
|
|
638 line));
|
|
639 tmplistvar->num_items++;
|
|
640 }
|
|
641 else if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
642 buf)) != NULL &&
|
|
643 gftp_option_types[tmpconfigvar->otype].read_function != NULL)
|
|
644 {
|
|
645 if (gftp_option_types[tmpconfigvar->otype].read_function (curpos + 1,
|
|
646 tmpconfigvar, line) != 0)
|
|
647 {
|
|
648 printf (_("Terminating due to parse errors at line %d in the config file\n"), line);
|
|
649 exit (1);
|
|
650 }
|
|
651 }
|
|
652 else
|
|
653 {
|
|
654 printf (_("gFTP Warning: Skipping line %d in config file: %s\n"),
|
|
655 line, buf);
|
|
656 }
|
|
657 }
|
|
658
|
|
659 if ((tempstr = expand_path (LOG_FILE)) == NULL)
|
|
660 {
|
|
661 printf (_("gFTP Error: Bad log file name %s\n"), LOG_FILE);
|
|
662 exit (1);
|
|
663 }
|
|
664
|
|
665 if ((gftp_logfd = fopen (tempstr, "w")) == NULL)
|
|
666 {
|
|
667 printf (_("gFTP Warning: Cannot open %s for writing: %s\n"),
|
|
668 tempstr, g_strerror (errno));
|
|
669 }
|
|
670 g_free (tempstr);
|
|
671
|
|
672 gftp_bookmarks = g_malloc0 (sizeof (*gftp_bookmarks));
|
|
673 gftp_bookmarks->isfolder = 1;
|
|
674 gftp_bookmarks->path = g_malloc0 (1);
|
|
675 gftp_bookmarks_htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
676
|
124
|
677 gftp_read_bookmarks (global_data_path);
|
122
|
678 }
|
|
679
|
|
680
|
|
681 static void
|
|
682 write_comment (FILE * fd, const char *comment)
|
|
683 {
|
|
684 const char *pos, *endpos;
|
|
685
|
|
686 fwrite ("# ", 1, 2, fd);
|
|
687 pos = comment;
|
|
688 while (strlen (pos) > 76)
|
|
689 {
|
|
690 for (endpos = pos + 76; *endpos != ' ' && endpos > pos; endpos--);
|
|
691 if (endpos == pos)
|
|
692 {
|
|
693 for (endpos = pos + 76; *endpos != ' ' && *endpos != '\0';
|
|
694 endpos++);
|
|
695 }
|
|
696 fwrite (pos, 1, endpos - pos, fd);
|
|
697 fwrite ("\n# ", 1, 3, fd);
|
|
698 if (*endpos == '\0')
|
|
699 {
|
|
700 pos = endpos;
|
|
701 break;
|
|
702 }
|
|
703 else
|
|
704 pos = endpos + 1;
|
|
705 }
|
|
706 if (strlen (pos) > 1)
|
|
707 {
|
|
708 fwrite (pos, 1, strlen (pos), fd);
|
|
709 fwrite ("\n", 1, 1, fd);
|
|
710 }
|
|
711 }
|
|
712
|
|
713
|
|
714 void
|
|
715 gftp_write_bookmarks_file (void)
|
|
716 {
|
|
717 gftp_bookmarks_var * tempentry;
|
|
718 char *bmhdr, *tempstr;
|
|
719 FILE * bmfile;
|
199
|
720 int i;
|
122
|
721
|
|
722 bmhdr = N_("Bookmarks file for gFTP. Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten");
|
|
723
|
|
724 if ((tempstr = expand_path (BOOKMARKS_FILE)) == NULL)
|
|
725 {
|
|
726 printf (_("gFTP Error: Bad bookmarks file name %s\n"), CONFIG_FILE);
|
|
727 exit (1);
|
|
728 }
|
|
729
|
|
730 if ((bmfile = fopen (tempstr, "w+")) == NULL)
|
|
731 {
|
|
732 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"),
|
|
733 CONFIG_FILE, g_strerror (errno));
|
|
734 exit (1);
|
|
735 }
|
|
736
|
|
737 g_free (tempstr);
|
|
738
|
|
739 write_comment (bmfile, _(bmhdr));
|
|
740 fwrite ("\n", 1, 1, bmfile);
|
|
741
|
|
742 tempentry = gftp_bookmarks->children;
|
|
743 while (tempentry != NULL)
|
|
744 {
|
|
745 if (tempentry->children != NULL)
|
|
746 {
|
|
747 tempentry = tempentry->children;
|
|
748 continue;
|
|
749 }
|
|
750 tempstr = tempentry->path;
|
|
751 while (*tempstr == '/')
|
|
752 tempstr++;
|
|
753 fprintf (bmfile,
|
|
754 "[%s]\nhostname=%s\nport=%d\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nusername=%s\npassword=%s\naccount=%s\n",
|
|
755 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname,
|
|
756 tempentry->port, tempentry->protocol == NULL
|
|
757 || *tempentry->protocol ==
|
|
758 '\0' ? gftp_protocols[0].name : tempentry->protocol,
|
|
759 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir,
|
|
760 tempentry->local_dir == NULL ? "" : tempentry->local_dir,
|
|
761 tempentry->user == NULL ? "" : tempentry->user,
|
|
762 !tempentry->save_password
|
|
763 || tempentry->pass == NULL ? "" : tempentry->pass,
|
|
764 tempentry->acct == NULL ? "" : tempentry->acct);
|
|
765
|
199
|
766 if (tempentry->local_options_vars != NULL)
|
|
767 {
|
|
768 for (i=0; i<tempentry->num_local_options_vars; i++)
|
|
769 {
|
|
770 fprintf (bmfile, "%s=", tempentry->local_options_vars[i].key);
|
|
771 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], bmfile, 1);
|
|
772 fprintf (bmfile, "\n");
|
|
773 }
|
|
774 }
|
122
|
775
|
|
776 fprintf (bmfile, "\n");
|
|
777
|
|
778 if (tempentry->next == NULL)
|
|
779 {
|
|
780 tempentry = tempentry->prev;
|
|
781 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
782 tempentry = tempentry->prev;
|
|
783 tempentry = tempentry->next;
|
|
784 }
|
|
785 else
|
|
786 tempentry = tempentry->next;
|
|
787 }
|
|
788
|
|
789 fclose (bmfile);
|
|
790 }
|
|
791
|
|
792
|
|
793 void
|
|
794 gftp_write_config_file (void)
|
|
795 {
|
|
796 gftp_config_vars * cv;
|
|
797 GList *templist;
|
|
798 FILE *conffile;
|
|
799 char *tempstr;
|
|
800 int i;
|
|
801
|
|
802 if ((tempstr = expand_path (CONFIG_FILE)) == NULL)
|
|
803 {
|
|
804 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE);
|
|
805 exit (1);
|
|
806 }
|
|
807
|
|
808 if ((conffile = fopen (tempstr, "w+")) == NULL)
|
|
809 {
|
|
810 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE,
|
|
811 g_strerror (errno));
|
|
812 exit (1);
|
|
813 }
|
|
814
|
|
815 g_free (tempstr);
|
|
816
|
|
817 write_comment (conffile, _("Config file for gFTP. Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten. If a entry has a (*) in it's comment, you can't change it inside gFTP"));
|
|
818
|
|
819 for (templist = gftp_options_list;
|
|
820 templist != NULL;
|
|
821 templist = templist->next)
|
|
822 {
|
|
823 cv = templist->data;
|
|
824
|
|
825 for (i=0; cv[i].key != NULL; i++)
|
|
826 {
|
|
827 if (gftp_option_types[cv[i].otype].write_function == NULL ||
|
|
828 *cv[i].key == '\0')
|
|
829 continue;
|
|
830
|
|
831 fprintf (conffile, "\n");
|
|
832 if (cv[i].comment != NULL)
|
|
833 write_comment (conffile, _(cv[i].comment));
|
|
834
|
|
835 fprintf (conffile, "%s=", cv[i].key);
|
|
836 gftp_option_types[cv[i].otype].write_function (&cv[i], conffile, 1);
|
|
837 fprintf (conffile, "\n");
|
|
838 }
|
|
839 }
|
|
840
|
218
|
841 for (i=0; gftp_config_list[i].key != NULL; i++)
|
122
|
842 {
|
218
|
843 if (gftp_config_list[i].header == NULL &&
|
|
844 gftp_config_list[i].list == NULL)
|
|
845 continue;
|
|
846
|
122
|
847 fprintf (conffile, "\n");
|
|
848 if (gftp_config_list[i].header != NULL)
|
|
849 write_comment (conffile, _(gftp_config_list[i].header));
|
|
850
|
218
|
851 for (templist = gftp_config_list[i].list;
|
122
|
852 templist != NULL;
|
|
853 templist = templist->next)
|
|
854 {
|
|
855 fprintf (conffile, "%s=", gftp_config_list[i].key);
|
|
856 gftp_config_list[i].write_func (conffile, templist->data);
|
|
857 fprintf (conffile, "\n");
|
|
858 }
|
|
859 }
|
|
860
|
|
861 fclose (conffile);
|
|
862 }
|
|
863
|
|
864
|
1
|
865 GHashTable *
|
122
|
866 build_bookmarks_hash_table (gftp_bookmarks_var * entry)
|
1
|
867 {
|
122
|
868 gftp_bookmarks_var * tempentry;
|
1
|
869 GHashTable * htable;
|
|
870
|
|
871 htable = g_hash_table_new (string_hash_function, string_hash_compare);
|
|
872 tempentry = entry;
|
|
873 while (tempentry != NULL)
|
|
874 {
|
|
875 g_hash_table_insert (htable, tempentry->path, tempentry);
|
|
876 if (tempentry->children != NULL)
|
|
877 {
|
|
878 tempentry = tempentry->children;
|
|
879 continue;
|
|
880 }
|
|
881 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
882 tempentry = tempentry->prev;
|
|
883 tempentry = tempentry->next;
|
|
884 }
|
|
885 return (htable);
|
|
886 }
|
|
887
|
|
888
|
|
889 void
|
122
|
890 print_bookmarks (gftp_bookmarks_var * bookmarks)
|
1
|
891 {
|
122
|
892 gftp_bookmarks_var * tempentry;
|
1
|
893
|
|
894 tempentry = bookmarks->children;
|
|
895 while (tempentry != NULL)
|
|
896 {
|
|
897 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL);
|
|
898 if (tempentry->children != NULL)
|
|
899 {
|
|
900 tempentry = tempentry->children;
|
|
901 continue;
|
|
902 }
|
|
903
|
|
904 if (tempentry->next == NULL)
|
|
905 {
|
|
906 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
907 tempentry = tempentry->prev;
|
|
908 tempentry = tempentry->next;
|
|
909 }
|
|
910 else
|
|
911 tempentry = tempentry->next;
|
|
912 }
|
|
913 }
|
|
914
|
122
|
915
|
|
916 static int
|
|
917 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line)
|
|
918 {
|
136
|
919 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
920 g_free (cv->value);
|
|
921
|
122
|
922 if (str != NULL)
|
|
923 {
|
|
924 cv->value = g_strdup (str);
|
136
|
925 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
926 return (0);
|
|
927 }
|
|
928 else
|
136
|
929 {
|
|
930 cv->value = NULL;
|
|
931 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
932 return (-1);
|
|
933 }
|
122
|
934 }
|
|
935
|
|
936
|
|
937 static int
|
|
938 gftp_config_file_write_text (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
939 {
|
|
940 char *outstr;
|
|
941
|
|
942 if (cv->value != NULL)
|
|
943 {
|
|
944 outstr = cv->value;
|
|
945 if (*outstr != '\0')
|
|
946 fprintf (fd, "%s", outstr);
|
|
947 return (0);
|
|
948 }
|
|
949 else
|
|
950 return (-1);
|
|
951 }
|
|
952
|
|
953
|
|
954 static int
|
|
955 gftp_config_file_write_hidetext (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
956 {
|
|
957 char *outstr;
|
|
958
|
|
959 if (cv->value != NULL)
|
|
960 {
|
|
961 outstr = cv->value;
|
|
962 if (*outstr != '\0')
|
|
963 {
|
|
964 if (to_config_file)
|
|
965 fprintf (fd, "%s", outstr);
|
|
966 else
|
|
967 fprintf (fd, "*****");
|
|
968 }
|
|
969 return (0);
|
|
970 }
|
|
971 else
|
|
972 return (-1);
|
|
973 }
|
|
974
|
|
975
|
207
|
976 static void
|
|
977 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
978 {
|
|
979 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
980 g_free (dest_cv->value);
|
|
981
|
|
982 if (cv->value != NULL)
|
|
983 {
|
|
984 dest_cv->value = g_strdup ((char *) cv->value);
|
|
985 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
986 }
|
|
987 else
|
|
988 dest_cv->value = NULL;
|
|
989 }
|
|
990
|
|
991
|
227
|
992 static int
|
|
993 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
994 {
|
|
995 char *str1, *str2;
|
|
996
|
|
997 str1 = cv1->value;
|
|
998 str2 = cv2->value;
|
|
999
|
|
1000 if (cv1->value == NULL && cv2->value == NULL)
|
|
1001 return (0);
|
|
1002
|
|
1003 if ((cv1->value == NULL && cv2->value != NULL) ||
|
|
1004 (cv1->value != NULL && cv2->value == NULL))
|
|
1005 return (-1);
|
|
1006
|
|
1007 return (strcmp (str1, str2));
|
|
1008 }
|
|
1009
|
|
1010
|
207
|
1011 static void
|
|
1012 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1013 {
|
|
1014 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value));
|
|
1015 }
|
|
1016
|
122
|
1017 static int
|
|
1018 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line)
|
|
1019 {
|
|
1020 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10));
|
|
1021 return (0);
|
|
1022 }
|
|
1023
|
|
1024
|
|
1025 static int
|
|
1026 gftp_config_file_write_int (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1027 {
|
|
1028 fprintf (fd, "%d", GPOINTER_TO_INT(cv->value));
|
|
1029 return (0);
|
|
1030 }
|
|
1031
|
|
1032
|
|
1033 static int
|
227
|
1034 gftp_config_file_compare_int (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1035 {
|
|
1036 return (GPOINTER_TO_INT(cv1->value) == GPOINTER_TO_INT(cv2->value) ? 0 : -1);
|
|
1037 }
|
|
1038
|
|
1039
|
|
1040 static int
|
122
|
1041 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line)
|
|
1042 {
|
|
1043 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0);
|
|
1044 return (0);
|
|
1045 }
|
|
1046
|
|
1047
|
|
1048 static int
|
|
1049 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line)
|
|
1050 {
|
131
|
1051 float f;
|
|
1052
|
151
|
1053 f = strtod (str, NULL);
|
131
|
1054 memcpy (&cv->value, &f, sizeof (cv->value));
|
122
|
1055 return (0);
|
|
1056 }
|
|
1057
|
|
1058
|
|
1059 static int
|
|
1060 gftp_config_file_write_float (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1061 {
|
131
|
1062 float f;
|
|
1063
|
|
1064 memcpy (&f, &cv->value, sizeof (f));
|
|
1065 fprintf (fd, "%.2f", f);
|
122
|
1066 return (0);
|
|
1067 }
|
|
1068
|
|
1069
|
|
1070 static int
|
227
|
1071 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1072 {
|
|
1073 float f1, f2;
|
|
1074
|
|
1075 memcpy (&f1, &cv1->value, sizeof (f1));
|
|
1076 memcpy (&f2, &cv2->value, sizeof (f2));
|
|
1077 return (f1 == f2 ? 0 : -1);
|
|
1078 }
|
|
1079
|
|
1080
|
|
1081 static int
|
122
|
1082 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line)
|
|
1083 {
|
|
1084 char *red, *green, *blue;
|
|
1085 gftp_color * color;
|
|
1086
|
136
|
1087 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL)
|
|
1088 g_free (cv->value);
|
|
1089
|
198
|
1090 gftp_config_parse_args (str, 3, line, &red, &green, &blue);
|
122
|
1091
|
|
1092 color = g_malloc (sizeof (*color));
|
|
1093 color->red = strtol (red, NULL, 16);
|
|
1094 color->green = strtol (green, NULL, 16);
|
|
1095 color->blue = strtol (blue, NULL, 16);
|
|
1096 g_free (red);
|
|
1097 g_free (green);
|
|
1098 g_free (blue);
|
|
1099
|
|
1100 cv->value = color;
|
136
|
1101 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
122
|
1102
|
|
1103 return (0);
|
|
1104 }
|
|
1105
|
|
1106
|
|
1107 static int
|
|
1108 gftp_config_file_write_color (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1109 {
|
|
1110 gftp_color * color;
|
|
1111
|
|
1112 color = cv->value;
|
|
1113 fprintf (fd, "%x:%x:%x", color->red, color->green, color->blue);
|
|
1114 return (0);
|
|
1115 }
|
|
1116
|
|
1117
|
207
|
1118 static void
|
|
1119 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv)
|
|
1120 {
|
|
1121 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL)
|
|
1122 g_free (dest_cv->value);
|
|
1123
|
|
1124 dest_cv->value = g_malloc (sizeof (gftp_color));
|
|
1125 memcpy (dest_cv->value, cv->value, sizeof (gftp_color));
|
|
1126 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM;
|
|
1127 }
|
|
1128
|
|
1129
|
122
|
1130 static int
|
227
|
1131 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2)
|
|
1132 {
|
|
1133 gftp_color * color1, * color2;
|
|
1134
|
|
1135 color1 = cv1->value;
|
|
1136 color2 = cv2->value;
|
|
1137
|
|
1138 return (color1->red == color2->red && color1->green == color2->green &&
|
|
1139 color1->blue == color2->blue ? 0 : -1);
|
|
1140 }
|
|
1141
|
|
1142
|
|
1143 static int
|
122
|
1144 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line)
|
|
1145 {
|
|
1146 char **clist;
|
|
1147 int i;
|
|
1148
|
|
1149 cv->value = 0;
|
|
1150 if (cv->listdata != NULL)
|
|
1151 {
|
|
1152 clist = cv->listdata;
|
|
1153 for (i=0; clist[i] != NULL; i++)
|
|
1154 {
|
125
|
1155 if (strcasecmp (clist[i], str) == 0)
|
122
|
1156 {
|
|
1157 cv->value = GINT_TO_POINTER(i);
|
|
1158 break;
|
|
1159 }
|
|
1160 }
|
|
1161 }
|
|
1162
|
|
1163 return (0);
|
|
1164 }
|
|
1165
|
|
1166
|
|
1167 static int
|
|
1168 gftp_config_file_write_intcombo (gftp_config_vars * cv, FILE * fd, int to_config_file)
|
|
1169 {
|
|
1170 char **clist;
|
|
1171
|
|
1172 clist = cv->listdata;
|
|
1173 if (clist != NULL)
|
125
|
1174 fprintf (fd, "%s", clist[GPOINTER_TO_INT(cv->value)]);
|
122
|
1175 else
|
|
1176 fprintf (fd, _("<unknown>"));
|
|
1177
|
|
1178 return (0);
|
|
1179 }
|
|
1180
|
|
1181
|
125
|
1182 static int
|
|
1183 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line)
|
|
1184 {
|
|
1185 char **clist;
|
|
1186 int i;
|
|
1187
|
|
1188 cv->value = NULL;
|
|
1189 if (cv->listdata != NULL)
|
|
1190 {
|
|
1191 clist = cv->listdata;
|
|
1192 for (i=0; clist[i] != NULL; i++)
|
|
1193 {
|
|
1194 if (strcasecmp (clist[i], str) == 0)
|
|
1195 {
|
|
1196 cv->value = clist[i];
|
|
1197 break;
|
|
1198 }
|
|
1199 }
|
|
1200 }
|
|
1201
|
|
1202 return (0);
|
|
1203 }
|
|
1204
|
|
1205
|
|
1206 /* Note, the index numbers of this array must match up to the numbers in
|
122
|
1207 gftp_option_type_enum in gftp.h */
|
|
1208 gftp_option_type_var gftp_option_types[] = {
|
227
|
1209 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1210 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1211 {gftp_config_file_read_textcombo, gftp_config_file_write_text,
|
|
1212 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1213 {gftp_config_file_read_text, gftp_config_file_write_text,
|
|
1214 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1215 {gftp_config_file_read_text, gftp_config_file_write_hidetext,
|
|
1216 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL},
|
|
1217 {gftp_config_file_read_int, gftp_config_file_write_int,
|
|
1218 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1219 NULL, NULL, NULL},
|
|
1220 {gftp_config_file_read_checkbox, gftp_config_file_write_int,
|
|
1221 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1222 NULL, NULL, NULL},
|
|
1223 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo,
|
|
1224 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int,
|
|
1225 NULL, NULL, NULL},
|
|
1226 {gftp_config_file_read_float, gftp_config_file_write_float,
|
|
1227 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float,
|
|
1228 NULL, NULL, NULL},
|
|
1229 {gftp_config_file_read_color, gftp_config_file_write_color,
|
|
1230 gftp_config_file_copy_color, gftp_config_file_compare_color,
|
|
1231 NULL, NULL, NULL},
|
|
1232 {NULL, NULL, NULL, NULL, NULL, NULL},
|
|
1233 {NULL, NULL, NULL, NULL, NULL, NULL}
|
122
|
1234 };
|
|
1235
|
|
1236
|
|
1237 void
|
|
1238 gftp_lookup_global_option (char * key, void *value)
|
|
1239 {
|
|
1240 gftp_config_list_vars * tmplistvar;
|
|
1241 gftp_config_vars * tmpconfigvar;
|
|
1242
|
|
1243 if (gftp_global_options_htable != NULL &&
|
|
1244 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1245 key)) != NULL)
|
124
|
1246 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
122
|
1247 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable,
|
|
1248 key)) != NULL)
|
124
|
1249 *(gftp_config_list_vars **) value = tmplistvar;
|
122
|
1250 else
|
|
1251 {
|
|
1252 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1253 exit (1);
|
|
1254 }
|
|
1255 }
|
|
1256
|
|
1257
|
|
1258 void
|
|
1259 gftp_lookup_request_option (gftp_request * request, char * key, void *value)
|
|
1260 {
|
143
|
1261 gftp_config_vars * tmpconfigvar;
|
|
1262
|
|
1263 if (request != NULL && request->local_options_hash != NULL &&
|
|
1264 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1265 key)) != NULL)
|
|
1266 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1267 else
|
|
1268 gftp_lookup_global_option (key, value);
|
122
|
1269 }
|
|
1270
|
|
1271
|
|
1272 void
|
229
|
1273 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, char * key, void *value)
|
|
1274 {
|
|
1275 gftp_config_vars * tmpconfigvar;
|
|
1276
|
|
1277 if (bm != NULL && bm->local_options_hash != NULL &&
|
|
1278 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1279 key)) != NULL)
|
|
1280 memcpy (value, &tmpconfigvar->value, sizeof (value));
|
|
1281 else
|
|
1282 gftp_lookup_global_option (key, value);
|
|
1283 }
|
|
1284
|
|
1285
|
|
1286 void
|
227
|
1287 gftp_set_global_option (char * key, const void *value)
|
122
|
1288 {
|
227
|
1289 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1290 void *nc_ptr;
|
|
1291 int ret;
|
129
|
1292
|
|
1293 if (gftp_global_options_htable != NULL &&
|
|
1294 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1295 key)) != NULL)
|
201
|
1296 {
|
227
|
1297 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1298
|
|
1299 /* Cheap warning fix for const pointer... */
|
|
1300 memcpy (&nc_ptr, &value, sizeof (nc_ptr));
|
|
1301 newconfigvar.value = nc_ptr;
|
|
1302 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
201
|
1303
|
227
|
1304 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1305 if (ret != 0)
|
|
1306 {
|
|
1307 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, tmpconfigvar);
|
|
1308 gftp_configuration_changed = 1;
|
|
1309 }
|
201
|
1310 }
|
129
|
1311 else
|
|
1312 {
|
|
1313 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1314 exit (1);
|
|
1315 }
|
122
|
1316 }
|
|
1317
|
|
1318
|
229
|
1319 static void
|
|
1320 _gftp_set_option_value (gftp_config_vars * cv, const void * newval)
|
|
1321 {
|
|
1322 gftp_config_vars newconfigvar;
|
|
1323 void *nc_ptr;
|
|
1324
|
|
1325 memcpy (&newconfigvar, cv, sizeof (newconfigvar));
|
|
1326
|
|
1327 /* Cheap warning fix for const pointer... */
|
|
1328 memcpy (&nc_ptr, &newval, sizeof (nc_ptr));
|
|
1329 newconfigvar.value = nc_ptr;
|
|
1330 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1331
|
|
1332 gftp_option_types[newconfigvar.otype].copy_function (&newconfigvar, cv);
|
|
1333 }
|
|
1334
|
|
1335
|
122
|
1336 void
|
229
|
1337 gftp_set_request_option (gftp_request * request, char * key, const void *value)
|
122
|
1338 {
|
136
|
1339 gftp_config_vars * tmpconfigvar;
|
|
1340
|
|
1341 if (request->local_options_hash == NULL)
|
|
1342 request->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1343 string_hash_compare);
|
|
1344
|
|
1345 if ((tmpconfigvar = g_hash_table_lookup (request->local_options_hash,
|
|
1346 key)) != NULL)
|
229
|
1347 _gftp_set_option_value (tmpconfigvar, value);
|
136
|
1348 else
|
|
1349 {
|
143
|
1350 if (gftp_global_options_htable == NULL ||
|
136
|
1351 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1352 key)) == NULL)
|
|
1353 {
|
|
1354 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1355 exit (1);
|
|
1356 }
|
|
1357
|
|
1358 request->num_local_options_vars++;
|
|
1359 request->local_options_vars = g_realloc (request->local_options_vars,
|
199
|
1360 sizeof (gftp_config_vars) * request->num_local_options_vars);
|
136
|
1361
|
229
|
1362 memcpy (&request->local_options_vars[request->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1363 _gftp_set_option_value (&request->local_options_vars[request->num_local_options_vars - 1], value);
|
143
|
1364
|
|
1365 g_hash_table_insert (request->local_options_hash, request->local_options_vars[request->num_local_options_vars - 1].key, &request->local_options_vars[request->num_local_options_vars - 1]);
|
136
|
1366 }
|
122
|
1367 }
|
|
1368
|
|
1369
|
|
1370 void
|
229
|
1371 gftp_set_bookmark_option (gftp_bookmarks_var * bm, char * key, const void *value)
|
|
1372 {
|
|
1373 gftp_config_vars * tmpconfigvar, newconfigvar;
|
|
1374 int ret;
|
|
1375
|
|
1376 if (bm->local_options_hash != NULL &&
|
|
1377 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash,
|
|
1378 key)) != NULL)
|
|
1379 _gftp_set_option_value (tmpconfigvar, value);
|
|
1380 else
|
|
1381 {
|
|
1382 if (gftp_global_options_htable == NULL ||
|
|
1383 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable,
|
|
1384 key)) == NULL)
|
|
1385 {
|
|
1386 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key);
|
|
1387 exit (1);
|
|
1388 }
|
|
1389
|
|
1390 /* Check to see if this is set to the same value as the global option.
|
|
1391 If so, don't add it to the bookmark preferences */
|
|
1392 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar));
|
|
1393 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value));
|
|
1394 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
|
1395
|
|
1396 ret = gftp_option_types[newconfigvar.otype].compare_function (&newconfigvar, tmpconfigvar);
|
|
1397 if (ret == 0)
|
|
1398 return;
|
|
1399
|
|
1400 if (bm->local_options_hash == NULL)
|
|
1401 bm->local_options_hash = g_hash_table_new (string_hash_function,
|
|
1402 string_hash_compare);
|
|
1403
|
|
1404 bm->num_local_options_vars++;
|
|
1405 bm->local_options_vars = g_realloc (bm->local_options_vars,
|
|
1406 sizeof (gftp_config_vars) * bm->num_local_options_vars);
|
|
1407
|
|
1408 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar));
|
|
1409 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value);
|
|
1410
|
|
1411 g_hash_table_insert (bm->local_options_hash, bm->local_options_vars[bm->num_local_options_vars - 1].key, &bm->local_options_vars[bm->num_local_options_vars - 1]);
|
|
1412 }
|
|
1413 }
|
|
1414
|
|
1415
|
|
1416 void
|
122
|
1417 gftp_register_config_vars (gftp_config_vars * config_vars)
|
|
1418 {
|
|
1419 gftp_options_list = g_list_append (gftp_options_list, config_vars);
|
|
1420 gftp_setup_global_options (config_vars);
|
|
1421 }
|
|
1422
|
199
|
1423
|
|
1424 void
|
|
1425 gftp_copy_local_options (gftp_config_vars ** new_options_vars,
|
|
1426 GHashTable ** new_options_hash,
|
|
1427 gftp_config_vars * orig_options,
|
|
1428 int num_local_options_vars)
|
|
1429 {
|
|
1430 int i;
|
|
1431
|
|
1432 if (orig_options == NULL || num_local_options_vars == 0)
|
|
1433 {
|
|
1434 *new_options_vars = NULL;
|
|
1435 *new_options_hash = NULL;
|
|
1436 return;
|
|
1437 }
|
|
1438
|
|
1439 *new_options_hash = g_hash_table_new (string_hash_function,
|
|
1440 string_hash_compare);
|
|
1441
|
|
1442 *new_options_vars = g_malloc (sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1443 memcpy (*new_options_vars, orig_options,
|
|
1444 sizeof (gftp_config_vars) * num_local_options_vars);
|
|
1445
|
|
1446 for (i=0; i<num_local_options_vars; i++)
|
|
1447 {
|
|
1448 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key,
|
|
1449 &(*new_options_vars)[i]);
|
207
|
1450
|
|
1451 (*new_options_vars)[i].value = NULL;
|
|
1452 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM;
|
199
|
1453
|
207
|
1454 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]);
|
199
|
1455 }
|
|
1456 }
|
|
1457
|
201
|
1458
|
|
1459 void
|
|
1460 gftp_config_free_options (gftp_config_vars * options_vars,
|
|
1461 GHashTable * options_hash,
|
|
1462 int num_options_vars)
|
|
1463 {
|
|
1464 int i;
|
|
1465
|
|
1466 if (num_options_vars == 0)
|
|
1467 return;
|
|
1468
|
|
1469 if (num_options_vars > 0)
|
|
1470 {
|
|
1471 /* If num_options_vars is 0, then the options was allocated with malloc */
|
|
1472
|
|
1473 for (i=0; i<num_options_vars; i++)
|
|
1474 {
|
|
1475 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1476 options_vars[i].value != NULL)
|
|
1477 g_free (options_vars[i].value);
|
|
1478 }
|
|
1479
|
|
1480 g_free (options_vars);
|
|
1481 }
|
|
1482 else if (num_options_vars < 0)
|
|
1483 {
|
|
1484 /* Otherwise we are freeing the global options */
|
|
1485
|
|
1486 for (i=0; options_vars[i].key != NULL; i++)
|
|
1487 {
|
|
1488 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM &&
|
|
1489 options_vars[i].value != NULL)
|
|
1490 g_free (options_vars[i].value);
|
|
1491
|
|
1492 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM &&
|
|
1493 options_vars[i].listdata != NULL)
|
|
1494 g_free (options_vars[i].listdata);
|
|
1495 }
|
|
1496 }
|
|
1497
|
|
1498 if (options_hash != NULL)
|
|
1499 g_hash_table_destroy (options_hash);
|
|
1500 }
|
|
1501
|
|
1502
|
|
1503 void
|
|
1504 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks)
|
|
1505 {
|
|
1506 gftp_bookmarks_var * tempentry, * delentry;
|
|
1507
|
|
1508 if (bookmarks == NULL)
|
|
1509 return;
|
|
1510
|
|
1511 tempentry = bookmarks;
|
|
1512 while (tempentry != NULL)
|
|
1513 {
|
|
1514 gftp_free_bookmark (tempentry);
|
227
|
1515
|
|
1516 if (tempentry->path != NULL)
|
|
1517 g_free (tempentry->path);
|
201
|
1518
|
|
1519 if (tempentry->children != NULL)
|
|
1520 {
|
|
1521 tempentry = tempentry->children;
|
|
1522 continue;
|
|
1523 }
|
|
1524
|
|
1525 while (tempentry->next == NULL && tempentry->prev != NULL)
|
|
1526 {
|
|
1527 delentry = tempentry;
|
|
1528 tempentry = tempentry->prev;
|
|
1529 g_free (delentry);
|
|
1530 }
|
|
1531
|
|
1532 delentry = tempentry;
|
|
1533 tempentry = tempentry->next;
|
|
1534 g_free (delentry);
|
|
1535 }
|
|
1536 }
|
|
1537
|
218
|
1538
|
|
1539 void
|
|
1540 gftp_free_proxy_hosts (GList * proxy_hosts)
|
|
1541 {
|
|
1542 gftp_proxy_hosts * hosts;
|
|
1543 GList * templist;
|
|
1544
|
|
1545 for (templist = proxy_hosts;
|
|
1546 templist != NULL;
|
|
1547 templist = templist->next)
|
|
1548 {
|
|
1549 hosts = templist->data;
|
|
1550
|
|
1551 if (hosts->domain)
|
|
1552 g_free (hosts->domain);
|
|
1553 g_free (hosts);
|
|
1554 }
|
|
1555
|
|
1556 g_list_free (proxy_hosts);
|
|
1557 }
|
|
1558
|
|
1559
|
|
1560 GList *
|
|
1561 gftp_copy_proxy_hosts (GList * proxy_hosts)
|
|
1562 {
|
|
1563 gftp_proxy_hosts * oldhosts, * newhosts;
|
|
1564 GList * templist, * new_proxy_hosts;
|
|
1565
|
|
1566 new_proxy_hosts = NULL;
|
|
1567
|
|
1568 if (proxy_hosts != NULL)
|
|
1569 {
|
|
1570 for (templist = proxy_hosts;
|
|
1571 templist != NULL;
|
|
1572 templist = templist->next)
|
|
1573 {
|
|
1574 oldhosts = templist->data;
|
|
1575
|
|
1576 newhosts = g_malloc0 (sizeof (*newhosts));
|
|
1577 memcpy (newhosts, oldhosts, sizeof (*newhosts));
|
|
1578
|
|
1579 if (oldhosts->domain)
|
|
1580 newhosts->domain = g_strdup (oldhosts->domain);
|
|
1581
|
|
1582 new_proxy_hosts = g_list_append (new_proxy_hosts, newhosts);
|
|
1583 }
|
|
1584 }
|
|
1585
|
|
1586 return (new_proxy_hosts);
|
|
1587 }
|
|
1588
|