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