Mercurial > gftp.yaz
annotate lib/misc.c @ 227:a85a097bbb02
2003-7-20 Brian Masney <masneyb@gftp.org>
* lib/config_file.c lib/gftp.h - added compare_function to
gftp_config_vars structure. (gftp_set_global_option) use the compare
function to see if the value was actually changed, and if so set the
gftp_configuration_changed variable
* lib/misc.c lib/gftp.h - For glib 1.2, added my version of
g_build_path() since it's not there
* lib/misc.c - GLIB/GTK+ 1.2 fixes
* lib/protocols.c (gftp_fd_open) - cleaned up some
* lib/rfc959.c (rfc959_init) - if the email address is blank, get the
users address here instead of in register_module. It was being blanked
out when the config file was being read
* lib/options.h lib/rfc2068.c lib/rfc959.c lib/sshv2.c - mark the
config variables that can show up in the bookmarks editor
* src/text/gftp-text.c src/gtk/options_dialog.c - use
gftp_set_global_option() to set the new configuration values
* src/gtk/bookmarks.c - fixed crash in bookmarks dialog. Added notebook
widget to the dialog as well. The options that can be edited for this
site will show up in other tabs
* src/gtk/gftp-gtk.c - fixes to the calls to gftp_set_global_option()
* src/gtk/options_dialog.c - added gftp_gtk_setup_bookmark_options()
to display all the editable options for this bookmark
author | masneyb |
---|---|
date | Mon, 21 Jul 2003 00:26:43 +0000 |
parents | 6de9a8280aa4 |
children | b42e7233533a |
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 { | |
227 | 511 #if GLIB_MAJOR_VERSION == 1 |
512 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
513 #endif | |
129 | 514 gftp_transfer * tdata; |
515 | |
516 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 517 |
518 #if GLIB_MAJOR_VERSION == 1 | |
519 tdata->statmutex = init_mutex; | |
520 tdata->structmutex = init_mutex; | |
521 #else | |
168 | 522 g_static_mutex_init (&tdata->statmutex); |
523 g_static_mutex_init (&tdata->structmutex); | |
227 | 524 #endif |
525 | |
129 | 526 return (tdata); |
527 } | |
528 | |
529 | |
1 | 530 void |
531 free_tdata (gftp_transfer * tdata) | |
532 { | |
533 if (tdata->fromreq != NULL) | |
67 | 534 gftp_request_destroy (tdata->fromreq, 1); |
1 | 535 if (tdata->toreq != NULL) |
67 | 536 gftp_request_destroy (tdata->toreq, 1); |
1 | 537 free_file_list (tdata->files); |
538 g_free (tdata); | |
539 } | |
540 | |
541 | |
542 gftp_request * | |
151 | 543 copy_request (gftp_request * req, int copy_local_options) |
1 | 544 { |
545 gftp_request * newreq; | |
546 | |
66 | 547 newreq = gftp_request_new (); |
1 | 548 |
549 if (req->hostname) | |
56 | 550 newreq->hostname = g_strdup (req->hostname); |
1 | 551 if (req->username) |
56 | 552 newreq->username = g_strdup (req->username); |
1 | 553 if (req->password) |
56 | 554 newreq->password = g_strdup (req->password); |
1 | 555 if (req->account) |
56 | 556 newreq->account = g_strdup (req->account); |
1 | 557 if (req->directory) |
56 | 558 newreq->directory = g_strdup (req->directory); |
66 | 559 newreq->port = req->port; |
560 newreq->use_proxy = req->use_proxy; | |
561 newreq->logging_function = req->logging_function; | |
151 | 562 newreq->free_hostp = 0; |
563 newreq->hostp = req->hostp; | |
564 | |
565 if (copy_local_options) | |
199 | 566 gftp_copy_local_options (&newreq->local_options_vars, |
567 &newreq->local_options_hash, | |
568 req->local_options_vars, | |
569 req->num_local_options_vars); | |
1 | 570 |
173 | 571 if (req->init (newreq) < 0) |
572 { | |
573 gftp_request_destroy (newreq, 1); | |
574 return (NULL); | |
575 } | |
1 | 576 |
577 return (newreq); | |
578 } | |
579 | |
580 | |
16 | 581 static gint |
582 gftp_file_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->file, f2->file)); | |
589 } | |
590 | |
591 | |
592 static gint | |
593 gftp_file_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->file, f2->file); | |
84 | 601 return (ret * -1); |
16 | 602 } |
603 | |
604 | |
605 static gint | |
606 gftp_user_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->user, f2->user)); | |
613 } | |
614 | |
615 | |
616 static gint | |
617 gftp_user_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->user, f2->user); | |
84 | 625 return (ret * -1); |
16 | 626 } |
627 | |
628 | |
629 static gint | |
630 gftp_group_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->group, f2->group)); | |
637 } | |
638 | |
639 | |
640 static gint | |
641 gftp_group_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->group, f2->group); | |
84 | 649 return (ret * -1); |
16 | 650 } |
651 | |
652 | |
653 static gint | |
654 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
655 { | |
656 const gftp_file * f1, * f2; | |
657 | |
658 f1 = a; | |
659 f2 = b; | |
660 return (strcmp (f1->attribs, f2->attribs)); | |
661 } | |
662 | |
663 | |
664 static gint | |
665 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
666 { | |
667 const gftp_file * f1, * f2; | |
668 gint ret; | |
669 | |
670 f1 = a; | |
671 f2 = b; | |
672 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 673 return (ret * -1); |
16 | 674 } |
675 | |
676 | |
677 static gint | |
678 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
679 { | |
680 const gftp_file * f1, * f2; | |
681 | |
682 f1 = a; | |
683 f2 = b; | |
684 if (f1->size < f2->size) | |
685 return (-1); | |
686 else if (f1->size == f2->size) | |
687 return (0); | |
688 else | |
689 return (1); | |
690 } | |
691 | |
692 | |
693 static gint | |
694 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
695 { | |
696 const gftp_file * f1, * f2; | |
697 | |
698 f1 = a; | |
699 f2 = b; | |
700 if (f1->size < f2->size) | |
701 return (1); | |
702 else if (f1->size == f2->size) | |
703 return (0); | |
704 else | |
705 return (-1); | |
706 } | |
707 | |
708 | |
709 static gint | |
710 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
711 { | |
712 const gftp_file * f1, * f2; | |
713 | |
714 f1 = a; | |
715 f2 = b; | |
716 if (f1->datetime < f2->datetime) | |
717 return (-1); | |
718 else if (f1->datetime == f2->datetime) | |
719 return (0); | |
720 else | |
721 return (1); | |
722 } | |
723 | |
724 | |
725 static gint | |
726 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
727 { | |
728 const gftp_file * f1, * f2; | |
729 | |
730 f1 = a; | |
731 f2 = b; | |
732 if (f1->datetime < f2->datetime) | |
733 return (1); | |
734 else if (f1->datetime == f2->datetime) | |
735 return (0); | |
736 else | |
737 return (-1); | |
738 } | |
739 | |
740 | |
741 GList * | |
742 gftp_sort_filelist (GList * filelist, int column, int asds) | |
743 { | |
744 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
745 GCompareFunc sortfunc; | |
746 gftp_file * tempfle; | |
122 | 747 int sort_dirs_first; |
16 | 748 |
749 files = dirs = dotdot = NULL; | |
750 | |
751 if (column == GFTP_SORT_COL_FILE) | |
752 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
753 else if (column == GFTP_SORT_COL_SIZE) | |
754 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
755 else if (column == GFTP_SORT_COL_USER) | |
756 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
757 else if (column == GFTP_SORT_COL_GROUP) | |
758 sortfunc = asds ? | |
759 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
760 else if (column == GFTP_SORT_COL_DATETIME) | |
761 sortfunc = asds ? | |
122 | 762 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
763 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 764 sortfunc = asds ? |
765 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 766 else /* Don't sort */ |
767 return (filelist); | |
768 | |
769 sort_dirs_first = 1; | |
770 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 771 |
772 for (tempitem = filelist; tempitem != NULL; ) | |
773 { | |
774 tempfle = tempitem->data; | |
775 insitem = tempitem; | |
776 tempitem = tempitem->next; | |
777 insitem->next = NULL; | |
778 | |
779 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
780 dotdot = insitem; | |
781 else if (sort_dirs_first && tempfle->isdir) | |
782 { | |
783 insitem->next = dirs; | |
784 dirs = insitem; | |
785 } | |
786 else | |
787 { | |
788 insitem->next = files; | |
789 files = insitem; | |
790 } | |
791 } | |
792 | |
793 if (dirs != NULL) | |
794 dirs = g_list_sort (dirs, sortfunc); | |
795 if (files != NULL) | |
796 files = g_list_sort (files, sortfunc); | |
797 | |
798 filelist = dotdot; | |
799 | |
800 if (filelist == NULL) | |
801 filelist = dirs; | |
802 else | |
803 filelist = g_list_concat (filelist, dirs); | |
804 | |
805 if (filelist == NULL) | |
806 filelist = files; | |
807 else | |
808 filelist = g_list_concat (filelist, files); | |
809 | |
39 | 810 /* I haven't check this, but I'm pretty sure some older versions of glib |
811 had a bug that the prev pointer wasn't being sent to NULL */ | |
812 filelist->prev = NULL; | |
16 | 813 return (filelist); |
814 } | |
815 | |
125 | 816 |
817 mode_t | |
818 gftp_parse_attribs (char *attribs) | |
819 { | |
820 mode_t mode; | |
821 int cur; | |
822 | |
823 cur = 0; | |
824 if (attribs[1] == 'r') | |
825 cur += 4; | |
826 if (attribs[2] == 'w') | |
827 cur += 2; | |
828 if (attribs[3] == 'x' || | |
829 attribs[3] == 's') | |
830 cur += 1; | |
831 mode = cur; | |
832 | |
833 cur = 0; | |
834 if (attribs[4] == 'r') | |
835 cur += 4; | |
836 if (attribs[5] == 'w') | |
837 cur += 2; | |
838 if (attribs[6] == 'x' || | |
839 attribs[6] == 's') | |
840 cur += 1; | |
841 mode = (mode * 10) + cur; | |
842 | |
843 cur = 0; | |
844 if (attribs[7] == 'r') | |
845 cur += 4; | |
846 if (attribs[8] == 'w') | |
847 cur += 2; | |
848 if (attribs[9] == 'x' || | |
849 attribs[9] == 's') | |
850 cur += 1; | |
851 mode = (mode * 10) + cur; | |
852 | |
853 return (mode); | |
854 } | |
855 | |
856 | |
131 | 857 char * |
858 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
859 { | |
860 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
861 struct tm *lt; | |
862 time_t t; | |
863 | |
864 lt = localtime (&fle->datetime); | |
865 | |
866 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
867 fle->group); | |
868 | |
869 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
870 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
871 else | |
872 { | |
873 #if defined (_LARGEFILE_SOURCE) | |
874 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
875 #else | |
876 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
877 #endif | |
878 } | |
879 | |
880 time (&t); | |
881 | |
882 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
883 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
884 else | |
885 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
886 | |
887 if (file_prefixstr == NULL) | |
888 file_prefixstr = ""; | |
889 if (file_suffixstr == NULL) | |
890 file_suffixstr = ""; | |
891 | |
892 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 893 file_prefixstr, |
894 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
895 file_suffixstr); | |
131 | 896 |
897 g_free (tempstr1); | |
898 g_free (tempstr2); | |
899 | |
900 return (ret); | |
901 } | |
902 | |
903 | |
125 | 904 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
905 | |
906 struct hostent * | |
907 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
908 { | |
909 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
910 struct hostent *hent; | |
911 | |
912 if (g_thread_supported ()) | |
913 g_static_mutex_lock (&hostfunclock); | |
914 | |
915 if ((hent = gethostbyname (name)) == NULL) | |
916 { | |
917 if (h_errnop) | |
918 *h_errnop = h_errno; | |
919 } | |
920 else | |
921 { | |
922 *result_buf = *hent; | |
923 hent = result_buf; | |
924 } | |
925 | |
926 if (g_thread_supported ()) | |
927 g_static_mutex_unlock (&hostfunclock); | |
928 | |
929 return (hent); | |
930 } | |
931 | |
932 #endif /* !HAVE_GETADDRINFO */ | |
933 | |
934 struct servent * | |
935 r_getservbyname (const char *name, const char *proto, | |
936 struct servent *result_buf, int *h_errnop) | |
937 { | |
938 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
939 struct servent *sent; | |
940 | |
941 if (g_thread_supported ()) | |
942 g_static_mutex_lock (&servfunclock); | |
943 | |
944 if ((sent = getservbyname (name, proto)) == NULL) | |
945 { | |
946 if (h_errnop) | |
947 *h_errnop = h_errno; | |
948 } | |
949 else | |
950 { | |
951 *result_buf = *sent; | |
952 sent = result_buf; | |
953 } | |
954 | |
955 if (g_thread_supported ()) | |
956 g_static_mutex_unlock (&servfunclock); | |
957 return (sent); | |
958 } | |
959 | |
168 | 960 |
961 char * | |
962 base64_encode (char *str) | |
963 { | |
964 | |
965 /* The standard to Base64 encoding can be found in RFC2045 */ | |
966 | |
967 char *newstr, *newpos, *fillpos, *pos; | |
968 unsigned char table[64], encode[3]; | |
969 int i, num; | |
970 | |
971 for (i = 0; i < 26; i++) | |
972 { | |
973 table[i] = 'A' + i; | |
974 table[i + 26] = 'a' + i; | |
975 } | |
976 | |
977 for (i = 0; i < 10; i++) | |
978 table[i + 52] = '0' + i; | |
979 | |
980 table[62] = '+'; | |
207 | 981 table[63] = '/'; |
168 | 982 |
983 num = strlen (str) / 3; | |
984 if (strlen (str) % 3 > 0) | |
985 num++; | |
986 newstr = g_malloc (num * 4 + 1); | |
987 newstr[num * 4] = '\0'; | |
988 newpos = newstr; | |
989 | |
990 pos = str; | |
991 while (*pos != '\0') | |
992 { | |
993 memset (encode, 0, sizeof (encode)); | |
994 for (i = 0; i < 3 && *pos != '\0'; i++) | |
995 encode[i] = *pos++; | |
996 | |
997 fillpos = newpos; | |
998 *newpos++ = table[encode[0] >> 2]; | |
999 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1000 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1001 *newpos++ = table[encode[2] & 0x3F]; | |
1002 while (i < 3) | |
1003 fillpos[++i] = '='; | |
1004 } | |
1005 return (newstr); | |
1006 } | |
1007 | |
199 | 1008 |
1009 void | |
1010 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1011 { | |
1012 if (entry->hostname) | |
1013 g_free (entry->hostname); | |
1014 if (entry->remote_dir) | |
1015 g_free (entry->remote_dir); | |
1016 if (entry->local_dir) | |
1017 g_free (entry->local_dir); | |
1018 if (entry->user) | |
1019 g_free (entry->user); | |
1020 if (entry->pass) | |
1021 g_free (entry->pass); | |
1022 if (entry->acct) | |
1023 g_free (entry->acct); | |
1024 if (entry->protocol) | |
1025 g_free (entry->protocol); | |
1026 | |
1027 if (entry->local_options_vars != NULL) | |
1028 { | |
201 | 1029 gftp_config_free_options (entry->local_options_vars, |
1030 entry->local_options_hash, | |
1031 entry->num_local_options_vars); | |
1032 | |
199 | 1033 entry->local_options_vars = NULL; |
201 | 1034 entry->local_options_hash = NULL; |
199 | 1035 entry->num_local_options_vars = 0; |
1036 } | |
1037 } | |
1038 | |
201 | 1039 |
1040 void | |
1041 gftp_shutdown (void) | |
1042 { | |
203 | 1043 #ifdef WITH_DMALLOC |
201 | 1044 gftp_config_vars * cv; |
1045 GList * templist; | |
203 | 1046 #endif |
201 | 1047 |
1048 if (gftp_logfd != NULL) | |
1049 fclose (gftp_logfd); | |
1050 | |
1051 gftp_clear_cache_files (); | |
1052 | |
1053 if (gftp_configuration_changed) | |
1054 gftp_write_config_file (); | |
1055 | |
1056 #ifdef WITH_DMALLOC | |
1057 if (gftp_global_options_htable != NULL) | |
1058 g_hash_table_destroy (gftp_global_options_htable); | |
1059 | |
1060 if (gftp_config_list_htable != NULL) | |
1061 g_hash_table_destroy (gftp_config_list_htable); | |
1062 | |
1063 if (gftp_bookmarks_htable != NULL) | |
1064 g_hash_table_destroy (gftp_bookmarks_htable); | |
1065 | |
1066 for (templist = gftp_options_list; | |
1067 templist != NULL; | |
1068 templist = templist->next) | |
1069 { | |
1070 cv = templist->data; | |
1071 gftp_config_free_options (cv, NULL, -1); | |
1072 } | |
1073 | |
1074 gftp_bookmarks_destroy (gftp_bookmarks); | |
1075 | |
1076 dmalloc_shutdown (); | |
1077 #endif | |
1078 | |
1079 exit (0); | |
1080 } | |
1081 | |
207 | 1082 |
1083 GList * | |
1084 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1085 { | |
1086 gftp_file * tempfle; | |
1087 int i, newpos; | |
1088 | |
1089 newpos = GPOINTER_TO_INT (selection->data); | |
1090 i = *curnum - newpos; | |
1091 | |
1092 if (i < 0) | |
1093 { | |
1094 while (i != 0) | |
1095 { | |
1096 tempfle = (*list)->data; | |
1097 if (tempfle->shown) | |
1098 { | |
1099 ++*curnum; | |
1100 i++; | |
1101 } | |
1102 *list = (*list)->next; | |
1103 } | |
1104 } | |
1105 else if (i > 0) | |
1106 { | |
1107 while (i != 0) | |
1108 { | |
1109 tempfle = (*list)->data; | |
1110 if (tempfle->shown) | |
1111 { | |
1112 --*curnum; | |
1113 i--; | |
1114 } | |
1115 *list = (*list)->prev; | |
1116 } | |
1117 } | |
1118 | |
1119 tempfle = (*list)->data; | |
1120 while ((*list)->next && !tempfle->shown) | |
1121 { | |
1122 *list = (*list)->next; | |
1123 tempfle = (*list)->data; | |
1124 } | |
1125 return (selection->next); | |
1126 } | |
1127 | |
1128 | |
227 | 1129 #if GLIB_MAJOR_VERSION == 1 |
1130 | |
1131 char * | |
1132 g_build_path (const char *separator, const char *first_element, ...) | |
1133 { | |
1134 const char *element; | |
1135 size_t len, retlen; | |
1136 int add_separator; | |
1137 va_list args; | |
1138 char *ret; | |
1139 | |
1140 g_return_val_if_fail (separator != NULL, NULL); | |
1141 | |
1142 ret = g_malloc0 (1); | |
1143 retlen = 0; | |
1144 | |
1145 va_start (args, first_element); | |
1146 for (element = first_element; | |
1147 element != NULL; | |
1148 element = va_arg (args, char *)) | |
1149 { | |
1150 len = strlen (element); | |
1151 | |
1152 if (len > 0 && element[len - 1] == *separator) | |
1153 add_separator = 0; | |
1154 else | |
1155 { | |
1156 add_separator = 1; | |
1157 len++; | |
1158 } | |
1159 | |
1160 retlen += len; | |
1161 ret = g_realloc (ret, retlen + 1); | |
1162 strncat (ret, element, retlen); | |
1163 | |
1164 if (add_separator) | |
1165 strncat (ret, separator, retlen); | |
1166 } | |
1167 | |
1168 return (ret); | |
1169 } | |
1170 | |
1171 #endif | |
1172 |