Mercurial > gftp.yaz
annotate lib/misc.c @ 207:cf4098008615
2003-7-1 Brian Masney <masneyb@gftp.org>
* lib/httpcommon.h lib/rfc2068.c - more HTTP improvements. Mostly with
chunked file transfers. Also fixed several memory leaks. Added more
informative error messages to aid in debugging. Size parsing fixes
* lib/misc.c (base64_encode) - alphabet fix (+ wasn't a valid
character, it should have been /) (thanks to Holger Kiehl
<Holger.Kiehl@dwd.de> for pointing this out)
* lib/config_file.c lib/gftp.h - added copy function for options
variables. The functions gftp_config_file_copy_text(),
gftp_config_file_copy_ptr_contents() and gftp_config_file_copy_color()
were added
* lib/protocols.c - make sure the remote_charsets string is restored
properly after it is used
* src/gtk/bookmarks.c - remove memory leak comments, these are fixed
now
* src/gtk/transfer.c - removed start_file_transfers variable
* src/gtk/misc-gtk.c src/gtk/gftp-gtk.h lib/misc.c lib/gftp.h - moved
get_next_selection() to lib
* acinclude.h (AC_TYPE_SOCKLEN_T) - AC_DEFINE fix (from
Nathan Robertson <nathanr@nathanr.net>)
author | masneyb |
---|---|
date | Wed, 02 Jul 2003 01:44:57 +0000 |
parents | 9b2de8d3fafe |
children | 6de9a8280aa4 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* misc.c - general purpose 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 | |
102 | 20 static const char cvsid[] = "$Id$"; |
21 | |
1 | 22 #include "gftp.h" |
23 #include "options.h" | |
24 | |
87 | 25 /* FIXME - this isn't right for all locales. Use glib's printf instead */ |
1 | 26 char * |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
27 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 28 { |
29 char *frompos, *topos, src[50], *dest; | |
30 int len, num, rem, i; | |
31 | |
32 if (dest_str != NULL) | |
33 *dest_str = '\0'; | |
56 | 34 len = (number > 0 ? log10 (number) : 0) + 2; |
1 | 35 |
36 if (len <= 0) | |
37 { | |
38 if (dest_str != NULL) | |
39 strncpy (dest_str, "0", dest_len); | |
40 else | |
56 | 41 dest_str = g_strdup ("0"); |
1 | 42 return (dest_str); |
43 } | |
44 | |
45 len += len / 3; | |
46 if (dest_str != NULL && len > dest_len) | |
47 { | |
48 | |
49 for (i=0; i<dest_len - 1; i++) | |
50 dest_str[i] = 'X'; | |
51 dest_str[dest_len - 1] = '\0'; | |
52 return (dest_str); | |
53 } | |
54 | |
55 if (dest_str == NULL) | |
56 dest = g_malloc0 (len); | |
57 else | |
58 dest = dest_str; | |
59 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
60 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
61 g_snprintf (src, sizeof (src), "%lld", number); |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
62 #else |
1 | 63 g_snprintf (src, sizeof (src), "%ld", number); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
64 #endif |
1 | 65 |
66 num = strlen (src) / 3 - 1; | |
67 rem = strlen (src) % 3; | |
68 frompos = src; | |
69 topos = dest; | |
70 for (i = 0; i < rem; i++) | |
71 *topos++ = *frompos++; | |
72 | |
73 if (*frompos != '\0') | |
74 { | |
75 if (rem != 0) | |
76 *topos++ = ','; | |
77 while (num > 0) | |
78 { | |
79 for (i = 0; i < 3; i++) | |
80 *topos++ = *frompos++; | |
81 *topos++ = ','; | |
82 num--; | |
83 } | |
84 for (i = 0; i < 3; i++) | |
85 *topos++ = *frompos++; | |
86 } | |
87 *topos = '\0'; | |
88 return (dest); | |
89 } | |
90 | |
91 | |
92 char * | |
93 alltrim (char *str) | |
94 { | |
95 char *pos, *newpos; | |
96 int diff; | |
97 | |
98 pos = str + strlen (str) - 1; | |
87 | 99 while (pos >= str && (*pos == ' ' || *pos == '\t')) |
1 | 100 *pos-- = '\0'; |
101 | |
102 pos = str; | |
103 diff = 0; | |
104 while (*pos++ == ' ') | |
105 diff++; | |
106 | |
107 if (diff == 0) | |
108 return (str); | |
109 | |
110 pos = str + diff; | |
111 newpos = str; | |
112 while (*pos != '\0') | |
113 *newpos++ = *pos++; | |
114 *newpos = '\0'; | |
115 | |
116 return (str); | |
117 } | |
118 | |
119 | |
120 char * | |
121 expand_path (const char *src) | |
122 { | |
123 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
124 struct passwd *pw; | |
125 | |
126 pw = NULL; | |
124 | 127 str = g_strdup (src); |
1 | 128 |
129 if (*str == '~') | |
130 { | |
131 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
132 pw = getpwuid (geteuid ()); | |
133 else | |
134 { | |
135 if ((pos = strchr (str, '/')) != NULL) | |
136 *pos = '\0'; | |
137 | |
138 pw = getpwnam (str + 1); | |
139 | |
140 if (pos != NULL) | |
141 *pos = '/'; | |
142 } | |
143 } | |
144 | |
145 endpos = str; | |
146 newstr = NULL; | |
147 while ((pos = strchr (endpos, '/')) != NULL) | |
148 { | |
149 pos++; | |
150 while (*pos == '/') | |
151 pos++; | |
204 | 152 |
1 | 153 if ((endpos = strchr (pos, '/')) == NULL) |
154 endpos = pos + strlen (pos); | |
204 | 155 |
1 | 156 tempchar = *endpos; |
157 *endpos = '\0'; | |
204 | 158 |
1 | 159 if (strcmp (pos, "..") == 0) |
160 { | |
161 *(pos - 1) = '\0'; | |
162 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
163 *prevpos = '\0'; | |
164 } | |
165 else if (strcmp (pos, ".") != 0) | |
166 { | |
167 if (newstr == NULL) | |
56 | 168 newstr = g_strdup (pos - 1); |
1 | 169 else |
170 { | |
171 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
172 g_free (newstr); | |
173 newstr = tempstr; | |
174 } | |
175 } | |
204 | 176 |
1 | 177 *endpos = tempchar; |
178 if (*endpos == '\0') | |
179 break; | |
204 | 180 |
1 | 181 endpos = pos + 1; |
182 } | |
183 | |
204 | 184 if (endpos != NULL && *endpos != '\0' && newstr == NULL) |
185 { | |
186 if (strcmp (endpos, "..") == 0) | |
187 newstr = g_malloc0 (1); | |
188 else | |
189 newstr = g_strdup (endpos); | |
190 } | |
191 | |
1 | 192 if (newstr == NULL || *newstr == '\0') |
193 { | |
194 if (newstr != NULL) | |
195 g_free (newstr); | |
204 | 196 |
197 newstr = g_malloc0 (1); | |
1 | 198 } |
199 | |
200 g_free (str); | |
201 | |
202 if (pw != NULL) | |
203 { | |
204 if ((pos = strchr (newstr, '/')) == NULL) | |
124 | 205 str = g_strdup (pw->pw_dir); |
1 | 206 else |
207 str = g_strconcat (pw->pw_dir, pos, NULL); | |
208 | |
209 g_free (newstr); | |
210 newstr = str; | |
211 } | |
212 | |
213 return (newstr); | |
214 } | |
215 | |
216 | |
217 void | |
218 remove_double_slashes (char *string) | |
219 { | |
220 char *newpos, *oldpos; | |
87 | 221 size_t len; |
1 | 222 |
223 oldpos = newpos = string; | |
224 while (*oldpos != '\0') | |
225 { | |
226 *newpos++ = *oldpos++; | |
227 if (*oldpos == '\0') | |
228 break; | |
229 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
230 oldpos++; | |
231 } | |
232 *newpos = '\0'; | |
87 | 233 |
234 len = strlen (string); | |
235 if (string[len - 1] == '/') | |
236 string[len - 1] = '\0'; | |
1 | 237 } |
238 | |
239 | |
240 void | |
241 make_nonnull (char **str) | |
242 { | |
243 if (*str == NULL) | |
244 *str = g_malloc0 (1); | |
245 } | |
246 | |
247 | |
248 int | |
249 copyfile (char *source, char *dest) | |
250 { | |
58 | 251 int srcfd, destfd; |
1 | 252 char buf[8192]; |
58 | 253 ssize_t n; |
1 | 254 |
182 | 255 if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1) |
58 | 256 { |
257 printf (_("Error: Cannot open local file %s: %s\n"), | |
258 source, g_strerror (errno)); | |
259 exit (1); | |
260 } | |
1 | 261 |
182 | 262 if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
1 | 263 { |
58 | 264 printf (_("Error: Cannot open local file %s: %s\n"), |
265 dest, g_strerror (errno)); | |
266 close (srcfd); | |
267 exit (1); | |
1 | 268 } |
269 | |
58 | 270 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
271 { | |
272 if (write (destfd, buf, n) == -1) | |
273 { | |
274 printf (_("Error: Could not write to socket: %s\n"), | |
275 g_strerror (errno)); | |
276 exit (1); | |
277 } | |
278 } | |
1 | 279 |
58 | 280 if (n == -1) |
281 { | |
282 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
283 exit (1); | |
284 } | |
285 | |
87 | 286 if (close (srcfd) == -1) |
287 { | |
288 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
289 exit (1); | |
290 } | |
291 | |
292 if (close (destfd) == -1) | |
293 { | |
294 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
295 exit (1); | |
296 } | |
1 | 297 |
298 return (1); | |
299 } | |
300 | |
301 | |
87 | 302 /* FIXME - is there a replacement for this */ |
1 | 303 int |
304 gftp_match_filespec (char *filename, char *filespec) | |
305 { | |
306 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
307 size_t len, curlen; | |
308 | |
309 if (filename == NULL || *filename == '\0' || | |
310 filespec == NULL || *filespec == '\0') | |
311 return(1); | |
312 | |
313 filepos = filename; | |
314 wcpos = filespec; | |
315 while(1) | |
316 { | |
317 if (*wcpos == '\0') | |
318 return (1); | |
319 else if (*filepos == '\0') | |
320 return(0); | |
321 else if(*wcpos == '?') | |
322 { | |
323 wcpos++; | |
324 filepos++; | |
325 } | |
326 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
327 return(1); | |
328 else if(*wcpos == '*') | |
329 { | |
330 len = sizeof (search_str); | |
331 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
332 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
333 curlen++, *newpos++ = *pos++); | |
334 *newpos = '\0'; | |
335 | |
336 if ((filepos = strstr (filepos, search_str)) == NULL) | |
337 return(0); | |
338 wcpos += curlen + 1; | |
339 filepos += curlen; | |
340 } | |
341 else if(*wcpos++ != *filepos++) | |
342 return(0); | |
343 } | |
344 return (1); | |
345 } | |
346 | |
347 | |
348 int | |
349 gftp_parse_command_line (int *argc, char ***argv) | |
350 { | |
351 if (*argc > 1) | |
352 { | |
87 | 353 if (strcmp (argv[0][1], "--help") == 0 || |
354 strcmp (argv[0][1], "-h") == 0) | |
1 | 355 gftp_usage (); |
87 | 356 else if (strcmp (argv[0][1], "--version") == 0 || |
357 strcmp (argv[0][1], "-v") == 0) | |
1 | 358 { |
122 | 359 printf ("%s\n", gftp_version); |
1 | 360 exit (0); |
361 } | |
362 } | |
363 return (0); | |
364 } | |
365 | |
366 | |
367 void | |
368 gftp_usage (void) | |
369 { | |
162 | 370 printf (_("usage: gftp [[protocol://][user[:pass]@]site[:port][/directory]]\n")); |
1 | 371 exit (0); |
372 } | |
373 | |
374 | |
375 gint | |
376 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
377 { | |
378 return (strcmp ((char *) path1, (char *) path2) == 0); | |
379 } | |
380 | |
381 | |
382 guint | |
383 string_hash_function (gconstpointer key) | |
384 { | |
59 | 385 guint ret; |
386 int i; | |
387 | |
388 ret = 0; | |
87 | 389 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 390 ret += ((char *) key)[i]; |
391 | |
392 return (ret); | |
1 | 393 } |
394 | |
395 | |
396 void | |
397 free_file_list (GList * filelist) | |
398 { | |
399 gftp_file * tempfle; | |
400 GList * templist; | |
401 | |
402 templist = filelist; | |
403 while (templist != NULL) | |
404 { | |
405 tempfle = templist->data; | |
406 free_fdata (tempfle); | |
407 templist = templist->next; | |
408 } | |
409 g_list_free (filelist); | |
410 } | |
411 | |
412 | |
413 void | |
414 free_fdata (gftp_file * fle) | |
415 { | |
416 if (fle->file) | |
417 g_free (fle->file); | |
184 | 418 if (fle->utf8_file) |
419 g_free (fle->utf8_file); | |
1 | 420 if (fle->user) |
421 g_free (fle->user); | |
422 if (fle->group) | |
423 g_free (fle->group); | |
424 if (fle->attribs) | |
425 g_free (fle->attribs); | |
426 if (fle->destfile) | |
427 g_free (fle->destfile); | |
58 | 428 if (fle->fd > 0) |
102 | 429 close (fle->fd); |
1 | 430 g_free (fle); |
431 } | |
432 | |
433 | |
434 gftp_file * | |
435 copy_fdata (gftp_file * fle) | |
436 { | |
437 gftp_file * newfle; | |
438 | |
439 newfle = g_malloc0 (sizeof (*newfle)); | |
440 memcpy (newfle, fle, sizeof (*newfle)); | |
441 | |
442 if (fle->file) | |
87 | 443 newfle->file = g_strdup (fle->file); |
1 | 444 |
445 if (fle->user) | |
87 | 446 newfle->user = g_strdup (fle->user); |
1 | 447 |
448 if (fle->group) | |
87 | 449 newfle->group = g_strdup (fle->group); |
1 | 450 |
451 if (fle->attribs) | |
87 | 452 newfle->attribs = g_strdup (fle->attribs); |
1 | 453 |
454 if (fle->destfile) | |
87 | 455 newfle->destfile = g_strdup (fle->destfile); |
456 | |
1 | 457 return (newfle); |
458 } | |
459 | |
460 | |
461 int | |
462 compare_request (gftp_request * request1, gftp_request * request2, | |
463 int compare_dirs) | |
464 { | |
465 char *strarr[3][2]; | |
466 int i, ret; | |
467 | |
468 ret = 1; | |
87 | 469 if (request1->protonum == request2->protonum && |
1 | 470 request1->port == request2->port) |
471 { | |
472 strarr[0][0] = request1->hostname; | |
473 strarr[0][1] = request2->hostname; | |
474 strarr[1][0] = request1->username; | |
475 strarr[1][1] = request2->username; | |
476 if (compare_dirs) | |
477 { | |
478 strarr[2][0] = request1->directory; | |
479 strarr[2][1] = request2->directory; | |
480 } | |
481 else | |
482 strarr[2][0] = strarr[2][1] = ""; | |
483 | |
484 for (i = 0; i < 3; i++) | |
485 { | |
486 if ((strarr[i][0] && !strarr[i][1]) || | |
487 (!strarr[i][0] && strarr[i][1])) | |
488 { | |
489 ret = 0; | |
490 break; | |
491 } | |
492 | |
493 if (strarr[i][0] && strarr[i][1] && | |
494 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
495 { | |
496 ret = 0; | |
497 break; | |
498 } | |
499 } | |
500 } | |
501 else | |
502 ret = 0; | |
503 return (ret); | |
504 } | |
505 | |
506 | |
129 | 507 gftp_transfer * |
508 gftp_tdata_new (void) | |
509 { | |
510 gftp_transfer * tdata; | |
511 | |
512 tdata = g_malloc0 (sizeof (*tdata)); | |
168 | 513 g_static_mutex_init (&tdata->statmutex); |
514 g_static_mutex_init (&tdata->structmutex); | |
129 | 515 return (tdata); |
516 } | |
517 | |
518 | |
1 | 519 void |
520 free_tdata (gftp_transfer * tdata) | |
521 { | |
522 if (tdata->fromreq != NULL) | |
67 | 523 gftp_request_destroy (tdata->fromreq, 1); |
1 | 524 if (tdata->toreq != NULL) |
67 | 525 gftp_request_destroy (tdata->toreq, 1); |
1 | 526 free_file_list (tdata->files); |
527 g_free (tdata); | |
528 } | |
529 | |
530 | |
531 gftp_request * | |
151 | 532 copy_request (gftp_request * req, int copy_local_options) |
1 | 533 { |
534 gftp_request * newreq; | |
535 | |
66 | 536 newreq = gftp_request_new (); |
1 | 537 |
538 if (req->hostname) | |
56 | 539 newreq->hostname = g_strdup (req->hostname); |
1 | 540 if (req->username) |
56 | 541 newreq->username = g_strdup (req->username); |
1 | 542 if (req->password) |
56 | 543 newreq->password = g_strdup (req->password); |
1 | 544 if (req->account) |
56 | 545 newreq->account = g_strdup (req->account); |
1 | 546 if (req->directory) |
56 | 547 newreq->directory = g_strdup (req->directory); |
66 | 548 newreq->port = req->port; |
549 newreq->use_proxy = req->use_proxy; | |
550 newreq->logging_function = req->logging_function; | |
151 | 551 newreq->free_hostp = 0; |
552 newreq->hostp = req->hostp; | |
553 | |
554 if (copy_local_options) | |
199 | 555 gftp_copy_local_options (&newreq->local_options_vars, |
556 &newreq->local_options_hash, | |
557 req->local_options_vars, | |
558 req->num_local_options_vars); | |
1 | 559 |
173 | 560 if (req->init (newreq) < 0) |
561 { | |
562 gftp_request_destroy (newreq, 1); | |
563 return (NULL); | |
564 } | |
1 | 565 |
566 return (newreq); | |
567 } | |
568 | |
569 | |
16 | 570 static gint |
571 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
572 { | |
573 const gftp_file * f1, * f2; | |
574 | |
575 f1 = a; | |
576 f2 = b; | |
577 return (strcmp (f1->file, f2->file)); | |
578 } | |
579 | |
580 | |
581 static gint | |
582 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
583 { | |
584 const gftp_file * f1, * f2; | |
585 gint ret; | |
586 | |
587 f1 = a; | |
588 f2 = b; | |
589 ret = strcmp (f1->file, f2->file); | |
84 | 590 return (ret * -1); |
16 | 591 } |
592 | |
593 | |
594 static gint | |
595 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
596 { | |
597 const gftp_file * f1, * f2; | |
598 | |
599 f1 = a; | |
600 f2 = b; | |
601 return (strcmp (f1->user, f2->user)); | |
602 } | |
603 | |
604 | |
605 static gint | |
606 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
607 { | |
608 const gftp_file * f1, * f2; | |
609 gint ret; | |
610 | |
611 f1 = a; | |
612 f2 = b; | |
613 ret = strcmp (f1->user, f2->user); | |
84 | 614 return (ret * -1); |
16 | 615 } |
616 | |
617 | |
618 static gint | |
619 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
620 { | |
621 const gftp_file * f1, * f2; | |
622 | |
623 f1 = a; | |
624 f2 = b; | |
625 return (strcmp (f1->group, f2->group)); | |
626 } | |
627 | |
628 | |
629 static gint | |
630 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
631 { | |
632 const gftp_file * f1, * f2; | |
633 gint ret; | |
634 | |
635 f1 = a; | |
636 f2 = b; | |
637 ret = strcmp (f1->group, f2->group); | |
84 | 638 return (ret * -1); |
16 | 639 } |
640 | |
641 | |
642 static gint | |
643 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
644 { | |
645 const gftp_file * f1, * f2; | |
646 | |
647 f1 = a; | |
648 f2 = b; | |
649 return (strcmp (f1->attribs, f2->attribs)); | |
650 } | |
651 | |
652 | |
653 static gint | |
654 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
655 { | |
656 const gftp_file * f1, * f2; | |
657 gint ret; | |
658 | |
659 f1 = a; | |
660 f2 = b; | |
661 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 662 return (ret * -1); |
16 | 663 } |
664 | |
665 | |
666 static gint | |
667 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
668 { | |
669 const gftp_file * f1, * f2; | |
670 | |
671 f1 = a; | |
672 f2 = b; | |
673 if (f1->size < f2->size) | |
674 return (-1); | |
675 else if (f1->size == f2->size) | |
676 return (0); | |
677 else | |
678 return (1); | |
679 } | |
680 | |
681 | |
682 static gint | |
683 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
684 { | |
685 const gftp_file * f1, * f2; | |
686 | |
687 f1 = a; | |
688 f2 = b; | |
689 if (f1->size < f2->size) | |
690 return (1); | |
691 else if (f1->size == f2->size) | |
692 return (0); | |
693 else | |
694 return (-1); | |
695 } | |
696 | |
697 | |
698 static gint | |
699 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
700 { | |
701 const gftp_file * f1, * f2; | |
702 | |
703 f1 = a; | |
704 f2 = b; | |
705 if (f1->datetime < f2->datetime) | |
706 return (-1); | |
707 else if (f1->datetime == f2->datetime) | |
708 return (0); | |
709 else | |
710 return (1); | |
711 } | |
712 | |
713 | |
714 static gint | |
715 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
716 { | |
717 const gftp_file * f1, * f2; | |
718 | |
719 f1 = a; | |
720 f2 = b; | |
721 if (f1->datetime < f2->datetime) | |
722 return (1); | |
723 else if (f1->datetime == f2->datetime) | |
724 return (0); | |
725 else | |
726 return (-1); | |
727 } | |
728 | |
729 | |
730 GList * | |
731 gftp_sort_filelist (GList * filelist, int column, int asds) | |
732 { | |
733 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
734 GCompareFunc sortfunc; | |
735 gftp_file * tempfle; | |
122 | 736 int sort_dirs_first; |
16 | 737 |
738 files = dirs = dotdot = NULL; | |
739 | |
740 if (column == GFTP_SORT_COL_FILE) | |
741 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
742 else if (column == GFTP_SORT_COL_SIZE) | |
743 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
744 else if (column == GFTP_SORT_COL_USER) | |
745 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
746 else if (column == GFTP_SORT_COL_GROUP) | |
747 sortfunc = asds ? | |
748 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
749 else if (column == GFTP_SORT_COL_DATETIME) | |
750 sortfunc = asds ? | |
122 | 751 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
752 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 753 sortfunc = asds ? |
754 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 755 else /* Don't sort */ |
756 return (filelist); | |
757 | |
758 sort_dirs_first = 1; | |
759 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 760 |
761 for (tempitem = filelist; tempitem != NULL; ) | |
762 { | |
763 tempfle = tempitem->data; | |
764 insitem = tempitem; | |
765 tempitem = tempitem->next; | |
766 insitem->next = NULL; | |
767 | |
768 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
769 dotdot = insitem; | |
770 else if (sort_dirs_first && tempfle->isdir) | |
771 { | |
772 insitem->next = dirs; | |
773 dirs = insitem; | |
774 } | |
775 else | |
776 { | |
777 insitem->next = files; | |
778 files = insitem; | |
779 } | |
780 } | |
781 | |
782 if (dirs != NULL) | |
783 dirs = g_list_sort (dirs, sortfunc); | |
784 if (files != NULL) | |
785 files = g_list_sort (files, sortfunc); | |
786 | |
787 filelist = dotdot; | |
788 | |
789 if (filelist == NULL) | |
790 filelist = dirs; | |
791 else | |
792 filelist = g_list_concat (filelist, dirs); | |
793 | |
794 if (filelist == NULL) | |
795 filelist = files; | |
796 else | |
797 filelist = g_list_concat (filelist, files); | |
798 | |
39 | 799 /* I haven't check this, but I'm pretty sure some older versions of glib |
800 had a bug that the prev pointer wasn't being sent to NULL */ | |
801 filelist->prev = NULL; | |
16 | 802 return (filelist); |
803 } | |
804 | |
125 | 805 |
806 mode_t | |
807 gftp_parse_attribs (char *attribs) | |
808 { | |
809 mode_t mode; | |
810 int cur; | |
811 | |
812 cur = 0; | |
813 if (attribs[1] == 'r') | |
814 cur += 4; | |
815 if (attribs[2] == 'w') | |
816 cur += 2; | |
817 if (attribs[3] == 'x' || | |
818 attribs[3] == 's') | |
819 cur += 1; | |
820 mode = cur; | |
821 | |
822 cur = 0; | |
823 if (attribs[4] == 'r') | |
824 cur += 4; | |
825 if (attribs[5] == 'w') | |
826 cur += 2; | |
827 if (attribs[6] == 'x' || | |
828 attribs[6] == 's') | |
829 cur += 1; | |
830 mode = (mode * 10) + cur; | |
831 | |
832 cur = 0; | |
833 if (attribs[7] == 'r') | |
834 cur += 4; | |
835 if (attribs[8] == 'w') | |
836 cur += 2; | |
837 if (attribs[9] == 'x' || | |
838 attribs[9] == 's') | |
839 cur += 1; | |
840 mode = (mode * 10) + cur; | |
841 | |
842 return (mode); | |
843 } | |
844 | |
845 | |
131 | 846 char * |
847 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
848 { | |
849 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
850 struct tm *lt; | |
851 time_t t; | |
852 | |
853 lt = localtime (&fle->datetime); | |
854 | |
855 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
856 fle->group); | |
857 | |
858 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
859 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
860 else | |
861 { | |
862 #if defined (_LARGEFILE_SOURCE) | |
863 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
864 #else | |
865 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
866 #endif | |
867 } | |
868 | |
869 time (&t); | |
870 | |
871 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
872 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
873 else | |
874 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
875 | |
876 if (file_prefixstr == NULL) | |
877 file_prefixstr = ""; | |
878 if (file_suffixstr == NULL) | |
879 file_suffixstr = ""; | |
880 | |
881 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 882 file_prefixstr, |
883 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
884 file_suffixstr); | |
131 | 885 |
886 g_free (tempstr1); | |
887 g_free (tempstr2); | |
888 | |
889 return (ret); | |
890 } | |
891 | |
892 | |
125 | 893 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
894 | |
895 struct hostent * | |
896 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
897 { | |
898 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
899 struct hostent *hent; | |
900 | |
901 if (g_thread_supported ()) | |
902 g_static_mutex_lock (&hostfunclock); | |
903 | |
904 if ((hent = gethostbyname (name)) == NULL) | |
905 { | |
906 if (h_errnop) | |
907 *h_errnop = h_errno; | |
908 } | |
909 else | |
910 { | |
911 *result_buf = *hent; | |
912 hent = result_buf; | |
913 } | |
914 | |
915 if (g_thread_supported ()) | |
916 g_static_mutex_unlock (&hostfunclock); | |
917 | |
918 return (hent); | |
919 } | |
920 | |
921 #endif /* !HAVE_GETADDRINFO */ | |
922 | |
923 struct servent * | |
924 r_getservbyname (const char *name, const char *proto, | |
925 struct servent *result_buf, int *h_errnop) | |
926 { | |
927 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
928 struct servent *sent; | |
929 | |
930 if (g_thread_supported ()) | |
931 g_static_mutex_lock (&servfunclock); | |
932 | |
933 if ((sent = getservbyname (name, proto)) == NULL) | |
934 { | |
935 if (h_errnop) | |
936 *h_errnop = h_errno; | |
937 } | |
938 else | |
939 { | |
940 *result_buf = *sent; | |
941 sent = result_buf; | |
942 } | |
943 | |
944 if (g_thread_supported ()) | |
945 g_static_mutex_unlock (&servfunclock); | |
946 return (sent); | |
947 } | |
948 | |
168 | 949 |
950 char * | |
951 base64_encode (char *str) | |
952 { | |
953 | |
954 /* The standard to Base64 encoding can be found in RFC2045 */ | |
955 | |
956 char *newstr, *newpos, *fillpos, *pos; | |
957 unsigned char table[64], encode[3]; | |
958 int i, num; | |
959 | |
960 for (i = 0; i < 26; i++) | |
961 { | |
962 table[i] = 'A' + i; | |
963 table[i + 26] = 'a' + i; | |
964 } | |
965 | |
966 for (i = 0; i < 10; i++) | |
967 table[i + 52] = '0' + i; | |
968 | |
969 table[62] = '+'; | |
207 | 970 table[63] = '/'; |
168 | 971 |
972 num = strlen (str) / 3; | |
973 if (strlen (str) % 3 > 0) | |
974 num++; | |
975 newstr = g_malloc (num * 4 + 1); | |
976 newstr[num * 4] = '\0'; | |
977 newpos = newstr; | |
978 | |
979 pos = str; | |
980 while (*pos != '\0') | |
981 { | |
982 memset (encode, 0, sizeof (encode)); | |
983 for (i = 0; i < 3 && *pos != '\0'; i++) | |
984 encode[i] = *pos++; | |
985 | |
986 fillpos = newpos; | |
987 *newpos++ = table[encode[0] >> 2]; | |
988 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
989 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
990 *newpos++ = table[encode[2] & 0x3F]; | |
991 while (i < 3) | |
992 fillpos[++i] = '='; | |
993 } | |
994 return (newstr); | |
995 } | |
996 | |
199 | 997 |
998 void | |
999 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1000 { | |
1001 if (entry->hostname) | |
1002 g_free (entry->hostname); | |
1003 if (entry->remote_dir) | |
1004 g_free (entry->remote_dir); | |
1005 if (entry->local_dir) | |
1006 g_free (entry->local_dir); | |
1007 if (entry->user) | |
1008 g_free (entry->user); | |
1009 if (entry->pass) | |
1010 g_free (entry->pass); | |
1011 if (entry->acct) | |
1012 g_free (entry->acct); | |
1013 if (entry->protocol) | |
1014 g_free (entry->protocol); | |
1015 | |
1016 if (entry->local_options_vars != NULL) | |
1017 { | |
201 | 1018 gftp_config_free_options (entry->local_options_vars, |
1019 entry->local_options_hash, | |
1020 entry->num_local_options_vars); | |
1021 | |
199 | 1022 entry->local_options_vars = NULL; |
201 | 1023 entry->local_options_hash = NULL; |
199 | 1024 entry->num_local_options_vars = 0; |
1025 } | |
1026 } | |
1027 | |
201 | 1028 |
1029 void | |
1030 gftp_shutdown (void) | |
1031 { | |
203 | 1032 #ifdef WITH_DMALLOC |
201 | 1033 gftp_config_vars * cv; |
1034 GList * templist; | |
203 | 1035 #endif |
201 | 1036 |
1037 if (gftp_logfd != NULL) | |
1038 fclose (gftp_logfd); | |
1039 | |
1040 gftp_clear_cache_files (); | |
1041 | |
1042 if (gftp_configuration_changed) | |
1043 gftp_write_config_file (); | |
1044 | |
1045 #ifdef WITH_DMALLOC | |
1046 if (gftp_global_options_htable != NULL) | |
1047 g_hash_table_destroy (gftp_global_options_htable); | |
1048 | |
1049 if (gftp_config_list_htable != NULL) | |
1050 g_hash_table_destroy (gftp_config_list_htable); | |
1051 | |
1052 if (gftp_bookmarks_htable != NULL) | |
1053 g_hash_table_destroy (gftp_bookmarks_htable); | |
1054 | |
1055 for (templist = gftp_options_list; | |
1056 templist != NULL; | |
1057 templist = templist->next) | |
1058 { | |
1059 cv = templist->data; | |
1060 gftp_config_free_options (cv, NULL, -1); | |
1061 } | |
1062 | |
1063 gftp_bookmarks_destroy (gftp_bookmarks); | |
1064 | |
1065 dmalloc_shutdown (); | |
1066 #endif | |
1067 | |
1068 exit (0); | |
1069 } | |
1070 | |
207 | 1071 |
1072 GList * | |
1073 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1074 { | |
1075 gftp_file * tempfle; | |
1076 int i, newpos; | |
1077 | |
1078 newpos = GPOINTER_TO_INT (selection->data); | |
1079 i = *curnum - newpos; | |
1080 | |
1081 if (i < 0) | |
1082 { | |
1083 while (i != 0) | |
1084 { | |
1085 tempfle = (*list)->data; | |
1086 if (tempfle->shown) | |
1087 { | |
1088 ++*curnum; | |
1089 i++; | |
1090 } | |
1091 *list = (*list)->next; | |
1092 } | |
1093 } | |
1094 else if (i > 0) | |
1095 { | |
1096 while (i != 0) | |
1097 { | |
1098 tempfle = (*list)->data; | |
1099 if (tempfle->shown) | |
1100 { | |
1101 --*curnum; | |
1102 i--; | |
1103 } | |
1104 *list = (*list)->prev; | |
1105 } | |
1106 } | |
1107 | |
1108 tempfle = (*list)->data; | |
1109 while ((*list)->next && !tempfle->shown) | |
1110 { | |
1111 *list = (*list)->next; | |
1112 tempfle = (*list)->data; | |
1113 } | |
1114 return (selection->next); | |
1115 } | |
1116 | |
1117 |