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