Mercurial > gftp.yaz
annotate lib/misc.c @ 378:712d3810f4e1
2003-1-27 Brian Masney <masneyb@gftp.org>
* lib/gftp.h lib/misc.c src/gtk/gtkui_transfer.c src/text/textui.c -
added gftp_get_transfer_action(). When a file is to be transfered and
already exists, this will return the default action that should be
taken.
* src/text/textui.c (gftpui_ask_transfer) - implemented this function
so that whenever a file exists and is to be transfered, the user will
be prompted on whether or not to overwrite/skip/resume
* src/text/gftp-text.c (gftp_text_ask_question) - fixups for inputing
a single character
author | masneyb |
---|---|
date | Tue, 27 Jan 2004 23:48:26 +0000 |
parents | d207b8241e96 |
children | 38bfc112ab46 |
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) | |
372 | 33 g_snprintf (dest_str, dest_len, "%'lld", (long long) number); |
289 | 34 #else |
35 g_snprintf (dest_str, dest_len, "%'ld", number); | |
36 #endif | |
37 } | |
38 else | |
39 { | |
40 #if defined (_LARGEFILE_SOURCE) | |
372 | 41 dest_str = g_strdup_printf ("%'lld", (long long) number); |
289 | 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) |
372 | 59 g_snprintf (src, sizeof (src), "%lld", (long long) number); |
220 | 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 |
374 | 265 gftp_match_filespec (const char *filename, const char *filespec) |
1 | 266 { |
374 | 267 const char *filepos, *wcpos, *pos; |
268 char search_str[20], *newpos; | |
1 | 269 size_t len, curlen; |
270 | |
271 if (filename == NULL || *filename == '\0' || | |
272 filespec == NULL || *filespec == '\0') | |
273 return(1); | |
274 | |
275 filepos = filename; | |
276 wcpos = filespec; | |
277 while(1) | |
278 { | |
279 if (*wcpos == '\0') | |
280 return (1); | |
281 else if (*filepos == '\0') | |
282 return(0); | |
283 else if(*wcpos == '?') | |
284 { | |
285 wcpos++; | |
286 filepos++; | |
287 } | |
288 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
289 return(1); | |
290 else if(*wcpos == '*') | |
291 { | |
292 len = sizeof (search_str); | |
293 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
294 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
295 curlen++, *newpos++ = *pos++); | |
296 *newpos = '\0'; | |
297 | |
298 if ((filepos = strstr (filepos, search_str)) == NULL) | |
299 return(0); | |
300 wcpos += curlen + 1; | |
301 filepos += curlen; | |
302 } | |
303 else if(*wcpos++ != *filepos++) | |
304 return(0); | |
305 } | |
306 return (1); | |
307 } | |
308 | |
309 | |
243 | 310 static void |
311 gftp_info (void) | |
312 { | |
260 | 313 int i; |
314 | |
243 | 315 printf ("%s\n", gftp_version); |
316 | |
307 | 317 #ifdef _REENTRANT |
318 printf ("#define _REENTRANT\n"); | |
319 #endif | |
320 | |
289 | 321 #ifdef _GNU_SOURCE |
322 printf ("#define _GNU_SOURCE\n"); | |
323 #endif | |
324 | |
243 | 325 #ifdef _LARGEFILE_SOURCE |
260 | 326 printf ("#define _LARGEFILE_SOURCE\n"); |
243 | 327 #endif |
328 | |
329 #ifdef _FILE_OFFSET_BITS | |
330 printf ("#define _FILE_OFFSET_BITS %d\n", _FILE_OFFSET_BITS); | |
331 #endif | |
332 | |
333 printf ("sizeof (off_t) = %d\n", sizeof (off_t)); | |
334 | |
335 #ifdef HAVE_GETADDRINFO | |
336 printf ("#define HAVE_GETADDRINFO\n"); | |
337 #endif | |
338 | |
339 #ifdef HAVE_GAI_STRERROR | |
340 printf ("#define HAVE_GAI_STRERROR\n"); | |
341 #endif | |
342 | |
343 #ifdef HAVE_GETDTABLESIZE | |
344 printf ("#define HAVE_GETDTABLESIZE\n"); | |
345 #endif | |
346 | |
347 #ifdef G_HAVE_GINT64 | |
348 printf ("#define G_HAVE_GINT64\n"); | |
349 #endif | |
350 | |
351 #ifdef HAVE_LIBREADLINE | |
352 printf ("#define HAVE_LIBREADLINE\n"); | |
353 #endif | |
354 | |
355 #ifdef ENABLE_NLS | |
356 printf ("#define ENABLE_NLS\n"); | |
357 #endif | |
358 | |
359 #ifdef HAVE_GETTEXT | |
360 printf ("#define HAVE_GETTEXT\n"); | |
361 #endif | |
362 | |
328 | 363 #ifdef HAVE_INTL_PRINTF |
364 printf ("#define HAVE_INTL_PRINTF\n"); | |
365 #endif | |
366 | |
243 | 367 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, |
368 GLIB_MICRO_VERSION); | |
369 | |
370 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
371 | |
372 #ifdef USE_SSL | |
373 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
374 #endif | |
260 | 375 |
376 printf ("Enabled protocols: "); | |
377 for (i=0; gftp_protocols[i].name != NULL; i++) | |
378 { | |
379 printf ("%s ", gftp_protocols[i].name); | |
380 } | |
381 printf ("\n"); | |
243 | 382 } |
383 | |
384 | |
1 | 385 int |
386 gftp_parse_command_line (int *argc, char ***argv) | |
387 { | |
388 if (*argc > 1) | |
389 { | |
87 | 390 if (strcmp (argv[0][1], "--help") == 0 || |
391 strcmp (argv[0][1], "-h") == 0) | |
349 | 392 { |
393 gftp_usage (); | |
394 exit (0); | |
395 } | |
87 | 396 else if (strcmp (argv[0][1], "--version") == 0 || |
397 strcmp (argv[0][1], "-v") == 0) | |
1 | 398 { |
349 | 399 printf ("%s\n", gftp_version); |
400 exit (0); | |
1 | 401 } |
243 | 402 else if (strcmp (argv[0][1], "--info") == 0) |
403 { | |
404 gftp_info (); | |
349 | 405 exit (0); |
243 | 406 } |
1 | 407 } |
408 return (0); | |
409 } | |
410 | |
411 | |
412 void | |
413 gftp_usage (void) | |
414 { | |
356 | 415 printf (_("usage: gftp " GFTP_URL_USAGE "\n")); |
1 | 416 exit (0); |
417 } | |
418 | |
419 | |
420 gint | |
421 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
422 { | |
423 return (strcmp ((char *) path1, (char *) path2) == 0); | |
424 } | |
425 | |
426 | |
427 guint | |
428 string_hash_function (gconstpointer key) | |
429 { | |
59 | 430 guint ret; |
431 int i; | |
432 | |
433 ret = 0; | |
87 | 434 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 435 ret += ((char *) key)[i]; |
436 | |
437 return (ret); | |
1 | 438 } |
439 | |
440 | |
441 void | |
442 free_file_list (GList * filelist) | |
443 { | |
444 gftp_file * tempfle; | |
445 GList * templist; | |
446 | |
447 templist = filelist; | |
448 while (templist != NULL) | |
449 { | |
450 tempfle = templist->data; | |
451 free_fdata (tempfle); | |
452 templist = templist->next; | |
453 } | |
454 g_list_free (filelist); | |
455 } | |
456 | |
457 | |
458 void | |
459 free_fdata (gftp_file * fle) | |
460 { | |
461 if (fle->file) | |
462 g_free (fle->file); | |
184 | 463 if (fle->utf8_file) |
464 g_free (fle->utf8_file); | |
1 | 465 if (fle->user) |
466 g_free (fle->user); | |
467 if (fle->group) | |
468 g_free (fle->group); | |
469 if (fle->attribs) | |
470 g_free (fle->attribs); | |
471 if (fle->destfile) | |
472 g_free (fle->destfile); | |
58 | 473 if (fle->fd > 0) |
102 | 474 close (fle->fd); |
1 | 475 g_free (fle); |
476 } | |
477 | |
478 | |
479 gftp_file * | |
480 copy_fdata (gftp_file * fle) | |
481 { | |
482 gftp_file * newfle; | |
483 | |
484 newfle = g_malloc0 (sizeof (*newfle)); | |
485 memcpy (newfle, fle, sizeof (*newfle)); | |
486 | |
487 if (fle->file) | |
87 | 488 newfle->file = g_strdup (fle->file); |
1 | 489 |
243 | 490 if (fle->utf8_file) |
491 newfle->utf8_file = g_strdup (fle->utf8_file); | |
492 | |
1 | 493 if (fle->user) |
87 | 494 newfle->user = g_strdup (fle->user); |
1 | 495 |
496 if (fle->group) | |
87 | 497 newfle->group = g_strdup (fle->group); |
1 | 498 |
499 if (fle->attribs) | |
87 | 500 newfle->attribs = g_strdup (fle->attribs); |
1 | 501 |
502 if (fle->destfile) | |
87 | 503 newfle->destfile = g_strdup (fle->destfile); |
504 | |
1 | 505 return (newfle); |
506 } | |
507 | |
508 | |
509 int | |
510 compare_request (gftp_request * request1, gftp_request * request2, | |
511 int compare_dirs) | |
512 { | |
513 char *strarr[3][2]; | |
514 int i, ret; | |
515 | |
516 ret = 1; | |
87 | 517 if (request1->protonum == request2->protonum && |
1 | 518 request1->port == request2->port) |
519 { | |
520 strarr[0][0] = request1->hostname; | |
521 strarr[0][1] = request2->hostname; | |
522 strarr[1][0] = request1->username; | |
523 strarr[1][1] = request2->username; | |
524 if (compare_dirs) | |
525 { | |
526 strarr[2][0] = request1->directory; | |
527 strarr[2][1] = request2->directory; | |
528 } | |
529 else | |
530 strarr[2][0] = strarr[2][1] = ""; | |
531 | |
532 for (i = 0; i < 3; i++) | |
533 { | |
534 if ((strarr[i][0] && !strarr[i][1]) || | |
535 (!strarr[i][0] && strarr[i][1])) | |
536 { | |
537 ret = 0; | |
538 break; | |
539 } | |
540 | |
541 if (strarr[i][0] && strarr[i][1] && | |
542 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
543 { | |
544 ret = 0; | |
545 break; | |
546 } | |
547 } | |
548 } | |
549 else | |
550 ret = 0; | |
551 return (ret); | |
552 } | |
553 | |
554 | |
129 | 555 gftp_transfer * |
556 gftp_tdata_new (void) | |
557 { | |
227 | 558 #if GLIB_MAJOR_VERSION == 1 |
559 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
560 #endif | |
129 | 561 gftp_transfer * tdata; |
562 | |
563 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 564 |
565 #if GLIB_MAJOR_VERSION == 1 | |
566 tdata->statmutex = init_mutex; | |
567 tdata->structmutex = init_mutex; | |
568 #else | |
168 | 569 g_static_mutex_init (&tdata->statmutex); |
570 g_static_mutex_init (&tdata->structmutex); | |
227 | 571 #endif |
572 | |
129 | 573 return (tdata); |
574 } | |
575 | |
576 | |
1 | 577 void |
578 free_tdata (gftp_transfer * tdata) | |
579 { | |
580 if (tdata->fromreq != NULL) | |
67 | 581 gftp_request_destroy (tdata->fromreq, 1); |
1 | 582 if (tdata->toreq != NULL) |
67 | 583 gftp_request_destroy (tdata->toreq, 1); |
1 | 584 free_file_list (tdata->files); |
585 g_free (tdata); | |
586 } | |
587 | |
588 | |
589 gftp_request * | |
368 | 590 gftp_copy_request (gftp_request * req) |
1 | 591 { |
592 gftp_request * newreq; | |
593 | |
66 | 594 newreq = gftp_request_new (); |
1 | 595 |
596 if (req->hostname) | |
56 | 597 newreq->hostname = g_strdup (req->hostname); |
1 | 598 if (req->username) |
56 | 599 newreq->username = g_strdup (req->username); |
1 | 600 if (req->password) |
56 | 601 newreq->password = g_strdup (req->password); |
1 | 602 if (req->account) |
56 | 603 newreq->account = g_strdup (req->account); |
1 | 604 if (req->directory) |
56 | 605 newreq->directory = g_strdup (req->directory); |
66 | 606 newreq->port = req->port; |
607 newreq->use_proxy = req->use_proxy; | |
608 newreq->logging_function = req->logging_function; | |
151 | 609 newreq->free_hostp = 0; |
296 | 610 newreq->hostp = NULL; |
151 | 611 |
368 | 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) | |
372 | 922 tempstr2 = g_strdup_printf ("%11lld", (long long) fle->size); |
131 | 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 | |
369 | 1198 if (retlen > 0 && (ret[retlen - 1] == '/' || element[0] == '/')) |
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 | |
378 | 1319 |
1320 int | |
1321 gftp_get_transfer_action (gftp_request * request, gftp_file * fle) | |
1322 { | |
1323 intptr_t overwrite_default; | |
1324 | |
1325 gftp_lookup_request_option (request, "overwrite_default", &overwrite_default); | |
1326 | |
1327 if (overwrite_default) | |
1328 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1329 else if (fle->startsize == fle->size) | |
1330 fle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
1331 else if (fle->startsize > fle->size) | |
1332 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
1333 else | |
1334 fle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
1335 | |
1336 return (fle->transfer_action); | |
1337 } | |
1338 |