Mercurial > gftp.yaz
annotate lib/misc.c @ 201:0098dae654a5
2003-6-25 Brian Masney <masneyb@gftp.org>
* configure.in - added mk to ALL_LINGUAS
* lib/gftp.h - added several macros for dmalloc
* lib/misc.c src/text/gftp-text.c src/gtk/gftp-gtk.c - added
gftp_shutdown() to misc.c. This will write out the configuration
file, clear the cache, and if dmalloc is enabled, free the memory
that was allocated on startup
* lib/config_file.c lib/gftp.h lib/misc.c lib/options.h - added
gftp_configuration_changed parameter
* lib/config_file.c lib/misc.c lib/protocols.c - added
gftp_config_free_options()
* lib/config_file.c src/gtk/bookmarks.c - added gftp_bookmarks() which
is derived mostly from bm_close_dialog()
* lib/rfc959.c - added rfc959_request_destroy(). Free the getline
buffers in this function
* src/gtk/misc-gtk.c (gftp_item_factory_translate) - remove double
g_strdup() call
* lib/config_file.c lib/gftp.h src/gtk/misc-gtk.c - moved
get_xpm_path() to GTK+ port. No longer call it startup when reading
the config file
author | masneyb |
---|---|
date | Thu, 26 Jun 2003 01:04:12 +0000 |
parents | 75eebb3b0592 |
children | 95e669973a84 |
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 gint | |
363 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
364 { | |
365 return (strcmp ((char *) path1, (char *) path2) == 0); | |
366 } | |
367 | |
368 | |
369 guint | |
370 string_hash_function (gconstpointer key) | |
371 { | |
59 | 372 guint ret; |
373 int i; | |
374 | |
375 ret = 0; | |
87 | 376 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 377 ret += ((char *) key)[i]; |
378 | |
379 return (ret); | |
1 | 380 } |
381 | |
382 | |
383 void | |
384 free_file_list (GList * filelist) | |
385 { | |
386 gftp_file * tempfle; | |
387 GList * templist; | |
388 | |
389 templist = filelist; | |
390 while (templist != NULL) | |
391 { | |
392 tempfle = templist->data; | |
393 free_fdata (tempfle); | |
394 templist = templist->next; | |
395 } | |
396 g_list_free (filelist); | |
397 } | |
398 | |
399 | |
400 void | |
401 free_fdata (gftp_file * fle) | |
402 { | |
403 if (fle->file) | |
404 g_free (fle->file); | |
184 | 405 if (fle->utf8_file) |
406 g_free (fle->utf8_file); | |
1 | 407 if (fle->user) |
408 g_free (fle->user); | |
409 if (fle->group) | |
410 g_free (fle->group); | |
411 if (fle->attribs) | |
412 g_free (fle->attribs); | |
413 if (fle->destfile) | |
414 g_free (fle->destfile); | |
58 | 415 if (fle->fd > 0) |
102 | 416 close (fle->fd); |
1 | 417 g_free (fle); |
418 } | |
419 | |
420 | |
421 gftp_file * | |
422 copy_fdata (gftp_file * fle) | |
423 { | |
424 gftp_file * newfle; | |
425 | |
426 newfle = g_malloc0 (sizeof (*newfle)); | |
427 memcpy (newfle, fle, sizeof (*newfle)); | |
428 | |
429 if (fle->file) | |
87 | 430 newfle->file = g_strdup (fle->file); |
1 | 431 |
432 if (fle->user) | |
87 | 433 newfle->user = g_strdup (fle->user); |
1 | 434 |
435 if (fle->group) | |
87 | 436 newfle->group = g_strdup (fle->group); |
1 | 437 |
438 if (fle->attribs) | |
87 | 439 newfle->attribs = g_strdup (fle->attribs); |
1 | 440 |
441 if (fle->destfile) | |
87 | 442 newfle->destfile = g_strdup (fle->destfile); |
443 | |
1 | 444 return (newfle); |
445 } | |
446 | |
447 | |
448 int | |
449 compare_request (gftp_request * request1, gftp_request * request2, | |
450 int compare_dirs) | |
451 { | |
452 char *strarr[3][2]; | |
453 int i, ret; | |
454 | |
455 ret = 1; | |
87 | 456 if (request1->protonum == request2->protonum && |
1 | 457 request1->port == request2->port) |
458 { | |
459 strarr[0][0] = request1->hostname; | |
460 strarr[0][1] = request2->hostname; | |
461 strarr[1][0] = request1->username; | |
462 strarr[1][1] = request2->username; | |
463 if (compare_dirs) | |
464 { | |
465 strarr[2][0] = request1->directory; | |
466 strarr[2][1] = request2->directory; | |
467 } | |
468 else | |
469 strarr[2][0] = strarr[2][1] = ""; | |
470 | |
471 for (i = 0; i < 3; i++) | |
472 { | |
473 if ((strarr[i][0] && !strarr[i][1]) || | |
474 (!strarr[i][0] && strarr[i][1])) | |
475 { | |
476 ret = 0; | |
477 break; | |
478 } | |
479 | |
480 if (strarr[i][0] && strarr[i][1] && | |
481 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
482 { | |
483 ret = 0; | |
484 break; | |
485 } | |
486 } | |
487 } | |
488 else | |
489 ret = 0; | |
490 return (ret); | |
491 } | |
492 | |
493 | |
129 | 494 gftp_transfer * |
495 gftp_tdata_new (void) | |
496 { | |
497 gftp_transfer * tdata; | |
498 | |
499 tdata = g_malloc0 (sizeof (*tdata)); | |
168 | 500 g_static_mutex_init (&tdata->statmutex); |
501 g_static_mutex_init (&tdata->structmutex); | |
129 | 502 return (tdata); |
503 } | |
504 | |
505 | |
1 | 506 void |
507 free_tdata (gftp_transfer * tdata) | |
508 { | |
509 if (tdata->fromreq != NULL) | |
67 | 510 gftp_request_destroy (tdata->fromreq, 1); |
1 | 511 if (tdata->toreq != NULL) |
67 | 512 gftp_request_destroy (tdata->toreq, 1); |
1 | 513 free_file_list (tdata->files); |
514 g_free (tdata); | |
515 } | |
516 | |
517 | |
518 gftp_request * | |
151 | 519 copy_request (gftp_request * req, int copy_local_options) |
1 | 520 { |
521 gftp_request * newreq; | |
522 | |
66 | 523 newreq = gftp_request_new (); |
1 | 524 |
525 if (req->hostname) | |
56 | 526 newreq->hostname = g_strdup (req->hostname); |
1 | 527 if (req->username) |
56 | 528 newreq->username = g_strdup (req->username); |
1 | 529 if (req->password) |
56 | 530 newreq->password = g_strdup (req->password); |
1 | 531 if (req->account) |
56 | 532 newreq->account = g_strdup (req->account); |
1 | 533 if (req->directory) |
56 | 534 newreq->directory = g_strdup (req->directory); |
66 | 535 newreq->port = req->port; |
536 newreq->use_proxy = req->use_proxy; | |
537 newreq->logging_function = req->logging_function; | |
151 | 538 newreq->free_hostp = 0; |
539 newreq->hostp = req->hostp; | |
540 | |
541 if (copy_local_options) | |
199 | 542 gftp_copy_local_options (&newreq->local_options_vars, |
543 &newreq->local_options_hash, | |
544 req->local_options_vars, | |
545 req->num_local_options_vars); | |
1 | 546 |
173 | 547 if (req->init (newreq) < 0) |
548 { | |
549 gftp_request_destroy (newreq, 1); | |
550 return (NULL); | |
551 } | |
1 | 552 |
553 return (newreq); | |
554 } | |
555 | |
556 | |
16 | 557 static gint |
558 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
559 { | |
560 const gftp_file * f1, * f2; | |
561 | |
562 f1 = a; | |
563 f2 = b; | |
564 return (strcmp (f1->file, f2->file)); | |
565 } | |
566 | |
567 | |
568 static gint | |
569 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
570 { | |
571 const gftp_file * f1, * f2; | |
572 gint ret; | |
573 | |
574 f1 = a; | |
575 f2 = b; | |
576 ret = strcmp (f1->file, f2->file); | |
84 | 577 return (ret * -1); |
16 | 578 } |
579 | |
580 | |
581 static gint | |
582 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
583 { | |
584 const gftp_file * f1, * f2; | |
585 | |
586 f1 = a; | |
587 f2 = b; | |
588 return (strcmp (f1->user, f2->user)); | |
589 } | |
590 | |
591 | |
592 static gint | |
593 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
594 { | |
595 const gftp_file * f1, * f2; | |
596 gint ret; | |
597 | |
598 f1 = a; | |
599 f2 = b; | |
600 ret = strcmp (f1->user, f2->user); | |
84 | 601 return (ret * -1); |
16 | 602 } |
603 | |
604 | |
605 static gint | |
606 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
607 { | |
608 const gftp_file * f1, * f2; | |
609 | |
610 f1 = a; | |
611 f2 = b; | |
612 return (strcmp (f1->group, f2->group)); | |
613 } | |
614 | |
615 | |
616 static gint | |
617 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
618 { | |
619 const gftp_file * f1, * f2; | |
620 gint ret; | |
621 | |
622 f1 = a; | |
623 f2 = b; | |
624 ret = strcmp (f1->group, f2->group); | |
84 | 625 return (ret * -1); |
16 | 626 } |
627 | |
628 | |
629 static gint | |
630 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
631 { | |
632 const gftp_file * f1, * f2; | |
633 | |
634 f1 = a; | |
635 f2 = b; | |
636 return (strcmp (f1->attribs, f2->attribs)); | |
637 } | |
638 | |
639 | |
640 static gint | |
641 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
642 { | |
643 const gftp_file * f1, * f2; | |
644 gint ret; | |
645 | |
646 f1 = a; | |
647 f2 = b; | |
648 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 649 return (ret * -1); |
16 | 650 } |
651 | |
652 | |
653 static gint | |
654 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
655 { | |
656 const gftp_file * f1, * f2; | |
657 | |
658 f1 = a; | |
659 f2 = b; | |
660 if (f1->size < f2->size) | |
661 return (-1); | |
662 else if (f1->size == f2->size) | |
663 return (0); | |
664 else | |
665 return (1); | |
666 } | |
667 | |
668 | |
669 static gint | |
670 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
671 { | |
672 const gftp_file * f1, * f2; | |
673 | |
674 f1 = a; | |
675 f2 = b; | |
676 if (f1->size < f2->size) | |
677 return (1); | |
678 else if (f1->size == f2->size) | |
679 return (0); | |
680 else | |
681 return (-1); | |
682 } | |
683 | |
684 | |
685 static gint | |
686 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
687 { | |
688 const gftp_file * f1, * f2; | |
689 | |
690 f1 = a; | |
691 f2 = b; | |
692 if (f1->datetime < f2->datetime) | |
693 return (-1); | |
694 else if (f1->datetime == f2->datetime) | |
695 return (0); | |
696 else | |
697 return (1); | |
698 } | |
699 | |
700 | |
701 static gint | |
702 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
703 { | |
704 const gftp_file * f1, * f2; | |
705 | |
706 f1 = a; | |
707 f2 = b; | |
708 if (f1->datetime < f2->datetime) | |
709 return (1); | |
710 else if (f1->datetime == f2->datetime) | |
711 return (0); | |
712 else | |
713 return (-1); | |
714 } | |
715 | |
716 | |
717 GList * | |
718 gftp_sort_filelist (GList * filelist, int column, int asds) | |
719 { | |
720 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
721 GCompareFunc sortfunc; | |
722 gftp_file * tempfle; | |
122 | 723 int sort_dirs_first; |
16 | 724 |
725 files = dirs = dotdot = NULL; | |
726 | |
727 if (column == GFTP_SORT_COL_FILE) | |
728 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
729 else if (column == GFTP_SORT_COL_SIZE) | |
730 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
731 else if (column == GFTP_SORT_COL_USER) | |
732 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
733 else if (column == GFTP_SORT_COL_GROUP) | |
734 sortfunc = asds ? | |
735 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
736 else if (column == GFTP_SORT_COL_DATETIME) | |
737 sortfunc = asds ? | |
122 | 738 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
739 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 740 sortfunc = asds ? |
741 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 742 else /* Don't sort */ |
743 return (filelist); | |
744 | |
745 sort_dirs_first = 1; | |
746 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 747 |
748 for (tempitem = filelist; tempitem != NULL; ) | |
749 { | |
750 tempfle = tempitem->data; | |
751 insitem = tempitem; | |
752 tempitem = tempitem->next; | |
753 insitem->next = NULL; | |
754 | |
755 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
756 dotdot = insitem; | |
757 else if (sort_dirs_first && tempfle->isdir) | |
758 { | |
759 insitem->next = dirs; | |
760 dirs = insitem; | |
761 } | |
762 else | |
763 { | |
764 insitem->next = files; | |
765 files = insitem; | |
766 } | |
767 } | |
768 | |
769 if (dirs != NULL) | |
770 dirs = g_list_sort (dirs, sortfunc); | |
771 if (files != NULL) | |
772 files = g_list_sort (files, sortfunc); | |
773 | |
774 filelist = dotdot; | |
775 | |
776 if (filelist == NULL) | |
777 filelist = dirs; | |
778 else | |
779 filelist = g_list_concat (filelist, dirs); | |
780 | |
781 if (filelist == NULL) | |
782 filelist = files; | |
783 else | |
784 filelist = g_list_concat (filelist, files); | |
785 | |
39 | 786 /* I haven't check this, but I'm pretty sure some older versions of glib |
787 had a bug that the prev pointer wasn't being sent to NULL */ | |
788 filelist->prev = NULL; | |
16 | 789 return (filelist); |
790 } | |
791 | |
125 | 792 |
793 mode_t | |
794 gftp_parse_attribs (char *attribs) | |
795 { | |
796 mode_t mode; | |
797 int cur; | |
798 | |
799 cur = 0; | |
800 if (attribs[1] == 'r') | |
801 cur += 4; | |
802 if (attribs[2] == 'w') | |
803 cur += 2; | |
804 if (attribs[3] == 'x' || | |
805 attribs[3] == 's') | |
806 cur += 1; | |
807 mode = cur; | |
808 | |
809 cur = 0; | |
810 if (attribs[4] == 'r') | |
811 cur += 4; | |
812 if (attribs[5] == 'w') | |
813 cur += 2; | |
814 if (attribs[6] == 'x' || | |
815 attribs[6] == 's') | |
816 cur += 1; | |
817 mode = (mode * 10) + cur; | |
818 | |
819 cur = 0; | |
820 if (attribs[7] == 'r') | |
821 cur += 4; | |
822 if (attribs[8] == 'w') | |
823 cur += 2; | |
824 if (attribs[9] == 'x' || | |
825 attribs[9] == 's') | |
826 cur += 1; | |
827 mode = (mode * 10) + cur; | |
828 | |
829 return (mode); | |
830 } | |
831 | |
832 | |
131 | 833 char * |
834 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
835 { | |
836 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
837 struct tm *lt; | |
838 time_t t; | |
839 | |
840 lt = localtime (&fle->datetime); | |
841 | |
842 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
843 fle->group); | |
844 | |
845 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
846 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
847 else | |
848 { | |
849 #if defined (_LARGEFILE_SOURCE) | |
850 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
851 #else | |
852 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
853 #endif | |
854 } | |
855 | |
856 time (&t); | |
857 | |
858 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
859 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
860 else | |
861 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
862 | |
863 if (file_prefixstr == NULL) | |
864 file_prefixstr = ""; | |
865 if (file_suffixstr == NULL) | |
866 file_suffixstr = ""; | |
867 | |
868 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 869 file_prefixstr, |
870 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
871 file_suffixstr); | |
131 | 872 |
873 g_free (tempstr1); | |
874 g_free (tempstr2); | |
875 | |
876 return (ret); | |
877 } | |
878 | |
879 | |
125 | 880 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
881 | |
882 struct hostent * | |
883 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
884 { | |
885 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
886 struct hostent *hent; | |
887 | |
888 if (g_thread_supported ()) | |
889 g_static_mutex_lock (&hostfunclock); | |
890 | |
891 if ((hent = gethostbyname (name)) == NULL) | |
892 { | |
893 if (h_errnop) | |
894 *h_errnop = h_errno; | |
895 } | |
896 else | |
897 { | |
898 *result_buf = *hent; | |
899 hent = result_buf; | |
900 } | |
901 | |
902 if (g_thread_supported ()) | |
903 g_static_mutex_unlock (&hostfunclock); | |
904 | |
905 return (hent); | |
906 } | |
907 | |
908 #endif /* !HAVE_GETADDRINFO */ | |
909 | |
910 struct servent * | |
911 r_getservbyname (const char *name, const char *proto, | |
912 struct servent *result_buf, int *h_errnop) | |
913 { | |
914 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
915 struct servent *sent; | |
916 | |
917 if (g_thread_supported ()) | |
918 g_static_mutex_lock (&servfunclock); | |
919 | |
920 if ((sent = getservbyname (name, proto)) == NULL) | |
921 { | |
922 if (h_errnop) | |
923 *h_errnop = h_errno; | |
924 } | |
925 else | |
926 { | |
927 *result_buf = *sent; | |
928 sent = result_buf; | |
929 } | |
930 | |
931 if (g_thread_supported ()) | |
932 g_static_mutex_unlock (&servfunclock); | |
933 return (sent); | |
934 } | |
935 | |
168 | 936 |
937 char * | |
938 base64_encode (char *str) | |
939 { | |
940 | |
941 /* The standard to Base64 encoding can be found in RFC2045 */ | |
942 | |
943 char *newstr, *newpos, *fillpos, *pos; | |
944 unsigned char table[64], encode[3]; | |
945 int i, num; | |
946 | |
947 for (i = 0; i < 26; i++) | |
948 { | |
949 table[i] = 'A' + i; | |
950 table[i + 26] = 'a' + i; | |
951 } | |
952 | |
953 for (i = 0; i < 10; i++) | |
954 table[i + 52] = '0' + i; | |
955 | |
956 table[62] = '+'; | |
957 table[63] = '-'; | |
958 | |
959 num = strlen (str) / 3; | |
960 if (strlen (str) % 3 > 0) | |
961 num++; | |
962 newstr = g_malloc (num * 4 + 1); | |
963 newstr[num * 4] = '\0'; | |
964 newpos = newstr; | |
965 | |
966 pos = str; | |
967 while (*pos != '\0') | |
968 { | |
969 memset (encode, 0, sizeof (encode)); | |
970 for (i = 0; i < 3 && *pos != '\0'; i++) | |
971 encode[i] = *pos++; | |
972 | |
973 fillpos = newpos; | |
974 *newpos++ = table[encode[0] >> 2]; | |
975 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
976 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
977 *newpos++ = table[encode[2] & 0x3F]; | |
978 while (i < 3) | |
979 fillpos[++i] = '='; | |
980 } | |
981 return (newstr); | |
982 } | |
983 | |
199 | 984 |
985 void | |
986 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
987 { | |
988 if (entry->hostname) | |
989 g_free (entry->hostname); | |
990 if (entry->remote_dir) | |
991 g_free (entry->remote_dir); | |
992 if (entry->local_dir) | |
993 g_free (entry->local_dir); | |
994 if (entry->user) | |
995 g_free (entry->user); | |
996 if (entry->pass) | |
997 g_free (entry->pass); | |
998 if (entry->acct) | |
999 g_free (entry->acct); | |
1000 if (entry->protocol) | |
1001 g_free (entry->protocol); | |
1002 | |
1003 if (entry->local_options_vars != NULL) | |
1004 { | |
201 | 1005 gftp_config_free_options (entry->local_options_vars, |
1006 entry->local_options_hash, | |
1007 entry->num_local_options_vars); | |
1008 | |
199 | 1009 entry->local_options_vars = NULL; |
201 | 1010 entry->local_options_hash = NULL; |
199 | 1011 entry->num_local_options_vars = 0; |
1012 } | |
1013 } | |
1014 | |
201 | 1015 |
1016 void | |
1017 gftp_shutdown (void) | |
1018 { | |
1019 gftp_config_vars * cv; | |
1020 GList * templist; | |
1021 | |
1022 if (gftp_logfd != NULL) | |
1023 fclose (gftp_logfd); | |
1024 | |
1025 gftp_clear_cache_files (); | |
1026 | |
1027 if (gftp_configuration_changed) | |
1028 gftp_write_config_file (); | |
1029 | |
1030 #ifdef WITH_DMALLOC | |
1031 if (gftp_global_options_htable != NULL) | |
1032 g_hash_table_destroy (gftp_global_options_htable); | |
1033 | |
1034 if (gftp_config_list_htable != NULL) | |
1035 g_hash_table_destroy (gftp_config_list_htable); | |
1036 | |
1037 if (gftp_bookmarks_htable != NULL) | |
1038 g_hash_table_destroy (gftp_bookmarks_htable); | |
1039 | |
1040 for (templist = gftp_options_list; | |
1041 templist != NULL; | |
1042 templist = templist->next) | |
1043 { | |
1044 cv = templist->data; | |
1045 gftp_config_free_options (cv, NULL, -1); | |
1046 } | |
1047 | |
1048 gftp_bookmarks_destroy (gftp_bookmarks); | |
1049 | |
1050 dmalloc_shutdown (); | |
1051 #endif | |
1052 | |
1053 exit (0); | |
1054 } | |
1055 |