Mercurial > gftp.yaz
annotate lib/misc.c @ 290:6e255984c0b0
2003-10-18 Brian Masney <masneyb@gftp.org>
* lib/misc.c lib/gftp.h src/text/gftp-text.c src/gtk/gftp-gtk.c - added
gftp_locale_init().
* src/gtk/Makefile.am src/text/Makefile.am - removed declaration of
LOCALE_DIR
* lib/Makefile.am - added declaration of LOCALE_DIR
author | masneyb |
---|---|
date | Sat, 18 Oct 2003 15:21:39 +0000 |
parents | 5f66f09e5194 |
children | 51725086634d |
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 { | |
154 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
155 struct passwd *pw; | |
156 | |
157 pw = NULL; | |
124 | 158 str = g_strdup (src); |
1 | 159 |
160 if (*str == '~') | |
161 { | |
162 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
163 pw = getpwuid (geteuid ()); | |
164 else | |
165 { | |
166 if ((pos = strchr (str, '/')) != NULL) | |
167 *pos = '\0'; | |
168 | |
169 pw = getpwnam (str + 1); | |
170 | |
171 if (pos != NULL) | |
172 *pos = '/'; | |
173 } | |
174 } | |
175 | |
176 endpos = str; | |
177 newstr = NULL; | |
178 while ((pos = strchr (endpos, '/')) != NULL) | |
179 { | |
180 pos++; | |
181 while (*pos == '/') | |
182 pos++; | |
204 | 183 |
1 | 184 if ((endpos = strchr (pos, '/')) == NULL) |
185 endpos = pos + strlen (pos); | |
204 | 186 |
1 | 187 tempchar = *endpos; |
188 *endpos = '\0'; | |
204 | 189 |
1 | 190 if (strcmp (pos, "..") == 0) |
191 { | |
192 *(pos - 1) = '\0'; | |
193 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
194 *prevpos = '\0'; | |
195 } | |
196 else if (strcmp (pos, ".") != 0) | |
197 { | |
198 if (newstr == NULL) | |
56 | 199 newstr = g_strdup (pos - 1); |
1 | 200 else |
201 { | |
202 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
203 g_free (newstr); | |
204 newstr = tempstr; | |
205 } | |
206 } | |
204 | 207 |
1 | 208 *endpos = tempchar; |
209 if (*endpos == '\0') | |
210 break; | |
204 | 211 |
1 | 212 endpos = pos + 1; |
213 } | |
214 | |
204 | 215 if (endpos != NULL && *endpos != '\0' && newstr == NULL) |
216 { | |
217 if (strcmp (endpos, "..") == 0) | |
218 newstr = g_malloc0 (1); | |
219 else | |
220 newstr = g_strdup (endpos); | |
221 } | |
222 | |
1 | 223 if (newstr == NULL || *newstr == '\0') |
224 { | |
225 if (newstr != NULL) | |
226 g_free (newstr); | |
204 | 227 |
247 | 228 newstr = g_strdup ("/"); |
1 | 229 } |
230 | |
231 g_free (str); | |
232 | |
233 if (pw != NULL) | |
234 { | |
235 if ((pos = strchr (newstr, '/')) == NULL) | |
124 | 236 str = g_strdup (pw->pw_dir); |
1 | 237 else |
247 | 238 str = gftp_build_path (pw->pw_dir, pos, NULL); |
1 | 239 |
240 g_free (newstr); | |
241 newstr = str; | |
242 } | |
243 | |
244 return (newstr); | |
245 } | |
246 | |
247 | |
248 void | |
249 remove_double_slashes (char *string) | |
250 { | |
251 char *newpos, *oldpos; | |
87 | 252 size_t len; |
1 | 253 |
254 oldpos = newpos = string; | |
255 while (*oldpos != '\0') | |
256 { | |
257 *newpos++ = *oldpos++; | |
258 if (*oldpos == '\0') | |
259 break; | |
260 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
261 oldpos++; | |
262 } | |
263 *newpos = '\0'; | |
87 | 264 |
265 len = strlen (string); | |
266 if (string[len - 1] == '/') | |
267 string[len - 1] = '\0'; | |
1 | 268 } |
269 | |
270 | |
271 void | |
272 make_nonnull (char **str) | |
273 { | |
274 if (*str == NULL) | |
275 *str = g_malloc0 (1); | |
276 } | |
277 | |
278 | |
279 int | |
280 copyfile (char *source, char *dest) | |
281 { | |
58 | 282 int srcfd, destfd; |
1 | 283 char buf[8192]; |
58 | 284 ssize_t n; |
1 | 285 |
182 | 286 if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1) |
58 | 287 { |
288 printf (_("Error: Cannot open local file %s: %s\n"), | |
289 source, g_strerror (errno)); | |
290 exit (1); | |
291 } | |
1 | 292 |
182 | 293 if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
1 | 294 { |
58 | 295 printf (_("Error: Cannot open local file %s: %s\n"), |
296 dest, g_strerror (errno)); | |
297 close (srcfd); | |
298 exit (1); | |
1 | 299 } |
300 | |
58 | 301 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
302 { | |
303 if (write (destfd, buf, n) == -1) | |
304 { | |
305 printf (_("Error: Could not write to socket: %s\n"), | |
306 g_strerror (errno)); | |
307 exit (1); | |
308 } | |
309 } | |
1 | 310 |
58 | 311 if (n == -1) |
312 { | |
313 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
314 exit (1); | |
315 } | |
316 | |
87 | 317 if (close (srcfd) == -1) |
318 { | |
319 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
320 exit (1); | |
321 } | |
322 | |
323 if (close (destfd) == -1) | |
324 { | |
325 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
326 exit (1); | |
327 } | |
1 | 328 |
329 return (1); | |
330 } | |
331 | |
332 | |
87 | 333 /* FIXME - is there a replacement for this */ |
1 | 334 int |
335 gftp_match_filespec (char *filename, char *filespec) | |
336 { | |
337 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
338 size_t len, curlen; | |
339 | |
340 if (filename == NULL || *filename == '\0' || | |
341 filespec == NULL || *filespec == '\0') | |
342 return(1); | |
343 | |
344 filepos = filename; | |
345 wcpos = filespec; | |
346 while(1) | |
347 { | |
348 if (*wcpos == '\0') | |
349 return (1); | |
350 else if (*filepos == '\0') | |
351 return(0); | |
352 else if(*wcpos == '?') | |
353 { | |
354 wcpos++; | |
355 filepos++; | |
356 } | |
357 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
358 return(1); | |
359 else if(*wcpos == '*') | |
360 { | |
361 len = sizeof (search_str); | |
362 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
363 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
364 curlen++, *newpos++ = *pos++); | |
365 *newpos = '\0'; | |
366 | |
367 if ((filepos = strstr (filepos, search_str)) == NULL) | |
368 return(0); | |
369 wcpos += curlen + 1; | |
370 filepos += curlen; | |
371 } | |
372 else if(*wcpos++ != *filepos++) | |
373 return(0); | |
374 } | |
375 return (1); | |
376 } | |
377 | |
378 | |
243 | 379 static void |
380 gftp_info (void) | |
381 { | |
260 | 382 int i; |
383 | |
243 | 384 printf ("%s\n", gftp_version); |
385 | |
289 | 386 #ifdef _GNU_SOURCE |
387 printf ("#define _GNU_SOURCE\n"); | |
388 #endif | |
389 | |
243 | 390 #ifdef _LARGEFILE_SOURCE |
260 | 391 printf ("#define _LARGEFILE_SOURCE\n"); |
243 | 392 #endif |
393 | |
394 #ifdef _FILE_OFFSET_BITS | |
395 printf ("#define _FILE_OFFSET_BITS %d\n", _FILE_OFFSET_BITS); | |
396 #endif | |
397 | |
398 printf ("sizeof (off_t) = %d\n", sizeof (off_t)); | |
399 | |
400 #ifdef HAVE_GETADDRINFO | |
401 printf ("#define HAVE_GETADDRINFO\n"); | |
402 #endif | |
403 | |
404 #ifdef HAVE_GAI_STRERROR | |
405 printf ("#define HAVE_GAI_STRERROR\n"); | |
406 #endif | |
407 | |
408 #ifdef HAVE_GETDTABLESIZE | |
409 printf ("#define HAVE_GETDTABLESIZE\n"); | |
410 #endif | |
411 | |
412 #ifdef G_HAVE_GINT64 | |
413 printf ("#define G_HAVE_GINT64\n"); | |
414 #endif | |
415 | |
416 #ifdef HAVE_LIBREADLINE | |
417 printf ("#define HAVE_LIBREADLINE\n"); | |
418 #endif | |
419 | |
420 #ifdef ENABLE_NLS | |
421 printf ("#define ENABLE_NLS\n"); | |
422 #endif | |
423 | |
424 #ifdef HAVE_GETTEXT | |
425 printf ("#define HAVE_GETTEXT\n"); | |
426 #endif | |
427 | |
428 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, | |
429 GLIB_MICRO_VERSION); | |
430 | |
431 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
432 | |
433 #ifdef USE_SSL | |
434 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
435 #endif | |
260 | 436 |
437 printf ("Enabled protocols: "); | |
438 for (i=0; gftp_protocols[i].name != NULL; i++) | |
439 { | |
440 printf ("%s ", gftp_protocols[i].name); | |
441 } | |
442 printf ("\n"); | |
243 | 443 } |
444 | |
445 | |
1 | 446 int |
447 gftp_parse_command_line (int *argc, char ***argv) | |
448 { | |
449 if (*argc > 1) | |
450 { | |
87 | 451 if (strcmp (argv[0][1], "--help") == 0 || |
452 strcmp (argv[0][1], "-h") == 0) | |
1 | 453 gftp_usage (); |
87 | 454 else if (strcmp (argv[0][1], "--version") == 0 || |
455 strcmp (argv[0][1], "-v") == 0) | |
1 | 456 { |
122 | 457 printf ("%s\n", gftp_version); |
1 | 458 exit (0); |
459 } | |
243 | 460 else if (strcmp (argv[0][1], "--info") == 0) |
461 { | |
462 gftp_info (); | |
463 exit (0); | |
464 } | |
1 | 465 } |
466 return (0); | |
467 } | |
468 | |
469 | |
470 void | |
471 gftp_usage (void) | |
472 { | |
162 | 473 printf (_("usage: gftp [[protocol://][user[:pass]@]site[:port][/directory]]\n")); |
1 | 474 exit (0); |
475 } | |
476 | |
477 | |
478 gint | |
479 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
480 { | |
481 return (strcmp ((char *) path1, (char *) path2) == 0); | |
482 } | |
483 | |
484 | |
485 guint | |
486 string_hash_function (gconstpointer key) | |
487 { | |
59 | 488 guint ret; |
489 int i; | |
490 | |
491 ret = 0; | |
87 | 492 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 493 ret += ((char *) key)[i]; |
494 | |
495 return (ret); | |
1 | 496 } |
497 | |
498 | |
499 void | |
500 free_file_list (GList * filelist) | |
501 { | |
502 gftp_file * tempfle; | |
503 GList * templist; | |
504 | |
505 templist = filelist; | |
506 while (templist != NULL) | |
507 { | |
508 tempfle = templist->data; | |
509 free_fdata (tempfle); | |
510 templist = templist->next; | |
511 } | |
512 g_list_free (filelist); | |
513 } | |
514 | |
515 | |
516 void | |
517 free_fdata (gftp_file * fle) | |
518 { | |
519 if (fle->file) | |
520 g_free (fle->file); | |
184 | 521 if (fle->utf8_file) |
522 g_free (fle->utf8_file); | |
1 | 523 if (fle->user) |
524 g_free (fle->user); | |
525 if (fle->group) | |
526 g_free (fle->group); | |
527 if (fle->attribs) | |
528 g_free (fle->attribs); | |
529 if (fle->destfile) | |
530 g_free (fle->destfile); | |
58 | 531 if (fle->fd > 0) |
102 | 532 close (fle->fd); |
1 | 533 g_free (fle); |
534 } | |
535 | |
536 | |
537 gftp_file * | |
538 copy_fdata (gftp_file * fle) | |
539 { | |
540 gftp_file * newfle; | |
541 | |
542 newfle = g_malloc0 (sizeof (*newfle)); | |
543 memcpy (newfle, fle, sizeof (*newfle)); | |
544 | |
545 if (fle->file) | |
87 | 546 newfle->file = g_strdup (fle->file); |
1 | 547 |
243 | 548 if (fle->utf8_file) |
549 newfle->utf8_file = g_strdup (fle->utf8_file); | |
550 | |
1 | 551 if (fle->user) |
87 | 552 newfle->user = g_strdup (fle->user); |
1 | 553 |
554 if (fle->group) | |
87 | 555 newfle->group = g_strdup (fle->group); |
1 | 556 |
557 if (fle->attribs) | |
87 | 558 newfle->attribs = g_strdup (fle->attribs); |
1 | 559 |
560 if (fle->destfile) | |
87 | 561 newfle->destfile = g_strdup (fle->destfile); |
562 | |
1 | 563 return (newfle); |
564 } | |
565 | |
566 | |
567 int | |
568 compare_request (gftp_request * request1, gftp_request * request2, | |
569 int compare_dirs) | |
570 { | |
571 char *strarr[3][2]; | |
572 int i, ret; | |
573 | |
574 ret = 1; | |
87 | 575 if (request1->protonum == request2->protonum && |
1 | 576 request1->port == request2->port) |
577 { | |
578 strarr[0][0] = request1->hostname; | |
579 strarr[0][1] = request2->hostname; | |
580 strarr[1][0] = request1->username; | |
581 strarr[1][1] = request2->username; | |
582 if (compare_dirs) | |
583 { | |
584 strarr[2][0] = request1->directory; | |
585 strarr[2][1] = request2->directory; | |
586 } | |
587 else | |
588 strarr[2][0] = strarr[2][1] = ""; | |
589 | |
590 for (i = 0; i < 3; i++) | |
591 { | |
592 if ((strarr[i][0] && !strarr[i][1]) || | |
593 (!strarr[i][0] && strarr[i][1])) | |
594 { | |
595 ret = 0; | |
596 break; | |
597 } | |
598 | |
599 if (strarr[i][0] && strarr[i][1] && | |
600 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
601 { | |
602 ret = 0; | |
603 break; | |
604 } | |
605 } | |
606 } | |
607 else | |
608 ret = 0; | |
609 return (ret); | |
610 } | |
611 | |
612 | |
129 | 613 gftp_transfer * |
614 gftp_tdata_new (void) | |
615 { | |
227 | 616 #if GLIB_MAJOR_VERSION == 1 |
617 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
618 #endif | |
129 | 619 gftp_transfer * tdata; |
620 | |
621 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 622 |
623 #if GLIB_MAJOR_VERSION == 1 | |
624 tdata->statmutex = init_mutex; | |
625 tdata->structmutex = init_mutex; | |
626 #else | |
168 | 627 g_static_mutex_init (&tdata->statmutex); |
628 g_static_mutex_init (&tdata->structmutex); | |
227 | 629 #endif |
630 | |
129 | 631 return (tdata); |
632 } | |
633 | |
634 | |
1 | 635 void |
636 free_tdata (gftp_transfer * tdata) | |
637 { | |
638 if (tdata->fromreq != NULL) | |
67 | 639 gftp_request_destroy (tdata->fromreq, 1); |
1 | 640 if (tdata->toreq != NULL) |
67 | 641 gftp_request_destroy (tdata->toreq, 1); |
1 | 642 free_file_list (tdata->files); |
643 g_free (tdata); | |
644 } | |
645 | |
646 | |
647 gftp_request * | |
151 | 648 copy_request (gftp_request * req, int copy_local_options) |
1 | 649 { |
650 gftp_request * newreq; | |
651 | |
66 | 652 newreq = gftp_request_new (); |
1 | 653 |
654 if (req->hostname) | |
56 | 655 newreq->hostname = g_strdup (req->hostname); |
1 | 656 if (req->username) |
56 | 657 newreq->username = g_strdup (req->username); |
1 | 658 if (req->password) |
56 | 659 newreq->password = g_strdup (req->password); |
1 | 660 if (req->account) |
56 | 661 newreq->account = g_strdup (req->account); |
1 | 662 if (req->directory) |
56 | 663 newreq->directory = g_strdup (req->directory); |
66 | 664 newreq->port = req->port; |
665 newreq->use_proxy = req->use_proxy; | |
666 newreq->logging_function = req->logging_function; | |
151 | 667 newreq->free_hostp = 0; |
668 newreq->hostp = req->hostp; | |
669 | |
670 if (copy_local_options) | |
199 | 671 gftp_copy_local_options (&newreq->local_options_vars, |
672 &newreq->local_options_hash, | |
673 req->local_options_vars, | |
674 req->num_local_options_vars); | |
1 | 675 |
173 | 676 if (req->init (newreq) < 0) |
677 { | |
678 gftp_request_destroy (newreq, 1); | |
679 return (NULL); | |
680 } | |
1 | 681 |
682 return (newreq); | |
683 } | |
684 | |
685 | |
16 | 686 static gint |
687 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
688 { | |
689 const gftp_file * f1, * f2; | |
690 | |
691 f1 = a; | |
692 f2 = b; | |
693 return (strcmp (f1->file, f2->file)); | |
694 } | |
695 | |
696 | |
697 static gint | |
698 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
699 { | |
700 const gftp_file * f1, * f2; | |
701 gint ret; | |
702 | |
703 f1 = a; | |
704 f2 = b; | |
705 ret = strcmp (f1->file, f2->file); | |
84 | 706 return (ret * -1); |
16 | 707 } |
708 | |
709 | |
710 static gint | |
711 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
712 { | |
713 const gftp_file * f1, * f2; | |
714 | |
715 f1 = a; | |
716 f2 = b; | |
717 return (strcmp (f1->user, f2->user)); | |
718 } | |
719 | |
720 | |
721 static gint | |
722 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
723 { | |
724 const gftp_file * f1, * f2; | |
725 gint ret; | |
726 | |
727 f1 = a; | |
728 f2 = b; | |
729 ret = strcmp (f1->user, f2->user); | |
84 | 730 return (ret * -1); |
16 | 731 } |
732 | |
733 | |
734 static gint | |
735 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
736 { | |
737 const gftp_file * f1, * f2; | |
738 | |
739 f1 = a; | |
740 f2 = b; | |
741 return (strcmp (f1->group, f2->group)); | |
742 } | |
743 | |
744 | |
745 static gint | |
746 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
747 { | |
748 const gftp_file * f1, * f2; | |
749 gint ret; | |
750 | |
751 f1 = a; | |
752 f2 = b; | |
753 ret = strcmp (f1->group, f2->group); | |
84 | 754 return (ret * -1); |
16 | 755 } |
756 | |
757 | |
758 static gint | |
759 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
760 { | |
761 const gftp_file * f1, * f2; | |
762 | |
763 f1 = a; | |
764 f2 = b; | |
765 return (strcmp (f1->attribs, f2->attribs)); | |
766 } | |
767 | |
768 | |
769 static gint | |
770 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
771 { | |
772 const gftp_file * f1, * f2; | |
773 gint ret; | |
774 | |
775 f1 = a; | |
776 f2 = b; | |
777 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 778 return (ret * -1); |
16 | 779 } |
780 | |
781 | |
782 static gint | |
783 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
784 { | |
785 const gftp_file * f1, * f2; | |
786 | |
787 f1 = a; | |
788 f2 = b; | |
789 if (f1->size < f2->size) | |
790 return (-1); | |
791 else if (f1->size == f2->size) | |
792 return (0); | |
793 else | |
794 return (1); | |
795 } | |
796 | |
797 | |
798 static gint | |
799 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
800 { | |
801 const gftp_file * f1, * f2; | |
802 | |
803 f1 = a; | |
804 f2 = b; | |
805 if (f1->size < f2->size) | |
806 return (1); | |
807 else if (f1->size == f2->size) | |
808 return (0); | |
809 else | |
810 return (-1); | |
811 } | |
812 | |
813 | |
814 static gint | |
815 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
816 { | |
817 const gftp_file * f1, * f2; | |
818 | |
819 f1 = a; | |
820 f2 = b; | |
821 if (f1->datetime < f2->datetime) | |
822 return (-1); | |
823 else if (f1->datetime == f2->datetime) | |
824 return (0); | |
825 else | |
826 return (1); | |
827 } | |
828 | |
829 | |
830 static gint | |
831 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
832 { | |
833 const gftp_file * f1, * f2; | |
834 | |
835 f1 = a; | |
836 f2 = b; | |
837 if (f1->datetime < f2->datetime) | |
838 return (1); | |
839 else if (f1->datetime == f2->datetime) | |
840 return (0); | |
841 else | |
842 return (-1); | |
843 } | |
844 | |
845 | |
846 GList * | |
847 gftp_sort_filelist (GList * filelist, int column, int asds) | |
848 { | |
849 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
850 GCompareFunc sortfunc; | |
851 gftp_file * tempfle; | |
122 | 852 int sort_dirs_first; |
16 | 853 |
854 files = dirs = dotdot = NULL; | |
855 | |
856 if (column == GFTP_SORT_COL_FILE) | |
857 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
858 else if (column == GFTP_SORT_COL_SIZE) | |
859 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
860 else if (column == GFTP_SORT_COL_USER) | |
861 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
862 else if (column == GFTP_SORT_COL_GROUP) | |
863 sortfunc = asds ? | |
864 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
865 else if (column == GFTP_SORT_COL_DATETIME) | |
866 sortfunc = asds ? | |
122 | 867 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
868 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 869 sortfunc = asds ? |
870 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 871 else /* Don't sort */ |
872 return (filelist); | |
873 | |
874 sort_dirs_first = 1; | |
875 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 876 |
877 for (tempitem = filelist; tempitem != NULL; ) | |
878 { | |
879 tempfle = tempitem->data; | |
880 insitem = tempitem; | |
881 tempitem = tempitem->next; | |
882 insitem->next = NULL; | |
883 | |
884 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
885 dotdot = insitem; | |
886 else if (sort_dirs_first && tempfle->isdir) | |
887 { | |
888 insitem->next = dirs; | |
889 dirs = insitem; | |
890 } | |
891 else | |
892 { | |
893 insitem->next = files; | |
894 files = insitem; | |
895 } | |
896 } | |
897 | |
898 if (dirs != NULL) | |
899 dirs = g_list_sort (dirs, sortfunc); | |
900 if (files != NULL) | |
901 files = g_list_sort (files, sortfunc); | |
902 | |
903 filelist = dotdot; | |
904 | |
905 if (filelist == NULL) | |
906 filelist = dirs; | |
907 else | |
908 filelist = g_list_concat (filelist, dirs); | |
909 | |
910 if (filelist == NULL) | |
911 filelist = files; | |
912 else | |
913 filelist = g_list_concat (filelist, files); | |
914 | |
39 | 915 /* I haven't check this, but I'm pretty sure some older versions of glib |
916 had a bug that the prev pointer wasn't being sent to NULL */ | |
917 filelist->prev = NULL; | |
16 | 918 return (filelist); |
919 } | |
920 | |
125 | 921 |
922 mode_t | |
923 gftp_parse_attribs (char *attribs) | |
924 { | |
925 mode_t mode; | |
926 int cur; | |
927 | |
928 cur = 0; | |
929 if (attribs[1] == 'r') | |
930 cur += 4; | |
931 if (attribs[2] == 'w') | |
932 cur += 2; | |
933 if (attribs[3] == 'x' || | |
934 attribs[3] == 's') | |
935 cur += 1; | |
936 mode = cur; | |
937 | |
938 cur = 0; | |
939 if (attribs[4] == 'r') | |
940 cur += 4; | |
941 if (attribs[5] == 'w') | |
942 cur += 2; | |
943 if (attribs[6] == 'x' || | |
944 attribs[6] == 's') | |
945 cur += 1; | |
946 mode = (mode * 10) + cur; | |
947 | |
948 cur = 0; | |
949 if (attribs[7] == 'r') | |
950 cur += 4; | |
951 if (attribs[8] == 'w') | |
952 cur += 2; | |
953 if (attribs[9] == 'x' || | |
954 attribs[9] == 's') | |
955 cur += 1; | |
956 mode = (mode * 10) + cur; | |
957 | |
958 return (mode); | |
959 } | |
960 | |
961 | |
131 | 962 char * |
963 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
964 { | |
965 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
966 struct tm *lt; | |
967 time_t t; | |
968 | |
969 lt = localtime (&fle->datetime); | |
970 | |
971 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
972 fle->group); | |
973 | |
974 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
975 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
976 else | |
977 { | |
978 #if defined (_LARGEFILE_SOURCE) | |
979 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
980 #else | |
981 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
982 #endif | |
983 } | |
984 | |
985 time (&t); | |
986 | |
987 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
988 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
989 else | |
990 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
991 | |
992 if (file_prefixstr == NULL) | |
993 file_prefixstr = ""; | |
994 if (file_suffixstr == NULL) | |
995 file_suffixstr = ""; | |
996 | |
997 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 998 file_prefixstr, |
999 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
1000 file_suffixstr); | |
131 | 1001 |
1002 g_free (tempstr1); | |
1003 g_free (tempstr2); | |
1004 | |
1005 return (ret); | |
1006 } | |
1007 | |
1008 | |
125 | 1009 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
1010 | |
1011 struct hostent * | |
1012 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
1013 { | |
1014 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
1015 struct hostent *hent; | |
1016 | |
1017 if (g_thread_supported ()) | |
1018 g_static_mutex_lock (&hostfunclock); | |
1019 | |
1020 if ((hent = gethostbyname (name)) == NULL) | |
1021 { | |
1022 if (h_errnop) | |
1023 *h_errnop = h_errno; | |
1024 } | |
1025 else | |
1026 { | |
1027 *result_buf = *hent; | |
1028 hent = result_buf; | |
1029 } | |
1030 | |
1031 if (g_thread_supported ()) | |
1032 g_static_mutex_unlock (&hostfunclock); | |
1033 | |
1034 return (hent); | |
1035 } | |
1036 | |
1037 #endif /* !HAVE_GETADDRINFO */ | |
1038 | |
1039 struct servent * | |
1040 r_getservbyname (const char *name, const char *proto, | |
1041 struct servent *result_buf, int *h_errnop) | |
1042 { | |
1043 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
1044 struct servent *sent; | |
1045 | |
1046 if (g_thread_supported ()) | |
1047 g_static_mutex_lock (&servfunclock); | |
1048 | |
1049 if ((sent = getservbyname (name, proto)) == NULL) | |
1050 { | |
1051 if (h_errnop) | |
1052 *h_errnop = h_errno; | |
1053 } | |
1054 else | |
1055 { | |
1056 *result_buf = *sent; | |
1057 sent = result_buf; | |
1058 } | |
1059 | |
1060 if (g_thread_supported ()) | |
1061 g_static_mutex_unlock (&servfunclock); | |
1062 return (sent); | |
1063 } | |
1064 | |
168 | 1065 |
1066 char * | |
1067 base64_encode (char *str) | |
1068 { | |
1069 | |
1070 /* The standard to Base64 encoding can be found in RFC2045 */ | |
1071 | |
1072 char *newstr, *newpos, *fillpos, *pos; | |
1073 unsigned char table[64], encode[3]; | |
1074 int i, num; | |
1075 | |
1076 for (i = 0; i < 26; i++) | |
1077 { | |
1078 table[i] = 'A' + i; | |
1079 table[i + 26] = 'a' + i; | |
1080 } | |
1081 | |
1082 for (i = 0; i < 10; i++) | |
1083 table[i + 52] = '0' + i; | |
1084 | |
1085 table[62] = '+'; | |
207 | 1086 table[63] = '/'; |
168 | 1087 |
1088 num = strlen (str) / 3; | |
1089 if (strlen (str) % 3 > 0) | |
1090 num++; | |
1091 newstr = g_malloc (num * 4 + 1); | |
1092 newstr[num * 4] = '\0'; | |
1093 newpos = newstr; | |
1094 | |
1095 pos = str; | |
1096 while (*pos != '\0') | |
1097 { | |
1098 memset (encode, 0, sizeof (encode)); | |
1099 for (i = 0; i < 3 && *pos != '\0'; i++) | |
1100 encode[i] = *pos++; | |
1101 | |
1102 fillpos = newpos; | |
1103 *newpos++ = table[encode[0] >> 2]; | |
1104 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1105 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1106 *newpos++ = table[encode[2] & 0x3F]; | |
1107 while (i < 3) | |
1108 fillpos[++i] = '='; | |
1109 } | |
1110 return (newstr); | |
1111 } | |
1112 | |
199 | 1113 |
1114 void | |
1115 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1116 { | |
1117 if (entry->hostname) | |
1118 g_free (entry->hostname); | |
1119 if (entry->remote_dir) | |
1120 g_free (entry->remote_dir); | |
1121 if (entry->local_dir) | |
1122 g_free (entry->local_dir); | |
1123 if (entry->user) | |
1124 g_free (entry->user); | |
1125 if (entry->pass) | |
1126 g_free (entry->pass); | |
1127 if (entry->acct) | |
1128 g_free (entry->acct); | |
1129 if (entry->protocol) | |
1130 g_free (entry->protocol); | |
1131 | |
1132 if (entry->local_options_vars != NULL) | |
1133 { | |
201 | 1134 gftp_config_free_options (entry->local_options_vars, |
1135 entry->local_options_hash, | |
1136 entry->num_local_options_vars); | |
1137 | |
199 | 1138 entry->local_options_vars = NULL; |
201 | 1139 entry->local_options_hash = NULL; |
199 | 1140 entry->num_local_options_vars = 0; |
1141 } | |
1142 } | |
1143 | |
201 | 1144 |
1145 void | |
1146 gftp_shutdown (void) | |
1147 { | |
203 | 1148 #ifdef WITH_DMALLOC |
201 | 1149 gftp_config_vars * cv; |
1150 GList * templist; | |
203 | 1151 #endif |
201 | 1152 |
1153 if (gftp_logfd != NULL) | |
1154 fclose (gftp_logfd); | |
1155 | |
1156 gftp_clear_cache_files (); | |
1157 | |
1158 if (gftp_configuration_changed) | |
1159 gftp_write_config_file (); | |
1160 | |
1161 #ifdef WITH_DMALLOC | |
1162 if (gftp_global_options_htable != NULL) | |
1163 g_hash_table_destroy (gftp_global_options_htable); | |
1164 | |
1165 if (gftp_config_list_htable != NULL) | |
1166 g_hash_table_destroy (gftp_config_list_htable); | |
1167 | |
1168 if (gftp_bookmarks_htable != NULL) | |
1169 g_hash_table_destroy (gftp_bookmarks_htable); | |
1170 | |
1171 for (templist = gftp_options_list; | |
1172 templist != NULL; | |
1173 templist = templist->next) | |
1174 { | |
1175 cv = templist->data; | |
1176 gftp_config_free_options (cv, NULL, -1); | |
1177 } | |
1178 | |
1179 gftp_bookmarks_destroy (gftp_bookmarks); | |
1180 | |
1181 dmalloc_shutdown (); | |
1182 #endif | |
1183 | |
1184 exit (0); | |
1185 } | |
1186 | |
207 | 1187 |
1188 GList * | |
1189 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1190 { | |
1191 gftp_file * tempfle; | |
1192 int i, newpos; | |
1193 | |
1194 newpos = GPOINTER_TO_INT (selection->data); | |
1195 i = *curnum - newpos; | |
1196 | |
1197 if (i < 0) | |
1198 { | |
1199 while (i != 0) | |
1200 { | |
1201 tempfle = (*list)->data; | |
1202 if (tempfle->shown) | |
1203 { | |
1204 ++*curnum; | |
1205 i++; | |
1206 } | |
1207 *list = (*list)->next; | |
1208 } | |
1209 } | |
1210 else if (i > 0) | |
1211 { | |
1212 while (i != 0) | |
1213 { | |
1214 tempfle = (*list)->data; | |
1215 if (tempfle->shown) | |
1216 { | |
1217 --*curnum; | |
1218 i--; | |
1219 } | |
1220 *list = (*list)->prev; | |
1221 } | |
1222 } | |
1223 | |
1224 tempfle = (*list)->data; | |
1225 while ((*list)->next && !tempfle->shown) | |
1226 { | |
1227 *list = (*list)->next; | |
1228 tempfle = (*list)->data; | |
1229 } | |
1230 return (selection->next); | |
1231 } | |
1232 | |
1233 | |
227 | 1234 char * |
245 | 1235 gftp_build_path (const char *first_element, ...) |
227 | 1236 { |
1237 const char *element; | |
1238 size_t len, retlen; | |
1239 int add_separator; | |
1240 va_list args; | |
1241 char *ret; | |
1242 | |
245 | 1243 g_return_val_if_fail (first_element != NULL, NULL); |
227 | 1244 |
247 | 1245 ret = g_strdup (first_element); |
1246 retlen = strlen (ret); | |
227 | 1247 |
1248 va_start (args, first_element); | |
247 | 1249 for (element = va_arg (args, char *); |
227 | 1250 element != NULL; |
1251 element = va_arg (args, char *)) | |
1252 { | |
1253 len = strlen (element); | |
1254 | |
245 | 1255 if (len > 0 && element[len - 1] == '/') |
227 | 1256 add_separator = 0; |
1257 else | |
1258 { | |
1259 add_separator = 1; | |
1260 len++; | |
1261 } | |
1262 | |
1263 retlen += len; | |
1264 ret = g_realloc (ret, retlen + 1); | |
1265 | |
1266 if (add_separator) | |
245 | 1267 strncat (ret, "/", retlen); |
1268 | |
1269 strncat (ret, element, retlen); | |
227 | 1270 } |
1271 | |
1272 return (ret); | |
1273 } | |
1274 | |
244 | 1275 |
1276 off_t | |
1277 gftp_parse_file_size (char *str) | |
1278 { | |
1279 #if defined (_LARGEFILE_SOURCE) | |
1280 return (strtoll (str, NULL, 10)); | |
1281 #else | |
1282 return (strtol (str, NULL, 10)); | |
1283 #endif | |
1284 } | |
1285 | |
290 | 1286 |
1287 void | |
1288 gftp_locale_init (void) | |
1289 { | |
1290 #ifdef HAVE_GETTEXT | |
1291 | |
1292 setlocale (LC_ALL, ""); | |
1293 textdomain ("gftp"); | |
1294 | |
1295 #if GLIB_MAJOR_VERSION > 1 | |
1296 bind_textdomain_codeset ("gftp", "UTF-8"); | |
1297 #endif | |
1298 | |
1299 textdomain ("gftp"); | |
1300 | |
1301 #endif | |
1302 } | |
1303 |