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