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