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