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