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