Mercurial > gftp.yaz
annotate lib/misc.c @ 244:afbbc72b73e2
2003-8-3 Brian Masney <masneyb@gftp.org>
* lib/local.c (local_put_file) - specify an initial file creation mode
of 0644
* lib/misc.c lib/gftp.h - added gftp_parse_file_size(). This function
works correctly for files greater than 2.1GB
* lib/rfc959.c (rfc959_get_file) lib/protocols.c lib/rfc2068.c
lib/rfc959.c - use gftp_parse_file_size()
* lib/protocols.c lib/gftp.h (gftp_get_file, gftp_put_file,
gftp_transfer_file) - changed type of startsize paramter from size_t
to off_t
author | masneyb |
---|---|
date | Sun, 03 Aug 2003 18:44:29 +0000 |
parents | b42e7233533a |
children | 41af60bc1f88 |
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 | |
243 | 349 static void |
350 gftp_info (void) | |
351 { | |
352 printf ("%s\n", gftp_version); | |
353 | |
354 #ifdef _LARGEFILE_SOURCE | |
355 printf ("#define_LARGEFILE_SOURCE\n"); | |
356 #endif | |
357 | |
358 #ifdef _FILE_OFFSET_BITS | |
359 printf ("#define _FILE_OFFSET_BITS %d\n", _FILE_OFFSET_BITS); | |
360 #endif | |
361 | |
362 printf ("sizeof (off_t) = %d\n", sizeof (off_t)); | |
363 | |
364 #ifdef USE_SSL | |
365 printf ("#define USE_SSL\n"); | |
366 #endif | |
367 | |
368 #ifdef HAVE_GETADDRINFO | |
369 printf ("#define HAVE_GETADDRINFO\n"); | |
370 #endif | |
371 | |
372 #ifdef HAVE_GAI_STRERROR | |
373 printf ("#define HAVE_GAI_STRERROR\n"); | |
374 #endif | |
375 | |
376 #ifdef HAVE_GETDTABLESIZE | |
377 printf ("#define HAVE_GETDTABLESIZE\n"); | |
378 #endif | |
379 | |
380 #ifdef G_HAVE_GINT64 | |
381 printf ("#define G_HAVE_GINT64\n"); | |
382 #endif | |
383 | |
384 #ifdef HAVE_LIBREADLINE | |
385 printf ("#define HAVE_LIBREADLINE\n"); | |
386 #endif | |
387 | |
388 #ifdef ENABLE_NLS | |
389 printf ("#define ENABLE_NLS\n"); | |
390 #endif | |
391 | |
392 #ifdef HAVE_GETTEXT | |
393 printf ("#define HAVE_GETTEXT\n"); | |
394 #endif | |
395 | |
396 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, | |
397 GLIB_MICRO_VERSION); | |
398 | |
399 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
400 | |
401 #ifdef USE_SSL | |
402 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
403 #endif | |
404 } | |
405 | |
406 | |
1 | 407 int |
408 gftp_parse_command_line (int *argc, char ***argv) | |
409 { | |
410 if (*argc > 1) | |
411 { | |
87 | 412 if (strcmp (argv[0][1], "--help") == 0 || |
413 strcmp (argv[0][1], "-h") == 0) | |
1 | 414 gftp_usage (); |
87 | 415 else if (strcmp (argv[0][1], "--version") == 0 || |
416 strcmp (argv[0][1], "-v") == 0) | |
1 | 417 { |
122 | 418 printf ("%s\n", gftp_version); |
1 | 419 exit (0); |
420 } | |
243 | 421 else if (strcmp (argv[0][1], "--info") == 0) |
422 { | |
423 gftp_info (); | |
424 exit (0); | |
425 } | |
1 | 426 } |
427 return (0); | |
428 } | |
429 | |
430 | |
431 void | |
432 gftp_usage (void) | |
433 { | |
162 | 434 printf (_("usage: gftp [[protocol://][user[:pass]@]site[:port][/directory]]\n")); |
1 | 435 exit (0); |
436 } | |
437 | |
438 | |
439 gint | |
440 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
441 { | |
442 return (strcmp ((char *) path1, (char *) path2) == 0); | |
443 } | |
444 | |
445 | |
446 guint | |
447 string_hash_function (gconstpointer key) | |
448 { | |
59 | 449 guint ret; |
450 int i; | |
451 | |
452 ret = 0; | |
87 | 453 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 454 ret += ((char *) key)[i]; |
455 | |
456 return (ret); | |
1 | 457 } |
458 | |
459 | |
460 void | |
461 free_file_list (GList * filelist) | |
462 { | |
463 gftp_file * tempfle; | |
464 GList * templist; | |
465 | |
466 templist = filelist; | |
467 while (templist != NULL) | |
468 { | |
469 tempfle = templist->data; | |
470 free_fdata (tempfle); | |
471 templist = templist->next; | |
472 } | |
473 g_list_free (filelist); | |
474 } | |
475 | |
476 | |
477 void | |
478 free_fdata (gftp_file * fle) | |
479 { | |
480 if (fle->file) | |
481 g_free (fle->file); | |
184 | 482 if (fle->utf8_file) |
483 g_free (fle->utf8_file); | |
1 | 484 if (fle->user) |
485 g_free (fle->user); | |
486 if (fle->group) | |
487 g_free (fle->group); | |
488 if (fle->attribs) | |
489 g_free (fle->attribs); | |
490 if (fle->destfile) | |
491 g_free (fle->destfile); | |
58 | 492 if (fle->fd > 0) |
102 | 493 close (fle->fd); |
1 | 494 g_free (fle); |
495 } | |
496 | |
497 | |
498 gftp_file * | |
499 copy_fdata (gftp_file * fle) | |
500 { | |
501 gftp_file * newfle; | |
502 | |
503 newfle = g_malloc0 (sizeof (*newfle)); | |
504 memcpy (newfle, fle, sizeof (*newfle)); | |
505 | |
506 if (fle->file) | |
87 | 507 newfle->file = g_strdup (fle->file); |
1 | 508 |
243 | 509 if (fle->utf8_file) |
510 newfle->utf8_file = g_strdup (fle->utf8_file); | |
511 | |
1 | 512 if (fle->user) |
87 | 513 newfle->user = g_strdup (fle->user); |
1 | 514 |
515 if (fle->group) | |
87 | 516 newfle->group = g_strdup (fle->group); |
1 | 517 |
518 if (fle->attribs) | |
87 | 519 newfle->attribs = g_strdup (fle->attribs); |
1 | 520 |
521 if (fle->destfile) | |
87 | 522 newfle->destfile = g_strdup (fle->destfile); |
523 | |
1 | 524 return (newfle); |
525 } | |
526 | |
527 | |
528 int | |
529 compare_request (gftp_request * request1, gftp_request * request2, | |
530 int compare_dirs) | |
531 { | |
532 char *strarr[3][2]; | |
533 int i, ret; | |
534 | |
535 ret = 1; | |
87 | 536 if (request1->protonum == request2->protonum && |
1 | 537 request1->port == request2->port) |
538 { | |
539 strarr[0][0] = request1->hostname; | |
540 strarr[0][1] = request2->hostname; | |
541 strarr[1][0] = request1->username; | |
542 strarr[1][1] = request2->username; | |
543 if (compare_dirs) | |
544 { | |
545 strarr[2][0] = request1->directory; | |
546 strarr[2][1] = request2->directory; | |
547 } | |
548 else | |
549 strarr[2][0] = strarr[2][1] = ""; | |
550 | |
551 for (i = 0; i < 3; i++) | |
552 { | |
553 if ((strarr[i][0] && !strarr[i][1]) || | |
554 (!strarr[i][0] && strarr[i][1])) | |
555 { | |
556 ret = 0; | |
557 break; | |
558 } | |
559 | |
560 if (strarr[i][0] && strarr[i][1] && | |
561 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
562 { | |
563 ret = 0; | |
564 break; | |
565 } | |
566 } | |
567 } | |
568 else | |
569 ret = 0; | |
570 return (ret); | |
571 } | |
572 | |
573 | |
129 | 574 gftp_transfer * |
575 gftp_tdata_new (void) | |
576 { | |
227 | 577 #if GLIB_MAJOR_VERSION == 1 |
578 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
579 #endif | |
129 | 580 gftp_transfer * tdata; |
581 | |
582 tdata = g_malloc0 (sizeof (*tdata)); | |
227 | 583 |
584 #if GLIB_MAJOR_VERSION == 1 | |
585 tdata->statmutex = init_mutex; | |
586 tdata->structmutex = init_mutex; | |
587 #else | |
168 | 588 g_static_mutex_init (&tdata->statmutex); |
589 g_static_mutex_init (&tdata->structmutex); | |
227 | 590 #endif |
591 | |
129 | 592 return (tdata); |
593 } | |
594 | |
595 | |
1 | 596 void |
597 free_tdata (gftp_transfer * tdata) | |
598 { | |
599 if (tdata->fromreq != NULL) | |
67 | 600 gftp_request_destroy (tdata->fromreq, 1); |
1 | 601 if (tdata->toreq != NULL) |
67 | 602 gftp_request_destroy (tdata->toreq, 1); |
1 | 603 free_file_list (tdata->files); |
604 g_free (tdata); | |
605 } | |
606 | |
607 | |
608 gftp_request * | |
151 | 609 copy_request (gftp_request * req, int copy_local_options) |
1 | 610 { |
611 gftp_request * newreq; | |
612 | |
66 | 613 newreq = gftp_request_new (); |
1 | 614 |
615 if (req->hostname) | |
56 | 616 newreq->hostname = g_strdup (req->hostname); |
1 | 617 if (req->username) |
56 | 618 newreq->username = g_strdup (req->username); |
1 | 619 if (req->password) |
56 | 620 newreq->password = g_strdup (req->password); |
1 | 621 if (req->account) |
56 | 622 newreq->account = g_strdup (req->account); |
1 | 623 if (req->directory) |
56 | 624 newreq->directory = g_strdup (req->directory); |
66 | 625 newreq->port = req->port; |
626 newreq->use_proxy = req->use_proxy; | |
627 newreq->logging_function = req->logging_function; | |
151 | 628 newreq->free_hostp = 0; |
629 newreq->hostp = req->hostp; | |
630 | |
631 if (copy_local_options) | |
199 | 632 gftp_copy_local_options (&newreq->local_options_vars, |
633 &newreq->local_options_hash, | |
634 req->local_options_vars, | |
635 req->num_local_options_vars); | |
1 | 636 |
173 | 637 if (req->init (newreq) < 0) |
638 { | |
639 gftp_request_destroy (newreq, 1); | |
640 return (NULL); | |
641 } | |
1 | 642 |
643 return (newreq); | |
644 } | |
645 | |
646 | |
16 | 647 static gint |
648 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
649 { | |
650 const gftp_file * f1, * f2; | |
651 | |
652 f1 = a; | |
653 f2 = b; | |
654 return (strcmp (f1->file, f2->file)); | |
655 } | |
656 | |
657 | |
658 static gint | |
659 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
660 { | |
661 const gftp_file * f1, * f2; | |
662 gint ret; | |
663 | |
664 f1 = a; | |
665 f2 = b; | |
666 ret = strcmp (f1->file, f2->file); | |
84 | 667 return (ret * -1); |
16 | 668 } |
669 | |
670 | |
671 static gint | |
672 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
673 { | |
674 const gftp_file * f1, * f2; | |
675 | |
676 f1 = a; | |
677 f2 = b; | |
678 return (strcmp (f1->user, f2->user)); | |
679 } | |
680 | |
681 | |
682 static gint | |
683 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
684 { | |
685 const gftp_file * f1, * f2; | |
686 gint ret; | |
687 | |
688 f1 = a; | |
689 f2 = b; | |
690 ret = strcmp (f1->user, f2->user); | |
84 | 691 return (ret * -1); |
16 | 692 } |
693 | |
694 | |
695 static gint | |
696 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
697 { | |
698 const gftp_file * f1, * f2; | |
699 | |
700 f1 = a; | |
701 f2 = b; | |
702 return (strcmp (f1->group, f2->group)); | |
703 } | |
704 | |
705 | |
706 static gint | |
707 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
708 { | |
709 const gftp_file * f1, * f2; | |
710 gint ret; | |
711 | |
712 f1 = a; | |
713 f2 = b; | |
714 ret = strcmp (f1->group, f2->group); | |
84 | 715 return (ret * -1); |
16 | 716 } |
717 | |
718 | |
719 static gint | |
720 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
721 { | |
722 const gftp_file * f1, * f2; | |
723 | |
724 f1 = a; | |
725 f2 = b; | |
726 return (strcmp (f1->attribs, f2->attribs)); | |
727 } | |
728 | |
729 | |
730 static gint | |
731 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
732 { | |
733 const gftp_file * f1, * f2; | |
734 gint ret; | |
735 | |
736 f1 = a; | |
737 f2 = b; | |
738 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 739 return (ret * -1); |
16 | 740 } |
741 | |
742 | |
743 static gint | |
744 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
745 { | |
746 const gftp_file * f1, * f2; | |
747 | |
748 f1 = a; | |
749 f2 = b; | |
750 if (f1->size < f2->size) | |
751 return (-1); | |
752 else if (f1->size == f2->size) | |
753 return (0); | |
754 else | |
755 return (1); | |
756 } | |
757 | |
758 | |
759 static gint | |
760 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
761 { | |
762 const gftp_file * f1, * f2; | |
763 | |
764 f1 = a; | |
765 f2 = b; | |
766 if (f1->size < f2->size) | |
767 return (1); | |
768 else if (f1->size == f2->size) | |
769 return (0); | |
770 else | |
771 return (-1); | |
772 } | |
773 | |
774 | |
775 static gint | |
776 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
777 { | |
778 const gftp_file * f1, * f2; | |
779 | |
780 f1 = a; | |
781 f2 = b; | |
782 if (f1->datetime < f2->datetime) | |
783 return (-1); | |
784 else if (f1->datetime == f2->datetime) | |
785 return (0); | |
786 else | |
787 return (1); | |
788 } | |
789 | |
790 | |
791 static gint | |
792 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
793 { | |
794 const gftp_file * f1, * f2; | |
795 | |
796 f1 = a; | |
797 f2 = b; | |
798 if (f1->datetime < f2->datetime) | |
799 return (1); | |
800 else if (f1->datetime == f2->datetime) | |
801 return (0); | |
802 else | |
803 return (-1); | |
804 } | |
805 | |
806 | |
807 GList * | |
808 gftp_sort_filelist (GList * filelist, int column, int asds) | |
809 { | |
810 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
811 GCompareFunc sortfunc; | |
812 gftp_file * tempfle; | |
122 | 813 int sort_dirs_first; |
16 | 814 |
815 files = dirs = dotdot = NULL; | |
816 | |
817 if (column == GFTP_SORT_COL_FILE) | |
818 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
819 else if (column == GFTP_SORT_COL_SIZE) | |
820 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
821 else if (column == GFTP_SORT_COL_USER) | |
822 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
823 else if (column == GFTP_SORT_COL_GROUP) | |
824 sortfunc = asds ? | |
825 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
826 else if (column == GFTP_SORT_COL_DATETIME) | |
827 sortfunc = asds ? | |
122 | 828 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
829 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 830 sortfunc = asds ? |
831 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 832 else /* Don't sort */ |
833 return (filelist); | |
834 | |
835 sort_dirs_first = 1; | |
836 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 837 |
838 for (tempitem = filelist; tempitem != NULL; ) | |
839 { | |
840 tempfle = tempitem->data; | |
841 insitem = tempitem; | |
842 tempitem = tempitem->next; | |
843 insitem->next = NULL; | |
844 | |
845 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
846 dotdot = insitem; | |
847 else if (sort_dirs_first && tempfle->isdir) | |
848 { | |
849 insitem->next = dirs; | |
850 dirs = insitem; | |
851 } | |
852 else | |
853 { | |
854 insitem->next = files; | |
855 files = insitem; | |
856 } | |
857 } | |
858 | |
859 if (dirs != NULL) | |
860 dirs = g_list_sort (dirs, sortfunc); | |
861 if (files != NULL) | |
862 files = g_list_sort (files, sortfunc); | |
863 | |
864 filelist = dotdot; | |
865 | |
866 if (filelist == NULL) | |
867 filelist = dirs; | |
868 else | |
869 filelist = g_list_concat (filelist, dirs); | |
870 | |
871 if (filelist == NULL) | |
872 filelist = files; | |
873 else | |
874 filelist = g_list_concat (filelist, files); | |
875 | |
39 | 876 /* I haven't check this, but I'm pretty sure some older versions of glib |
877 had a bug that the prev pointer wasn't being sent to NULL */ | |
878 filelist->prev = NULL; | |
16 | 879 return (filelist); |
880 } | |
881 | |
125 | 882 |
883 mode_t | |
884 gftp_parse_attribs (char *attribs) | |
885 { | |
886 mode_t mode; | |
887 int cur; | |
888 | |
889 cur = 0; | |
890 if (attribs[1] == 'r') | |
891 cur += 4; | |
892 if (attribs[2] == 'w') | |
893 cur += 2; | |
894 if (attribs[3] == 'x' || | |
895 attribs[3] == 's') | |
896 cur += 1; | |
897 mode = cur; | |
898 | |
899 cur = 0; | |
900 if (attribs[4] == 'r') | |
901 cur += 4; | |
902 if (attribs[5] == 'w') | |
903 cur += 2; | |
904 if (attribs[6] == 'x' || | |
905 attribs[6] == 's') | |
906 cur += 1; | |
907 mode = (mode * 10) + cur; | |
908 | |
909 cur = 0; | |
910 if (attribs[7] == 'r') | |
911 cur += 4; | |
912 if (attribs[8] == 'w') | |
913 cur += 2; | |
914 if (attribs[9] == 'x' || | |
915 attribs[9] == 's') | |
916 cur += 1; | |
917 mode = (mode * 10) + cur; | |
918 | |
919 return (mode); | |
920 } | |
921 | |
922 | |
131 | 923 char * |
924 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
925 { | |
926 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
927 struct tm *lt; | |
928 time_t t; | |
929 | |
930 lt = localtime (&fle->datetime); | |
931 | |
932 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
933 fle->group); | |
934 | |
935 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
936 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
937 else | |
938 { | |
939 #if defined (_LARGEFILE_SOURCE) | |
940 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
941 #else | |
942 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
943 #endif | |
944 } | |
945 | |
946 time (&t); | |
947 | |
948 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
949 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
950 else | |
951 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
952 | |
953 if (file_prefixstr == NULL) | |
954 file_prefixstr = ""; | |
955 if (file_suffixstr == NULL) | |
956 file_suffixstr = ""; | |
957 | |
958 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
184 | 959 file_prefixstr, |
960 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
961 file_suffixstr); | |
131 | 962 |
963 g_free (tempstr1); | |
964 g_free (tempstr2); | |
965 | |
966 return (ret); | |
967 } | |
968 | |
969 | |
125 | 970 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
971 | |
972 struct hostent * | |
973 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
974 { | |
975 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
976 struct hostent *hent; | |
977 | |
978 if (g_thread_supported ()) | |
979 g_static_mutex_lock (&hostfunclock); | |
980 | |
981 if ((hent = gethostbyname (name)) == NULL) | |
982 { | |
983 if (h_errnop) | |
984 *h_errnop = h_errno; | |
985 } | |
986 else | |
987 { | |
988 *result_buf = *hent; | |
989 hent = result_buf; | |
990 } | |
991 | |
992 if (g_thread_supported ()) | |
993 g_static_mutex_unlock (&hostfunclock); | |
994 | |
995 return (hent); | |
996 } | |
997 | |
998 #endif /* !HAVE_GETADDRINFO */ | |
999 | |
1000 struct servent * | |
1001 r_getservbyname (const char *name, const char *proto, | |
1002 struct servent *result_buf, int *h_errnop) | |
1003 { | |
1004 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
1005 struct servent *sent; | |
1006 | |
1007 if (g_thread_supported ()) | |
1008 g_static_mutex_lock (&servfunclock); | |
1009 | |
1010 if ((sent = getservbyname (name, proto)) == NULL) | |
1011 { | |
1012 if (h_errnop) | |
1013 *h_errnop = h_errno; | |
1014 } | |
1015 else | |
1016 { | |
1017 *result_buf = *sent; | |
1018 sent = result_buf; | |
1019 } | |
1020 | |
1021 if (g_thread_supported ()) | |
1022 g_static_mutex_unlock (&servfunclock); | |
1023 return (sent); | |
1024 } | |
1025 | |
168 | 1026 |
1027 char * | |
1028 base64_encode (char *str) | |
1029 { | |
1030 | |
1031 /* The standard to Base64 encoding can be found in RFC2045 */ | |
1032 | |
1033 char *newstr, *newpos, *fillpos, *pos; | |
1034 unsigned char table[64], encode[3]; | |
1035 int i, num; | |
1036 | |
1037 for (i = 0; i < 26; i++) | |
1038 { | |
1039 table[i] = 'A' + i; | |
1040 table[i + 26] = 'a' + i; | |
1041 } | |
1042 | |
1043 for (i = 0; i < 10; i++) | |
1044 table[i + 52] = '0' + i; | |
1045 | |
1046 table[62] = '+'; | |
207 | 1047 table[63] = '/'; |
168 | 1048 |
1049 num = strlen (str) / 3; | |
1050 if (strlen (str) % 3 > 0) | |
1051 num++; | |
1052 newstr = g_malloc (num * 4 + 1); | |
1053 newstr[num * 4] = '\0'; | |
1054 newpos = newstr; | |
1055 | |
1056 pos = str; | |
1057 while (*pos != '\0') | |
1058 { | |
1059 memset (encode, 0, sizeof (encode)); | |
1060 for (i = 0; i < 3 && *pos != '\0'; i++) | |
1061 encode[i] = *pos++; | |
1062 | |
1063 fillpos = newpos; | |
1064 *newpos++ = table[encode[0] >> 2]; | |
1065 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1066 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1067 *newpos++ = table[encode[2] & 0x3F]; | |
1068 while (i < 3) | |
1069 fillpos[++i] = '='; | |
1070 } | |
1071 return (newstr); | |
1072 } | |
1073 | |
199 | 1074 |
1075 void | |
1076 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
1077 { | |
1078 if (entry->hostname) | |
1079 g_free (entry->hostname); | |
1080 if (entry->remote_dir) | |
1081 g_free (entry->remote_dir); | |
1082 if (entry->local_dir) | |
1083 g_free (entry->local_dir); | |
1084 if (entry->user) | |
1085 g_free (entry->user); | |
1086 if (entry->pass) | |
1087 g_free (entry->pass); | |
1088 if (entry->acct) | |
1089 g_free (entry->acct); | |
1090 if (entry->protocol) | |
1091 g_free (entry->protocol); | |
1092 | |
1093 if (entry->local_options_vars != NULL) | |
1094 { | |
201 | 1095 gftp_config_free_options (entry->local_options_vars, |
1096 entry->local_options_hash, | |
1097 entry->num_local_options_vars); | |
1098 | |
199 | 1099 entry->local_options_vars = NULL; |
201 | 1100 entry->local_options_hash = NULL; |
199 | 1101 entry->num_local_options_vars = 0; |
1102 } | |
1103 } | |
1104 | |
201 | 1105 |
1106 void | |
1107 gftp_shutdown (void) | |
1108 { | |
203 | 1109 #ifdef WITH_DMALLOC |
201 | 1110 gftp_config_vars * cv; |
1111 GList * templist; | |
203 | 1112 #endif |
201 | 1113 |
1114 if (gftp_logfd != NULL) | |
1115 fclose (gftp_logfd); | |
1116 | |
1117 gftp_clear_cache_files (); | |
1118 | |
1119 if (gftp_configuration_changed) | |
1120 gftp_write_config_file (); | |
1121 | |
1122 #ifdef WITH_DMALLOC | |
1123 if (gftp_global_options_htable != NULL) | |
1124 g_hash_table_destroy (gftp_global_options_htable); | |
1125 | |
1126 if (gftp_config_list_htable != NULL) | |
1127 g_hash_table_destroy (gftp_config_list_htable); | |
1128 | |
1129 if (gftp_bookmarks_htable != NULL) | |
1130 g_hash_table_destroy (gftp_bookmarks_htable); | |
1131 | |
1132 for (templist = gftp_options_list; | |
1133 templist != NULL; | |
1134 templist = templist->next) | |
1135 { | |
1136 cv = templist->data; | |
1137 gftp_config_free_options (cv, NULL, -1); | |
1138 } | |
1139 | |
1140 gftp_bookmarks_destroy (gftp_bookmarks); | |
1141 | |
1142 dmalloc_shutdown (); | |
1143 #endif | |
1144 | |
1145 exit (0); | |
1146 } | |
1147 | |
207 | 1148 |
1149 GList * | |
1150 get_next_selection (GList * selection, GList ** list, int *curnum) | |
1151 { | |
1152 gftp_file * tempfle; | |
1153 int i, newpos; | |
1154 | |
1155 newpos = GPOINTER_TO_INT (selection->data); | |
1156 i = *curnum - newpos; | |
1157 | |
1158 if (i < 0) | |
1159 { | |
1160 while (i != 0) | |
1161 { | |
1162 tempfle = (*list)->data; | |
1163 if (tempfle->shown) | |
1164 { | |
1165 ++*curnum; | |
1166 i++; | |
1167 } | |
1168 *list = (*list)->next; | |
1169 } | |
1170 } | |
1171 else if (i > 0) | |
1172 { | |
1173 while (i != 0) | |
1174 { | |
1175 tempfle = (*list)->data; | |
1176 if (tempfle->shown) | |
1177 { | |
1178 --*curnum; | |
1179 i--; | |
1180 } | |
1181 *list = (*list)->prev; | |
1182 } | |
1183 } | |
1184 | |
1185 tempfle = (*list)->data; | |
1186 while ((*list)->next && !tempfle->shown) | |
1187 { | |
1188 *list = (*list)->next; | |
1189 tempfle = (*list)->data; | |
1190 } | |
1191 return (selection->next); | |
1192 } | |
1193 | |
1194 | |
227 | 1195 #if GLIB_MAJOR_VERSION == 1 |
1196 | |
1197 char * | |
1198 g_build_path (const char *separator, const char *first_element, ...) | |
1199 { | |
1200 const char *element; | |
1201 size_t len, retlen; | |
1202 int add_separator; | |
1203 va_list args; | |
1204 char *ret; | |
1205 | |
1206 g_return_val_if_fail (separator != NULL, NULL); | |
1207 | |
1208 ret = g_malloc0 (1); | |
1209 retlen = 0; | |
1210 | |
1211 va_start (args, first_element); | |
1212 for (element = first_element; | |
1213 element != NULL; | |
1214 element = va_arg (args, char *)) | |
1215 { | |
1216 len = strlen (element); | |
1217 | |
1218 if (len > 0 && element[len - 1] == *separator) | |
1219 add_separator = 0; | |
1220 else | |
1221 { | |
1222 add_separator = 1; | |
1223 len++; | |
1224 } | |
1225 | |
1226 retlen += len; | |
1227 ret = g_realloc (ret, retlen + 1); | |
1228 strncat (ret, element, retlen); | |
1229 | |
1230 if (add_separator) | |
1231 strncat (ret, separator, retlen); | |
1232 } | |
1233 | |
1234 return (ret); | |
1235 } | |
1236 | |
1237 #endif | |
1238 | |
244 | 1239 |
1240 off_t | |
1241 gftp_parse_file_size (char *str) | |
1242 { | |
1243 #if defined (_LARGEFILE_SOURCE) | |
1244 return (strtoll (str, NULL, 10)); | |
1245 #else | |
1246 return (strtol (str, NULL, 10)); | |
1247 #endif | |
1248 } | |
1249 |