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