Mercurial > gftp.yaz
annotate lib/misc.c @ 598:fa0838b22b14
2004-10-29 Brian Masney <masneyb@gftp.org>
* lib/gftp.h lib/misc.c lib/protocols.c lib/rfc2068.c lib/rfc959.c
lib/sshv2.c src/gtk/dnd.c src/gtk/view_dialog.c src/uicommon/gftpui.c
src/uicommon/gftpuicallbacks.c - removed free_fdata(). Added
free_it parameter to gftp_file_destroy()
author | masneyb |
---|---|
date | Fri, 29 Oct 2004 20:05:32 +0000 |
parents | 7f54d0c0edbc |
children | d97e985f5dbe |
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 | |
20 #include "gftp.h" | |
21 #include "options.h" | |
516 | 22 static const char cvsid[] = "$Id$"; |
1 | 23 |
328 | 24 #ifdef HAVE_INTL_PRINTF |
289 | 25 |
26 char * | |
27 insert_commas (off_t number, char *dest_str, size_t dest_len) | |
28 { | |
29 if (dest_str != NULL) | |
532 | 30 g_snprintf (dest_str, dest_len, GFTP_OFF_T_INTL_PRINTF_MOD, number); |
289 | 31 else |
532 | 32 dest_str = g_strdup_printf (GFTP_OFF_T_INTL_PRINTF_MOD, number); |
289 | 33 |
34 return (dest_str); | |
35 } | |
36 | |
37 #else | |
38 | |
1 | 39 char * |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
40 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 41 { |
42 char *frompos, *topos, src[50], *dest; | |
460 | 43 size_t num, rem, srclen; |
44 int len, i; | |
1 | 45 |
516 | 46 g_snprintf (src, sizeof (src), GFTP_OFF_T_PRINTF_MOD, number); |
220 | 47 |
1 | 48 if (dest_str != NULL) |
49 *dest_str = '\0'; | |
50 | |
220 | 51 len = strlen (src); |
52 if (len == 0) | |
1 | 53 { |
54 if (dest_str != NULL) | |
249 | 55 { |
56 strncpy (dest_str, "0", dest_len); | |
57 dest_str[dest_len - 1] = '\0'; | |
58 } | |
1 | 59 else |
56 | 60 dest_str = g_strdup ("0"); |
1 | 61 return (dest_str); |
62 } | |
63 | |
64 len += len / 3; | |
220 | 65 |
1 | 66 if (dest_str != NULL && len > dest_len) |
67 { | |
68 | |
69 for (i=0; i<dest_len - 1; i++) | |
70 dest_str[i] = 'X'; | |
71 dest_str[dest_len - 1] = '\0'; | |
72 return (dest_str); | |
73 } | |
74 | |
75 if (dest_str == NULL) | |
76 dest = g_malloc0 (len); | |
77 else | |
78 dest = dest_str; | |
79 | |
460 | 80 srclen = strlen (src); |
81 num = srclen / 3 - 1; | |
82 rem = srclen % 3; | |
83 | |
1 | 84 frompos = src; |
85 topos = dest; | |
86 for (i = 0; i < rem; i++) | |
87 *topos++ = *frompos++; | |
88 | |
89 if (*frompos != '\0') | |
90 { | |
91 if (rem != 0) | |
92 *topos++ = ','; | |
93 while (num > 0) | |
94 { | |
95 for (i = 0; i < 3; i++) | |
96 *topos++ = *frompos++; | |
97 *topos++ = ','; | |
98 num--; | |
99 } | |
100 for (i = 0; i < 3; i++) | |
101 *topos++ = *frompos++; | |
102 } | |
103 *topos = '\0'; | |
104 return (dest); | |
105 } | |
106 | |
289 | 107 #endif |
1 | 108 |
109 char * | |
110 alltrim (char *str) | |
111 { | |
112 char *pos, *newpos; | |
113 int diff; | |
114 | |
115 pos = str + strlen (str) - 1; | |
87 | 116 while (pos >= str && (*pos == ' ' || *pos == '\t')) |
1 | 117 *pos-- = '\0'; |
118 | |
119 pos = str; | |
120 diff = 0; | |
121 while (*pos++ == ' ') | |
122 diff++; | |
123 | |
124 if (diff == 0) | |
125 return (str); | |
126 | |
127 pos = str + diff; | |
128 newpos = str; | |
129 while (*pos != '\0') | |
130 *newpos++ = *pos++; | |
131 *newpos = '\0'; | |
132 | |
133 return (str); | |
134 } | |
135 | |
136 | |
137 char * | |
555 | 138 gftp_expand_path (gftp_request * request, const char *src) |
1 | 139 { |
307 | 140 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, *ntoken, |
141 tempchar; | |
1 | 142 struct passwd *pw; |
143 | |
144 pw = NULL; | |
124 | 145 str = g_strdup (src); |
1 | 146 |
147 if (*str == '~') | |
148 { | |
149 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
150 pw = getpwuid (geteuid ()); | |
151 else | |
152 { | |
153 if ((pos = strchr (str, '/')) != NULL) | |
154 *pos = '\0'; | |
155 | |
156 pw = getpwnam (str + 1); | |
157 | |
158 if (pos != NULL) | |
159 *pos = '/'; | |
160 } | |
161 } | |
162 | |
163 endpos = str; | |
164 newstr = NULL; | |
165 while ((pos = strchr (endpos, '/')) != NULL) | |
166 { | |
167 pos++; | |
204 | 168 |
307 | 169 for (ntoken = pos; *ntoken == '/'; ntoken++); |
170 | |
171 if ((endpos = strchr (ntoken, '/')) == NULL) | |
1 | 172 endpos = pos + strlen (pos); |
204 | 173 |
1 | 174 tempchar = *endpos; |
175 *endpos = '\0'; | |
204 | 176 |
307 | 177 if (strcmp (ntoken, "..") == 0) |
1 | 178 { |
179 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
307 | 180 { |
181 *prevpos = '\0'; | |
182 if (*newstr == '\0') | |
183 { | |
184 g_free (newstr); | |
185 newstr = NULL; | |
186 } | |
187 } | |
1 | 188 } |
307 | 189 else if (strcmp (ntoken, ".") != 0) |
1 | 190 { |
191 if (newstr == NULL) | |
56 | 192 newstr = g_strdup (pos - 1); |
1 | 193 else |
194 { | |
195 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
196 g_free (newstr); | |
197 newstr = tempstr; | |
198 } | |
199 } | |
204 | 200 |
1 | 201 *endpos = tempchar; |
202 if (*endpos == '\0') | |
203 break; | |
204 | 204 |
1 | 205 endpos = pos + 1; |
206 } | |
207 | |
204 | 208 if (endpos != NULL && *endpos != '\0' && newstr == NULL) |
209 { | |
210 if (strcmp (endpos, "..") == 0) | |
307 | 211 newstr = g_strdup ("/"); |
204 | 212 else |
213 newstr = g_strdup (endpos); | |
214 } | |
215 | |
1 | 216 if (newstr == NULL || *newstr == '\0') |
217 { | |
218 if (newstr != NULL) | |
219 g_free (newstr); | |
204 | 220 |
247 | 221 newstr = g_strdup ("/"); |
1 | 222 } |
223 | |
224 g_free (str); | |
225 | |
226 if (pw != NULL) | |
227 { | |
228 if ((pos = strchr (newstr, '/')) == NULL) | |
124 | 229 str = g_strdup (pw->pw_dir); |
1 | 230 else |
555 | 231 str = gftp_build_path (request, pw->pw_dir, pos, NULL); |
1 | 232 |
233 g_free (newstr); | |
234 newstr = str; | |
235 } | |
236 | |
237 return (newstr); | |
238 } | |
239 | |
240 | |
241 void | |
242 make_nonnull (char **str) | |
243 { | |
244 if (*str == NULL) | |
245 *str = g_malloc0 (1); | |
246 } | |
247 | |
248 | |
87 | 249 /* FIXME - is there a replacement for this */ |
1 | 250 int |
374 | 251 gftp_match_filespec (const char *filename, const char *filespec) |
1 | 252 { |
374 | 253 const char *filepos, *wcpos, *pos; |
254 char search_str[20], *newpos; | |
1 | 255 size_t len, curlen; |
256 | |
257 if (filename == NULL || *filename == '\0' || | |
258 filespec == NULL || *filespec == '\0') | |
259 return(1); | |
260 | |
261 filepos = filename; | |
262 wcpos = filespec; | |
263 while(1) | |
264 { | |
265 if (*wcpos == '\0') | |
266 return (1); | |
267 else if (*filepos == '\0') | |
268 return(0); | |
269 else if(*wcpos == '?') | |
270 { | |
271 wcpos++; | |
272 filepos++; | |
273 } | |
274 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
275 return(1); | |
276 else if(*wcpos == '*') | |
277 { | |
278 len = sizeof (search_str); | |
279 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
280 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
281 curlen++, *newpos++ = *pos++); | |
282 *newpos = '\0'; | |
283 | |
284 if ((filepos = strstr (filepos, search_str)) == NULL) | |
285 return(0); | |
286 wcpos += curlen + 1; | |
287 filepos += curlen; | |
288 } | |
289 else if(*wcpos++ != *filepos++) | |
290 return(0); | |
291 } | |
292 return (1); | |
293 } | |
294 | |
295 | |
243 | 296 static void |
297 gftp_info (void) | |
298 { | |
260 | 299 int i; |
300 | |
243 | 301 printf ("%s\n", gftp_version); |
302 | |
307 | 303 #ifdef _REENTRANT |
304 printf ("#define _REENTRANT\n"); | |
305 #endif | |
306 | |
289 | 307 #ifdef _GNU_SOURCE |
308 printf ("#define _GNU_SOURCE\n"); | |
309 #endif | |
310 | |
243 | 311 #ifdef _LARGEFILE_SOURCE |
260 | 312 printf ("#define _LARGEFILE_SOURCE\n"); |
243 | 313 #endif |
314 | |
315 #ifdef _FILE_OFFSET_BITS | |
316 printf ("#define _FILE_OFFSET_BITS %d\n", _FILE_OFFSET_BITS); | |
317 #endif | |
318 | |
319 printf ("sizeof (off_t) = %d\n", sizeof (off_t)); | |
320 | |
321 #ifdef HAVE_GETADDRINFO | |
322 printf ("#define HAVE_GETADDRINFO\n"); | |
323 #endif | |
324 | |
325 #ifdef HAVE_GAI_STRERROR | |
326 printf ("#define HAVE_GAI_STRERROR\n"); | |
327 #endif | |
328 | |
329 #ifdef HAVE_GETDTABLESIZE | |
330 printf ("#define HAVE_GETDTABLESIZE\n"); | |
331 #endif | |
332 | |
333 #ifdef G_HAVE_GINT64 | |
334 printf ("#define G_HAVE_GINT64\n"); | |
335 #endif | |
336 | |
337 #ifdef HAVE_LIBREADLINE | |
338 printf ("#define HAVE_LIBREADLINE\n"); | |
339 #endif | |
340 | |
341 #ifdef ENABLE_NLS | |
342 printf ("#define ENABLE_NLS\n"); | |
343 #endif | |
344 | |
345 #ifdef HAVE_GETTEXT | |
346 printf ("#define HAVE_GETTEXT\n"); | |
347 #endif | |
348 | |
328 | 349 #ifdef HAVE_INTL_PRINTF |
350 printf ("#define HAVE_INTL_PRINTF\n"); | |
351 #endif | |
352 | |
243 | 353 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, |
354 GLIB_MICRO_VERSION); | |
355 | |
356 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
357 | |
358 #ifdef USE_SSL | |
359 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
360 #endif | |
260 | 361 |
362 printf ("Enabled protocols: "); | |
363 for (i=0; gftp_protocols[i].name != NULL; i++) | |
364 { | |
365 printf ("%s ", gftp_protocols[i].name); | |
366 } | |
367 printf ("\n"); | |
243 | 368 } |
369 | |
370 | |
1 | 371 int |
372 gftp_parse_command_line (int *argc, char ***argv) | |
373 { | |
374 if (*argc > 1) | |
375 { | |
87 | 376 if (strcmp (argv[0][1], "--help") == 0 || |
377 strcmp (argv[0][1], "-h") == 0) | |
349 | 378 { |
379 gftp_usage (); | |
380 exit (0); | |
381 } | |
87 | 382 else if (strcmp (argv[0][1], "--version") == 0 || |
383 strcmp (argv[0][1], "-v") == 0) | |
1 | 384 { |
349 | 385 printf ("%s\n", gftp_version); |
386 exit (0); | |
1 | 387 } |
243 | 388 else if (strcmp (argv[0][1], "--info") == 0) |
389 { | |
390 gftp_info (); | |
349 | 391 exit (0); |
243 | 392 } |
1 | 393 } |
394 return (0); | |
395 } | |
396 | |
397 | |
398 void | |
399 gftp_usage (void) | |
400 { | |
356 | 401 printf (_("usage: gftp " GFTP_URL_USAGE "\n")); |
1 | 402 exit (0); |
403 } | |
404 | |
405 | |
406 gint | |
407 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
408 { | |
409 return (strcmp ((char *) path1, (char *) path2) == 0); | |
410 } | |
411 | |
412 | |
413 guint | |
414 string_hash_function (gconstpointer key) | |
415 { | |
59 | 416 guint ret; |
417 int i; | |
418 | |
419 ret = 0; | |
87 | 420 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 421 ret += ((char *) key)[i]; |
422 | |
423 return (ret); | |
1 | 424 } |
425 | |
426 | |
427 void | |
428 free_file_list (GList * filelist) | |
429 { | |
430 gftp_file * tempfle; | |
431 GList * templist; | |
432 | |
433 templist = filelist; | |
434 while (templist != NULL) | |
435 { | |
436 tempfle = templist->data; | |
598 | 437 gftp_file_destroy (tempfle, 1); |
1 | 438 templist = templist->next; |
439 } | |
440 g_list_free (filelist); | |
441 } | |
442 | |
443 | |
444 gftp_file * | |
445 copy_fdata (gftp_file * fle) | |
446 { | |
447 gftp_file * newfle; | |
448 | |
449 newfle = g_malloc0 (sizeof (*newfle)); | |
450 memcpy (newfle, fle, sizeof (*newfle)); | |
451 | |
452 if (fle->file) | |
87 | 453 newfle->file = g_strdup (fle->file); |
1 | 454 |
243 | 455 if (fle->utf8_file) |
456 newfle->utf8_file = g_strdup (fle->utf8_file); | |
457 | |
1 | 458 if (fle->user) |
87 | 459 newfle->user = g_strdup (fle->user); |
1 | 460 |
461 if (fle->group) | |
87 | 462 newfle->group = g_strdup (fle->group); |
1 | 463 |
464 if (fle->destfile) | |
87 | 465 newfle->destfile = g_strdup (fle->destfile); |
466 | |
1 | 467 return (newfle); |
468 } | |
469 | |
470 | |
471 int | |
472 compare_request (gftp_request * request1, gftp_request * request2, | |
473 int compare_dirs) | |
474 { | |
475 char *strarr[3][2]; | |
476 int i, ret; | |
477 | |
478 ret = 1; | |
87 | 479 if (request1->protonum == request2->protonum && |
1 | 480 request1->port == request2->port) |
481 { | |
482 strarr[0][0] = request1->hostname; | |
483 strarr[0][1] = request2->hostname; | |
484 strarr[1][0] = request1->username; | |
485 strarr[1][1] = request2->username; | |
486 if (compare_dirs) | |
487 { | |
488 strarr[2][0] = request1->directory; | |
489 strarr[2][1] = request2->directory; | |
490 } | |
491 else | |
492 strarr[2][0] = strarr[2][1] = ""; | |
493 | |
494 for (i = 0; i < 3; i++) | |
495 { | |
496 if ((strarr[i][0] && !strarr[i][1]) || | |
497 (!strarr[i][0] && strarr[i][1])) | |
498 { | |
499 ret = 0; | |
500 break; | |
501 } | |
502 | |
503 if (strarr[i][0] && strarr[i][1] && | |
504 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
505 { | |
506 ret = 0; | |
507 break; | |
508 } | |
509 } | |
510 } | |
511 else | |
512 ret = 0; | |
513 return (ret); | |
514 } | |
515 | |
516 | |
129 | 517 gftp_transfer * |
518 gftp_tdata_new (void) | |
519 { | |
227 | 520 #if GLIB_MAJOR_VERSION == 1 |
521 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
522 #endif | |
129 | 523 gftp_transfer * tdata; |
524 | |
525 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 526 |
527 #if GLIB_MAJOR_VERSION == 1 | |
528 tdata->statmutex = init_mutex; | |
529 tdata->structmutex = init_mutex; | |
530 #else | |
168 | 531 g_static_mutex_init (&tdata->statmutex); |
532 g_static_mutex_init (&tdata->structmutex); | |
227 | 533 #endif |
534 | |
129 | 535 return (tdata); |
536 } | |
537 | |
538 | |
1 | 539 void |
540 free_tdata (gftp_transfer * tdata) | |
541 { | |
542 if (tdata->fromreq != NULL) | |
67 | 543 gftp_request_destroy (tdata->fromreq, 1); |
1 | 544 if (tdata->toreq != NULL) |
67 | 545 gftp_request_destroy (tdata->toreq, 1); |
1 | 546 free_file_list (tdata->files); |
547 g_free (tdata); | |
548 } | |
549 | |
550 | |
551 gftp_request * | |
368 | 552 gftp_copy_request (gftp_request * req) |
1 | 553 { |
554 gftp_request * newreq; | |
555 | |
66 | 556 newreq = gftp_request_new (); |
1 | 557 |
558 if (req->hostname) | |
56 | 559 newreq->hostname = g_strdup (req->hostname); |
1 | 560 if (req->username) |
56 | 561 newreq->username = g_strdup (req->username); |
1 | 562 if (req->password) |
56 | 563 newreq->password = g_strdup (req->password); |
1 | 564 if (req->account) |
56 | 565 newreq->account = g_strdup (req->account); |
1 | 566 if (req->directory) |
56 | 567 newreq->directory = g_strdup (req->directory); |
66 | 568 newreq->port = req->port; |
569 newreq->use_proxy = req->use_proxy; | |
570 newreq->logging_function = req->logging_function; | |
547 | 571 newreq->ai_family = req->ai_family; |
151 | 572 newreq->free_hostp = 0; |
296 | 573 newreq->hostp = NULL; |
151 | 574 |
368 | 575 gftp_copy_local_options (&newreq->local_options_vars, |
576 &newreq->local_options_hash, | |
429 | 577 &newreq->num_local_options_vars, |
368 | 578 req->local_options_vars, |
579 req->num_local_options_vars); | |
1 | 580 |
451 | 581 if (req->init != NULL && req->init (newreq) < 0) |
173 | 582 { |
583 gftp_request_destroy (newreq, 1); | |
584 return (NULL); | |
585 } | |
1 | 586 |
309 | 587 gftp_copy_param_options (newreq, req); |
588 | |
1 | 589 return (newreq); |
590 } | |
591 | |
592 | |
16 | 593 static gint |
594 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
595 { | |
596 const gftp_file * f1, * f2; | |
597 | |
598 f1 = a; | |
599 f2 = b; | |
535 | 600 return (strcasecmp (f1->file, f2->file)); |
16 | 601 } |
602 | |
603 | |
604 static gint | |
605 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
606 { | |
607 const gftp_file * f1, * f2; | |
608 gint ret; | |
609 | |
610 f1 = a; | |
611 f2 = b; | |
535 | 612 ret = strcasecmp (f1->file, f2->file); |
84 | 613 return (ret * -1); |
16 | 614 } |
615 | |
616 | |
617 static gint | |
618 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
619 { | |
620 const gftp_file * f1, * f2; | |
621 | |
622 f1 = a; | |
623 f2 = b; | |
535 | 624 return (strcasecmp (f1->user, f2->user)); |
16 | 625 } |
626 | |
627 | |
628 static gint | |
629 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
630 { | |
631 const gftp_file * f1, * f2; | |
632 gint ret; | |
633 | |
634 f1 = a; | |
635 f2 = b; | |
535 | 636 ret = strcasecmp (f1->user, f2->user); |
84 | 637 return (ret * -1); |
16 | 638 } |
639 | |
640 | |
641 static gint | |
642 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
643 { | |
644 const gftp_file * f1, * f2; | |
645 | |
646 f1 = a; | |
647 f2 = b; | |
535 | 648 return (strcasecmp (f1->group, f2->group)); |
16 | 649 } |
650 | |
651 | |
652 static gint | |
653 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
654 { | |
655 const gftp_file * f1, * f2; | |
656 gint ret; | |
657 | |
658 f1 = a; | |
659 f2 = b; | |
535 | 660 ret = strcasecmp (f1->group, f2->group); |
84 | 661 return (ret * -1); |
16 | 662 } |
663 | |
664 | |
665 static gint | |
666 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
667 { | |
668 const gftp_file * f1, * f2; | |
669 | |
670 f1 = a; | |
671 f2 = b; | |
499 | 672 if (f1->st_mode < f2->st_mode) |
673 return (-1); | |
674 else if (f1->st_mode == f2->st_mode) | |
675 return (0); | |
676 else | |
677 return (1); | |
16 | 678 } |
679 | |
680 | |
681 static gint | |
682 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
683 { | |
684 const gftp_file * f1, * f2; | |
685 | |
686 f1 = a; | |
687 f2 = b; | |
499 | 688 if (f1->st_mode < f2->st_mode) |
689 return (1); | |
690 else if (f1->st_mode == f2->st_mode) | |
691 return (0); | |
692 else | |
693 return (-1); | |
16 | 694 } |
695 | |
696 | |
697 static gint | |
698 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
699 { | |
700 const gftp_file * f1, * f2; | |
701 | |
702 f1 = a; | |
703 f2 = b; | |
704 if (f1->size < f2->size) | |
705 return (-1); | |
706 else if (f1->size == f2->size) | |
707 return (0); | |
708 else | |
709 return (1); | |
710 } | |
711 | |
712 | |
713 static gint | |
714 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
715 { | |
716 const gftp_file * f1, * f2; | |
717 | |
718 f1 = a; | |
719 f2 = b; | |
720 if (f1->size < f2->size) | |
721 return (1); | |
722 else if (f1->size == f2->size) | |
723 return (0); | |
724 else | |
725 return (-1); | |
726 } | |
727 | |
728 | |
729 static gint | |
730 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
731 { | |
732 const gftp_file * f1, * f2; | |
733 | |
734 f1 = a; | |
735 f2 = b; | |
736 if (f1->datetime < f2->datetime) | |
737 return (-1); | |
738 else if (f1->datetime == f2->datetime) | |
739 return (0); | |
740 else | |
741 return (1); | |
742 } | |
743 | |
744 | |
745 static gint | |
746 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
747 { | |
748 const gftp_file * f1, * f2; | |
749 | |
750 f1 = a; | |
751 f2 = b; | |
752 if (f1->datetime < f2->datetime) | |
753 return (1); | |
754 else if (f1->datetime == f2->datetime) | |
755 return (0); | |
756 else | |
757 return (-1); | |
758 } | |
759 | |
760 | |
761 GList * | |
762 gftp_sort_filelist (GList * filelist, int column, int asds) | |
763 { | |
764 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
765 GCompareFunc sortfunc; | |
766 gftp_file * tempfle; | |
325 | 767 intptr_t sort_dirs_first; |
16 | 768 |
422 | 769 if (filelist == NULL) /* nothing to sort */ |
770 return (filelist); | |
771 | |
16 | 772 files = dirs = dotdot = NULL; |
773 | |
774 if (column == GFTP_SORT_COL_FILE) | |
775 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
776 else if (column == GFTP_SORT_COL_SIZE) | |
777 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
778 else if (column == GFTP_SORT_COL_USER) | |
779 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
780 else if (column == GFTP_SORT_COL_GROUP) | |
781 sortfunc = asds ? | |
782 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
783 else if (column == GFTP_SORT_COL_DATETIME) | |
784 sortfunc = asds ? | |
122 | 785 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
786 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 787 sortfunc = asds ? |
788 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 789 else /* Don't sort */ |
790 return (filelist); | |
791 | |
792 sort_dirs_first = 1; | |
793 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 794 |
795 for (tempitem = filelist; tempitem != NULL; ) | |
796 { | |
797 tempfle = tempitem->data; | |
798 insitem = tempitem; | |
799 tempitem = tempitem->next; | |
800 insitem->next = NULL; | |
801 | |
802 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
803 dotdot = insitem; | |
499 | 804 else if (sort_dirs_first && S_ISDIR (tempfle->st_mode)) |
16 | 805 { |
806 insitem->next = dirs; | |
807 dirs = insitem; | |
808 } | |
809 else | |
810 { | |
811 insitem->next = files; | |
812 files = insitem; | |
813 } | |
814 } | |
815 | |
816 if (dirs != NULL) | |
817 dirs = g_list_sort (dirs, sortfunc); | |
818 if (files != NULL) | |
819 files = g_list_sort (files, sortfunc); | |
820 | |
821 filelist = dotdot; | |
822 | |
823 if (filelist == NULL) | |
824 filelist = dirs; | |
825 else | |
826 filelist = g_list_concat (filelist, dirs); | |
827 | |
828 if (filelist == NULL) | |
829 filelist = files; | |
830 else | |
831 filelist = g_list_concat (filelist, files); | |
832 | |
39 | 833 /* I haven't check this, but I'm pretty sure some older versions of glib |
834 had a bug that the prev pointer wasn't being sent to NULL */ | |
835 filelist->prev = NULL; | |
16 | 836 return (filelist); |
837 } | |
838 | |
125 | 839 |
131 | 840 char * |
841 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
842 { | |
499 | 843 char *tempstr1, *tempstr2, *ret, tstr[50], *attribs; |
131 | 844 struct tm *lt; |
845 time_t t; | |
846 | |
847 lt = localtime (&fle->datetime); | |
848 | |
499 | 849 attribs = gftp_convert_attributes_from_mode_t (fle->st_mode); |
850 tempstr1 = g_strdup_printf ("%10s %8s %8s", attribs, fle->user, fle->group); | |
851 g_free (attribs); | |
131 | 852 |
499 | 853 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode)) |
131 | 854 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); |
855 else | |
532 | 856 tempstr2 = g_strdup_printf (GFTP_OFF_T_11PRINTF_MOD, fle->size); |
131 | 857 |
858 time (&t); | |
859 | |
860 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
861 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
862 else | |
863 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
864 | |
865 if (file_prefixstr == NULL) | |
866 file_prefixstr = ""; | |
867 if (file_suffixstr == NULL) | |
868 file_suffixstr = ""; | |
869 | |
870 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 871 file_prefixstr, |
872 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
873 file_suffixstr); | |
131 | 874 |
875 g_free (tempstr1); | |
876 g_free (tempstr2); | |
877 | |
878 return (ret); | |
879 } | |
880 | |
881 | |
125 | 882 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
883 | |
884 struct hostent * | |
885 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
886 { | |
887 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
888 struct hostent *hent; | |
889 | |
890 if (g_thread_supported ()) | |
891 g_static_mutex_lock (&hostfunclock); | |
892 | |
893 if ((hent = gethostbyname (name)) == NULL) | |
894 { | |
895 if (h_errnop) | |
896 *h_errnop = h_errno; | |
897 } | |
898 else | |
899 { | |
900 *result_buf = *hent; | |
901 hent = result_buf; | |
902 } | |
903 | |
904 if (g_thread_supported ()) | |
905 g_static_mutex_unlock (&hostfunclock); | |
906 | |
907 return (hent); | |
908 } | |
909 | |
910 #endif /* !HAVE_GETADDRINFO */ | |
911 | |
912 struct servent * | |
913 r_getservbyname (const char *name, const char *proto, | |
914 struct servent *result_buf, int *h_errnop) | |
915 { | |
916 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
917 struct servent *sent; | |
918 | |
919 if (g_thread_supported ()) | |
920 g_static_mutex_lock (&servfunclock); | |
921 | |
922 if ((sent = getservbyname (name, proto)) == NULL) | |
923 { | |
924 if (h_errnop) | |
925 *h_errnop = h_errno; | |
926 } | |
927 else | |
928 { | |
929 *result_buf = *sent; | |
930 sent = result_buf; | |
931 } | |
932 | |
933 if (g_thread_supported ()) | |
934 g_static_mutex_unlock (&servfunclock); | |
935 return (sent); | |
936 } | |
937 | |
168 | 938 |
939 char * | |
940 base64_encode (char *str) | |
941 { | |
942 | |
943 /* The standard to Base64 encoding can be found in RFC2045 */ | |
944 | |
945 char *newstr, *newpos, *fillpos, *pos; | |
946 unsigned char table[64], encode[3]; | |
460 | 947 size_t slen, num; |
948 int i; | |
168 | 949 |
950 for (i = 0; i < 26; i++) | |
951 { | |
952 table[i] = 'A' + i; | |
953 table[i + 26] = 'a' + i; | |
954 } | |
955 | |
956 for (i = 0; i < 10; i++) | |
957 table[i + 52] = '0' + i; | |
958 | |
959 table[62] = '+'; | |
207 | 960 table[63] = '/'; |
168 | 961 |
460 | 962 slen = strlen (str); |
963 num = slen / 3; | |
964 if (slen % 3 > 0) | |
168 | 965 num++; |
460 | 966 |
168 | 967 newstr = g_malloc (num * 4 + 1); |
968 newstr[num * 4] = '\0'; | |
969 newpos = newstr; | |
970 | |
971 pos = str; | |
972 while (*pos != '\0') | |
973 { | |
974 memset (encode, 0, sizeof (encode)); | |
975 for (i = 0; i < 3 && *pos != '\0'; i++) | |
976 encode[i] = *pos++; | |
977 | |
978 fillpos = newpos; | |
979 *newpos++ = table[encode[0] >> 2]; | |
980 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
981 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
982 *newpos++ = table[encode[2] & 0x3F]; | |
983 while (i < 3) | |
984 fillpos[++i] = '='; | |
985 } | |
986 return (newstr); | |
987 } | |
988 | |
199 | 989 |
990 void | |
991 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
992 { | |
993 if (entry->hostname) | |
994 g_free (entry->hostname); | |
995 if (entry->remote_dir) | |
996 g_free (entry->remote_dir); | |
997 if (entry->local_dir) | |
998 g_free (entry->local_dir); | |
999 if (entry->user) | |
1000 g_free (entry->user); | |
1001 if (entry->pass) | |
1002 g_free (entry->pass); | |
1003 if (entry->acct) | |
1004 g_free (entry->acct); | |
1005 if (entry->protocol) | |
1006 g_free (entry->protocol); | |
1007 | |
1008 if (entry->local_options_vars != NULL) | |
1009 { | |
201 | 1010 gftp_config_free_options (entry->local_options_vars, |
1011 entry->local_options_hash, | |
1012 entry->num_local_options_vars); | |
1013 | |
199 | 1014 entry->local_options_vars = NULL; |
201 | 1015 entry->local_options_hash = NULL; |
199 | 1016 entry->num_local_options_vars = 0; |
1017 } | |
1018 } | |
1019 | |
201 | 1020 |
1021 void | |
1022 gftp_shutdown (void) | |
1023 { | |
203 | 1024 #ifdef WITH_DMALLOC |
201 | 1025 gftp_config_vars * cv; |
1026 GList * templist; | |
203 | 1027 #endif |
201 | 1028 |
1029 if (gftp_logfd != NULL) | |
1030 fclose (gftp_logfd); | |
1031 | |
1032 gftp_clear_cache_files (); | |
1033 | |
1034 if (gftp_configuration_changed) | |
1035 gftp_write_config_file (); | |
1036 | |
1037 #ifdef WITH_DMALLOC | |
1038 if (gftp_global_options_htable != NULL) | |
1039 g_hash_table_destroy (gftp_global_options_htable); | |
1040 | |
1041 if (gftp_config_list_htable != NULL) | |
1042 g_hash_table_destroy (gftp_config_list_htable); | |
1043 | |
1044 if (gftp_bookmarks_htable != NULL) | |
1045 g_hash_table_destroy (gftp_bookmarks_htable); | |
1046 | |
1047 for (templist = gftp_options_list; | |
1048 templist != NULL; | |
1049 templist = templist->next) | |
1050 { | |
1051 cv = templist->data; | |
1052 gftp_config_free_options (cv, NULL, -1); | |
1053 } | |
1054 | |
1055 gftp_bookmarks_destroy (gftp_bookmarks); | |
1056 | |
1057 dmalloc_shutdown (); | |
1058 #endif | |
1059 | |
1060 exit (0); | |
1061 } | |
1062 | |
207 | 1063 |
1064 GList * | |
1065 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1066 { | |
1067 gftp_file * tempfle; | |
1068 int i, newpos; | |
1069 | |
1070 newpos = GPOINTER_TO_INT (selection->data); | |
1071 i = *curnum - newpos; | |
1072 | |
1073 if (i < 0) | |
1074 { | |
1075 while (i != 0) | |
1076 { | |
1077 tempfle = (*list)->data; | |
1078 if (tempfle->shown) | |
1079 { | |
1080 ++*curnum; | |
1081 i++; | |
1082 } | |
1083 *list = (*list)->next; | |
1084 } | |
1085 } | |
1086 else if (i > 0) | |
1087 { | |
1088 while (i != 0) | |
1089 { | |
1090 tempfle = (*list)->data; | |
1091 if (tempfle->shown) | |
1092 { | |
1093 --*curnum; | |
1094 i--; | |
1095 } | |
1096 *list = (*list)->prev; | |
1097 } | |
1098 } | |
1099 | |
1100 tempfle = (*list)->data; | |
1101 while ((*list)->next && !tempfle->shown) | |
1102 { | |
1103 *list = (*list)->next; | |
1104 tempfle = (*list)->data; | |
1105 } | |
1106 return (selection->next); | |
1107 } | |
1108 | |
1109 | |
227 | 1110 char * |
555 | 1111 gftp_build_path (gftp_request * request, const char *first_element, ...) |
227 | 1112 { |
1113 const char *element; | |
1114 size_t len, retlen; | |
1115 int add_separator; | |
1116 va_list args; | |
1117 char *ret; | |
1118 | |
245 | 1119 g_return_val_if_fail (first_element != NULL, NULL); |
227 | 1120 |
247 | 1121 ret = g_strdup (first_element); |
1122 retlen = strlen (ret); | |
227 | 1123 |
1124 va_start (args, first_element); | |
247 | 1125 for (element = va_arg (args, char *); |
227 | 1126 element != NULL; |
1127 element = va_arg (args, char *)) | |
1128 { | |
1129 len = strlen (element); | |
1130 | |
422 | 1131 if (len == 0) |
1132 continue; | |
1133 | |
369 | 1134 if (retlen > 0 && (ret[retlen - 1] == '/' || element[0] == '/')) |
227 | 1135 add_separator = 0; |
1136 else | |
1137 { | |
1138 add_separator = 1; | |
1139 len++; | |
1140 } | |
1141 | |
1142 retlen += len; | |
1143 ret = g_realloc (ret, retlen + 1); | |
1144 | |
555 | 1145 /* Don't append a / for VMS servers... */ |
1146 if (add_separator && | |
1147 (request == NULL || request->server_type != GFTP_DIRTYPE_VMS)) | |
245 | 1148 strncat (ret, "/", retlen); |
1149 | |
1150 strncat (ret, element, retlen); | |
227 | 1151 } |
1152 | |
1153 return (ret); | |
1154 } | |
1155 | |
244 | 1156 |
290 | 1157 void |
1158 gftp_locale_init (void) | |
1159 { | |
1160 #ifdef HAVE_GETTEXT | |
1161 | |
1162 setlocale (LC_ALL, ""); | |
1163 textdomain ("gftp"); | |
320 | 1164 bindtextdomain ("gftp", LOCALE_DIR); |
290 | 1165 |
1166 #if GLIB_MAJOR_VERSION > 1 | |
1167 bind_textdomain_codeset ("gftp", "UTF-8"); | |
1168 #endif | |
1169 | |
320 | 1170 #endif /* HAVE_GETTEXT */ |
290 | 1171 } |
1172 | |
330 | 1173 /* Very primary encryption/decryption to make the passwords unreadable |
1174 with 'cat ~/.gftp/bookmarks'. | |
1175 | |
1176 Each character is separated in two nibbles. Then each nibble is stored | |
1177 under the form 01xxxx01. The resulted string is prefixed by a '$'. | |
1178 */ | |
1179 | |
1180 | |
1181 char * | |
1182 gftp_scramble_password (const char *password) | |
1183 { | |
1184 char *newstr, *newpos; | |
1185 | |
1186 if (strcmp (password, "@EMAIL@") == 0) | |
1187 return (g_strdup (password)); | |
1188 | |
460 | 1189 newstr = g_malloc (strlen (password) * 2 + 2); |
330 | 1190 newpos = newstr; |
1191 | |
1192 *newpos++ = '$'; | |
1193 | |
1194 while (*password != 0) | |
1195 { | |
1196 *newpos++ = ((*password >> 2) & 0x3c) | 0x41; | |
1197 *newpos++ = ((*password << 2) & 0x3c) | 0x41; | |
1198 password++; | |
1199 } | |
1200 *newpos = 0; | |
1201 | |
1202 return (newstr); | |
1203 } | |
1204 | |
1205 | |
1206 char * | |
1207 gftp_descramble_password (const char *password) | |
1208 { | |
1209 const char *passwordpos; | |
1210 char *newstr, *newpos; | |
1211 int error; | |
1212 | |
1213 if (*password != '$') | |
1214 return (g_strdup (password)); | |
1215 | |
1216 passwordpos = password + 1; | |
1217 newstr = g_malloc (strlen (passwordpos) / 2 + 1); | |
1218 newpos = newstr; | |
1219 | |
1220 error = 0; | |
1221 while (*passwordpos != '\0' && (*passwordpos + 1) != '\0') | |
1222 { | |
1223 if ((*passwordpos & 0xc3) != 0x41 || | |
1224 (*(passwordpos + 1) & 0xc3) != 0x41) | |
1225 { | |
1226 error = 1; | |
1227 break; | |
1228 } | |
1229 | |
1230 *newpos++ = ((*passwordpos & 0x3c) << 2) | | |
1231 ((*(passwordpos + 1) & 0x3c) >> 2); | |
1232 | |
1233 passwordpos += 2; | |
1234 } | |
1235 | |
1236 if (error) | |
1237 { | |
1238 g_free (newstr); | |
1239 return (g_strdup (password)); | |
1240 } | |
1241 | |
1242 *newpos = '\0'; | |
1243 return (newstr); | |
1244 } | |
1245 | |
378 | 1246 |
1247 int | |
1248 gftp_get_transfer_action (gftp_request * request, gftp_file * fle) | |
1249 { | |
1250 intptr_t overwrite_default; | |
1251 | |
1252 gftp_lookup_request_option (request, "overwrite_default", &overwrite_default); | |
1253 | |
1254 if (overwrite_default) | |
1255 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1256 else if (fle->startsize == fle->size) | |
1257 fle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
1258 else if (fle->startsize > fle->size) | |
1259 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1260 else | |
1261 fle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
1262 | |
1263 return (fle->transfer_action); | |
1264 } | |
1265 | |
483 | 1266 |
1267 char * | |
1268 gftp_get_share_dir (void) | |
1269 { | |
1270 static char *gftp_share_dir = NULL; | |
1271 char *envval; | |
1272 | |
1273 if (gftp_share_dir == NULL) | |
1274 { | |
1275 envval = getenv ("GFTP_SHARE_DIR"); | |
1276 | |
1277 if (envval != NULL && *envval != '\0') | |
1278 gftp_share_dir = g_strdup (envval); | |
1279 else | |
1280 gftp_share_dir = SHARE_DIR; | |
1281 } | |
1282 | |
1283 return (gftp_share_dir); | |
1284 } | |
1285 |