Mercurial > gftp.yaz
annotate lib/misc.c @ 173:4c288d05b26a
2003-6-8 Brian Masney <masneyb@gftp.org>
* lib/bookmark.c lib/gftp.h lib/https.c lib/local.c lib/misc.c
lib/protocols.c lib/rfc2068.c lib/rfc959.c lib/sshv2.c
src/text/gftp-text.c src/gtk/gftp-gtk.c - made the init function for
all the protocols return an integer instead of nothing. If there was an
error setting up the protocol, GFTP_EFATAL should be returned and the
connection should be aborted. The HTTPS protocol uses this to return if
SSL support was not compiled in
* lib/protocols.c src/text/gftp-text.c src/gtk/dnd.c
src/gtk/gftp-gtk.c src/gtk/menu-items.c - have gftp_parse_url() log the
error messages to the user. This shouldn't have been done in the
individual ports
* lib/https.c - only initialize the SSL engine the first time a SSL
connection is made.
author | masneyb |
---|---|
date | Mon, 09 Jun 2003 00:53:20 +0000 |
parents | c505d9ba9d53 |
children | 33b394ebba68 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* misc.c - general purpose routines */ | |
122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
102 | 20 static const char cvsid[] = "$Id$"; |
21 | |
1 | 22 #include "gftp.h" |
23 #include "options.h" | |
24 | |
87 | 25 /* FIXME - this isn't right for all locales. Use glib's printf instead */ |
1 | 26 char * |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
27 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 28 { |
29 char *frompos, *topos, src[50], *dest; | |
30 int len, num, rem, i; | |
31 | |
32 if (dest_str != NULL) | |
33 *dest_str = '\0'; | |
56 | 34 len = (number > 0 ? log10 (number) : 0) + 2; |
1 | 35 |
36 if (len <= 0) | |
37 { | |
38 if (dest_str != NULL) | |
39 strncpy (dest_str, "0", dest_len); | |
40 else | |
56 | 41 dest_str = g_strdup ("0"); |
1 | 42 return (dest_str); |
43 } | |
44 | |
45 len += len / 3; | |
46 if (dest_str != NULL && len > dest_len) | |
47 { | |
48 | |
49 for (i=0; i<dest_len - 1; i++) | |
50 dest_str[i] = 'X'; | |
51 dest_str[dest_len - 1] = '\0'; | |
52 return (dest_str); | |
53 } | |
54 | |
55 if (dest_str == NULL) | |
56 dest = g_malloc0 (len); | |
57 else | |
58 dest = dest_str; | |
59 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
60 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
61 g_snprintf (src, sizeof (src), "%lld", number); |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
62 #else |
1 | 63 g_snprintf (src, sizeof (src), "%ld", number); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
64 #endif |
1 | 65 |
66 num = strlen (src) / 3 - 1; | |
67 rem = strlen (src) % 3; | |
68 frompos = src; | |
69 topos = dest; | |
70 for (i = 0; i < rem; i++) | |
71 *topos++ = *frompos++; | |
72 | |
73 if (*frompos != '\0') | |
74 { | |
75 if (rem != 0) | |
76 *topos++ = ','; | |
77 while (num > 0) | |
78 { | |
79 for (i = 0; i < 3; i++) | |
80 *topos++ = *frompos++; | |
81 *topos++ = ','; | |
82 num--; | |
83 } | |
84 for (i = 0; i < 3; i++) | |
85 *topos++ = *frompos++; | |
86 } | |
87 *topos = '\0'; | |
88 return (dest); | |
89 } | |
90 | |
91 | |
92 char * | |
93 alltrim (char *str) | |
94 { | |
95 char *pos, *newpos; | |
96 int diff; | |
97 | |
98 pos = str + strlen (str) - 1; | |
87 | 99 while (pos >= str && (*pos == ' ' || *pos == '\t')) |
1 | 100 *pos-- = '\0'; |
101 | |
102 pos = str; | |
103 diff = 0; | |
104 while (*pos++ == ' ') | |
105 diff++; | |
106 | |
107 if (diff == 0) | |
108 return (str); | |
109 | |
110 pos = str + diff; | |
111 newpos = str; | |
112 while (*pos != '\0') | |
113 *newpos++ = *pos++; | |
114 *newpos = '\0'; | |
115 | |
116 return (str); | |
117 } | |
118 | |
119 | |
120 char * | |
121 expand_path (const char *src) | |
122 { | |
123 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
124 struct passwd *pw; | |
125 | |
126 pw = NULL; | |
124 | 127 str = g_strdup (src); |
1 | 128 |
129 if (*str == '~') | |
130 { | |
131 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
132 pw = getpwuid (geteuid ()); | |
133 else | |
134 { | |
135 if ((pos = strchr (str, '/')) != NULL) | |
136 *pos = '\0'; | |
137 | |
138 pw = getpwnam (str + 1); | |
139 | |
140 if (pos != NULL) | |
141 *pos = '/'; | |
142 } | |
143 } | |
144 | |
145 endpos = str; | |
146 newstr = NULL; | |
147 while ((pos = strchr (endpos, '/')) != NULL) | |
148 { | |
149 pos++; | |
150 while (*pos == '/') | |
151 pos++; | |
152 if ((endpos = strchr (pos, '/')) == NULL) | |
153 endpos = pos + strlen (pos); | |
154 tempchar = *endpos; | |
155 *endpos = '\0'; | |
156 if (strcmp (pos, "..") == 0) | |
157 { | |
158 *(pos - 1) = '\0'; | |
159 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
160 *prevpos = '\0'; | |
161 } | |
162 else if (strcmp (pos, ".") != 0) | |
163 { | |
164 if (newstr == NULL) | |
56 | 165 newstr = g_strdup (pos - 1); |
1 | 166 else |
167 { | |
168 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
169 g_free (newstr); | |
170 newstr = tempstr; | |
171 } | |
172 } | |
173 *endpos = tempchar; | |
174 if (*endpos == '\0') | |
175 break; | |
176 endpos = pos + 1; | |
177 } | |
178 | |
179 if (newstr == NULL || *newstr == '\0') | |
180 { | |
181 if (newstr != NULL) | |
182 g_free (newstr); | |
183 newstr = g_malloc0 (2); | |
184 *newstr = '/'; | |
185 } | |
186 | |
187 g_free (str); | |
188 | |
189 if (pw != NULL) | |
190 { | |
191 if ((pos = strchr (newstr, '/')) == NULL) | |
124 | 192 str = g_strdup (pw->pw_dir); |
1 | 193 else |
194 str = g_strconcat (pw->pw_dir, pos, NULL); | |
195 | |
196 g_free (newstr); | |
197 newstr = str; | |
198 } | |
199 | |
200 return (newstr); | |
201 } | |
202 | |
203 | |
204 void | |
205 remove_double_slashes (char *string) | |
206 { | |
207 char *newpos, *oldpos; | |
87 | 208 size_t len; |
1 | 209 |
210 oldpos = newpos = string; | |
211 while (*oldpos != '\0') | |
212 { | |
213 *newpos++ = *oldpos++; | |
214 if (*oldpos == '\0') | |
215 break; | |
216 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
217 oldpos++; | |
218 } | |
219 *newpos = '\0'; | |
87 | 220 |
221 len = strlen (string); | |
222 if (string[len - 1] == '/') | |
223 string[len - 1] = '\0'; | |
1 | 224 } |
225 | |
226 | |
227 void | |
228 make_nonnull (char **str) | |
229 { | |
230 if (*str == NULL) | |
231 *str = g_malloc0 (1); | |
232 } | |
233 | |
234 | |
235 int | |
236 copyfile (char *source, char *dest) | |
237 { | |
58 | 238 int srcfd, destfd; |
1 | 239 char buf[8192]; |
58 | 240 ssize_t n; |
1 | 241 |
58 | 242 if ((srcfd = open (source, O_RDONLY)) == -1) |
243 { | |
244 printf (_("Error: Cannot open local file %s: %s\n"), | |
245 source, g_strerror (errno)); | |
246 exit (1); | |
247 } | |
1 | 248 |
58 | 249 if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
1 | 250 { |
58 | 251 printf (_("Error: Cannot open local file %s: %s\n"), |
252 dest, g_strerror (errno)); | |
253 close (srcfd); | |
254 exit (1); | |
1 | 255 } |
256 | |
58 | 257 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
258 { | |
259 if (write (destfd, buf, n) == -1) | |
260 { | |
261 printf (_("Error: Could not write to socket: %s\n"), | |
262 g_strerror (errno)); | |
263 exit (1); | |
264 } | |
265 } | |
1 | 266 |
58 | 267 if (n == -1) |
268 { | |
269 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
270 exit (1); | |
271 } | |
272 | |
87 | 273 if (close (srcfd) == -1) |
274 { | |
275 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
276 exit (1); | |
277 } | |
278 | |
279 if (close (destfd) == -1) | |
280 { | |
281 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
282 exit (1); | |
283 } | |
1 | 284 |
285 return (1); | |
286 } | |
287 | |
288 | |
87 | 289 /* FIXME - is there a replacement for this */ |
1 | 290 int |
291 gftp_match_filespec (char *filename, char *filespec) | |
292 { | |
293 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
294 size_t len, curlen; | |
295 | |
296 if (filename == NULL || *filename == '\0' || | |
297 filespec == NULL || *filespec == '\0') | |
298 return(1); | |
299 | |
300 filepos = filename; | |
301 wcpos = filespec; | |
302 while(1) | |
303 { | |
304 if (*wcpos == '\0') | |
305 return (1); | |
306 else if (*filepos == '\0') | |
307 return(0); | |
308 else if(*wcpos == '?') | |
309 { | |
310 wcpos++; | |
311 filepos++; | |
312 } | |
313 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
314 return(1); | |
315 else if(*wcpos == '*') | |
316 { | |
317 len = sizeof (search_str); | |
318 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
319 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
320 curlen++, *newpos++ = *pos++); | |
321 *newpos = '\0'; | |
322 | |
323 if ((filepos = strstr (filepos, search_str)) == NULL) | |
324 return(0); | |
325 wcpos += curlen + 1; | |
326 filepos += curlen; | |
327 } | |
328 else if(*wcpos++ != *filepos++) | |
329 return(0); | |
330 } | |
331 return (1); | |
332 } | |
333 | |
334 | |
335 int | |
336 gftp_parse_command_line (int *argc, char ***argv) | |
337 { | |
338 if (*argc > 1) | |
339 { | |
87 | 340 if (strcmp (argv[0][1], "--help") == 0 || |
341 strcmp (argv[0][1], "-h") == 0) | |
1 | 342 gftp_usage (); |
87 | 343 else if (strcmp (argv[0][1], "--version") == 0 || |
344 strcmp (argv[0][1], "-v") == 0) | |
1 | 345 { |
122 | 346 printf ("%s\n", gftp_version); |
1 | 347 exit (0); |
348 } | |
349 } | |
350 return (0); | |
351 } | |
352 | |
353 | |
354 void | |
355 gftp_usage (void) | |
356 { | |
162 | 357 printf (_("usage: gftp [[protocol://][user[:pass]@]site[:port][/directory]]\n")); |
1 | 358 exit (0); |
359 } | |
360 | |
361 | |
362 char * | |
363 get_xpm_path (char *filename, int quit_on_err) | |
364 { | |
365 char *tempstr, *exfile; | |
366 | |
367 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
368 exfile = expand_path (tempstr); | |
369 g_free (tempstr); | |
370 if (access (exfile, F_OK) != 0) | |
371 { | |
372 g_free (exfile); | |
373 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
374 exfile = expand_path (tempstr); | |
375 g_free (tempstr); | |
376 if (access (exfile, F_OK) != 0) | |
377 { | |
378 g_free (exfile); | |
379 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
380 if (access (exfile, F_OK) != 0) | |
381 { | |
382 g_free (exfile); | |
383 if (!quit_on_err) | |
384 return (NULL); | |
87 | 385 |
1 | 386 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), |
387 filename, SHARE_DIR, BASE_CONF_DIR); | |
87 | 388 exit (1); |
1 | 389 } |
390 } | |
391 } | |
392 return (exfile); | |
393 } | |
394 | |
395 | |
396 gint | |
397 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
398 { | |
399 return (strcmp ((char *) path1, (char *) path2) == 0); | |
400 } | |
401 | |
402 | |
403 guint | |
404 string_hash_function (gconstpointer key) | |
405 { | |
59 | 406 guint ret; |
407 int i; | |
408 | |
409 ret = 0; | |
87 | 410 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 411 ret += ((char *) key)[i]; |
412 | |
413 return (ret); | |
1 | 414 } |
415 | |
416 | |
417 void | |
418 free_file_list (GList * filelist) | |
419 { | |
420 gftp_file * tempfle; | |
421 GList * templist; | |
422 | |
423 templist = filelist; | |
424 while (templist != NULL) | |
425 { | |
426 tempfle = templist->data; | |
427 free_fdata (tempfle); | |
428 templist = templist->next; | |
429 } | |
430 g_list_free (filelist); | |
431 } | |
432 | |
433 | |
434 void | |
435 free_fdata (gftp_file * fle) | |
436 { | |
437 if (fle->file) | |
438 g_free (fle->file); | |
439 if (fle->user) | |
440 g_free (fle->user); | |
441 if (fle->group) | |
442 g_free (fle->group); | |
443 if (fle->attribs) | |
444 g_free (fle->attribs); | |
445 if (fle->destfile) | |
446 g_free (fle->destfile); | |
58 | 447 if (fle->fd > 0) |
102 | 448 close (fle->fd); |
1 | 449 g_free (fle); |
450 } | |
451 | |
452 | |
453 gftp_file * | |
454 copy_fdata (gftp_file * fle) | |
455 { | |
456 gftp_file * newfle; | |
457 | |
458 newfle = g_malloc0 (sizeof (*newfle)); | |
459 memcpy (newfle, fle, sizeof (*newfle)); | |
460 | |
461 if (fle->file) | |
87 | 462 newfle->file = g_strdup (fle->file); |
1 | 463 |
464 if (fle->user) | |
87 | 465 newfle->user = g_strdup (fle->user); |
1 | 466 |
467 if (fle->group) | |
87 | 468 newfle->group = g_strdup (fle->group); |
1 | 469 |
470 if (fle->attribs) | |
87 | 471 newfle->attribs = g_strdup (fle->attribs); |
1 | 472 |
473 if (fle->destfile) | |
87 | 474 newfle->destfile = g_strdup (fle->destfile); |
475 | |
1 | 476 return (newfle); |
477 } | |
478 | |
479 | |
480 int | |
481 compare_request (gftp_request * request1, gftp_request * request2, | |
482 int compare_dirs) | |
483 { | |
484 char *strarr[3][2]; | |
485 int i, ret; | |
486 | |
487 ret = 1; | |
87 | 488 if (request1->protonum == request2->protonum && |
1 | 489 request1->port == request2->port) |
490 { | |
491 strarr[0][0] = request1->hostname; | |
492 strarr[0][1] = request2->hostname; | |
493 strarr[1][0] = request1->username; | |
494 strarr[1][1] = request2->username; | |
495 if (compare_dirs) | |
496 { | |
497 strarr[2][0] = request1->directory; | |
498 strarr[2][1] = request2->directory; | |
499 } | |
500 else | |
501 strarr[2][0] = strarr[2][1] = ""; | |
502 | |
503 for (i = 0; i < 3; i++) | |
504 { | |
505 if ((strarr[i][0] && !strarr[i][1]) || | |
506 (!strarr[i][0] && strarr[i][1])) | |
507 { | |
508 ret = 0; | |
509 break; | |
510 } | |
511 | |
512 if (strarr[i][0] && strarr[i][1] && | |
513 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
514 { | |
515 ret = 0; | |
516 break; | |
517 } | |
518 } | |
519 } | |
520 else | |
521 ret = 0; | |
522 return (ret); | |
523 } | |
524 | |
525 | |
129 | 526 gftp_transfer * |
527 gftp_tdata_new (void) | |
528 { | |
529 gftp_transfer * tdata; | |
530 | |
531 tdata = g_malloc0 (sizeof (*tdata)); | |
168 | 532 g_static_mutex_init (&tdata->statmutex); |
533 g_static_mutex_init (&tdata->structmutex); | |
129 | 534 return (tdata); |
535 } | |
536 | |
537 | |
1 | 538 void |
539 free_tdata (gftp_transfer * tdata) | |
540 { | |
541 if (tdata->fromreq != NULL) | |
67 | 542 gftp_request_destroy (tdata->fromreq, 1); |
1 | 543 if (tdata->toreq != NULL) |
67 | 544 gftp_request_destroy (tdata->toreq, 1); |
1 | 545 free_file_list (tdata->files); |
546 g_free (tdata); | |
547 } | |
548 | |
549 | |
136 | 550 void |
551 gftp_copy_local_options (gftp_request * dest, gftp_request * source) | |
552 { | |
553 int i; | |
554 | |
555 if (source->local_options_vars == NULL) | |
556 { | |
557 dest->local_options_vars = NULL; | |
558 dest->num_local_options_vars = 0; | |
559 dest->local_options_hash = NULL; | |
560 return; | |
561 } | |
562 | |
563 dest->local_options_hash = g_hash_table_new (string_hash_function, | |
564 string_hash_compare); | |
565 | |
566 for (i=0; source->local_options_vars[i].key != NULL; i++); | |
567 | |
568 dest->local_options_vars = g_malloc (sizeof (gftp_config_vars) * (i + 1)); | |
569 memcpy (dest, source, sizeof (gftp_config_vars) * (i + 1)); | |
570 dest->num_local_options_vars = i; | |
571 | |
572 for (i=0; dest->local_options_vars[i].key != NULL; i++) | |
573 { | |
574 g_hash_table_insert (dest->local_options_hash, | |
575 dest->local_options_vars[i].key, | |
576 &dest->local_options_vars[i]); | |
577 } | |
578 } | |
579 | |
580 | |
1 | 581 gftp_request * |
151 | 582 copy_request (gftp_request * req, int copy_local_options) |
1 | 583 { |
584 gftp_request * newreq; | |
585 | |
66 | 586 newreq = gftp_request_new (); |
1 | 587 |
588 if (req->hostname) | |
56 | 589 newreq->hostname = g_strdup (req->hostname); |
1 | 590 if (req->username) |
56 | 591 newreq->username = g_strdup (req->username); |
1 | 592 if (req->password) |
56 | 593 newreq->password = g_strdup (req->password); |
1 | 594 if (req->account) |
56 | 595 newreq->account = g_strdup (req->account); |
1 | 596 if (req->directory) |
56 | 597 newreq->directory = g_strdup (req->directory); |
66 | 598 newreq->port = req->port; |
599 newreq->use_proxy = req->use_proxy; | |
600 newreq->logging_function = req->logging_function; | |
151 | 601 newreq->free_hostp = 0; |
602 newreq->hostp = req->hostp; | |
603 | |
604 if (copy_local_options) | |
605 gftp_copy_local_options (newreq, req); | |
1 | 606 |
173 | 607 if (req->init (newreq) < 0) |
608 { | |
609 gftp_request_destroy (newreq, 1); | |
610 return (NULL); | |
611 } | |
1 | 612 |
613 return (newreq); | |
614 } | |
615 | |
616 | |
16 | 617 static gint |
618 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
619 { | |
620 const gftp_file * f1, * f2; | |
621 | |
622 f1 = a; | |
623 f2 = b; | |
624 return (strcmp (f1->file, f2->file)); | |
625 } | |
626 | |
627 | |
628 static gint | |
629 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
630 { | |
631 const gftp_file * f1, * f2; | |
632 gint ret; | |
633 | |
634 f1 = a; | |
635 f2 = b; | |
636 ret = strcmp (f1->file, f2->file); | |
84 | 637 return (ret * -1); |
16 | 638 } |
639 | |
640 | |
641 static gint | |
642 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
643 { | |
644 const gftp_file * f1, * f2; | |
645 | |
646 f1 = a; | |
647 f2 = b; | |
648 return (strcmp (f1->user, f2->user)); | |
649 } | |
650 | |
651 | |
652 static gint | |
653 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
654 { | |
655 const gftp_file * f1, * f2; | |
656 gint ret; | |
657 | |
658 f1 = a; | |
659 f2 = b; | |
660 ret = strcmp (f1->user, f2->user); | |
84 | 661 return (ret * -1); |
16 | 662 } |
663 | |
664 | |
665 static gint | |
666 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
667 { | |
668 const gftp_file * f1, * f2; | |
669 | |
670 f1 = a; | |
671 f2 = b; | |
672 return (strcmp (f1->group, f2->group)); | |
673 } | |
674 | |
675 | |
676 static gint | |
677 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
678 { | |
679 const gftp_file * f1, * f2; | |
680 gint ret; | |
681 | |
682 f1 = a; | |
683 f2 = b; | |
684 ret = strcmp (f1->group, f2->group); | |
84 | 685 return (ret * -1); |
16 | 686 } |
687 | |
688 | |
689 static gint | |
690 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
691 { | |
692 const gftp_file * f1, * f2; | |
693 | |
694 f1 = a; | |
695 f2 = b; | |
696 return (strcmp (f1->attribs, f2->attribs)); | |
697 } | |
698 | |
699 | |
700 static gint | |
701 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
702 { | |
703 const gftp_file * f1, * f2; | |
704 gint ret; | |
705 | |
706 f1 = a; | |
707 f2 = b; | |
708 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 709 return (ret * -1); |
16 | 710 } |
711 | |
712 | |
713 static gint | |
714 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
715 { | |
716 const gftp_file * f1, * f2; | |
717 | |
718 f1 = a; | |
719 f2 = b; | |
720 if (f1->size < f2->size) | |
721 return (-1); | |
722 else if (f1->size == f2->size) | |
723 return (0); | |
724 else | |
725 return (1); | |
726 } | |
727 | |
728 | |
729 static gint | |
730 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
731 { | |
732 const gftp_file * f1, * f2; | |
733 | |
734 f1 = a; | |
735 f2 = b; | |
736 if (f1->size < f2->size) | |
737 return (1); | |
738 else if (f1->size == f2->size) | |
739 return (0); | |
740 else | |
741 return (-1); | |
742 } | |
743 | |
744 | |
745 static gint | |
746 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
747 { | |
748 const gftp_file * f1, * f2; | |
749 | |
750 f1 = a; | |
751 f2 = b; | |
752 if (f1->datetime < f2->datetime) | |
753 return (-1); | |
754 else if (f1->datetime == f2->datetime) | |
755 return (0); | |
756 else | |
757 return (1); | |
758 } | |
759 | |
760 | |
761 static gint | |
762 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
763 { | |
764 const gftp_file * f1, * f2; | |
765 | |
766 f1 = a; | |
767 f2 = b; | |
768 if (f1->datetime < f2->datetime) | |
769 return (1); | |
770 else if (f1->datetime == f2->datetime) | |
771 return (0); | |
772 else | |
773 return (-1); | |
774 } | |
775 | |
776 | |
777 GList * | |
778 gftp_sort_filelist (GList * filelist, int column, int asds) | |
779 { | |
780 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
781 GCompareFunc sortfunc; | |
782 gftp_file * tempfle; | |
122 | 783 int sort_dirs_first; |
16 | 784 |
785 files = dirs = dotdot = NULL; | |
786 | |
787 if (column == GFTP_SORT_COL_FILE) | |
788 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
789 else if (column == GFTP_SORT_COL_SIZE) | |
790 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
791 else if (column == GFTP_SORT_COL_USER) | |
792 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
793 else if (column == GFTP_SORT_COL_GROUP) | |
794 sortfunc = asds ? | |
795 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
796 else if (column == GFTP_SORT_COL_DATETIME) | |
797 sortfunc = asds ? | |
122 | 798 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
799 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 800 sortfunc = asds ? |
801 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 802 else /* Don't sort */ |
803 return (filelist); | |
804 | |
805 sort_dirs_first = 1; | |
806 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 807 |
808 for (tempitem = filelist; tempitem != NULL; ) | |
809 { | |
810 tempfle = tempitem->data; | |
811 insitem = tempitem; | |
812 tempitem = tempitem->next; | |
813 insitem->next = NULL; | |
814 | |
815 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
816 dotdot = insitem; | |
817 else if (sort_dirs_first && tempfle->isdir) | |
818 { | |
819 insitem->next = dirs; | |
820 dirs = insitem; | |
821 } | |
822 else | |
823 { | |
824 insitem->next = files; | |
825 files = insitem; | |
826 } | |
827 } | |
828 | |
829 if (dirs != NULL) | |
830 dirs = g_list_sort (dirs, sortfunc); | |
831 if (files != NULL) | |
832 files = g_list_sort (files, sortfunc); | |
833 | |
834 filelist = dotdot; | |
835 | |
836 if (filelist == NULL) | |
837 filelist = dirs; | |
838 else | |
839 filelist = g_list_concat (filelist, dirs); | |
840 | |
841 if (filelist == NULL) | |
842 filelist = files; | |
843 else | |
844 filelist = g_list_concat (filelist, files); | |
845 | |
39 | 846 /* I haven't check this, but I'm pretty sure some older versions of glib |
847 had a bug that the prev pointer wasn't being sent to NULL */ | |
848 filelist->prev = NULL; | |
16 | 849 return (filelist); |
850 } | |
851 | |
125 | 852 |
853 mode_t | |
854 gftp_parse_attribs (char *attribs) | |
855 { | |
856 mode_t mode; | |
857 int cur; | |
858 | |
859 cur = 0; | |
860 if (attribs[1] == 'r') | |
861 cur += 4; | |
862 if (attribs[2] == 'w') | |
863 cur += 2; | |
864 if (attribs[3] == 'x' || | |
865 attribs[3] == 's') | |
866 cur += 1; | |
867 mode = cur; | |
868 | |
869 cur = 0; | |
870 if (attribs[4] == 'r') | |
871 cur += 4; | |
872 if (attribs[5] == 'w') | |
873 cur += 2; | |
874 if (attribs[6] == 'x' || | |
875 attribs[6] == 's') | |
876 cur += 1; | |
877 mode = (mode * 10) + cur; | |
878 | |
879 cur = 0; | |
880 if (attribs[7] == 'r') | |
881 cur += 4; | |
882 if (attribs[8] == 'w') | |
883 cur += 2; | |
884 if (attribs[9] == 'x' || | |
885 attribs[9] == 's') | |
886 cur += 1; | |
887 mode = (mode * 10) + cur; | |
888 | |
889 return (mode); | |
890 } | |
891 | |
892 | |
131 | 893 char * |
894 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
895 { | |
896 char *tempstr1, *tempstr2, *ret, tstr[50]; | |
897 struct tm *lt; | |
898 time_t t; | |
899 | |
900 lt = localtime (&fle->datetime); | |
901 | |
902 tempstr1 = g_strdup_printf ("%10s %8s %8s", fle->attribs, fle->user, | |
903 fle->group); | |
904 | |
905 if (fle->attribs && (*fle->attribs == 'b' || *fle->attribs == 'c')) | |
906 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); | |
907 else | |
908 { | |
909 #if defined (_LARGEFILE_SOURCE) | |
910 tempstr2 = g_strdup_printf ("%11lld", fle->size); | |
911 #else | |
912 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
913 #endif | |
914 } | |
915 | |
916 time (&t); | |
917 | |
918 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
919 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
920 else | |
921 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
922 | |
923 if (file_prefixstr == NULL) | |
924 file_prefixstr = ""; | |
925 if (file_suffixstr == NULL) | |
926 file_suffixstr = ""; | |
927 | |
928 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
929 file_prefixstr, fle->file, file_suffixstr); | |
930 | |
931 g_free (tempstr1); | |
932 g_free (tempstr2); | |
933 | |
934 return (ret); | |
935 } | |
936 | |
937 | |
125 | 938 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
939 | |
940 struct hostent * | |
941 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
942 { | |
943 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
944 struct hostent *hent; | |
945 | |
946 if (g_thread_supported ()) | |
947 g_static_mutex_lock (&hostfunclock); | |
948 | |
949 if ((hent = gethostbyname (name)) == NULL) | |
950 { | |
951 if (h_errnop) | |
952 *h_errnop = h_errno; | |
953 } | |
954 else | |
955 { | |
956 *result_buf = *hent; | |
957 hent = result_buf; | |
958 } | |
959 | |
960 if (g_thread_supported ()) | |
961 g_static_mutex_unlock (&hostfunclock); | |
962 | |
963 return (hent); | |
964 } | |
965 | |
966 #endif /* !HAVE_GETADDRINFO */ | |
967 | |
968 struct servent * | |
969 r_getservbyname (const char *name, const char *proto, | |
970 struct servent *result_buf, int *h_errnop) | |
971 { | |
972 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
973 struct servent *sent; | |
974 | |
975 if (g_thread_supported ()) | |
976 g_static_mutex_lock (&servfunclock); | |
977 | |
978 if ((sent = getservbyname (name, proto)) == NULL) | |
979 { | |
980 if (h_errnop) | |
981 *h_errnop = h_errno; | |
982 } | |
983 else | |
984 { | |
985 *result_buf = *sent; | |
986 sent = result_buf; | |
987 } | |
988 | |
989 if (g_thread_supported ()) | |
990 g_static_mutex_unlock (&servfunclock); | |
991 return (sent); | |
992 } | |
993 | |
168 | 994 |
995 char * | |
996 base64_encode (char *str) | |
997 { | |
998 | |
999 /* The standard to Base64 encoding can be found in RFC2045 */ | |
1000 | |
1001 char *newstr, *newpos, *fillpos, *pos; | |
1002 unsigned char table[64], encode[3]; | |
1003 int i, num; | |
1004 | |
1005 for (i = 0; i < 26; i++) | |
1006 { | |
1007 table[i] = 'A' + i; | |
1008 table[i + 26] = 'a' + i; | |
1009 } | |
1010 | |
1011 for (i = 0; i < 10; i++) | |
1012 table[i + 52] = '0' + i; | |
1013 | |
1014 table[62] = '+'; | |
1015 table[63] = '-'; | |
1016 | |
1017 num = strlen (str) / 3; | |
1018 if (strlen (str) % 3 > 0) | |
1019 num++; | |
1020 newstr = g_malloc (num * 4 + 1); | |
1021 newstr[num * 4] = '\0'; | |
1022 newpos = newstr; | |
1023 | |
1024 pos = str; | |
1025 while (*pos != '\0') | |
1026 { | |
1027 memset (encode, 0, sizeof (encode)); | |
1028 for (i = 0; i < 3 && *pos != '\0'; i++) | |
1029 encode[i] = *pos++; | |
1030 | |
1031 fillpos = newpos; | |
1032 *newpos++ = table[encode[0] >> 2]; | |
1033 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
1034 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
1035 *newpos++ = table[encode[2] & 0x3F]; | |
1036 while (i < 3) | |
1037 fillpos[++i] = '='; | |
1038 } | |
1039 return (newstr); | |
1040 } | |
1041 |