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