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