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