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