Mercurial > gftp.yaz
annotate lib/misc.c @ 59:618423504fe0
2002-11-20 Brian Masney <masneyb@gftp.org>
* lib/config_file.c - don't check buf[-1] for blank newlines in
gftp_read_config_file() and gftp_read_bookmarks_file()
* lib/misc.c (string_hash_function) - Fixes if the string was less
than 2 characters
* src/gtk/transfer.c - unlock mutex before we destroy (for POSIX
compliance)
All 3 of these fixes are from Peter Osterlund <petero2@telia.com>
author | masneyb |
---|---|
date | Thu, 21 Nov 2002 02:49:06 +0000 |
parents | c01d91c10f6c |
children | 42df9e4be8e0 |
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'; | |
56 | 32 len = (number > 0 ? log10 (number) : 0) + 2; |
1 | 33 |
34 if (len <= 0) | |
35 { | |
36 if (dest_str != NULL) | |
37 strncpy (dest_str, "0", dest_len); | |
38 else | |
56 | 39 dest_str = g_strdup ("0"); |
1 | 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) | |
56 | 189 newstr = g_strdup (pos - 1); |
1 | 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 { | |
58 | 262 int srcfd, destfd; |
1 | 263 char buf[8192]; |
58 | 264 ssize_t n; |
1 | 265 |
58 | 266 if ((srcfd = open (source, O_RDONLY)) == -1) |
267 { | |
268 printf (_("Error: Cannot open local file %s: %s\n"), | |
269 source, g_strerror (errno)); | |
270 exit (1); | |
271 } | |
1 | 272 |
58 | 273 if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
1 | 274 { |
58 | 275 printf (_("Error: Cannot open local file %s: %s\n"), |
276 dest, g_strerror (errno)); | |
277 close (srcfd); | |
278 exit (1); | |
1 | 279 } |
280 | |
58 | 281 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
282 { | |
283 if (write (destfd, buf, n) == -1) | |
284 { | |
285 printf (_("Error: Could not write to socket: %s\n"), | |
286 g_strerror (errno)); | |
287 exit (1); | |
288 } | |
289 } | |
1 | 290 |
58 | 291 if (n == -1) |
292 { | |
293 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
294 exit (1); | |
295 } | |
296 | |
297 close (srcfd); | |
298 close (destfd); | |
1 | 299 |
300 return (1); | |
301 } | |
302 | |
303 | |
304 int | |
305 gftp_match_filespec (char *filename, char *filespec) | |
306 { | |
307 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
308 size_t len, curlen; | |
309 | |
310 if (filename == NULL || *filename == '\0' || | |
311 filespec == NULL || *filespec == '\0') | |
312 return(1); | |
313 | |
314 filepos = filename; | |
315 wcpos = filespec; | |
316 while(1) | |
317 { | |
318 if (*wcpos == '\0') | |
319 return (1); | |
320 else if (*filepos == '\0') | |
321 return(0); | |
322 else if(*wcpos == '?') | |
323 { | |
324 wcpos++; | |
325 filepos++; | |
326 } | |
327 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
328 return(1); | |
329 else if(*wcpos == '*') | |
330 { | |
331 len = sizeof (search_str); | |
332 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
333 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
334 curlen++, *newpos++ = *pos++); | |
335 *newpos = '\0'; | |
336 | |
337 if ((filepos = strstr (filepos, search_str)) == NULL) | |
338 return(0); | |
339 wcpos += curlen + 1; | |
340 filepos += curlen; | |
341 } | |
342 else if(*wcpos++ != *filepos++) | |
343 return(0); | |
344 } | |
345 return (1); | |
346 } | |
347 | |
348 | |
349 int | |
350 gftp_parse_command_line (int *argc, char ***argv) | |
351 { | |
352 if (*argc > 1) | |
353 { | |
354 if (strcmp (argv[0][1], "--help") == 0 || strcmp (argv[0][1], "-h") == 0) | |
355 gftp_usage (); | |
356 else if (strcmp (argv[0][1], "--version") == 0 || strcmp (argv[0][1], "-v") == 0) | |
357 { | |
358 printf ("%s\n", version); | |
359 exit (0); | |
360 } | |
361 } | |
362 return (0); | |
363 } | |
364 | |
365 | |
366 void | |
367 gftp_usage (void) | |
368 { | |
369 printf (_("usage: gftp [[ftp://][user:[pass]@]ftp-site[:port][/directory]]\n")); | |
370 exit (0); | |
371 } | |
372 | |
373 | |
374 char * | |
375 get_xpm_path (char *filename, int quit_on_err) | |
376 { | |
377 char *tempstr, *exfile; | |
378 | |
379 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
380 exfile = expand_path (tempstr); | |
381 g_free (tempstr); | |
382 if (access (exfile, F_OK) != 0) | |
383 { | |
384 g_free (exfile); | |
385 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
386 exfile = expand_path (tempstr); | |
387 g_free (tempstr); | |
388 if (access (exfile, F_OK) != 0) | |
389 { | |
390 g_free (exfile); | |
391 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
392 if (access (exfile, F_OK) != 0) | |
393 { | |
394 g_free (exfile); | |
395 if (!quit_on_err) | |
396 return (NULL); | |
397 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), | |
398 filename, SHARE_DIR, BASE_CONF_DIR); | |
399 exit (-1); | |
400 } | |
401 } | |
402 } | |
403 return (exfile); | |
404 } | |
405 | |
406 | |
407 gint | |
408 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
409 { | |
410 return (strcmp ((char *) path1, (char *) path2) == 0); | |
411 } | |
412 | |
413 | |
414 guint | |
415 string_hash_function (gconstpointer key) | |
416 { | |
59 | 417 guint ret; |
418 int i; | |
419 | |
420 ret = 0; | |
421 for (i=0; ((char *) key)[0] != '\0' && i < 3; i++) | |
422 ret += ((char *) key)[i]; | |
423 | |
424 return (ret); | |
1 | 425 } |
426 | |
427 | |
428 void | |
429 free_file_list (GList * filelist) | |
430 { | |
431 gftp_file * tempfle; | |
432 GList * templist; | |
433 | |
434 templist = filelist; | |
435 while (templist != NULL) | |
436 { | |
437 tempfle = templist->data; | |
438 free_fdata (tempfle); | |
439 templist = templist->next; | |
440 } | |
441 g_list_free (filelist); | |
442 } | |
443 | |
444 | |
445 void | |
446 free_fdata (gftp_file * fle) | |
447 { | |
448 if (fle->file) | |
449 g_free (fle->file); | |
450 if (fle->user) | |
451 g_free (fle->user); | |
452 if (fle->group) | |
453 g_free (fle->group); | |
454 if (fle->attribs) | |
455 g_free (fle->attribs); | |
456 if (fle->destfile) | |
457 g_free (fle->destfile); | |
58 | 458 if (fle->fd > 0) |
459 close (fle->fd); | |
1 | 460 g_free (fle); |
461 } | |
462 | |
463 | |
464 gftp_file * | |
465 copy_fdata (gftp_file * fle) | |
466 { | |
467 gftp_file * newfle; | |
468 | |
469 newfle = g_malloc0 (sizeof (*newfle)); | |
470 memcpy (newfle, fle, sizeof (*newfle)); | |
471 | |
472 if (fle->file) | |
473 { | |
474 newfle->file = g_malloc (strlen (fle->file) + 1); | |
475 strcpy (newfle->file, fle->file); | |
476 } | |
477 | |
478 if (fle->user) | |
479 { | |
480 newfle->user = g_malloc (strlen (fle->user) + 1); | |
481 strcpy (newfle->user, fle->user); | |
482 } | |
483 | |
484 if (fle->group) | |
485 { | |
486 newfle->group = g_malloc (strlen (fle->group) + 1); | |
487 strcpy (newfle->group, fle->group); | |
488 } | |
489 | |
490 if (fle->attribs) | |
491 { | |
492 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
493 strcpy (newfle->attribs, fle->attribs); | |
494 } | |
495 | |
496 if (fle->destfile) | |
497 { | |
498 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
499 strcpy (newfle->destfile, fle->destfile); | |
500 } | |
501 return (newfle); | |
502 } | |
503 | |
504 | |
505 void | |
506 swap_socks (gftp_request * dest, gftp_request * source) | |
507 { | |
508 dest->sockfd = source->sockfd; | |
509 dest->datafd = source->datafd; | |
510 dest->cached = 0; | |
511 if (!source->always_connected) | |
512 { | |
58 | 513 source->sockfd = -1; |
514 source->datafd = -1; | |
1 | 515 source->cached = 1; |
516 } | |
517 } | |
518 | |
519 | |
520 int | |
521 compare_request (gftp_request * request1, gftp_request * request2, | |
522 int compare_dirs) | |
523 { | |
524 char *strarr[3][2]; | |
525 int i, ret; | |
526 | |
527 ret = 1; | |
528 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
529 request1->port == request2->port) | |
530 { | |
531 strarr[0][0] = request1->hostname; | |
532 strarr[0][1] = request2->hostname; | |
533 strarr[1][0] = request1->username; | |
534 strarr[1][1] = request2->username; | |
535 if (compare_dirs) | |
536 { | |
537 strarr[2][0] = request1->directory; | |
538 strarr[2][1] = request2->directory; | |
539 } | |
540 else | |
541 strarr[2][0] = strarr[2][1] = ""; | |
542 | |
543 for (i = 0; i < 3; i++) | |
544 { | |
545 if ((strarr[i][0] && !strarr[i][1]) || | |
546 (!strarr[i][0] && strarr[i][1])) | |
547 { | |
548 ret = 0; | |
549 break; | |
550 } | |
551 | |
552 if (strarr[i][0] && strarr[i][1] && | |
553 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
554 { | |
555 ret = 0; | |
556 break; | |
557 } | |
558 } | |
559 } | |
560 else | |
561 ret = 0; | |
562 return (ret); | |
563 } | |
564 | |
565 | |
566 void | |
567 free_tdata (gftp_transfer * tdata) | |
568 { | |
569 if (tdata->statmutex) | |
570 g_free (tdata->statmutex); | |
571 if (tdata->structmutex) | |
572 g_free (tdata->structmutex); | |
573 if (tdata->fromreq != NULL) | |
574 gftp_request_destroy (tdata->fromreq); | |
575 if (tdata->toreq != NULL) | |
576 gftp_request_destroy (tdata->toreq); | |
577 free_file_list (tdata->files); | |
578 g_free (tdata); | |
579 } | |
580 | |
581 | |
582 gftp_request * | |
583 copy_request (gftp_request * req) | |
584 { | |
585 gftp_request * newreq; | |
586 | |
587 newreq = g_malloc0 (sizeof (*newreq)); | |
588 memcpy (newreq, req, sizeof (*newreq)); | |
589 | |
590 if (req->hostname) | |
56 | 591 newreq->hostname = g_strdup (req->hostname); |
1 | 592 if (req->username) |
56 | 593 newreq->username = g_strdup (req->username); |
1 | 594 if (req->password) |
56 | 595 newreq->password = g_strdup (req->password); |
1 | 596 if (req->account) |
56 | 597 newreq->account = g_strdup (req->account); |
1 | 598 if (req->directory) |
56 | 599 newreq->directory = g_strdup (req->directory); |
1 | 600 |
601 newreq->url_prefix = NULL; | |
602 newreq->protocol_name = NULL; | |
603 newreq->sftpserv_path = NULL; | |
604 newreq->proxy_config = NULL; | |
605 newreq->proxy_hostname = NULL; | |
606 newreq->proxy_username = NULL; | |
607 newreq->proxy_password = NULL; | |
608 newreq->proxy_account = NULL; | |
609 newreq->last_ftp_response = NULL; | |
610 newreq->last_dir_entry = NULL; | |
58 | 611 newreq->sockfd = -1; |
612 newreq->datafd = -1; | |
613 newreq->cachefd = -1; | |
1 | 614 newreq->hostp = NULL; |
615 | |
616 if (req->proxy_config != NULL) | |
56 | 617 newreq->proxy_config = g_strdup (req->proxy_config); |
1 | 618 |
619 req->init (newreq); | |
620 | |
621 return (newreq); | |
622 } | |
623 | |
624 | |
625 int | |
626 ptym_open (char *pts_name) | |
627 { | |
628 int fd; | |
629 | |
630 #ifdef __sgi | |
631 char *tempstr; | |
632 | |
633 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
634 return (-1); | |
635 | |
636 strcpy (pts_name, tempstr); | |
637 return (fd); | |
638 | |
639 #else /* !__sgi */ | |
640 | |
641 #ifdef SYSV | |
642 | |
643 char *tempstr; | |
644 | |
645 strcpy (pts_name, "/dev/ptmx"); | |
646 if ((fd = open (pts_name, O_RDWR)) < 0) | |
647 return (-1); | |
648 | |
649 if (grantpt (fd) < 0) | |
650 { | |
651 close (fd); | |
652 return (-1); | |
653 } | |
654 | |
655 if (unlockpt (fd) < 0) | |
656 { | |
657 close (fd); | |
658 return (-1); | |
659 } | |
660 | |
661 if ((tempstr = ptsname (fd)) == NULL) | |
662 { | |
663 close (fd); | |
664 return (-1); | |
665 } | |
666 | |
667 strcpy (pts_name, tempstr); | |
668 return (fd); | |
669 | |
670 #else /* !SYSV */ | |
671 | |
672 char *pos1, *pos2; | |
673 | |
674 strcpy (pts_name, "/dev/ptyXY"); | |
675 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
676 { | |
677 pts_name[8] = *pos1; | |
678 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
679 { | |
680 pts_name[9] = *pos2; | |
681 if ((fd = open (pts_name, O_RDWR)) < 0) | |
682 continue; | |
683 pts_name[5] = 't'; | |
684 return (fd); | |
685 } | |
686 } | |
687 return (-1); | |
688 | |
689 #endif | |
690 | |
691 #endif | |
692 | |
693 } | |
694 | |
695 | |
696 int | |
697 ptys_open (int fdm, char *pts_name) | |
698 { | |
699 int fds; | |
700 | |
701 #if !defined (SYSV) && !defined (__sgi) | |
702 | |
703 chmod (pts_name, S_IRUSR | S_IWUSR); | |
704 chown (pts_name, getuid (), -1); | |
705 | |
706 #endif | |
707 | |
708 if ((fds = open (pts_name, O_RDWR)) < 0) | |
709 { | |
710 close (fdm); | |
711 return (-1); | |
712 } | |
713 | |
714 #ifdef SYSV | |
715 | |
716 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
717 { | |
718 close (fdm); | |
719 close (fds); | |
720 return (-1); | |
721 } | |
722 | |
723 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
724 { | |
725 close (fdm); | |
726 close (fds); | |
727 return (-1); | |
728 } | |
729 | |
730 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
731 { | |
732 close (fdm); | |
733 close (fds); | |
734 return (-1); | |
735 } | |
736 | |
737 #endif | |
738 | |
739 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
740 | |
741 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
742 { | |
743 close (fdm); | |
744 return (-1); | |
745 } | |
746 | |
747 #endif | |
748 | |
749 return (fds); | |
750 } | |
751 | |
752 | |
753 int | |
754 tty_raw (int fd) | |
755 { | |
756 struct termios buf; | |
757 | |
758 if (tcgetattr (fd, &buf) < 0) | |
759 return (-1); | |
760 | |
761 buf.c_iflag |= IGNPAR; | |
762 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
763 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
764 #ifdef IEXTEN | |
765 buf.c_lflag &= ~(IEXTEN); | |
766 #endif | |
767 | |
768 buf.c_oflag &= ~(OPOST); | |
769 buf.c_cc[VMIN] = 1; | |
770 buf.c_cc[VTIME] = 0; | |
771 | |
772 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
773 return (-1); | |
774 return (0); | |
775 } | |
776 | |
777 | |
778 /* We have the caller send us a pointer to a string so we can write the port | |
779 into it. It makes it easier so we don't have to worry about freeing it | |
780 later on, the caller can just send us an auto variable, The string | |
781 should be at least 6 chars. I know this is messy... */ | |
782 | |
783 char ** | |
784 make_ssh_exec_args (gftp_request * request, char *execname, | |
785 int use_sftp_subsys, char *portstring) | |
786 { | |
787 char **args, *oldstr, *tempstr; | |
56 | 788 struct servent serv_struct; |
1 | 789 int i, j; |
790 | |
56 | 791 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 15)); |
1 | 792 |
793 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
794 ssh_prog_name : "ssh"; | |
795 i = 1; | |
56 | 796 tempstr = g_strdup (args[0]); |
1 | 797 |
798 if (ssh_extra_params_list != NULL) | |
799 { | |
800 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
801 { | |
802 oldstr = tempstr; | |
803 args[i++] = ssh_extra_params_list[j]; | |
804 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
805 g_free (oldstr); | |
806 } | |
807 } | |
808 | |
809 oldstr = tempstr; | |
810 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
811 g_free (oldstr); | |
812 args[i++] = "-e"; | |
813 args[i++] = "none"; | |
814 | |
815 if (request->username && *request->username != '\0') | |
816 { | |
817 oldstr = tempstr; | |
818 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
819 g_free (oldstr); | |
820 args[i++] = "-l"; | |
821 args[i++] = request->username; | |
822 } | |
823 | |
824 if (request->port != 0) | |
825 { | |
826 g_snprintf (portstring, 6, "%d", request->port); | |
827 oldstr = tempstr; | |
828 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
829 g_free (oldstr); | |
830 args[i++] = "-p"; | |
831 args[i++] = portstring; | |
832 } | |
56 | 833 else |
834 { | |
835 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL)) | |
836 request->port = 22; | |
837 else | |
838 request->port = ntohs (serv_struct.s_port); | |
839 } | |
1 | 840 |
841 if (use_sftp_subsys) | |
842 { | |
843 oldstr = tempstr; | |
844 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
845 g_free (oldstr); | |
846 args[i++] = request->hostname; | |
847 args[i++] = "-s"; | |
848 args[i++] = "sftp"; | |
849 args[i] = NULL; | |
850 } | |
851 else | |
852 { | |
853 oldstr = tempstr; | |
854 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
855 "\"", NULL); | |
856 g_free (oldstr); | |
857 args[i++] = request->hostname; | |
858 args[i++] = execname; | |
859 args[i] = NULL; | |
860 } | |
861 | |
862 request->logging_function (gftp_logging_misc, request->user_data, | |
863 _("Running program %s\n"), tempstr); | |
864 g_free (tempstr); | |
865 return (args); | |
866 } | |
867 | |
868 | |
869 char * | |
870 ssh_start_login_sequence (gftp_request * request, int fd) | |
871 { | |
872 size_t rem, len, diff, lastdiff; | |
58 | 873 int wrotepw, ok; |
874 char *tempstr, *pwstr; | |
1 | 875 ssize_t rd; |
876 | |
877 rem = len = 100; | |
878 tempstr = g_malloc0 (len); | |
879 diff = lastdiff = 0; | |
880 wrotepw = 0; | |
881 ok = 1; | |
882 | |
58 | 883 if (gftp_set_sockblocking (request, request->datafd, 1) == -1) |
884 return (NULL); | |
1 | 885 |
58 | 886 pwstr = g_strconcat (request->password, "\n", NULL); |
1 | 887 |
41 | 888 errno = 0; |
1 | 889 while (1) |
890 { | |
58 | 891 if ((rd = gftp_read (request, tempstr + diff, rem -1, fd)) <= 0) |
1 | 892 { |
893 ok = 0; | |
894 break; | |
895 } | |
896 tempstr[diff + rd] = '\0'; | |
897 rem -= rd; | |
898 diff += rd; | |
899 if (rem <= 1) | |
900 { | |
901 tempstr = g_realloc (tempstr, len + 100); | |
902 tempstr[diff] = '\0'; | |
903 request->logging_function (gftp_logging_recv, request->user_data, | |
904 "%s", tempstr + lastdiff); | |
905 lastdiff = diff; | |
906 len += 100; | |
907 rem = 100; | |
908 } | |
909 | |
910 if (!wrotepw && | |
911 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, | |
912 "password: ") == 0) | |
913 { | |
914 wrotepw = 1; | |
58 | 915 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
916 { | |
917 ok = 0; | |
918 break; | |
919 } | |
1 | 920 } |
921 | |
922 else if (!wrotepw && | |
923 (strstr (tempstr, "Enter passphrase for RSA key") != NULL || | |
924 strstr (tempstr, "Enter passphrase for key '") != NULL)) | |
925 { | |
926 wrotepw = 1; | |
58 | 927 if (gftp_write (request, pwstr, strlen (pwstr), fd) < 0) |
928 { | |
929 ok = 0; | |
930 break; | |
931 } | |
1 | 932 } |
933 else if (strlen (tempstr) >= 5 && | |
934 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) | |
935 break; | |
936 } | |
937 | |
58 | 938 g_free (pwstr); |
1 | 939 tempstr[diff] = '\0'; |
940 request->logging_function (gftp_logging_recv, request->user_data, | |
941 "%s\n", tempstr + lastdiff); | |
942 | |
943 if (!ok) | |
944 { | |
945 g_free (tempstr); | |
946 return (NULL); | |
947 } | |
948 | |
949 return (tempstr); | |
950 } | |
951 | |
952 | |
953 #ifdef G_HAVE_GINT64 | |
954 | |
955 gint64 | |
956 hton64 (gint64 val) | |
957 { | |
958 gint64 num; | |
959 char *pos; | |
960 | |
961 num = 0; | |
962 pos = (char *) # | |
963 pos[0] = (val >> 56) & 0xff; | |
964 pos[1] = (val >> 48) & 0xff; | |
965 pos[2] = (val >> 40) & 0xff; | |
966 pos[3] = (val >> 32) & 0xff; | |
967 pos[4] = (val >> 24) & 0xff; | |
968 pos[5] = (val >> 16) & 0xff; | |
969 pos[6] = (val >> 8) & 0xff; | |
970 pos[7] = val & 0xff; | |
971 return (num); | |
972 } | |
973 | |
974 #endif | |
975 | |
16 | 976 |
977 static gint | |
978 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
979 { | |
980 const gftp_file * f1, * f2; | |
981 | |
982 f1 = a; | |
983 f2 = b; | |
984 return (strcmp (f1->file, f2->file)); | |
985 } | |
986 | |
987 | |
988 static gint | |
989 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
990 { | |
991 const gftp_file * f1, * f2; | |
992 gint ret; | |
993 | |
994 f1 = a; | |
995 f2 = b; | |
996 ret = strcmp (f1->file, f2->file); | |
997 if (ret < 0) | |
998 ret = 1; | |
999 else if (ret > 0) | |
1000 ret = -1; | |
1001 return (ret); | |
1002 } | |
1003 | |
1004 | |
1005 static gint | |
1006 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
1007 { | |
1008 const gftp_file * f1, * f2; | |
1009 | |
1010 f1 = a; | |
1011 f2 = b; | |
1012 return (strcmp (f1->user, f2->user)); | |
1013 } | |
1014 | |
1015 | |
1016 static gint | |
1017 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
1018 { | |
1019 const gftp_file * f1, * f2; | |
1020 gint ret; | |
1021 | |
1022 f1 = a; | |
1023 f2 = b; | |
1024 ret = strcmp (f1->user, f2->user); | |
1025 if (ret < 0) | |
1026 ret = 1; | |
1027 else if (ret > 0) | |
1028 ret = -1; | |
1029 return (ret); | |
1030 } | |
1031 | |
1032 | |
1033 static gint | |
1034 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
1035 { | |
1036 const gftp_file * f1, * f2; | |
1037 | |
1038 f1 = a; | |
1039 f2 = b; | |
1040 return (strcmp (f1->group, f2->group)); | |
1041 } | |
1042 | |
1043 | |
1044 static gint | |
1045 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
1046 { | |
1047 const gftp_file * f1, * f2; | |
1048 gint ret; | |
1049 | |
1050 f1 = a; | |
1051 f2 = b; | |
1052 ret = strcmp (f1->group, f2->group); | |
1053 if (ret < 0) | |
1054 ret = 1; | |
1055 else if (ret > 0) | |
1056 ret = -1; | |
1057 return (ret); | |
1058 } | |
1059 | |
1060 | |
1061 static gint | |
1062 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
1063 { | |
1064 const gftp_file * f1, * f2; | |
1065 | |
1066 f1 = a; | |
1067 f2 = b; | |
1068 return (strcmp (f1->attribs, f2->attribs)); | |
1069 } | |
1070 | |
1071 | |
1072 static gint | |
1073 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
1074 { | |
1075 const gftp_file * f1, * f2; | |
1076 gint ret; | |
1077 | |
1078 f1 = a; | |
1079 f2 = b; | |
1080 ret = strcmp (f1->attribs, f2->attribs); | |
1081 if (ret < 0) | |
1082 ret = 1; | |
1083 else if (ret > 0) | |
1084 ret = -1; | |
1085 return (ret); | |
1086 } | |
1087 | |
1088 | |
1089 static gint | |
1090 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
1091 { | |
1092 const gftp_file * f1, * f2; | |
1093 | |
1094 f1 = a; | |
1095 f2 = b; | |
1096 if (f1->size < f2->size) | |
1097 return (-1); | |
1098 else if (f1->size == f2->size) | |
1099 return (0); | |
1100 else | |
1101 return (1); | |
1102 } | |
1103 | |
1104 | |
1105 static gint | |
1106 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
1107 { | |
1108 const gftp_file * f1, * f2; | |
1109 | |
1110 f1 = a; | |
1111 f2 = b; | |
1112 if (f1->size < f2->size) | |
1113 return (1); | |
1114 else if (f1->size == f2->size) | |
1115 return (0); | |
1116 else | |
1117 return (-1); | |
1118 } | |
1119 | |
1120 | |
1121 static gint | |
1122 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
1123 { | |
1124 const gftp_file * f1, * f2; | |
1125 | |
1126 f1 = a; | |
1127 f2 = b; | |
1128 if (f1->datetime < f2->datetime) | |
1129 return (-1); | |
1130 else if (f1->datetime == f2->datetime) | |
1131 return (0); | |
1132 else | |
1133 return (1); | |
1134 } | |
1135 | |
1136 | |
1137 static gint | |
1138 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
1139 { | |
1140 const gftp_file * f1, * f2; | |
1141 | |
1142 f1 = a; | |
1143 f2 = b; | |
1144 if (f1->datetime < f2->datetime) | |
1145 return (1); | |
1146 else if (f1->datetime == f2->datetime) | |
1147 return (0); | |
1148 else | |
1149 return (-1); | |
1150 } | |
1151 | |
1152 | |
1153 GList * | |
1154 gftp_sort_filelist (GList * filelist, int column, int asds) | |
1155 { | |
1156 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
1157 GCompareFunc sortfunc; | |
1158 gftp_file * tempfle; | |
1159 | |
1160 files = dirs = dotdot = NULL; | |
1161 | |
1162 if (column == GFTP_SORT_COL_FILE) | |
1163 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
1164 else if (column == GFTP_SORT_COL_SIZE) | |
1165 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
1166 else if (column == GFTP_SORT_COL_USER) | |
1167 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
1168 else if (column == GFTP_SORT_COL_GROUP) | |
1169 sortfunc = asds ? | |
1170 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
1171 else if (column == GFTP_SORT_COL_DATETIME) | |
1172 sortfunc = asds ? | |
1173 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
1174 sortfunc = asds ? | |
1175 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
1176 | |
1177 for (tempitem = filelist; tempitem != NULL; ) | |
1178 { | |
1179 tempfle = tempitem->data; | |
1180 insitem = tempitem; | |
1181 tempitem = tempitem->next; | |
1182 insitem->next = NULL; | |
1183 | |
1184 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
1185 dotdot = insitem; | |
1186 else if (sort_dirs_first && tempfle->isdir) | |
1187 { | |
1188 insitem->next = dirs; | |
1189 dirs = insitem; | |
1190 } | |
1191 else | |
1192 { | |
1193 insitem->next = files; | |
1194 files = insitem; | |
1195 } | |
1196 } | |
1197 | |
1198 if (dirs != NULL) | |
1199 dirs = g_list_sort (dirs, sortfunc); | |
1200 if (files != NULL) | |
1201 files = g_list_sort (files, sortfunc); | |
1202 | |
1203 filelist = dotdot; | |
1204 | |
1205 if (filelist == NULL) | |
1206 filelist = dirs; | |
1207 else | |
1208 filelist = g_list_concat (filelist, dirs); | |
1209 | |
1210 if (filelist == NULL) | |
1211 filelist = files; | |
1212 else | |
1213 filelist = g_list_concat (filelist, files); | |
1214 | |
39 | 1215 /* I haven't check this, but I'm pretty sure some older versions of glib |
1216 had a bug that the prev pointer wasn't being sent to NULL */ | |
1217 filelist->prev = NULL; | |
16 | 1218 return (filelist); |
1219 } | |
1220 |