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