Mercurial > gftp.yaz
annotate lib/misc.c @ 39:474d562c7268
2002-10-15 Brian Masney <masneyb@gftp.org>
* lib/config_file.c - enable combo in GTK port for Proxy server type
* lib/misc.c (gftp_sort_filelist) - make sure prev pointer to first
entry is NULL
* lib/protocols.c (copy_token) - when setting the end position of
the token to \0, after we're done set it back to the origional position
* src/gtk/gftp-gtk.c, src/gtk/menu-items.c - changed g_list_first (list) to just list
author | masneyb |
---|---|
date | Wed, 16 Oct 2002 02:11:09 +0000 |
parents | c8ec7877432e |
children | 4bcfaf6307b5 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* misc.c - general purpose routines */ | |
3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */ | |
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 | |
20 #include "gftp.h" | |
21 #include "options.h" | |
33 | 22 static const char cvsid[] = "$Id$"; |
1 | 23 |
24 char * | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
25 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 26 { |
27 char *frompos, *topos, src[50], *dest; | |
28 int len, num, rem, i; | |
29 | |
30 if (dest_str != NULL) | |
31 *dest_str = '\0'; | |
13 | 32 len = (number > 0 ? log10 (number) : 0) + 1; |
1 | 33 |
34 if (len <= 0) | |
35 { | |
36 if (dest_str != NULL) | |
37 strncpy (dest_str, "0", dest_len); | |
38 else | |
39 dest_str = g_strconcat ("0", NULL); | |
40 return (dest_str); | |
41 } | |
42 | |
43 len += len / 3; | |
44 if (dest_str != NULL && len > dest_len) | |
45 { | |
46 | |
47 for (i=0; i<dest_len - 1; i++) | |
48 dest_str[i] = 'X'; | |
49 dest_str[dest_len - 1] = '\0'; | |
50 return (dest_str); | |
51 } | |
52 | |
53 if (dest_str == NULL) | |
54 dest = g_malloc0 (len); | |
55 else | |
56 dest = dest_str; | |
57 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
58 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
59 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
|
60 #else |
1 | 61 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
|
62 #endif |
1 | 63 |
64 num = strlen (src) / 3 - 1; | |
65 rem = strlen (src) % 3; | |
66 frompos = src; | |
67 topos = dest; | |
68 for (i = 0; i < rem; i++) | |
69 *topos++ = *frompos++; | |
70 | |
71 if (*frompos != '\0') | |
72 { | |
73 if (rem != 0) | |
74 *topos++ = ','; | |
75 while (num > 0) | |
76 { | |
77 for (i = 0; i < 3; i++) | |
78 *topos++ = *frompos++; | |
79 *topos++ = ','; | |
80 num--; | |
81 } | |
82 for (i = 0; i < 3; i++) | |
83 *topos++ = *frompos++; | |
84 } | |
85 *topos = '\0'; | |
86 return (dest); | |
87 } | |
88 | |
89 | |
90 long | |
91 file_countlf (int filefd, long endpos) | |
92 { | |
93 char tempstr[255]; | |
94 long num, mypos; | |
95 ssize_t n; | |
96 int i; | |
97 | |
98 mypos = num = 0; | |
99 lseek (filefd, 0, SEEK_SET); | |
100 while ((n = read (filefd, tempstr, sizeof (tempstr))) > 0) | |
101 { | |
102 for (i = 0; i < n; i++) | |
103 { | |
104 if ((tempstr[i] == '\n') && (i > 0) && (tempstr[i - 1] != '\r') | |
105 && (endpos == 0 || mypos + i <= endpos)) | |
106 ++num; | |
107 } | |
108 mypos += n; | |
109 } | |
110 lseek (filefd, 0, SEEK_SET); | |
111 return (num); | |
112 } | |
113 | |
114 | |
115 char * | |
116 alltrim (char *str) | |
117 { | |
118 char *pos, *newpos; | |
119 int diff; | |
120 | |
121 pos = str + strlen (str) - 1; | |
122 while (pos >= str && *pos == ' ') | |
123 *pos-- = '\0'; | |
124 | |
125 pos = str; | |
126 diff = 0; | |
127 while (*pos++ == ' ') | |
128 diff++; | |
129 | |
130 if (diff == 0) | |
131 return (str); | |
132 | |
133 pos = str + diff; | |
134 newpos = str; | |
135 while (*pos != '\0') | |
136 *newpos++ = *pos++; | |
137 *newpos = '\0'; | |
138 | |
139 return (str); | |
140 } | |
141 | |
142 | |
143 char * | |
144 expand_path (const char *src) | |
145 { | |
146 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
147 struct passwd *pw; | |
148 | |
149 pw = NULL; | |
150 str = g_malloc (strlen (src) + 1); | |
151 strcpy (str, src); | |
152 | |
153 if (*str == '~') | |
154 { | |
155 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
156 pw = getpwuid (geteuid ()); | |
157 else | |
158 { | |
159 if ((pos = strchr (str, '/')) != NULL) | |
160 *pos = '\0'; | |
161 | |
162 pw = getpwnam (str + 1); | |
163 | |
164 if (pos != NULL) | |
165 *pos = '/'; | |
166 } | |
167 } | |
168 | |
169 endpos = str; | |
170 newstr = NULL; | |
171 while ((pos = strchr (endpos, '/')) != NULL) | |
172 { | |
173 pos++; | |
174 while (*pos == '/') | |
175 pos++; | |
176 if ((endpos = strchr (pos, '/')) == NULL) | |
177 endpos = pos + strlen (pos); | |
178 tempchar = *endpos; | |
179 *endpos = '\0'; | |
180 if (strcmp (pos, "..") == 0) | |
181 { | |
182 *(pos - 1) = '\0'; | |
183 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
184 *prevpos = '\0'; | |
185 } | |
186 else if (strcmp (pos, ".") != 0) | |
187 { | |
188 if (newstr == NULL) | |
189 newstr = g_strconcat (pos - 1, NULL); | |
190 else | |
191 { | |
192 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
193 g_free (newstr); | |
194 newstr = tempstr; | |
195 } | |
196 } | |
197 *endpos = tempchar; | |
198 if (*endpos == '\0') | |
199 break; | |
200 endpos = pos + 1; | |
201 } | |
202 | |
203 if (newstr == NULL || *newstr == '\0') | |
204 { | |
205 if (newstr != NULL) | |
206 g_free (newstr); | |
207 newstr = g_malloc0 (2); | |
208 *newstr = '/'; | |
209 } | |
210 | |
211 g_free (str); | |
212 | |
213 if (pw != NULL) | |
214 { | |
215 if ((pos = strchr (newstr, '/')) == NULL) | |
216 { | |
217 str = g_malloc (strlen (pw->pw_dir) + 1); | |
218 strcpy (str, pw->pw_dir); | |
219 } | |
220 else | |
221 str = g_strconcat (pw->pw_dir, pos, NULL); | |
222 | |
223 g_free (newstr); | |
224 newstr = str; | |
225 } | |
226 | |
227 return (newstr); | |
228 } | |
229 | |
230 | |
231 void | |
232 remove_double_slashes (char *string) | |
233 { | |
234 char *newpos, *oldpos; | |
235 | |
236 oldpos = newpos = string; | |
237 while (*oldpos != '\0') | |
238 { | |
239 *newpos++ = *oldpos++; | |
240 if (*oldpos == '\0') | |
241 break; | |
242 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
243 oldpos++; | |
244 } | |
245 *newpos = '\0'; | |
246 if (string[strlen (string) - 1] == '/') | |
247 string[strlen (string) - 1] = '\0'; | |
248 } | |
249 | |
250 | |
251 void | |
252 make_nonnull (char **str) | |
253 { | |
254 if (*str == NULL) | |
255 *str = g_malloc0 (1); | |
256 } | |
257 | |
258 | |
259 int | |
260 copyfile (char *source, char *dest) | |
261 { | |
262 FILE *srcfd, *destfd; | |
263 char buf[8192]; | |
264 size_t n; | |
265 | |
266 if ((srcfd = fopen (source, "rb")) == NULL) | |
267 return (0); | |
268 | |
269 if ((destfd = fopen (dest, "wb")) == NULL) | |
270 { | |
271 fclose (srcfd); | |
272 return (0); | |
273 } | |
274 | |
275 while ((n = fread (buf, 1, sizeof (buf), srcfd)) > 0) | |
276 fwrite (buf, 1, n, destfd); | |
277 | |
278 fclose (srcfd); | |
279 fclose (destfd); | |
280 | |
281 return (1); | |
282 } | |
283 | |
284 | |
285 int | |
286 gftp_match_filespec (char *filename, char *filespec) | |
287 { | |
288 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
289 size_t len, curlen; | |
290 | |
291 if (filename == NULL || *filename == '\0' || | |
292 filespec == NULL || *filespec == '\0') | |
293 return(1); | |
294 | |
295 filepos = filename; | |
296 wcpos = filespec; | |
297 while(1) | |
298 { | |
299 if (*wcpos == '\0') | |
300 return (1); | |
301 else if (*filepos == '\0') | |
302 return(0); | |
303 else if(*wcpos == '?') | |
304 { | |
305 wcpos++; | |
306 filepos++; | |
307 } | |
308 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
309 return(1); | |
310 else if(*wcpos == '*') | |
311 { | |
312 len = sizeof (search_str); | |
313 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
314 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
315 curlen++, *newpos++ = *pos++); | |
316 *newpos = '\0'; | |
317 | |
318 if ((filepos = strstr (filepos, search_str)) == NULL) | |
319 return(0); | |
320 wcpos += curlen + 1; | |
321 filepos += curlen; | |
322 } | |
323 else if(*wcpos++ != *filepos++) | |
324 return(0); | |
325 } | |
326 return (1); | |
327 } | |
328 | |
329 | |
330 int | |
331 gftp_parse_command_line (int *argc, char ***argv) | |
332 { | |
333 if (*argc > 1) | |
334 { | |
335 if (strcmp (argv[0][1], "--help") == 0 || strcmp (argv[0][1], "-h") == 0) | |
336 gftp_usage (); | |
337 else if (strcmp (argv[0][1], "--version") == 0 || strcmp (argv[0][1], "-v") == 0) | |
338 { | |
339 printf ("%s\n", version); | |
340 exit (0); | |
341 } | |
342 } | |
343 return (0); | |
344 } | |
345 | |
346 | |
347 void | |
348 gftp_usage (void) | |
349 { | |
350 printf (_("usage: gftp [[ftp://][user:[pass]@]ftp-site[:port][/directory]]\n")); | |
351 exit (0); | |
352 } | |
353 | |
354 | |
355 char * | |
356 get_xpm_path (char *filename, int quit_on_err) | |
357 { | |
358 char *tempstr, *exfile; | |
359 | |
360 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
361 exfile = expand_path (tempstr); | |
362 g_free (tempstr); | |
363 if (access (exfile, F_OK) != 0) | |
364 { | |
365 g_free (exfile); | |
366 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
367 exfile = expand_path (tempstr); | |
368 g_free (tempstr); | |
369 if (access (exfile, F_OK) != 0) | |
370 { | |
371 g_free (exfile); | |
372 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
373 if (access (exfile, F_OK) != 0) | |
374 { | |
375 g_free (exfile); | |
376 if (!quit_on_err) | |
377 return (NULL); | |
378 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), | |
379 filename, SHARE_DIR, BASE_CONF_DIR); | |
380 exit (-1); | |
381 } | |
382 } | |
383 } | |
384 return (exfile); | |
385 } | |
386 | |
387 | |
388 gint | |
389 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
390 { | |
391 return (strcmp ((char *) path1, (char *) path2) == 0); | |
392 } | |
393 | |
394 | |
395 guint | |
396 string_hash_function (gconstpointer key) | |
397 { | |
398 return (((char *) key)[0] + ((char *) key)[1] + ((char *) key)[2]); | |
399 } | |
400 | |
401 | |
402 void | |
403 free_file_list (GList * filelist) | |
404 { | |
405 gftp_file * tempfle; | |
406 GList * templist; | |
407 | |
408 templist = filelist; | |
409 while (templist != NULL) | |
410 { | |
411 tempfle = templist->data; | |
412 free_fdata (tempfle); | |
413 templist = templist->next; | |
414 } | |
415 g_list_free (filelist); | |
416 } | |
417 | |
418 | |
419 void | |
420 free_fdata (gftp_file * fle) | |
421 { | |
422 if (fle->file) | |
423 g_free (fle->file); | |
424 if (fle->user) | |
425 g_free (fle->user); | |
426 if (fle->group) | |
427 g_free (fle->group); | |
428 if (fle->attribs) | |
429 g_free (fle->attribs); | |
430 if (fle->destfile) | |
431 g_free (fle->destfile); | |
432 if (fle->fd) | |
433 fclose (fle->fd); | |
434 g_free (fle); | |
435 } | |
436 | |
437 | |
438 gftp_file * | |
439 copy_fdata (gftp_file * fle) | |
440 { | |
441 gftp_file * newfle; | |
442 | |
443 newfle = g_malloc0 (sizeof (*newfle)); | |
444 memcpy (newfle, fle, sizeof (*newfle)); | |
445 | |
446 if (fle->file) | |
447 { | |
448 newfle->file = g_malloc (strlen (fle->file) + 1); | |
449 strcpy (newfle->file, fle->file); | |
450 } | |
451 | |
452 if (fle->user) | |
453 { | |
454 newfle->user = g_malloc (strlen (fle->user) + 1); | |
455 strcpy (newfle->user, fle->user); | |
456 } | |
457 | |
458 if (fle->group) | |
459 { | |
460 newfle->group = g_malloc (strlen (fle->group) + 1); | |
461 strcpy (newfle->group, fle->group); | |
462 } | |
463 | |
464 if (fle->attribs) | |
465 { | |
466 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
467 strcpy (newfle->attribs, fle->attribs); | |
468 } | |
469 | |
470 if (fle->destfile) | |
471 { | |
472 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
473 strcpy (newfle->destfile, fle->destfile); | |
474 } | |
475 return (newfle); | |
476 } | |
477 | |
478 | |
479 void | |
480 swap_socks (gftp_request * dest, gftp_request * source) | |
481 { | |
482 dest->sockfd = source->sockfd; | |
483 dest->datafd = source->datafd; | |
484 dest->sockfd_write = source->sockfd_write; | |
485 dest->cached = 0; | |
486 if (!source->always_connected) | |
487 { | |
488 source->sockfd = NULL; | |
489 source->datafd = NULL; | |
490 source->sockfd_write = NULL; | |
491 source->cached = 1; | |
492 } | |
493 } | |
494 | |
495 | |
496 int | |
497 compare_request (gftp_request * request1, gftp_request * request2, | |
498 int compare_dirs) | |
499 { | |
500 char *strarr[3][2]; | |
501 int i, ret; | |
502 | |
503 ret = 1; | |
504 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
505 request1->port == request2->port) | |
506 { | |
507 strarr[0][0] = request1->hostname; | |
508 strarr[0][1] = request2->hostname; | |
509 strarr[1][0] = request1->username; | |
510 strarr[1][1] = request2->username; | |
511 if (compare_dirs) | |
512 { | |
513 strarr[2][0] = request1->directory; | |
514 strarr[2][1] = request2->directory; | |
515 } | |
516 else | |
517 strarr[2][0] = strarr[2][1] = ""; | |
518 | |
519 for (i = 0; i < 3; i++) | |
520 { | |
521 if ((strarr[i][0] && !strarr[i][1]) || | |
522 (!strarr[i][0] && strarr[i][1])) | |
523 { | |
524 ret = 0; | |
525 break; | |
526 } | |
527 | |
528 if (strarr[i][0] && strarr[i][1] && | |
529 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
530 { | |
531 ret = 0; | |
532 break; | |
533 } | |
534 } | |
535 } | |
536 else | |
537 ret = 0; | |
538 return (ret); | |
539 } | |
540 | |
541 | |
542 void | |
543 free_tdata (gftp_transfer * tdata) | |
544 { | |
545 if (tdata->statmutex) | |
546 g_free (tdata->statmutex); | |
547 if (tdata->structmutex) | |
548 g_free (tdata->structmutex); | |
549 if (tdata->fromreq != NULL) | |
550 gftp_request_destroy (tdata->fromreq); | |
551 if (tdata->toreq != NULL) | |
552 gftp_request_destroy (tdata->toreq); | |
553 free_file_list (tdata->files); | |
554 g_free (tdata); | |
555 } | |
556 | |
557 | |
558 gftp_request * | |
559 copy_request (gftp_request * req) | |
560 { | |
561 gftp_request * newreq; | |
562 | |
563 newreq = g_malloc0 (sizeof (*newreq)); | |
564 memcpy (newreq, req, sizeof (*newreq)); | |
565 | |
566 if (req->hostname) | |
567 newreq->hostname = g_strconcat (req->hostname, NULL); | |
568 if (req->username) | |
569 newreq->username = g_strconcat (req->username, NULL); | |
570 if (req->password) | |
571 newreq->password = g_strconcat (req->password, NULL); | |
572 if (req->account) | |
573 newreq->account = g_strconcat (req->account, NULL); | |
574 if (req->directory) | |
575 newreq->directory = g_strconcat (req->directory, NULL); | |
576 | |
577 newreq->url_prefix = NULL; | |
578 newreq->protocol_name = NULL; | |
579 newreq->sftpserv_path = NULL; | |
580 newreq->proxy_config = NULL; | |
581 newreq->proxy_hostname = NULL; | |
582 newreq->proxy_username = NULL; | |
583 newreq->proxy_password = NULL; | |
584 newreq->proxy_account = NULL; | |
585 newreq->last_ftp_response = NULL; | |
586 newreq->last_dir_entry = NULL; | |
587 newreq->sockfd = NULL; | |
588 newreq->sockfd_write = NULL; | |
589 newreq->datafd = NULL; | |
590 newreq->cachefd = NULL; | |
591 newreq->hostp = NULL; | |
592 newreq->protocol_data = NULL; | |
593 | |
594 if (req->proxy_config != NULL) | |
595 newreq->proxy_config = g_strconcat (req->proxy_config, NULL); | |
596 | |
597 req->init (newreq); | |
598 | |
599 return (newreq); | |
600 } | |
601 | |
602 | |
603 int | |
604 ptym_open (char *pts_name) | |
605 { | |
606 int fd; | |
607 | |
608 #ifdef __sgi | |
609 char *tempstr; | |
610 | |
611 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
612 return (-1); | |
613 | |
614 strcpy (pts_name, tempstr); | |
615 return (fd); | |
616 | |
617 #else /* !__sgi */ | |
618 | |
619 #ifdef SYSV | |
620 | |
621 char *tempstr; | |
622 | |
623 strcpy (pts_name, "/dev/ptmx"); | |
624 if ((fd = open (pts_name, O_RDWR)) < 0) | |
625 return (-1); | |
626 | |
627 if (grantpt (fd) < 0) | |
628 { | |
629 close (fd); | |
630 return (-1); | |
631 } | |
632 | |
633 if (unlockpt (fd) < 0) | |
634 { | |
635 close (fd); | |
636 return (-1); | |
637 } | |
638 | |
639 if ((tempstr = ptsname (fd)) == NULL) | |
640 { | |
641 close (fd); | |
642 return (-1); | |
643 } | |
644 | |
645 strcpy (pts_name, tempstr); | |
646 return (fd); | |
647 | |
648 #else /* !SYSV */ | |
649 | |
650 char *pos1, *pos2; | |
651 | |
652 strcpy (pts_name, "/dev/ptyXY"); | |
653 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
654 { | |
655 pts_name[8] = *pos1; | |
656 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
657 { | |
658 pts_name[9] = *pos2; | |
659 if ((fd = open (pts_name, O_RDWR)) < 0) | |
660 continue; | |
661 pts_name[5] = 't'; | |
662 return (fd); | |
663 } | |
664 } | |
665 return (-1); | |
666 | |
667 #endif | |
668 | |
669 #endif | |
670 | |
671 } | |
672 | |
673 | |
674 int | |
675 ptys_open (int fdm, char *pts_name) | |
676 { | |
677 int fds; | |
678 | |
679 #if !defined (SYSV) && !defined (__sgi) | |
680 | |
681 chmod (pts_name, S_IRUSR | S_IWUSR); | |
682 chown (pts_name, getuid (), -1); | |
683 | |
684 #endif | |
685 | |
686 if ((fds = open (pts_name, O_RDWR)) < 0) | |
687 { | |
688 close (fdm); | |
689 return (-1); | |
690 } | |
691 | |
692 #ifdef SYSV | |
693 | |
694 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
695 { | |
696 close (fdm); | |
697 close (fds); | |
698 return (-1); | |
699 } | |
700 | |
701 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
702 { | |
703 close (fdm); | |
704 close (fds); | |
705 return (-1); | |
706 } | |
707 | |
708 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
709 { | |
710 close (fdm); | |
711 close (fds); | |
712 return (-1); | |
713 } | |
714 | |
715 #endif | |
716 | |
717 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
718 | |
719 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
720 { | |
721 close (fdm); | |
722 return (-1); | |
723 } | |
724 | |
725 #endif | |
726 | |
727 return (fds); | |
728 } | |
729 | |
730 | |
731 int | |
732 tty_raw (int fd) | |
733 { | |
734 struct termios buf; | |
735 | |
736 if (tcgetattr (fd, &buf) < 0) | |
737 return (-1); | |
738 | |
739 buf.c_iflag |= IGNPAR; | |
740 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
741 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
742 #ifdef IEXTEN | |
743 buf.c_lflag &= ~(IEXTEN); | |
744 #endif | |
745 | |
746 buf.c_oflag &= ~(OPOST); | |
747 buf.c_cc[VMIN] = 1; | |
748 buf.c_cc[VTIME] = 0; | |
749 | |
750 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
751 return (-1); | |
752 return (0); | |
753 } | |
754 | |
755 | |
756 /* We have the caller send us a pointer to a string so we can write the port | |
757 into it. It makes it easier so we don't have to worry about freeing it | |
758 later on, the caller can just send us an auto variable, The string | |
759 should be at least 6 chars. I know this is messy... */ | |
760 | |
761 char ** | |
762 make_ssh_exec_args (gftp_request * request, char *execname, | |
763 int use_sftp_subsys, char *portstring) | |
764 { | |
765 char **args, *oldstr, *tempstr; | |
766 int i, j; | |
767 | |
768 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 10)); | |
769 | |
770 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
771 ssh_prog_name : "ssh"; | |
772 i = 1; | |
773 tempstr = g_strconcat (args[0], NULL); | |
774 | |
775 if (ssh_extra_params_list != NULL) | |
776 { | |
777 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
778 { | |
779 oldstr = tempstr; | |
780 args[i++] = ssh_extra_params_list[j]; | |
781 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
782 g_free (oldstr); | |
783 } | |
784 } | |
785 | |
786 oldstr = tempstr; | |
787 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
788 g_free (oldstr); | |
789 args[i++] = "-e"; | |
790 args[i++] = "none"; | |
791 | |
792 if (request->username && *request->username != '\0') | |
793 { | |
794 oldstr = tempstr; | |
795 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
796 g_free (oldstr); | |
797 args[i++] = "-l"; | |
798 args[i++] = request->username; | |
799 } | |
800 | |
801 if (request->port != 0) | |
802 { | |
803 g_snprintf (portstring, 6, "%d", request->port); | |
804 oldstr = tempstr; | |
805 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
806 g_free (oldstr); | |
807 args[i++] = "-p"; | |
808 args[i++] = portstring; | |
809 } | |
810 | |
811 if (use_sftp_subsys) | |
812 { | |
813 oldstr = tempstr; | |
814 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
815 g_free (oldstr); | |
816 args[i++] = request->hostname; | |
817 args[i++] = "-s"; | |
818 args[i++] = "sftp"; | |
819 args[i] = NULL; | |
820 } | |
821 else | |
822 { | |
823 oldstr = tempstr; | |
824 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
825 "\"", NULL); | |
826 g_free (oldstr); | |
827 args[i++] = request->hostname; | |
828 args[i++] = execname; | |
829 args[i] = NULL; | |
830 } | |
831 | |
832 request->logging_function (gftp_logging_misc, request->user_data, | |
833 _("Running program %s\n"), tempstr); | |
834 g_free (tempstr); | |
835 return (args); | |
836 } | |
837 | |
838 | |
839 char * | |
840 ssh_start_login_sequence (gftp_request * request, int fd) | |
841 { | |
842 size_t rem, len, diff, lastdiff; | |
843 int flags, wrotepw, ok; | |
844 struct timeval tv; | |
845 char *tempstr; | |
846 fd_set rdfds; | |
847 ssize_t rd; | |
848 | |
849 rem = len = 100; | |
850 tempstr = g_malloc0 (len); | |
851 diff = lastdiff = 0; | |
852 wrotepw = 0; | |
853 ok = 1; | |
854 | |
855 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
856 { | |
857 g_free (tempstr); | |
858 return (NULL); | |
859 } | |
860 | |
861 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0) | |
862 { | |
863 g_free (tempstr); | |
864 return (NULL); | |
865 } | |
866 | |
867 while (1) | |
868 { | |
869 FD_ZERO (&rdfds); | |
870 FD_SET (fd, &rdfds); | |
871 tv.tv_sec = 5; | |
872 tv.tv_usec = 0; | |
873 if (select (fd + 1, &rdfds, NULL, NULL, &tv) < 0) | |
874 { | |
875 if (errno == EINTR) | |
876 continue; | |
877 ok = 0; | |
878 break; | |
879 } | |
880 | |
881 if ((rd = read (fd, tempstr + diff, rem - 1)) < 0) | |
882 { | |
883 if (errno == EINTR) | |
884 continue; | |
885 ok = 0; | |
886 break; | |
887 } | |
888 else if (rd == 0) | |
889 { | |
890 ok = 0; | |
891 break; | |
892 } | |
893 tempstr[diff + rd] = '\0'; | |
894 rem -= rd; | |
895 diff += rd; | |
896 if (rem <= 1) | |
897 { | |
898 tempstr = g_realloc (tempstr, len + 100); | |
899 tempstr[diff] = '\0'; | |
900 request->logging_function (gftp_logging_recv, request->user_data, | |
901 "%s", tempstr + lastdiff); | |
902 lastdiff = diff; | |
903 len += 100; | |
904 rem = 100; | |
905 } | |
906 | |
907 if (!wrotepw && | |
908 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, | |
909 "password: ") == 0) | |
910 { | |
911 wrotepw = 1; | |
912 write (fd, request->password, strlen (request->password)); | |
913 write (fd, "\n", 1); | |
914 } | |
915 | |
916 else if (!wrotepw && | |
917 (strstr (tempstr, "Enter passphrase for RSA key") != NULL || | |
918 strstr (tempstr, "Enter passphrase for key '") != NULL)) | |
919 { | |
920 wrotepw = 1; | |
921 write (fd, request->password, strlen (request->password)); | |
922 write (fd, "\n", 1); | |
923 } | |
924 else if (strlen (tempstr) >= 5 && | |
925 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) | |
926 break; | |
927 } | |
928 | |
929 tempstr[diff] = '\0'; | |
930 request->logging_function (gftp_logging_recv, request->user_data, | |
931 "%s\n", tempstr + lastdiff); | |
932 | |
933 if (ok && fcntl (fd, F_SETFL, flags) < 0) | |
934 ok = 0; | |
935 | |
936 if (!ok) | |
937 { | |
938 g_free (tempstr); | |
939 return (NULL); | |
940 } | |
941 | |
942 return (tempstr); | |
943 } | |
944 | |
945 | |
946 #ifdef G_HAVE_GINT64 | |
947 | |
948 gint64 | |
949 hton64 (gint64 val) | |
950 { | |
951 gint64 num; | |
952 char *pos; | |
953 | |
954 num = 0; | |
955 pos = (char *) # | |
956 pos[0] = (val >> 56) & 0xff; | |
957 pos[1] = (val >> 48) & 0xff; | |
958 pos[2] = (val >> 40) & 0xff; | |
959 pos[3] = (val >> 32) & 0xff; | |
960 pos[4] = (val >> 24) & 0xff; | |
961 pos[5] = (val >> 16) & 0xff; | |
962 pos[6] = (val >> 8) & 0xff; | |
963 pos[7] = val & 0xff; | |
964 return (num); | |
965 } | |
966 | |
967 #endif | |
968 | |
16 | 969 |
970 static gint | |
971 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
972 { | |
973 const gftp_file * f1, * f2; | |
974 | |
975 f1 = a; | |
976 f2 = b; | |
977 return (strcmp (f1->file, f2->file)); | |
978 } | |
979 | |
980 | |
981 static gint | |
982 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
983 { | |
984 const gftp_file * f1, * f2; | |
985 gint ret; | |
986 | |
987 f1 = a; | |
988 f2 = b; | |
989 ret = strcmp (f1->file, f2->file); | |
990 if (ret < 0) | |
991 ret = 1; | |
992 else if (ret > 0) | |
993 ret = -1; | |
994 return (ret); | |
995 } | |
996 | |
997 | |
998 static gint | |
999 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
1000 { | |
1001 const gftp_file * f1, * f2; | |
1002 | |
1003 f1 = a; | |
1004 f2 = b; | |
1005 return (strcmp (f1->user, f2->user)); | |
1006 } | |
1007 | |
1008 | |
1009 static gint | |
1010 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
1011 { | |
1012 const gftp_file * f1, * f2; | |
1013 gint ret; | |
1014 | |
1015 f1 = a; | |
1016 f2 = b; | |
1017 ret = strcmp (f1->user, f2->user); | |
1018 if (ret < 0) | |
1019 ret = 1; | |
1020 else if (ret > 0) | |
1021 ret = -1; | |
1022 return (ret); | |
1023 } | |
1024 | |
1025 | |
1026 static gint | |
1027 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
1028 { | |
1029 const gftp_file * f1, * f2; | |
1030 | |
1031 f1 = a; | |
1032 f2 = b; | |
1033 return (strcmp (f1->group, f2->group)); | |
1034 } | |
1035 | |
1036 | |
1037 static gint | |
1038 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
1039 { | |
1040 const gftp_file * f1, * f2; | |
1041 gint ret; | |
1042 | |
1043 f1 = a; | |
1044 f2 = b; | |
1045 ret = strcmp (f1->group, f2->group); | |
1046 if (ret < 0) | |
1047 ret = 1; | |
1048 else if (ret > 0) | |
1049 ret = -1; | |
1050 return (ret); | |
1051 } | |
1052 | |
1053 | |
1054 static gint | |
1055 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
1056 { | |
1057 const gftp_file * f1, * f2; | |
1058 | |
1059 f1 = a; | |
1060 f2 = b; | |
1061 return (strcmp (f1->attribs, f2->attribs)); | |
1062 } | |
1063 | |
1064 | |
1065 static gint | |
1066 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
1067 { | |
1068 const gftp_file * f1, * f2; | |
1069 gint ret; | |
1070 | |
1071 f1 = a; | |
1072 f2 = b; | |
1073 ret = strcmp (f1->attribs, f2->attribs); | |
1074 if (ret < 0) | |
1075 ret = 1; | |
1076 else if (ret > 0) | |
1077 ret = -1; | |
1078 return (ret); | |
1079 } | |
1080 | |
1081 | |
1082 static gint | |
1083 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
1084 { | |
1085 const gftp_file * f1, * f2; | |
1086 | |
1087 f1 = a; | |
1088 f2 = b; | |
1089 if (f1->size < f2->size) | |
1090 return (-1); | |
1091 else if (f1->size == f2->size) | |
1092 return (0); | |
1093 else | |
1094 return (1); | |
1095 } | |
1096 | |
1097 | |
1098 static gint | |
1099 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
1100 { | |
1101 const gftp_file * f1, * f2; | |
1102 | |
1103 f1 = a; | |
1104 f2 = b; | |
1105 if (f1->size < f2->size) | |
1106 return (1); | |
1107 else if (f1->size == f2->size) | |
1108 return (0); | |
1109 else | |
1110 return (-1); | |
1111 } | |
1112 | |
1113 | |
1114 static gint | |
1115 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
1116 { | |
1117 const gftp_file * f1, * f2; | |
1118 | |
1119 f1 = a; | |
1120 f2 = b; | |
1121 if (f1->datetime < f2->datetime) | |
1122 return (-1); | |
1123 else if (f1->datetime == f2->datetime) | |
1124 return (0); | |
1125 else | |
1126 return (1); | |
1127 } | |
1128 | |
1129 | |
1130 static gint | |
1131 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
1132 { | |
1133 const gftp_file * f1, * f2; | |
1134 | |
1135 f1 = a; | |
1136 f2 = b; | |
1137 if (f1->datetime < f2->datetime) | |
1138 return (1); | |
1139 else if (f1->datetime == f2->datetime) | |
1140 return (0); | |
1141 else | |
1142 return (-1); | |
1143 } | |
1144 | |
1145 | |
1146 GList * | |
1147 gftp_sort_filelist (GList * filelist, int column, int asds) | |
1148 { | |
1149 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
1150 GCompareFunc sortfunc; | |
1151 gftp_file * tempfle; | |
1152 | |
1153 files = dirs = dotdot = NULL; | |
1154 | |
1155 if (column == GFTP_SORT_COL_FILE) | |
1156 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
1157 else if (column == GFTP_SORT_COL_SIZE) | |
1158 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
1159 else if (column == GFTP_SORT_COL_USER) | |
1160 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
1161 else if (column == GFTP_SORT_COL_GROUP) | |
1162 sortfunc = asds ? | |
1163 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
1164 else if (column == GFTP_SORT_COL_DATETIME) | |
1165 sortfunc = asds ? | |
1166 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
1167 sortfunc = asds ? | |
1168 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
1169 | |
1170 for (tempitem = filelist; tempitem != NULL; ) | |
1171 { | |
1172 tempfle = tempitem->data; | |
1173 insitem = tempitem; | |
1174 tempitem = tempitem->next; | |
1175 insitem->next = NULL; | |
1176 | |
1177 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
1178 dotdot = insitem; | |
1179 else if (sort_dirs_first && tempfle->isdir) | |
1180 { | |
1181 insitem->next = dirs; | |
1182 dirs = insitem; | |
1183 } | |
1184 else | |
1185 { | |
1186 insitem->next = files; | |
1187 files = insitem; | |
1188 } | |
1189 } | |
1190 | |
1191 if (dirs != NULL) | |
1192 dirs = g_list_sort (dirs, sortfunc); | |
1193 if (files != NULL) | |
1194 files = g_list_sort (files, sortfunc); | |
1195 | |
1196 filelist = dotdot; | |
1197 | |
1198 if (filelist == NULL) | |
1199 filelist = dirs; | |
1200 else | |
1201 filelist = g_list_concat (filelist, dirs); | |
1202 | |
1203 if (filelist == NULL) | |
1204 filelist = files; | |
1205 else | |
1206 filelist = g_list_concat (filelist, files); | |
1207 | |
39 | 1208 /* I haven't check this, but I'm pretty sure some older versions of glib |
1209 had a bug that the prev pointer wasn't being sent to NULL */ | |
1210 filelist->prev = NULL; | |
16 | 1211 return (filelist); |
1212 } | |
1213 |