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