Mercurial > gftp.yaz
annotate lib/misc.c @ 122:76e2b58a9440
2003-4-5 Brian Masney <masneyb@gftp.org>
* lib/config_file.c lib/options.h lib/gftp.h lib/rfc959.c
lib/rfc2068.c lib/ssh.c - added new internal configuration interface.
Rather than having a global variable for each option, I have a global
hash table (gftp_global_options_htable) that I can look up option names
by name using gftp_lookup_global_option(). I also an options hash
associated with a request structure, so I will be able to call
gftp_lookup_request_option(). I will be able to override options with
bookmarks or while transfers are in progress very easily now. Also, all
options no longer have to appear in config_file.c, the per protocol
options can appear inside their own file
* lib/gftp.h lib/bookmarks.c lib/local.c lib/rfc959.c lib/rfc2068.c -
remove set_data_type and protocol name from struct gftp_request
* lib/rfc959.c - renamed all firewall_* variables to ftp_proxy_* in
the config file
* lib/gftp.h lib/protocols.c lib/rfc959.c - renamed all GFTP_TYPE_*
vars to GFTP_DIRTYPE_*
* lib/gftp.h - removed ascii field and renamed the node pointer to
user_data in struct gftp_file. In gftp_request, removed any setting
that is now stored in the global/local hash tables. Added
register_module() pointer that will be called whenever the protocol is
first loaded into gftp
* lib/rfc959.c src/text/gftp-text.c - moved the ascii/binary
translation to rfc959.c. Also, moved any instance of automatically
setting the data type to rfc959.c as well.
* lib/misc.c lib/sshv2.c - moved all ssh functions from misc.c to
sshv2.c. I had these origionally in misc.c because I used to have 2
different SSH protocols
* lib/protocols.c src/text/gftp-text.c - added gftp_calc_kbs() to
protocols.c. This no longer needs to be in the different ports
* src/text/gftp-text.c - read/write options based on new configuration
interface
* Use new configuration interface in all source files
* Updated copyright dates on all source files
* Note: GTK+ port is completely broken at the moment. I'll upload
those changes whenever I get them done
author | masneyb |
---|---|
date | Sat, 05 Apr 2003 16:30:45 +0000 |
parents | 3b573c8ef706 |
children | 65048c959029 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* misc.c - general purpose routines */ | |
122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
102 | 20 static const char cvsid[] = "$Id$"; |
21 | |
1 | 22 #include "gftp.h" |
23 #include "options.h" | |
24 | |
87 | 25 /* FIXME - this isn't right for all locales. Use glib's printf instead */ |
1 | 26 char * |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
27 insert_commas (off_t number, char *dest_str, size_t dest_len) |
1 | 28 { |
29 char *frompos, *topos, src[50], *dest; | |
30 int len, num, rem, i; | |
31 | |
32 if (dest_str != NULL) | |
33 *dest_str = '\0'; | |
56 | 34 len = (number > 0 ? log10 (number) : 0) + 2; |
1 | 35 |
36 if (len <= 0) | |
37 { | |
38 if (dest_str != NULL) | |
39 strncpy (dest_str, "0", dest_len); | |
40 else | |
56 | 41 dest_str = g_strdup ("0"); |
1 | 42 return (dest_str); |
43 } | |
44 | |
45 len += len / 3; | |
46 if (dest_str != NULL && len > dest_len) | |
47 { | |
48 | |
49 for (i=0; i<dest_len - 1; i++) | |
50 dest_str[i] = 'X'; | |
51 dest_str[dest_len - 1] = '\0'; | |
52 return (dest_str); | |
53 } | |
54 | |
55 if (dest_str == NULL) | |
56 dest = g_malloc0 (len); | |
57 else | |
58 dest = dest_str; | |
59 | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
60 #if defined (_LARGEFILE_SOURCE) |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
61 g_snprintf (src, sizeof (src), "%lld", number); |
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
62 #else |
1 | 63 g_snprintf (src, sizeof (src), "%ld", number); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
64 #endif |
1 | 65 |
66 num = strlen (src) / 3 - 1; | |
67 rem = strlen (src) % 3; | |
68 frompos = src; | |
69 topos = dest; | |
70 for (i = 0; i < rem; i++) | |
71 *topos++ = *frompos++; | |
72 | |
73 if (*frompos != '\0') | |
74 { | |
75 if (rem != 0) | |
76 *topos++ = ','; | |
77 while (num > 0) | |
78 { | |
79 for (i = 0; i < 3; i++) | |
80 *topos++ = *frompos++; | |
81 *topos++ = ','; | |
82 num--; | |
83 } | |
84 for (i = 0; i < 3; i++) | |
85 *topos++ = *frompos++; | |
86 } | |
87 *topos = '\0'; | |
88 return (dest); | |
89 } | |
90 | |
91 | |
92 char * | |
93 alltrim (char *str) | |
94 { | |
95 char *pos, *newpos; | |
96 int diff; | |
97 | |
98 pos = str + strlen (str) - 1; | |
87 | 99 while (pos >= str && (*pos == ' ' || *pos == '\t')) |
1 | 100 *pos-- = '\0'; |
101 | |
102 pos = str; | |
103 diff = 0; | |
104 while (*pos++ == ' ') | |
105 diff++; | |
106 | |
107 if (diff == 0) | |
108 return (str); | |
109 | |
110 pos = str + diff; | |
111 newpos = str; | |
112 while (*pos != '\0') | |
113 *newpos++ = *pos++; | |
114 *newpos = '\0'; | |
115 | |
116 return (str); | |
117 } | |
118 | |
119 | |
120 char * | |
121 expand_path (const char *src) | |
122 { | |
123 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
124 struct passwd *pw; | |
125 | |
126 pw = NULL; | |
127 str = g_malloc (strlen (src) + 1); | |
128 strcpy (str, src); | |
129 | |
130 if (*str == '~') | |
131 { | |
132 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
133 pw = getpwuid (geteuid ()); | |
134 else | |
135 { | |
136 if ((pos = strchr (str, '/')) != NULL) | |
137 *pos = '\0'; | |
138 | |
139 pw = getpwnam (str + 1); | |
140 | |
141 if (pos != NULL) | |
142 *pos = '/'; | |
143 } | |
144 } | |
145 | |
146 endpos = str; | |
147 newstr = NULL; | |
148 while ((pos = strchr (endpos, '/')) != NULL) | |
149 { | |
150 pos++; | |
151 while (*pos == '/') | |
152 pos++; | |
153 if ((endpos = strchr (pos, '/')) == NULL) | |
154 endpos = pos + strlen (pos); | |
155 tempchar = *endpos; | |
156 *endpos = '\0'; | |
157 if (strcmp (pos, "..") == 0) | |
158 { | |
159 *(pos - 1) = '\0'; | |
160 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
161 *prevpos = '\0'; | |
162 } | |
163 else if (strcmp (pos, ".") != 0) | |
164 { | |
165 if (newstr == NULL) | |
56 | 166 newstr = g_strdup (pos - 1); |
1 | 167 else |
168 { | |
169 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
170 g_free (newstr); | |
171 newstr = tempstr; | |
172 } | |
173 } | |
174 *endpos = tempchar; | |
175 if (*endpos == '\0') | |
176 break; | |
177 endpos = pos + 1; | |
178 } | |
179 | |
180 if (newstr == NULL || *newstr == '\0') | |
181 { | |
182 if (newstr != NULL) | |
183 g_free (newstr); | |
184 newstr = g_malloc0 (2); | |
185 *newstr = '/'; | |
186 } | |
187 | |
188 g_free (str); | |
189 | |
190 if (pw != NULL) | |
191 { | |
192 if ((pos = strchr (newstr, '/')) == NULL) | |
193 { | |
194 str = g_malloc (strlen (pw->pw_dir) + 1); | |
195 strcpy (str, pw->pw_dir); | |
196 } | |
197 else | |
198 str = g_strconcat (pw->pw_dir, pos, NULL); | |
199 | |
200 g_free (newstr); | |
201 newstr = str; | |
202 } | |
203 | |
204 return (newstr); | |
205 } | |
206 | |
207 | |
208 void | |
209 remove_double_slashes (char *string) | |
210 { | |
211 char *newpos, *oldpos; | |
87 | 212 size_t len; |
1 | 213 |
214 oldpos = newpos = string; | |
215 while (*oldpos != '\0') | |
216 { | |
217 *newpos++ = *oldpos++; | |
218 if (*oldpos == '\0') | |
219 break; | |
220 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
221 oldpos++; | |
222 } | |
223 *newpos = '\0'; | |
87 | 224 |
225 len = strlen (string); | |
226 if (string[len - 1] == '/') | |
227 string[len - 1] = '\0'; | |
1 | 228 } |
229 | |
230 | |
231 void | |
232 make_nonnull (char **str) | |
233 { | |
234 if (*str == NULL) | |
235 *str = g_malloc0 (1); | |
236 } | |
237 | |
238 | |
239 int | |
240 copyfile (char *source, char *dest) | |
241 { | |
58 | 242 int srcfd, destfd; |
1 | 243 char buf[8192]; |
58 | 244 ssize_t n; |
1 | 245 |
58 | 246 if ((srcfd = open (source, O_RDONLY)) == -1) |
247 { | |
248 printf (_("Error: Cannot open local file %s: %s\n"), | |
249 source, g_strerror (errno)); | |
250 exit (1); | |
251 } | |
1 | 252 |
58 | 253 if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) |
1 | 254 { |
58 | 255 printf (_("Error: Cannot open local file %s: %s\n"), |
256 dest, g_strerror (errno)); | |
257 close (srcfd); | |
258 exit (1); | |
1 | 259 } |
260 | |
58 | 261 while ((n = read (srcfd, buf, sizeof (buf))) > 0) |
262 { | |
263 if (write (destfd, buf, n) == -1) | |
264 { | |
265 printf (_("Error: Could not write to socket: %s\n"), | |
266 g_strerror (errno)); | |
267 exit (1); | |
268 } | |
269 } | |
1 | 270 |
58 | 271 if (n == -1) |
272 { | |
273 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
274 exit (1); | |
275 } | |
276 | |
87 | 277 if (close (srcfd) == -1) |
278 { | |
279 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
280 exit (1); | |
281 } | |
282 | |
283 if (close (destfd) == -1) | |
284 { | |
285 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
286 exit (1); | |
287 } | |
1 | 288 |
289 return (1); | |
290 } | |
291 | |
292 | |
87 | 293 /* FIXME - is there a replacement for this */ |
1 | 294 int |
295 gftp_match_filespec (char *filename, char *filespec) | |
296 { | |
297 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
298 size_t len, curlen; | |
299 | |
300 if (filename == NULL || *filename == '\0' || | |
301 filespec == NULL || *filespec == '\0') | |
302 return(1); | |
303 | |
304 filepos = filename; | |
305 wcpos = filespec; | |
306 while(1) | |
307 { | |
308 if (*wcpos == '\0') | |
309 return (1); | |
310 else if (*filepos == '\0') | |
311 return(0); | |
312 else if(*wcpos == '?') | |
313 { | |
314 wcpos++; | |
315 filepos++; | |
316 } | |
317 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
318 return(1); | |
319 else if(*wcpos == '*') | |
320 { | |
321 len = sizeof (search_str); | |
322 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
323 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
324 curlen++, *newpos++ = *pos++); | |
325 *newpos = '\0'; | |
326 | |
327 if ((filepos = strstr (filepos, search_str)) == NULL) | |
328 return(0); | |
329 wcpos += curlen + 1; | |
330 filepos += curlen; | |
331 } | |
332 else if(*wcpos++ != *filepos++) | |
333 return(0); | |
334 } | |
335 return (1); | |
336 } | |
337 | |
338 | |
339 int | |
340 gftp_parse_command_line (int *argc, char ***argv) | |
341 { | |
342 if (*argc > 1) | |
343 { | |
87 | 344 if (strcmp (argv[0][1], "--help") == 0 || |
345 strcmp (argv[0][1], "-h") == 0) | |
1 | 346 gftp_usage (); |
87 | 347 else if (strcmp (argv[0][1], "--version") == 0 || |
348 strcmp (argv[0][1], "-v") == 0) | |
1 | 349 { |
122 | 350 printf ("%s\n", gftp_version); |
1 | 351 exit (0); |
352 } | |
353 } | |
354 return (0); | |
355 } | |
356 | |
357 | |
358 void | |
359 gftp_usage (void) | |
360 { | |
87 | 361 printf (_("usage: gftp [[protocol://][user:[pass]@]site[:port][/directory]]\n")); |
1 | 362 exit (0); |
363 } | |
364 | |
365 | |
366 char * | |
367 get_xpm_path (char *filename, int quit_on_err) | |
368 { | |
369 char *tempstr, *exfile; | |
370 | |
371 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
372 exfile = expand_path (tempstr); | |
373 g_free (tempstr); | |
374 if (access (exfile, F_OK) != 0) | |
375 { | |
376 g_free (exfile); | |
377 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
378 exfile = expand_path (tempstr); | |
379 g_free (tempstr); | |
380 if (access (exfile, F_OK) != 0) | |
381 { | |
382 g_free (exfile); | |
383 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
384 if (access (exfile, F_OK) != 0) | |
385 { | |
386 g_free (exfile); | |
387 if (!quit_on_err) | |
388 return (NULL); | |
87 | 389 |
1 | 390 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), |
391 filename, SHARE_DIR, BASE_CONF_DIR); | |
87 | 392 exit (1); |
1 | 393 } |
394 } | |
395 } | |
396 return (exfile); | |
397 } | |
398 | |
399 | |
400 gint | |
401 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
402 { | |
403 return (strcmp ((char *) path1, (char *) path2) == 0); | |
404 } | |
405 | |
406 | |
407 guint | |
408 string_hash_function (gconstpointer key) | |
409 { | |
59 | 410 guint ret; |
411 int i; | |
412 | |
413 ret = 0; | |
87 | 414 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
59 | 415 ret += ((char *) key)[i]; |
416 | |
417 return (ret); | |
1 | 418 } |
419 | |
420 | |
421 void | |
422 free_file_list (GList * filelist) | |
423 { | |
424 gftp_file * tempfle; | |
425 GList * templist; | |
426 | |
427 templist = filelist; | |
428 while (templist != NULL) | |
429 { | |
430 tempfle = templist->data; | |
431 free_fdata (tempfle); | |
432 templist = templist->next; | |
433 } | |
434 g_list_free (filelist); | |
435 } | |
436 | |
437 | |
438 void | |
439 free_fdata (gftp_file * fle) | |
440 { | |
441 if (fle->file) | |
442 g_free (fle->file); | |
443 if (fle->user) | |
444 g_free (fle->user); | |
445 if (fle->group) | |
446 g_free (fle->group); | |
447 if (fle->attribs) | |
448 g_free (fle->attribs); | |
449 if (fle->destfile) | |
450 g_free (fle->destfile); | |
58 | 451 if (fle->fd > 0) |
102 | 452 close (fle->fd); |
1 | 453 g_free (fle); |
454 } | |
455 | |
456 | |
457 gftp_file * | |
458 copy_fdata (gftp_file * fle) | |
459 { | |
460 gftp_file * newfle; | |
461 | |
462 newfle = g_malloc0 (sizeof (*newfle)); | |
463 memcpy (newfle, fle, sizeof (*newfle)); | |
464 | |
465 if (fle->file) | |
87 | 466 newfle->file = g_strdup (fle->file); |
1 | 467 |
468 if (fle->user) | |
87 | 469 newfle->user = g_strdup (fle->user); |
1 | 470 |
471 if (fle->group) | |
87 | 472 newfle->group = g_strdup (fle->group); |
1 | 473 |
474 if (fle->attribs) | |
87 | 475 newfle->attribs = g_strdup (fle->attribs); |
1 | 476 |
477 if (fle->destfile) | |
87 | 478 newfle->destfile = g_strdup (fle->destfile); |
479 | |
1 | 480 return (newfle); |
481 } | |
482 | |
483 | |
484 int | |
485 compare_request (gftp_request * request1, gftp_request * request2, | |
486 int compare_dirs) | |
487 { | |
488 char *strarr[3][2]; | |
489 int i, ret; | |
490 | |
491 ret = 1; | |
87 | 492 if (request1->protonum == request2->protonum && |
1 | 493 request1->port == request2->port) |
494 { | |
495 strarr[0][0] = request1->hostname; | |
496 strarr[0][1] = request2->hostname; | |
497 strarr[1][0] = request1->username; | |
498 strarr[1][1] = request2->username; | |
499 if (compare_dirs) | |
500 { | |
501 strarr[2][0] = request1->directory; | |
502 strarr[2][1] = request2->directory; | |
503 } | |
504 else | |
505 strarr[2][0] = strarr[2][1] = ""; | |
506 | |
507 for (i = 0; i < 3; i++) | |
508 { | |
509 if ((strarr[i][0] && !strarr[i][1]) || | |
510 (!strarr[i][0] && strarr[i][1])) | |
511 { | |
512 ret = 0; | |
513 break; | |
514 } | |
515 | |
516 if (strarr[i][0] && strarr[i][1] && | |
517 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
518 { | |
519 ret = 0; | |
520 break; | |
521 } | |
522 } | |
523 } | |
524 else | |
525 ret = 0; | |
526 return (ret); | |
527 } | |
528 | |
529 | |
530 void | |
531 free_tdata (gftp_transfer * tdata) | |
532 { | |
533 if (tdata->fromreq != NULL) | |
67 | 534 gftp_request_destroy (tdata->fromreq, 1); |
1 | 535 if (tdata->toreq != NULL) |
67 | 536 gftp_request_destroy (tdata->toreq, 1); |
1 | 537 free_file_list (tdata->files); |
538 g_free (tdata); | |
539 } | |
540 | |
541 | |
542 gftp_request * | |
543 copy_request (gftp_request * req) | |
544 { | |
545 gftp_request * newreq; | |
546 | |
66 | 547 newreq = gftp_request_new (); |
1 | 548 |
549 if (req->hostname) | |
56 | 550 newreq->hostname = g_strdup (req->hostname); |
1 | 551 if (req->username) |
56 | 552 newreq->username = g_strdup (req->username); |
1 | 553 if (req->password) |
56 | 554 newreq->password = g_strdup (req->password); |
1 | 555 if (req->account) |
56 | 556 newreq->account = g_strdup (req->account); |
1 | 557 if (req->directory) |
56 | 558 newreq->directory = g_strdup (req->directory); |
66 | 559 newreq->port = req->port; |
560 newreq->use_proxy = req->use_proxy; | |
561 newreq->logging_function = req->logging_function; | |
122 | 562 newreq->local_options = NULL; /* FIXME */ |
1 | 563 |
564 req->init (newreq); | |
565 | |
566 return (newreq); | |
567 } | |
568 | |
569 | |
570 int | |
571 ptym_open (char *pts_name) | |
572 { | |
573 int fd; | |
574 | |
575 #ifdef __sgi | |
576 char *tempstr; | |
577 | |
578 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
579 return (-1); | |
580 | |
581 strcpy (pts_name, tempstr); | |
582 return (fd); | |
583 | |
584 #else /* !__sgi */ | |
585 | |
80 | 586 #ifdef HAVE_GRANTPT |
1 | 587 |
588 char *tempstr; | |
589 | |
590 strcpy (pts_name, "/dev/ptmx"); | |
591 if ((fd = open (pts_name, O_RDWR)) < 0) | |
592 return (-1); | |
593 | |
594 if (grantpt (fd) < 0) | |
595 { | |
596 close (fd); | |
597 return (-1); | |
598 } | |
599 | |
600 if (unlockpt (fd) < 0) | |
601 { | |
602 close (fd); | |
603 return (-1); | |
604 } | |
605 | |
606 if ((tempstr = ptsname (fd)) == NULL) | |
607 { | |
608 close (fd); | |
609 return (-1); | |
610 } | |
611 | |
612 strcpy (pts_name, tempstr); | |
613 return (fd); | |
614 | |
80 | 615 #else /* !GRANTPT */ |
1 | 616 |
617 char *pos1, *pos2; | |
618 | |
619 strcpy (pts_name, "/dev/ptyXY"); | |
620 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
621 { | |
622 pts_name[8] = *pos1; | |
623 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
624 { | |
625 pts_name[9] = *pos2; | |
626 if ((fd = open (pts_name, O_RDWR)) < 0) | |
627 continue; | |
628 pts_name[5] = 't'; | |
629 return (fd); | |
630 } | |
631 } | |
632 return (-1); | |
633 | |
87 | 634 #endif /* GRANTPT */ |
1 | 635 |
87 | 636 #endif /* __sgi */ |
1 | 637 |
638 } | |
639 | |
640 | |
641 int | |
642 ptys_open (int fdm, char *pts_name) | |
643 { | |
644 int fds; | |
645 | |
80 | 646 #if !defined (HAVE_GRANTPT) && !defined (__sgi) |
1 | 647 |
648 chmod (pts_name, S_IRUSR | S_IWUSR); | |
649 chown (pts_name, getuid (), -1); | |
650 | |
651 #endif | |
652 | |
653 if ((fds = open (pts_name, O_RDWR)) < 0) | |
654 { | |
655 close (fdm); | |
656 return (-1); | |
657 } | |
658 | |
80 | 659 #ifdef HAVE_GRANTPT |
660 /* I intentionally ignore these errors */ | |
661 ioctl (fds, I_PUSH, "ptem"); | |
662 ioctl (fds, I_PUSH, "ldterm"); | |
663 ioctl (fds, I_PUSH, "ttcompat"); | |
1 | 664 #endif |
665 | |
80 | 666 #if !defined(HAVE_GRANTPT) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) |
1 | 667 |
668 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
669 { | |
670 close (fdm); | |
671 return (-1); | |
672 } | |
673 | |
674 #endif | |
675 | |
676 return (fds); | |
677 } | |
678 | |
679 | |
680 int | |
681 tty_raw (int fd) | |
682 { | |
683 struct termios buf; | |
684 | |
685 if (tcgetattr (fd, &buf) < 0) | |
686 return (-1); | |
687 | |
688 buf.c_iflag |= IGNPAR; | |
689 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
690 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
691 #ifdef IEXTEN | |
692 buf.c_lflag &= ~(IEXTEN); | |
693 #endif | |
694 | |
695 buf.c_oflag &= ~(OPOST); | |
696 buf.c_cc[VMIN] = 1; | |
697 buf.c_cc[VTIME] = 0; | |
698 | |
699 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
700 return (-1); | |
701 return (0); | |
702 } | |
703 | |
704 | |
16 | 705 static gint |
706 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
707 { | |
708 const gftp_file * f1, * f2; | |
709 | |
710 f1 = a; | |
711 f2 = b; | |
712 return (strcmp (f1->file, f2->file)); | |
713 } | |
714 | |
715 | |
716 static gint | |
717 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
718 { | |
719 const gftp_file * f1, * f2; | |
720 gint ret; | |
721 | |
722 f1 = a; | |
723 f2 = b; | |
724 ret = strcmp (f1->file, f2->file); | |
84 | 725 return (ret * -1); |
16 | 726 } |
727 | |
728 | |
729 static gint | |
730 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
731 { | |
732 const gftp_file * f1, * f2; | |
733 | |
734 f1 = a; | |
735 f2 = b; | |
736 return (strcmp (f1->user, f2->user)); | |
737 } | |
738 | |
739 | |
740 static gint | |
741 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
742 { | |
743 const gftp_file * f1, * f2; | |
744 gint ret; | |
745 | |
746 f1 = a; | |
747 f2 = b; | |
748 ret = strcmp (f1->user, f2->user); | |
84 | 749 return (ret * -1); |
16 | 750 } |
751 | |
752 | |
753 static gint | |
754 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
755 { | |
756 const gftp_file * f1, * f2; | |
757 | |
758 f1 = a; | |
759 f2 = b; | |
760 return (strcmp (f1->group, f2->group)); | |
761 } | |
762 | |
763 | |
764 static gint | |
765 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
766 { | |
767 const gftp_file * f1, * f2; | |
768 gint ret; | |
769 | |
770 f1 = a; | |
771 f2 = b; | |
772 ret = strcmp (f1->group, f2->group); | |
84 | 773 return (ret * -1); |
16 | 774 } |
775 | |
776 | |
777 static gint | |
778 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
779 { | |
780 const gftp_file * f1, * f2; | |
781 | |
782 f1 = a; | |
783 f2 = b; | |
784 return (strcmp (f1->attribs, f2->attribs)); | |
785 } | |
786 | |
787 | |
788 static gint | |
789 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
790 { | |
791 const gftp_file * f1, * f2; | |
792 gint ret; | |
793 | |
794 f1 = a; | |
795 f2 = b; | |
796 ret = strcmp (f1->attribs, f2->attribs); | |
84 | 797 return (ret * -1); |
16 | 798 } |
799 | |
800 | |
801 static gint | |
802 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
803 { | |
804 const gftp_file * f1, * f2; | |
805 | |
806 f1 = a; | |
807 f2 = b; | |
808 if (f1->size < f2->size) | |
809 return (-1); | |
810 else if (f1->size == f2->size) | |
811 return (0); | |
812 else | |
813 return (1); | |
814 } | |
815 | |
816 | |
817 static gint | |
818 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
819 { | |
820 const gftp_file * f1, * f2; | |
821 | |
822 f1 = a; | |
823 f2 = b; | |
824 if (f1->size < f2->size) | |
825 return (1); | |
826 else if (f1->size == f2->size) | |
827 return (0); | |
828 else | |
829 return (-1); | |
830 } | |
831 | |
832 | |
833 static gint | |
834 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
835 { | |
836 const gftp_file * f1, * f2; | |
837 | |
838 f1 = a; | |
839 f2 = b; | |
840 if (f1->datetime < f2->datetime) | |
841 return (-1); | |
842 else if (f1->datetime == f2->datetime) | |
843 return (0); | |
844 else | |
845 return (1); | |
846 } | |
847 | |
848 | |
849 static gint | |
850 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
851 { | |
852 const gftp_file * f1, * f2; | |
853 | |
854 f1 = a; | |
855 f2 = b; | |
856 if (f1->datetime < f2->datetime) | |
857 return (1); | |
858 else if (f1->datetime == f2->datetime) | |
859 return (0); | |
860 else | |
861 return (-1); | |
862 } | |
863 | |
864 | |
865 GList * | |
866 gftp_sort_filelist (GList * filelist, int column, int asds) | |
867 { | |
868 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
869 GCompareFunc sortfunc; | |
870 gftp_file * tempfle; | |
122 | 871 int sort_dirs_first; |
16 | 872 |
873 files = dirs = dotdot = NULL; | |
874 | |
875 if (column == GFTP_SORT_COL_FILE) | |
876 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
877 else if (column == GFTP_SORT_COL_SIZE) | |
878 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
879 else if (column == GFTP_SORT_COL_USER) | |
880 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
881 else if (column == GFTP_SORT_COL_GROUP) | |
882 sortfunc = asds ? | |
883 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
884 else if (column == GFTP_SORT_COL_DATETIME) | |
885 sortfunc = asds ? | |
122 | 886 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
887 else if (column == GFTP_SORT_COL_ATTRIBS) | |
16 | 888 sortfunc = asds ? |
889 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
122 | 890 else /* Don't sort */ |
891 return (filelist); | |
892 | |
893 sort_dirs_first = 1; | |
894 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
16 | 895 |
896 for (tempitem = filelist; tempitem != NULL; ) | |
897 { | |
898 tempfle = tempitem->data; | |
899 insitem = tempitem; | |
900 tempitem = tempitem->next; | |
901 insitem->next = NULL; | |
902 | |
903 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
904 dotdot = insitem; | |
905 else if (sort_dirs_first && tempfle->isdir) | |
906 { | |
907 insitem->next = dirs; | |
908 dirs = insitem; | |
909 } | |
910 else | |
911 { | |
912 insitem->next = files; | |
913 files = insitem; | |
914 } | |
915 } | |
916 | |
917 if (dirs != NULL) | |
918 dirs = g_list_sort (dirs, sortfunc); | |
919 if (files != NULL) | |
920 files = g_list_sort (files, sortfunc); | |
921 | |
922 filelist = dotdot; | |
923 | |
924 if (filelist == NULL) | |
925 filelist = dirs; | |
926 else | |
927 filelist = g_list_concat (filelist, dirs); | |
928 | |
929 if (filelist == NULL) | |
930 filelist = files; | |
931 else | |
932 filelist = g_list_concat (filelist, files); | |
933 | |
39 | 934 /* I haven't check this, but I'm pretty sure some older versions of glib |
935 had a bug that the prev pointer wasn't being sent to NULL */ | |
936 filelist->prev = NULL; | |
16 | 937 return (filelist); |
938 } | |
939 |