Mercurial > gftp.yaz
annotate lib/misc.c @ 325:0fcc6468a0af
2003-12-4 Brian Masney <masneyb@gftp.org>
* lib/cache.c lib/misc.c lib/protocols.c lib/rfc2068.c lib/rfc959.c
lib/sshv2.c lib/sslcommon.c src/text/gftp-text.c src/gtk/chmod_dialog.c
src/gtk/gftp-gtk.c src/gtk/menu-items.c src/gtk/misc-gtk.c
src/gtk/mkdir_dialog.c src/gtk/rename_dialog.c src/gtk/transfer.c -
when calling gftp_lookup_global_option() or
gftp_lookup_request_option(), if the value is an integer, declare
the variable type to be intptr_t. This fixes a bug on 64bit platforms
(from Gwenole Beauchesne <gbeauchesne@mandrakesoft.com>)
* lib/config_file.c (gftp_config_file_read_float) - 64bit fixup
* configure.in - increment version to 2.0.17pre0. Undefine _GNU_SOURCE.
Check for stdint.h.
* lib/gftp.h - include stdint.h if it is found on the system.
* src/gtk/gftp-gtk.c (CreateToolbar) - on startup, have the host edit
box grab the keyboard focus
author | masneyb |
---|---|
date | Fri, 05 Dec 2003 00:19:48 +0000 |
parents | 853981bbd4d7 |
children | 51bb530a100c |
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 | |
289 | 25 #ifdef _GNU_SOURCE |
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 | |
362 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, | |
363 GLIB_MICRO_VERSION); | |
364 | |
365 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
366 | |
367 #ifdef USE_SSL | |
368 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
369 #endif | |
260 | 370 |
371 printf ("Enabled protocols: "); | |
372 for (i=0; gftp_protocols[i].name != NULL; i++) | |
373 { | |
374 printf ("%s ", gftp_protocols[i].name); | |
375 } | |
376 printf ("\n"); | |
243 | 377 } |
378 | |
379 | |
1 | 380 int |
381 gftp_parse_command_line (int *argc, char ***argv) | |
382 { | |
383 if (*argc > 1) | |
384 { | |
87 | 385 if (strcmp (argv[0][1], "--help") == 0 || |
386 strcmp (argv[0][1], "-h") == 0) | |
1 | 387 gftp_usage (); |
87 | 388 else if (strcmp (argv[0][1], "--version") == 0 || |
389 strcmp (argv[0][1], "-v") == 0) | |
1 | 390 { |
122 | 391 printf ("%s\n", gftp_version); |
1 | 392 exit (0); |
393 } | |
243 | 394 else if (strcmp (argv[0][1], "--info") == 0) |
395 { | |
396 gftp_info (); | |
397 exit (0); | |
398 } | |
1 | 399 } |
400 return (0); | |
401 } | |
402 | |
403 | |
404 void | |
405 gftp_usage (void) | |
406 { | |
162 | 407 printf (_("usage: gftp [[protocol://][user[:pass]@]site[:port][/directory]]\n")); |
1 | 408 exit (0); |
409 } | |
410 | |
411 | |
412 gint | |
413 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
414 { | |
415 return (strcmp ((char *) path1, (char *) path2) == 0); | |
416 } | |
417 | |
418 | |
419 guint | |
420 string_hash_function (gconstpointer key) | |
421 { | |
59 | 422 guint ret; |
423 int i; | |
424 | |
425 ret = 0; | |
87 | 426 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 427 ret += ((char *) key)[i]; |
428 | |
429 return (ret); | |
1 | 430 } |
431 | |
432 | |
433 void | |
434 free_file_list (GList * filelist) | |
435 { | |
436 gftp_file * tempfle; | |
437 GList * templist; | |
438 | |
439 templist = filelist; | |
440 while (templist != NULL) | |
441 { | |
442 tempfle = templist->data; | |
443 free_fdata (tempfle); | |
444 templist = templist->next; | |
445 } | |
446 g_list_free (filelist); | |
447 } | |
448 | |
449 | |
450 void | |
451 free_fdata (gftp_file * fle) | |
452 { | |
453 if (fle->file) | |
454 g_free (fle->file); | |
184 | 455 if (fle->utf8_file) |
456 g_free (fle->utf8_file); | |
1 | 457 if (fle->user) |
458 g_free (fle->user); | |
459 if (fle->group) | |
460 g_free (fle->group); | |
461 if (fle->attribs) | |
462 g_free (fle->attribs); | |
463 if (fle->destfile) | |
464 g_free (fle->destfile); | |
58 | 465 if (fle->fd > 0) |
102 | 466 close (fle->fd); |
1 | 467 g_free (fle); |
468 } | |
469 | |
470 | |
471 gftp_file * | |
472 copy_fdata (gftp_file * fle) | |
473 { | |
474 gftp_file * newfle; | |
475 | |
476 newfle = g_malloc0 (sizeof (*newfle)); | |
477 memcpy (newfle, fle, sizeof (*newfle)); | |
478 | |
479 if (fle->file) | |
87 | 480 newfle->file = g_strdup (fle->file); |
1 | 481 |
243 | 482 if (fle->utf8_file) |
483 newfle->utf8_file = g_strdup (fle->utf8_file); | |
484 | |
1 | 485 if (fle->user) |
87 | 486 newfle->user = g_strdup (fle->user); |
1 | 487 |
488 if (fle->group) | |
87 | 489 newfle->group = g_strdup (fle->group); |
1 | 490 |
491 if (fle->attribs) | |
87 | 492 newfle->attribs = g_strdup (fle->attribs); |
1 | 493 |
494 if (fle->destfile) | |
87 | 495 newfle->destfile = g_strdup (fle->destfile); |
496 | |
1 | 497 return (newfle); |
498 } | |
499 | |
500 | |
501 int | |
502 compare_request (gftp_request * request1, gftp_request * request2, | |
503 int compare_dirs) | |
504 { | |
505 char *strarr[3][2]; | |
506 int i, ret; | |
507 | |
508 ret = 1; | |
87 | 509 if (request1->protonum == request2->protonum && |
1 | 510 request1->port == request2->port) |
511 { | |
512 strarr[0][0] = request1->hostname; | |
513 strarr[0][1] = request2->hostname; | |
514 strarr[1][0] = request1->username; | |
515 strarr[1][1] = request2->username; | |
516 if (compare_dirs) | |
517 { | |
518 strarr[2][0] = request1->directory; | |
519 strarr[2][1] = request2->directory; | |
520 } | |
521 else | |
522 strarr[2][0] = strarr[2][1] = ""; | |
523 | |
524 for (i = 0; i < 3; i++) | |
525 { | |
526 if ((strarr[i][0] && !strarr[i][1]) || | |
527 (!strarr[i][0] && strarr[i][1])) | |
528 { | |
529 ret = 0; | |
530 break; | |
531 } | |
532 | |
533 if (strarr[i][0] && strarr[i][1] && | |
534 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
535 { | |
536 ret = 0; | |
537 break; | |
538 } | |
539 } | |
540 } | |
541 else | |
542 ret = 0; | |
543 return (ret); | |
544 } | |
545 | |
546 | |
129 | 547 gftp_transfer * |
548 gftp_tdata_new (void) | |
549 { | |
227 | 550 #if GLIB_MAJOR_VERSION == 1 |
551 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
552 #endif | |
129 | 553 gftp_transfer * tdata; |
554 | |
555 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 556 |
557 #if GLIB_MAJOR_VERSION == 1 | |
558 tdata->statmutex = init_mutex; | |
559 tdata->structmutex = init_mutex; | |
560 #else | |
168 | 561 g_static_mutex_init (&tdata->statmutex); |
562 g_static_mutex_init (&tdata->structmutex); | |
227 | 563 #endif |
564 | |
129 | 565 return (tdata); |
566 } | |
567 | |
568 | |
1 | 569 void |
570 free_tdata (gftp_transfer * tdata) | |
571 { | |
572 if (tdata->fromreq != NULL) | |
67 | 573 gftp_request_destroy (tdata->fromreq, 1); |
1 | 574 if (tdata->toreq != NULL) |
67 | 575 gftp_request_destroy (tdata->toreq, 1); |
1 | 576 free_file_list (tdata->files); |
577 g_free (tdata); | |
578 } | |
579 | |
580 | |
581 gftp_request * | |
151 | 582 copy_request (gftp_request * req, int copy_local_options) |
1 | 583 { |
584 gftp_request * newreq; | |
585 | |
66 | 586 newreq = gftp_request_new (); |
1 | 587 |
588 if (req->hostname) | |
56 | 589 newreq->hostname = g_strdup (req->hostname); |
1 | 590 if (req->username) |
56 | 591 newreq->username = g_strdup (req->username); |
1 | 592 if (req->password) |
56 | 593 newreq->password = g_strdup (req->password); |
1 | 594 if (req->account) |
56 | 595 newreq->account = g_strdup (req->account); |
1 | 596 if (req->directory) |
56 | 597 newreq->directory = g_strdup (req->directory); |
66 | 598 newreq->port = req->port; |
599 newreq->use_proxy = req->use_proxy; | |
600 newreq->logging_function = req->logging_function; | |
151 | 601 newreq->free_hostp = 0; |
296 | 602 newreq->hostp = NULL; |
151 | 603 |
604 if (copy_local_options) | |
199 | 605 gftp_copy_local_options (&newreq->local_options_vars, |
606 &newreq->local_options_hash, | |
607 req->local_options_vars, | |
608 req->num_local_options_vars); | |
1 | 609 |
173 | 610 if (req->init (newreq) < 0) |
611 { | |
612 gftp_request_destroy (newreq, 1); | |
613 return (NULL); | |
614 } | |
1 | 615 |
309 | 616 gftp_copy_param_options (newreq, req); |
617 | |
1 | 618 return (newreq); |
619 } | |
620 | |
621 | |
16 | 622 static gint |
623 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
624 { | |
625 const gftp_file * f1, * f2; | |
626 | |
627 f1 = a; | |
628 f2 = b; | |
629 return (strcmp (f1->file, f2->file)); | |
630 } | |
631 | |
632 | |
633 static gint | |
634 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
635 { | |
636 const gftp_file * f1, * f2; | |
637 gint ret; | |
638 | |
639 f1 = a; | |
640 f2 = b; | |
641 ret = strcmp (f1->file, f2->file); | |
84 | 642 return (ret * -1); |
16 | 643 } |
644 | |
645 | |
646 static gint | |
647 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
648 { | |
649 const gftp_file * f1, * f2; | |
650 | |
651 f1 = a; | |
652 f2 = b; | |
653 return (strcmp (f1->user, f2->user)); | |
654 } | |
655 | |
656 | |
657 static gint | |
658 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
659 { | |
660 const gftp_file * f1, * f2; | |
661 gint ret; | |
662 | |
663 f1 = a; | |
664 f2 = b; | |
665 ret = strcmp (f1->user, f2->user); | |
84 | 666 return (ret * -1); |
16 | 667 } |
668 | |
669 | |
670 static gint | |
671 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
672 { | |
673 const gftp_file * f1, * f2; | |
674 | |
675 f1 = a; | |
676 f2 = b; | |
677 return (strcmp (f1->group, f2->group)); | |
678 } | |
679 | |
680 | |
681 static gint | |
682 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
683 { | |
684 const gftp_file * f1, * f2; | |
685 gint ret; | |
686 | |
687 f1 = a; | |
688 f2 = b; | |
689 ret = strcmp (f1->group, f2->group); | |
84 | 690 return (ret * -1); |
16 | 691 } |
692 | |
693 | |
694 static gint | |
695 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
696 { | |
697 const gftp_file * f1, * f2; | |
698 | |
699 f1 = a; | |
700 f2 = b; | |
701 return (strcmp (f1->attribs, f2->attribs)); | |
702 } | |
703 | |
704 | |
705 static gint | |
706 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
707 { | |
708 const gftp_file * f1, * f2; | |
709 gint ret; | |
710 | |
711 f1 = a; | |
712 f2 = b; | |
713 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 714 return (ret * -1); |
16 | 715 } |
716 | |
717 | |
718 static gint | |
719 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
720 { | |
721 const gftp_file * f1, * f2; | |
722 | |
723 f1 = a; | |
724 f2 = b; | |
725 if (f1->size < f2->size) | |
726 return (-1); | |
727 else if (f1->size == f2->size) | |
728 return (0); | |
729 else | |
730 return (1); | |
731 } | |
732 | |
733 | |
734 static gint | |
735 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
736 { | |
737 const gftp_file * f1, * f2; | |
738 | |
739 f1 = a; | |
740 f2 = b; | |
741 if (f1->size < f2->size) | |
742 return (1); | |
743 else if (f1->size == f2->size) | |
744 return (0); | |
745 else | |
746 return (-1); | |
747 } | |
748 | |
749 | |
750 static gint | |
751 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
752 { | |
753 const gftp_file * f1, * f2; | |
754 | |
755 f1 = a; | |
756 f2 = b; | |
757 if (f1->datetime < f2->datetime) | |
758 return (-1); | |
759 else if (f1->datetime == f2->datetime) | |
760 return (0); | |
761 else | |
762 return (1); | |
763 } | |
764 | |
765 | |
766 static gint | |
767 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
768 { | |
769 const gftp_file * f1, * f2; | |
770 | |
771 f1 = a; | |
772 f2 = b; | |
773 if (f1->datetime < f2->datetime) | |
774 return (1); | |
775 else if (f1->datetime == f2->datetime) | |
776 return (0); | |
777 else | |
778 return (-1); | |
779 } | |
780 | |
781 | |
782 GList * | |
783 gftp_sort_filelist (GList * filelist, int column, int asds) | |
784 { | |
785 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
786 GCompareFunc sortfunc; | |
787 gftp_file * tempfle; | |
325 | 788 intptr_t sort_dirs_first; |
16 | 789 |
790 files = dirs = dotdot = NULL; | |
791 | |
792 if (column == GFTP_SORT_COL_FILE) | |
793 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
794 else if (column == GFTP_SORT_COL_SIZE) | |
795 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
796 else if (column == GFTP_SORT_COL_USER) | |
797 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
798 else if (column == GFTP_SORT_COL_GROUP) | |
799 sortfunc = asds ? | |
800 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
801 else if (column == GFTP_SORT_COL_DATETIME) | |
802 sortfunc = asds ? | |
122 | 803 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
804 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 805 sortfunc = asds ? |
806 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 807 else /* Don't sort */ |
808 return (filelist); | |
809 | |
810 sort_dirs_first = 1; | |
811 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 812 |
813 for (tempitem = filelist; tempitem != NULL; ) | |
814 { | |
815 tempfle = tempitem->data; | |
816 insitem = tempitem; | |
817 tempitem = tempitem->next; | |
818 insitem->next = NULL; | |
819 | |
820 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
821 dotdot = insitem; | |
822 else if (sort_dirs_first && tempfle->isdir) | |
823 { | |
824 insitem->next = dirs; | |
825 dirs = insitem; | |
826 } | |
827 else | |
828 { | |
829 insitem->next = files; | |
830 files = insitem; | |
831 } | |
832 } | |
833 | |
834 if (dirs != NULL) | |
835 dirs = g_list_sort (dirs, sortfunc); | |
836 if (files != NULL) | |
837 files = g_list_sort (files, sortfunc); | |
838 | |
839 filelist = dotdot; | |
840 | |
841 if (filelist == NULL) | |
842 filelist = dirs; | |
843 else | |
844 filelist = g_list_concat (filelist, dirs); | |
845 | |
846 if (filelist == NULL) | |
847 filelist = files; | |
848 else | |
849 filelist = g_list_concat (filelist, files); | |
850 | |
39 | 851 /* I haven't check this, but I'm pretty sure some older versions of glib |
852 had a bug that the prev pointer wasn't being sent to NULL */ | |
853 filelist->prev = NULL; | |
16 | 854 return (filelist); |
855 } | |
856 | |
125 | 857 |
858 mode_t | |
859 gftp_parse_attribs (char *attribs) | |
860 { | |
861 mode_t mode; | |
862 int cur; | |
863 | |
864 cur = 0; | |
865 if (attribs[1] == 'r') | |
866 cur += 4; | |
867 if (attribs[2] == 'w') | |
868 cur += 2; | |
869 if (attribs[3] == 'x' || | |
870 attribs[3] == 's') | |
871 cur += 1; | |
872 mode = cur; | |
873 | |
874 cur = 0; | |
875 if (attribs[4] == 'r') | |
876 cur += 4; | |
877 if (attribs[5] == 'w') | |
878 cur += 2; | |
879 if (attribs[6] == 'x' || | |
880 attribs[6] == 's') | |
881 cur += 1; | |
882 mode = (mode * 10) + cur; | |
883 | |
884 cur = 0; | |
885 if (attribs[7] == 'r') | |
886 cur += 4; | |
887 if (attribs[8] == 'w') | |
888 cur += 2; | |
889 if (attribs[9] == 'x' || | |
890 attribs[9] == 's') | |
891 cur += 1; | |
892 mode = (mode * 10) + cur; | |
893 | |
894 return (mode); | |
895 } | |
896 | |
897 | |
131 | 898 char * |
899 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
900 { | |
901 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
902 struct tm *lt; | |
903 time_t t; | |
904 | |
905 lt = localtime (&fle->datetime); | |
906 | |
907 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
908 fle->group); | |
909 | |
910 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
911 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
912 else | |
913 { | |
914 #if defined (_LARGEFILE_SOURCE) | |
915 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
916 #else | |
917 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
918 #endif | |
919 } | |
920 | |
921 time (&t); | |
922 | |
923 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
924 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
925 else | |
926 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
927 | |
928 if (file_prefixstr == NULL) | |
929 file_prefixstr = ""; | |
930 if (file_suffixstr == NULL) | |
931 file_suffixstr = ""; | |
932 | |
933 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 934 file_prefixstr, |
935 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
936 file_suffixstr); | |
131 | 937 |
938 g_free (tempstr1); | |
939 g_free (tempstr2); | |
940 | |
941 return (ret); | |
942 } | |
943 | |
944 | |
125 | 945 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
946 | |
947 struct hostent * | |
948 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
949 { | |
950 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
951 struct hostent *hent; | |
952 | |
953 if (g_thread_supported ()) | |
954 g_static_mutex_lock (&hostfunclock); | |
955 | |
956 if ((hent = gethostbyname (name)) == NULL) | |
957 { | |
958 if (h_errnop) | |
959 *h_errnop = h_errno; | |
960 } | |
961 else | |
962 { | |
963 *result_buf = *hent; | |
964 hent = result_buf; | |
965 } | |
966 | |
967 if (g_thread_supported ()) | |
968 g_static_mutex_unlock (&hostfunclock); | |
969 | |
970 return (hent); | |
971 } | |
972 | |
973 #endif /* !HAVE_GETADDRINFO */ | |
974 | |
975 struct servent * | |
976 r_getservbyname (const char *name, const char *proto, | |
977 struct servent *result_buf, int *h_errnop) | |
978 { | |
979 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
980 struct servent *sent; | |
981 | |
982 if (g_thread_supported ()) | |
983 g_static_mutex_lock (&servfunclock); | |
984 | |
985 if ((sent = getservbyname (name, proto)) == NULL) | |
986 { | |
987 if (h_errnop) | |
988 *h_errnop = h_errno; | |
989 } | |
990 else | |
991 { | |
992 *result_buf = *sent; | |
993 sent = result_buf; | |
994 } | |
995 | |
996 if (g_thread_supported ()) | |
997 g_static_mutex_unlock (&servfunclock); | |
998 return (sent); | |
999 } | |
1000 | |
168 | 1001 |
1002 char * | |
1003 base64_encode (char *str) | |
1004 { | |
1005 | |
1006 /* The standard to Base64 encoding can be found in RFC2045 */ | |
1007 | |
1008 char *newstr, *newpos, *fillpos, *pos; | |
1009 unsigned char table[64], encode[3]; | |
1010 int i, num; | |
1011 | |
1012 for (i = 0; i < 26; i++) | |
1013 { | |
1014 table[i] = 'A' + i; | |
1015 table[i + 26] = 'a' + i; | |
1016 } | |
1017 | |
1018 for (i = 0; i < 10; i++) | |
1019 table[i + 52] = '0' + i; | |
1020 | |
1021 table[62] = '+'; | |
207 | 1022 table[63] = '/'; |
168 | 1023 |
1024 num = strlen (str) / 3; | |
1025 if (strlen (str) % 3 > 0) | |
1026 num++; | |
1027 newstr = g_malloc (num * 4 + 1); | |
1028 newstr[num * 4] = '\0'; | |
1029 newpos = newstr; | |
1030 | |
1031 pos = str; | |
1032 while (*pos != '\0') | |
1033 { | |
1034 memset (encode, 0, sizeof (encode)); | |
1035 for (i = 0; i < 3 && *pos != '\0'; i++) | |
1036 encode[i] = *pos++; | |
1037 | |
1038 fillpos = newpos; | |
1039 *newpos++ = table[encode[0] >> 2]; | |
1040 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1041 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1042 *newpos++ = table[encode[2] & 0x3F]; | |
1043 while (i < 3) | |
1044 fillpos[++i] = '='; | |
1045 } | |
1046 return (newstr); | |
1047 } | |
1048 | |
199 | 1049 |
1050 void | |
1051 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1052 { | |
1053 if (entry->hostname) | |
1054 g_free (entry->hostname); | |
1055 if (entry->remote_dir) | |
1056 g_free (entry->remote_dir); | |
1057 if (entry->local_dir) | |
1058 g_free (entry->local_dir); | |
1059 if (entry->user) | |
1060 g_free (entry->user); | |
1061 if (entry->pass) | |
1062 g_free (entry->pass); | |
1063 if (entry->acct) | |
1064 g_free (entry->acct); | |
1065 if (entry->protocol) | |
1066 g_free (entry->protocol); | |
1067 | |
1068 if (entry->local_options_vars != NULL) | |
1069 { | |
201 | 1070 gftp_config_free_options (entry->local_options_vars, |
1071 entry->local_options_hash, | |
1072 entry->num_local_options_vars); | |
1073 | |
199 | 1074 entry->local_options_vars = NULL; |
201 | 1075 entry->local_options_hash = NULL; |
199 | 1076 entry->num_local_options_vars = 0; |
1077 } | |
1078 } | |
1079 | |
201 | 1080 |
1081 void | |
1082 gftp_shutdown (void) | |
1083 { | |
203 | 1084 #ifdef WITH_DMALLOC |
201 | 1085 gftp_config_vars * cv; |
1086 GList * templist; | |
203 | 1087 #endif |
201 | 1088 |
1089 if (gftp_logfd != NULL) | |
1090 fclose (gftp_logfd); | |
1091 | |
1092 gftp_clear_cache_files (); | |
1093 | |
1094 if (gftp_configuration_changed) | |
1095 gftp_write_config_file (); | |
1096 | |
1097 #ifdef WITH_DMALLOC | |
1098 if (gftp_global_options_htable != NULL) | |
1099 g_hash_table_destroy (gftp_global_options_htable); | |
1100 | |
1101 if (gftp_config_list_htable != NULL) | |
1102 g_hash_table_destroy (gftp_config_list_htable); | |
1103 | |
1104 if (gftp_bookmarks_htable != NULL) | |
1105 g_hash_table_destroy (gftp_bookmarks_htable); | |
1106 | |
1107 for (templist = gftp_options_list; | |
1108 templist != NULL; | |
1109 templist = templist->next) | |
1110 { | |
1111 cv = templist->data; | |
1112 gftp_config_free_options (cv, NULL, -1); | |
1113 } | |
1114 | |
1115 gftp_bookmarks_destroy (gftp_bookmarks); | |
1116 | |
1117 dmalloc_shutdown (); | |
1118 #endif | |
1119 | |
1120 exit (0); | |
1121 } | |
1122 | |
207 | 1123 |
1124 GList * | |
1125 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1126 { | |
1127 gftp_file * tempfle; | |
1128 int i, newpos; | |
1129 | |
1130 newpos = GPOINTER_TO_INT (selection->data); | |
1131 i = *curnum - newpos; | |
1132 | |
1133 if (i < 0) | |
1134 { | |
1135 while (i != 0) | |
1136 { | |
1137 tempfle = (*list)->data; | |
1138 if (tempfle->shown) | |
1139 { | |
1140 ++*curnum; | |
1141 i++; | |
1142 } | |
1143 *list = (*list)->next; | |
1144 } | |
1145 } | |
1146 else if (i > 0) | |
1147 { | |
1148 while (i != 0) | |
1149 { | |
1150 tempfle = (*list)->data; | |
1151 if (tempfle->shown) | |
1152 { | |
1153 --*curnum; | |
1154 i--; | |
1155 } | |
1156 *list = (*list)->prev; | |
1157 } | |
1158 } | |
1159 | |
1160 tempfle = (*list)->data; | |
1161 while ((*list)->next && !tempfle->shown) | |
1162 { | |
1163 *list = (*list)->next; | |
1164 tempfle = (*list)->data; | |
1165 } | |
1166 return (selection->next); | |
1167 } | |
1168 | |
1169 | |
227 | 1170 char * |
245 | 1171 gftp_build_path (const char *first_element, ...) |
227 | 1172 { |
1173 const char *element; | |
1174 size_t len, retlen; | |
1175 int add_separator; | |
1176 va_list args; | |
1177 char *ret; | |
1178 | |
245 | 1179 g_return_val_if_fail (first_element != NULL, NULL); |
227 | 1180 |
247 | 1181 ret = g_strdup (first_element); |
1182 retlen = strlen (ret); | |
227 | 1183 |
1184 va_start (args, first_element); | |
247 | 1185 for (element = va_arg (args, char *); |
227 | 1186 element != NULL; |
1187 element = va_arg (args, char *)) | |
1188 { | |
1189 len = strlen (element); | |
1190 | |
307 | 1191 if (retlen > 0 && ret[retlen - 1] == '/') |
227 | 1192 add_separator = 0; |
1193 else | |
1194 { | |
1195 add_separator = 1; | |
1196 len++; | |
1197 } | |
1198 | |
1199 retlen += len; | |
1200 ret = g_realloc (ret, retlen + 1); | |
1201 | |
1202 if (add_separator) | |
245 | 1203 strncat (ret, "/", retlen); |
1204 | |
1205 strncat (ret, element, retlen); | |
227 | 1206 } |
1207 | |
1208 return (ret); | |
1209 } | |
1210 | |
244 | 1211 |
1212 off_t | |
1213 gftp_parse_file_size (char *str) | |
1214 { | |
1215 #if defined (_LARGEFILE_SOURCE) | |
1216 return (strtoll (str, NULL, 10)); | |
1217 #else | |
1218 return (strtol (str, NULL, 10)); | |
1219 #endif | |
1220 } | |
1221 | |
290 | 1222 |
1223 void | |
1224 gftp_locale_init (void) | |
1225 { | |
1226 #ifdef HAVE_GETTEXT | |
1227 | |
1228 setlocale (LC_ALL, ""); | |
1229 textdomain ("gftp"); | |
320 | 1230 bindtextdomain ("gftp", LOCALE_DIR); |
290 | 1231 |
1232 #if GLIB_MAJOR_VERSION > 1 | |
1233 bind_textdomain_codeset ("gftp", "UTF-8"); | |
1234 #endif | |
1235 | |
320 | 1236 #endif /* HAVE_GETTEXT */ |
290 | 1237 } |
1238 |