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