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