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