Mercurial > gftp.yaz
annotate lib/misc.c @ 566:502a126418cd
2004-9-26 Brian Masney <masneyb@gftp.org>
* lib/protocols.c (gftp_put_file) - use g_filename_from_utf8() to
change the encoding of the filename (if needed)
author | masneyb |
---|---|
date | Mon, 27 Sep 2004 00:12:11 +0000 |
parents | 7f54d0c0edbc |
children | fa0838b22b14 |
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; | |
437 free_fdata (tempfle); | |
438 templist = templist->next; | |
439 } | |
440 g_list_free (filelist); | |
441 } | |
442 | |
443 | |
444 void | |
445 free_fdata (gftp_file * fle) | |
446 { | |
447 if (fle->file) | |
448 g_free (fle->file); | |
184 | 449 if (fle->utf8_file) |
450 g_free (fle->utf8_file); | |
1 | 451 if (fle->user) |
452 g_free (fle->user); | |
453 if (fle->group) | |
454 g_free (fle->group); | |
455 if (fle->destfile) | |
456 g_free (fle->destfile); | |
58 | 457 if (fle->fd > 0) |
102 | 458 close (fle->fd); |
1 | 459 g_free (fle); |
460 } | |
461 | |
462 | |
463 gftp_file * | |
464 copy_fdata (gftp_file * fle) | |
465 { | |
466 gftp_file * newfle; | |
467 | |
468 newfle = g_malloc0 (sizeof (*newfle)); | |
469 memcpy (newfle, fle, sizeof (*newfle)); | |
470 | |
471 if (fle->file) | |
87 | 472 newfle->file = g_strdup (fle->file); |
1 | 473 |
243 | 474 if (fle->utf8_file) |
475 newfle->utf8_file = g_strdup (fle->utf8_file); | |
476 | |
1 | 477 if (fle->user) |
87 | 478 newfle->user = g_strdup (fle->user); |
1 | 479 |
480 if (fle->group) | |
87 | 481 newfle->group = g_strdup (fle->group); |
1 | 482 |
483 if (fle->destfile) | |
87 | 484 newfle->destfile = g_strdup (fle->destfile); |
485 | |
1 | 486 return (newfle); |
487 } | |
488 | |
489 | |
490 int | |
491 compare_request (gftp_request * request1, gftp_request * request2, | |
492 int compare_dirs) | |
493 { | |
494 char *strarr[3][2]; | |
495 int i, ret; | |
496 | |
497 ret = 1; | |
87 | 498 if (request1->protonum == request2->protonum && |
1 | 499 request1->port == request2->port) |
500 { | |
501 strarr[0][0] = request1->hostname; | |
502 strarr[0][1] = request2->hostname; | |
503 strarr[1][0] = request1->username; | |
504 strarr[1][1] = request2->username; | |
505 if (compare_dirs) | |
506 { | |
507 strarr[2][0] = request1->directory; | |
508 strarr[2][1] = request2->directory; | |
509 } | |
510 else | |
511 strarr[2][0] = strarr[2][1] = ""; | |
512 | |
513 for (i = 0; i < 3; i++) | |
514 { | |
515 if ((strarr[i][0] && !strarr[i][1]) || | |
516 (!strarr[i][0] && strarr[i][1])) | |
517 { | |
518 ret = 0; | |
519 break; | |
520 } | |
521 | |
522 if (strarr[i][0] && strarr[i][1] && | |
523 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
524 { | |
525 ret = 0; | |
526 break; | |
527 } | |
528 } | |
529 } | |
530 else | |
531 ret = 0; | |
532 return (ret); | |
533 } | |
534 | |
535 | |
129 | 536 gftp_transfer * |
537 gftp_tdata_new (void) | |
538 { | |
227 | 539 #if GLIB_MAJOR_VERSION == 1 |
540 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
541 #endif | |
129 | 542 gftp_transfer * tdata; |
543 | |
544 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 545 |
546 #if GLIB_MAJOR_VERSION == 1 | |
547 tdata->statmutex = init_mutex; | |
548 tdata->structmutex = init_mutex; | |
549 #else | |
168 | 550 g_static_mutex_init (&tdata->statmutex); |
551 g_static_mutex_init (&tdata->structmutex); | |
227 | 552 #endif |
553 | |
129 | 554 return (tdata); |
555 } | |
556 | |
557 | |
1 | 558 void |
559 free_tdata (gftp_transfer * tdata) | |
560 { | |
561 if (tdata->fromreq != NULL) | |
67 | 562 gftp_request_destroy (tdata->fromreq, 1); |
1 | 563 if (tdata->toreq != NULL) |
67 | 564 gftp_request_destroy (tdata->toreq, 1); |
1 | 565 free_file_list (tdata->files); |
566 g_free (tdata); | |
567 } | |
568 | |
569 | |
570 gftp_request * | |
368 | 571 gftp_copy_request (gftp_request * req) |
1 | 572 { |
573 gftp_request * newreq; | |
574 | |
66 | 575 newreq = gftp_request_new (); |
1 | 576 |
577 if (req->hostname) | |
56 | 578 newreq->hostname = g_strdup (req->hostname); |
1 | 579 if (req->username) |
56 | 580 newreq->username = g_strdup (req->username); |
1 | 581 if (req->password) |
56 | 582 newreq->password = g_strdup (req->password); |
1 | 583 if (req->account) |
56 | 584 newreq->account = g_strdup (req->account); |
1 | 585 if (req->directory) |
56 | 586 newreq->directory = g_strdup (req->directory); |
66 | 587 newreq->port = req->port; |
588 newreq->use_proxy = req->use_proxy; | |
589 newreq->logging_function = req->logging_function; | |
547 | 590 newreq->ai_family = req->ai_family; |
151 | 591 newreq->free_hostp = 0; |
296 | 592 newreq->hostp = NULL; |
151 | 593 |
368 | 594 gftp_copy_local_options (&newreq->local_options_vars, |
595 &newreq->local_options_hash, | |
429 | 596 &newreq->num_local_options_vars, |
368 | 597 req->local_options_vars, |
598 req->num_local_options_vars); | |
1 | 599 |
451 | 600 if (req->init != NULL && req->init (newreq) < 0) |
173 | 601 { |
602 gftp_request_destroy (newreq, 1); | |
603 return (NULL); | |
604 } | |
1 | 605 |
309 | 606 gftp_copy_param_options (newreq, req); |
607 | |
1 | 608 return (newreq); |
609 } | |
610 | |
611 | |
16 | 612 static gint |
613 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
614 { | |
615 const gftp_file * f1, * f2; | |
616 | |
617 f1 = a; | |
618 f2 = b; | |
535 | 619 return (strcasecmp (f1->file, f2->file)); |
16 | 620 } |
621 | |
622 | |
623 static gint | |
624 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
625 { | |
626 const gftp_file * f1, * f2; | |
627 gint ret; | |
628 | |
629 f1 = a; | |
630 f2 = b; | |
535 | 631 ret = strcasecmp (f1->file, f2->file); |
84 | 632 return (ret * -1); |
16 | 633 } |
634 | |
635 | |
636 static gint | |
637 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
638 { | |
639 const gftp_file * f1, * f2; | |
640 | |
641 f1 = a; | |
642 f2 = b; | |
535 | 643 return (strcasecmp (f1->user, f2->user)); |
16 | 644 } |
645 | |
646 | |
647 static gint | |
648 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
649 { | |
650 const gftp_file * f1, * f2; | |
651 gint ret; | |
652 | |
653 f1 = a; | |
654 f2 = b; | |
535 | 655 ret = strcasecmp (f1->user, f2->user); |
84 | 656 return (ret * -1); |
16 | 657 } |
658 | |
659 | |
660 static gint | |
661 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
662 { | |
663 const gftp_file * f1, * f2; | |
664 | |
665 f1 = a; | |
666 f2 = b; | |
535 | 667 return (strcasecmp (f1->group, f2->group)); |
16 | 668 } |
669 | |
670 | |
671 static gint | |
672 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
673 { | |
674 const gftp_file * f1, * f2; | |
675 gint ret; | |
676 | |
677 f1 = a; | |
678 f2 = b; | |
535 | 679 ret = strcasecmp (f1->group, f2->group); |
84 | 680 return (ret * -1); |
16 | 681 } |
682 | |
683 | |
684 static gint | |
685 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
686 { | |
687 const gftp_file * f1, * f2; | |
688 | |
689 f1 = a; | |
690 f2 = b; | |
499 | 691 if (f1->st_mode < f2->st_mode) |
692 return (-1); | |
693 else if (f1->st_mode == f2->st_mode) | |
694 return (0); | |
695 else | |
696 return (1); | |
16 | 697 } |
698 | |
699 | |
700 static gint | |
701 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
702 { | |
703 const gftp_file * f1, * f2; | |
704 | |
705 f1 = a; | |
706 f2 = b; | |
499 | 707 if (f1->st_mode < f2->st_mode) |
708 return (1); | |
709 else if (f1->st_mode == f2->st_mode) | |
710 return (0); | |
711 else | |
712 return (-1); | |
16 | 713 } |
714 | |
715 | |
716 static gint | |
717 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
718 { | |
719 const gftp_file * f1, * f2; | |
720 | |
721 f1 = a; | |
722 f2 = b; | |
723 if (f1->size < f2->size) | |
724 return (-1); | |
725 else if (f1->size == f2->size) | |
726 return (0); | |
727 else | |
728 return (1); | |
729 } | |
730 | |
731 | |
732 static gint | |
733 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
734 { | |
735 const gftp_file * f1, * f2; | |
736 | |
737 f1 = a; | |
738 f2 = b; | |
739 if (f1->size < f2->size) | |
740 return (1); | |
741 else if (f1->size == f2->size) | |
742 return (0); | |
743 else | |
744 return (-1); | |
745 } | |
746 | |
747 | |
748 static gint | |
749 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
750 { | |
751 const gftp_file * f1, * f2; | |
752 | |
753 f1 = a; | |
754 f2 = b; | |
755 if (f1->datetime < f2->datetime) | |
756 return (-1); | |
757 else if (f1->datetime == f2->datetime) | |
758 return (0); | |
759 else | |
760 return (1); | |
761 } | |
762 | |
763 | |
764 static gint | |
765 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
766 { | |
767 const gftp_file * f1, * f2; | |
768 | |
769 f1 = a; | |
770 f2 = b; | |
771 if (f1->datetime < f2->datetime) | |
772 return (1); | |
773 else if (f1->datetime == f2->datetime) | |
774 return (0); | |
775 else | |
776 return (-1); | |
777 } | |
778 | |
779 | |
780 GList * | |
781 gftp_sort_filelist (GList * filelist, int column, int asds) | |
782 { | |
783 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
784 GCompareFunc sortfunc; | |
785 gftp_file * tempfle; | |
325 | 786 intptr_t sort_dirs_first; |
16 | 787 |
422 | 788 if (filelist == NULL) /* nothing to sort */ |
789 return (filelist); | |
790 | |
16 | 791 files = dirs = dotdot = NULL; |
792 | |
793 if (column == GFTP_SORT_COL_FILE) | |
794 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
795 else if (column == GFTP_SORT_COL_SIZE) | |
796 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
797 else if (column == GFTP_SORT_COL_USER) | |
798 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
799 else if (column == GFTP_SORT_COL_GROUP) | |
800 sortfunc = asds ? | |
801 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
802 else if (column == GFTP_SORT_COL_DATETIME) | |
803 sortfunc = asds ? | |
122 | 804 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
805 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 806 sortfunc = asds ? |
807 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 808 else /* Don't sort */ |
809 return (filelist); | |
810 | |
811 sort_dirs_first = 1; | |
812 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 813 |
814 for (tempitem = filelist; tempitem != NULL; ) | |
815 { | |
816 tempfle = tempitem->data; | |
817 insitem = tempitem; | |
818 tempitem = tempitem->next; | |
819 insitem->next = NULL; | |
820 | |
821 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
822 dotdot = insitem; | |
499 | 823 else if (sort_dirs_first && S_ISDIR (tempfle->st_mode)) |
16 | 824 { |
825 insitem->next = dirs; | |
826 dirs = insitem; | |
827 } | |
828 else | |
829 { | |
830 insitem->next = files; | |
831 files = insitem; | |
832 } | |
833 } | |
834 | |
835 if (dirs != NULL) | |
836 dirs = g_list_sort (dirs, sortfunc); | |
837 if (files != NULL) | |
838 files = g_list_sort (files, sortfunc); | |
839 | |
840 filelist = dotdot; | |
841 | |
842 if (filelist == NULL) | |
843 filelist = dirs; | |
844 else | |
845 filelist = g_list_concat (filelist, dirs); | |
846 | |
847 if (filelist == NULL) | |
848 filelist = files; | |
849 else | |
850 filelist = g_list_concat (filelist, files); | |
851 | |
39 | 852 /* I haven't check this, but I'm pretty sure some older versions of glib |
853 had a bug that the prev pointer wasn't being sent to NULL */ | |
854 filelist->prev = NULL; | |
16 | 855 return (filelist); |
856 } | |
857 | |
125 | 858 |
131 | 859 char * |
860 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
861 { | |
499 | 862 char *tempstr1, *tempstr2, *ret, tstr[50], *attribs; |
131 | 863 struct tm *lt; |
864 time_t t; | |
865 | |
866 lt = localtime (&fle->datetime); | |
867 | |
499 | 868 attribs = gftp_convert_attributes_from_mode_t (fle->st_mode); |
869 tempstr1 = g_strdup_printf ("%10s %8s %8s", attribs, fle->user, fle->group); | |
870 g_free (attribs); | |
131 | 871 |
499 | 872 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode)) |
131 | 873 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); |
874 else | |
532 | 875 tempstr2 = g_strdup_printf (GFTP_OFF_T_11PRINTF_MOD, fle->size); |
131 | 876 |
877 time (&t); | |
878 | |
879 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
880 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
881 else | |
882 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
883 | |
884 if (file_prefixstr == NULL) | |
885 file_prefixstr = ""; | |
886 if (file_suffixstr == NULL) | |
887 file_suffixstr = ""; | |
888 | |
889 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 890 file_prefixstr, |
891 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
892 file_suffixstr); | |
131 | 893 |
894 g_free (tempstr1); | |
895 g_free (tempstr2); | |
896 | |
897 return (ret); | |
898 } | |
899 | |
900 | |
125 | 901 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
902 | |
903 struct hostent * | |
904 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
905 { | |
906 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
907 struct hostent *hent; | |
908 | |
909 if (g_thread_supported ()) | |
910 g_static_mutex_lock (&hostfunclock); | |
911 | |
912 if ((hent = gethostbyname (name)) == NULL) | |
913 { | |
914 if (h_errnop) | |
915 *h_errnop = h_errno; | |
916 } | |
917 else | |
918 { | |
919 *result_buf = *hent; | |
920 hent = result_buf; | |
921 } | |
922 | |
923 if (g_thread_supported ()) | |
924 g_static_mutex_unlock (&hostfunclock); | |
925 | |
926 return (hent); | |
927 } | |
928 | |
929 #endif /* !HAVE_GETADDRINFO */ | |
930 | |
931 struct servent * | |
932 r_getservbyname (const char *name, const char *proto, | |
933 struct servent *result_buf, int *h_errnop) | |
934 { | |
935 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
936 struct servent *sent; | |
937 | |
938 if (g_thread_supported ()) | |
939 g_static_mutex_lock (&servfunclock); | |
940 | |
941 if ((sent = getservbyname (name, proto)) == NULL) | |
942 { | |
943 if (h_errnop) | |
944 *h_errnop = h_errno; | |
945 } | |
946 else | |
947 { | |
948 *result_buf = *sent; | |
949 sent = result_buf; | |
950 } | |
951 | |
952 if (g_thread_supported ()) | |
953 g_static_mutex_unlock (&servfunclock); | |
954 return (sent); | |
955 } | |
956 | |
168 | 957 |
958 char * | |
959 base64_encode (char *str) | |
960 { | |
961 | |
962 /* The standard to Base64 encoding can be found in RFC2045 */ | |
963 | |
964 char *newstr, *newpos, *fillpos, *pos; | |
965 unsigned char table[64], encode[3]; | |
460 | 966 size_t slen, num; |
967 int i; | |
168 | 968 |
969 for (i = 0; i < 26; i++) | |
970 { | |
971 table[i] = 'A' + i; | |
972 table[i + 26] = 'a' + i; | |
973 } | |
974 | |
975 for (i = 0; i < 10; i++) | |
976 table[i + 52] = '0' + i; | |
977 | |
978 table[62] = '+'; | |
207 | 979 table[63] = '/'; |
168 | 980 |
460 | 981 slen = strlen (str); |
982 num = slen / 3; | |
983 if (slen % 3 > 0) | |
168 | 984 num++; |
460 | 985 |
168 | 986 newstr = g_malloc (num * 4 + 1); |
987 newstr[num * 4] = '\0'; | |
988 newpos = newstr; | |
989 | |
990 pos = str; | |
991 while (*pos != '\0') | |
992 { | |
993 memset (encode, 0, sizeof (encode)); | |
994 for (i = 0; i < 3 && *pos != '\0'; i++) | |
995 encode[i] = *pos++; | |
996 | |
997 fillpos = newpos; | |
998 *newpos++ = table[encode[0] >> 2]; | |
999 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1000 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1001 *newpos++ = table[encode[2] & 0x3F]; | |
1002 while (i < 3) | |
1003 fillpos[++i] = '='; | |
1004 } | |
1005 return (newstr); | |
1006 } | |
1007 | |
199 | 1008 |
1009 void | |
1010 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1011 { | |
1012 if (entry->hostname) | |
1013 g_free (entry->hostname); | |
1014 if (entry->remote_dir) | |
1015 g_free (entry->remote_dir); | |
1016 if (entry->local_dir) | |
1017 g_free (entry->local_dir); | |
1018 if (entry->user) | |
1019 g_free (entry->user); | |
1020 if (entry->pass) | |
1021 g_free (entry->pass); | |
1022 if (entry->acct) | |
1023 g_free (entry->acct); | |
1024 if (entry->protocol) | |
1025 g_free (entry->protocol); | |
1026 | |
1027 if (entry->local_options_vars != NULL) | |
1028 { | |
201 | 1029 gftp_config_free_options (entry->local_options_vars, |
1030 entry->local_options_hash, | |
1031 entry->num_local_options_vars); | |
1032 | |
199 | 1033 entry->local_options_vars = NULL; |
201 | 1034 entry->local_options_hash = NULL; |
199 | 1035 entry->num_local_options_vars = 0; |
1036 } | |
1037 } | |
1038 | |
201 | 1039 |
1040 void | |
1041 gftp_shutdown (void) | |
1042 { | |
203 | 1043 #ifdef WITH_DMALLOC |
201 | 1044 gftp_config_vars * cv; |
1045 GList * templist; | |
203 | 1046 #endif |
201 | 1047 |
1048 if (gftp_logfd != NULL) | |
1049 fclose (gftp_logfd); | |
1050 | |
1051 gftp_clear_cache_files (); | |
1052 | |
1053 if (gftp_configuration_changed) | |
1054 gftp_write_config_file (); | |
1055 | |
1056 #ifdef WITH_DMALLOC | |
1057 if (gftp_global_options_htable != NULL) | |
1058 g_hash_table_destroy (gftp_global_options_htable); | |
1059 | |
1060 if (gftp_config_list_htable != NULL) | |
1061 g_hash_table_destroy (gftp_config_list_htable); | |
1062 | |
1063 if (gftp_bookmarks_htable != NULL) | |
1064 g_hash_table_destroy (gftp_bookmarks_htable); | |
1065 | |
1066 for (templist = gftp_options_list; | |
1067 templist != NULL; | |
1068 templist = templist->next) | |
1069 { | |
1070 cv = templist->data; | |
1071 gftp_config_free_options (cv, NULL, -1); | |
1072 } | |
1073 | |
1074 gftp_bookmarks_destroy (gftp_bookmarks); | |
1075 | |
1076 dmalloc_shutdown (); | |
1077 #endif | |
1078 | |
1079 exit (0); | |
1080 } | |
1081 | |
207 | 1082 |
1083 GList * | |
1084 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1085 { | |
1086 gftp_file * tempfle; | |
1087 int i, newpos; | |
1088 | |
1089 newpos = GPOINTER_TO_INT (selection->data); | |
1090 i = *curnum - newpos; | |
1091 | |
1092 if (i < 0) | |
1093 { | |
1094 while (i != 0) | |
1095 { | |
1096 tempfle = (*list)->data; | |
1097 if (tempfle->shown) | |
1098 { | |
1099 ++*curnum; | |
1100 i++; | |
1101 } | |
1102 *list = (*list)->next; | |
1103 } | |
1104 } | |
1105 else if (i > 0) | |
1106 { | |
1107 while (i != 0) | |
1108 { | |
1109 tempfle = (*list)->data; | |
1110 if (tempfle->shown) | |
1111 { | |
1112 --*curnum; | |
1113 i--; | |
1114 } | |
1115 *list = (*list)->prev; | |
1116 } | |
1117 } | |
1118 | |
1119 tempfle = (*list)->data; | |
1120 while ((*list)->next && !tempfle->shown) | |
1121 { | |
1122 *list = (*list)->next; | |
1123 tempfle = (*list)->data; | |
1124 } | |
1125 return (selection->next); | |
1126 } | |
1127 | |
1128 | |
227 | 1129 char * |
555 | 1130 gftp_build_path (gftp_request * request, const char *first_element, ...) |
227 | 1131 { |
1132 const char *element; | |
1133 size_t len, retlen; | |
1134 int add_separator; | |
1135 va_list args; | |
1136 char *ret; | |
1137 | |
245 | 1138 g_return_val_if_fail (first_element != NULL, NULL); |
227 | 1139 |
247 | 1140 ret = g_strdup (first_element); |
1141 retlen = strlen (ret); | |
227 | 1142 |
1143 va_start (args, first_element); | |
247 | 1144 for (element = va_arg (args, char *); |
227 | 1145 element != NULL; |
1146 element = va_arg (args, char *)) | |
1147 { | |
1148 len = strlen (element); | |
1149 | |
422 | 1150 if (len == 0) |
1151 continue; | |
1152 | |
369 | 1153 if (retlen > 0 && (ret[retlen - 1] == '/' || element[0] == '/')) |
227 | 1154 add_separator = 0; |
1155 else | |
1156 { | |
1157 add_separator = 1; | |
1158 len++; | |
1159 } | |
1160 | |
1161 retlen += len; | |
1162 ret = g_realloc (ret, retlen + 1); | |
1163 | |
555 | 1164 /* Don't append a / for VMS servers... */ |
1165 if (add_separator && | |
1166 (request == NULL || request->server_type != GFTP_DIRTYPE_VMS)) | |
245 | 1167 strncat (ret, "/", retlen); |
1168 | |
1169 strncat (ret, element, retlen); | |
227 | 1170 } |
1171 | |
1172 return (ret); | |
1173 } | |
1174 | |
244 | 1175 |
290 | 1176 void |
1177 gftp_locale_init (void) | |
1178 { | |
1179 #ifdef HAVE_GETTEXT | |
1180 | |
1181 setlocale (LC_ALL, ""); | |
1182 textdomain ("gftp"); | |
320 | 1183 bindtextdomain ("gftp", LOCALE_DIR); |
290 | 1184 |
1185 #if GLIB_MAJOR_VERSION > 1 | |
1186 bind_textdomain_codeset ("gftp", "UTF-8"); | |
1187 #endif | |
1188 | |
320 | 1189 #endif /* HAVE_GETTEXT */ |
290 | 1190 } |
1191 | |
330 | 1192 /* Very primary encryption/decryption to make the passwords unreadable |
1193 with 'cat ~/.gftp/bookmarks'. | |
1194 | |
1195 Each character is separated in two nibbles. Then each nibble is stored | |
1196 under the form 01xxxx01. The resulted string is prefixed by a '$'. | |
1197 */ | |
1198 | |
1199 | |
1200 char * | |
1201 gftp_scramble_password (const char *password) | |
1202 { | |
1203 char *newstr, *newpos; | |
1204 | |
1205 if (strcmp (password, "@EMAIL@") == 0) | |
1206 return (g_strdup (password)); | |
1207 | |
460 | 1208 newstr = g_malloc (strlen (password) * 2 + 2); |
330 | 1209 newpos = newstr; |
1210 | |
1211 *newpos++ = '$'; | |
1212 | |
1213 while (*password != 0) | |
1214 { | |
1215 *newpos++ = ((*password >> 2) & 0x3c) | 0x41; | |
1216 *newpos++ = ((*password << 2) & 0x3c) | 0x41; | |
1217 password++; | |
1218 } | |
1219 *newpos = 0; | |
1220 | |
1221 return (newstr); | |
1222 } | |
1223 | |
1224 | |
1225 char * | |
1226 gftp_descramble_password (const char *password) | |
1227 { | |
1228 const char *passwordpos; | |
1229 char *newstr, *newpos; | |
1230 int error; | |
1231 | |
1232 if (*password != '$') | |
1233 return (g_strdup (password)); | |
1234 | |
1235 passwordpos = password + 1; | |
1236 newstr = g_malloc (strlen (passwordpos) / 2 + 1); | |
1237 newpos = newstr; | |
1238 | |
1239 error = 0; | |
1240 while (*passwordpos != '\0' && (*passwordpos + 1) != '\0') | |
1241 { | |
1242 if ((*passwordpos & 0xc3) != 0x41 || | |
1243 (*(passwordpos + 1) & 0xc3) != 0x41) | |
1244 { | |
1245 error = 1; | |
1246 break; | |
1247 } | |
1248 | |
1249 *newpos++ = ((*passwordpos & 0x3c) << 2) | | |
1250 ((*(passwordpos + 1) & 0x3c) >> 2); | |
1251 | |
1252 passwordpos += 2; | |
1253 } | |
1254 | |
1255 if (error) | |
1256 { | |
1257 g_free (newstr); | |
1258 return (g_strdup (password)); | |
1259 } | |
1260 | |
1261 *newpos = '\0'; | |
1262 return (newstr); | |
1263 } | |
1264 | |
378 | 1265 |
1266 int | |
1267 gftp_get_transfer_action (gftp_request * request, gftp_file * fle) | |
1268 { | |
1269 intptr_t overwrite_default; | |
1270 | |
1271 gftp_lookup_request_option (request, "overwrite_default", &overwrite_default); | |
1272 | |
1273 if (overwrite_default) | |
1274 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1275 else if (fle->startsize == fle->size) | |
1276 fle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
1277 else if (fle->startsize > fle->size) | |
1278 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1279 else | |
1280 fle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
1281 | |
1282 return (fle->transfer_action); | |
1283 } | |
1284 | |
483 | 1285 |
1286 char * | |
1287 gftp_get_share_dir (void) | |
1288 { | |
1289 static char *gftp_share_dir = NULL; | |
1290 char *envval; | |
1291 | |
1292 if (gftp_share_dir == NULL) | |
1293 { | |
1294 envval = getenv ("GFTP_SHARE_DIR"); | |
1295 | |
1296 if (envval != NULL && *envval != '\0') | |
1297 gftp_share_dir = g_strdup (envval); | |
1298 else | |
1299 gftp_share_dir = SHARE_DIR; | |
1300 } | |
1301 | |
1302 return (gftp_share_dir); | |
1303 } | |
1304 |