1
|
1 /*****************************************************************************/
|
|
2 /* gftp-text.c - text port of gftp */
|
|
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-text.h"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
|
23 static gftp_request * gftp_text_locreq = NULL;
|
|
24 static gftp_request * gftp_text_remreq = NULL;
|
|
25 static volatile int cancel = 0;
|
|
26 static int configuration_changed = 0,
|
|
27 number_commands = 30;
|
|
28
|
|
29 struct _gftp_text_methods gftp_text_methods[] = {
|
|
30 {N_("about"), 2, gftp_text_about, NULL,
|
37
|
31 N_("Shows gFTP information"), NULL},
|
1
|
32 {N_("ascii"), 2, gftp_text_ascii, &gftp_text_remreq,
|
37
|
33 N_("Sets the current file transfer mode to Ascii (only for FTP)"), NULL},
|
1
|
34 {N_("binary"), 1, gftp_text_binary, &gftp_text_remreq,
|
37
|
35 N_("Sets the current file transfer mode to Binary (only for FTP)"), NULL},
|
1
|
36 {N_("cd"), 2, gftp_text_cd, &gftp_text_remreq,
|
37
|
37 N_("Changes the remote working directory"), NULL},
|
1
|
38 {N_("chdir"), 3, gftp_text_cd, &gftp_text_remreq,
|
37
|
39 N_("Changes the remote working directory"), NULL},
|
1
|
40 {N_("chmod"), 3, gftp_text_chmod, &gftp_text_remreq,
|
37
|
41 N_("Changes the permissions of a remote file"), NULL},
|
|
42 {N_("clear"), 3, gftp_text_clear, NULL,
|
|
43 N_("Available options: cache"), gftp_text_clear_show_subhelp},
|
|
44 {N_("close"), 3, gftp_text_close, &gftp_text_remreq,
|
|
45 N_("Disconnects from the remote site"), NULL},
|
1
|
46 {N_("delete"), 1, gftp_text_delete, &gftp_text_remreq,
|
37
|
47 N_("Removes a remote file"), NULL},
|
1
|
48 {N_("get"), 1, gftp_text_mget_file, NULL,
|
37
|
49 N_("Downloads remote file(s)"), NULL},
|
1
|
50 {N_("help"), 1, gftp_text_help, NULL,
|
37
|
51 N_("Shows this help screen"), NULL},
|
1
|
52 {N_("lcd"), 3, gftp_text_cd, &gftp_text_locreq,
|
37
|
53 N_("Changes the local working directory"), NULL},
|
1
|
54 {N_("lchdir"), 4, gftp_text_cd, &gftp_text_locreq,
|
37
|
55 N_("Changes the local working directory"), NULL},
|
1
|
56 {N_("lchmod"), 4, gftp_text_chmod, &gftp_text_locreq,
|
37
|
57 N_("Changes the permissions of a local file"), NULL},
|
1
|
58 {N_("ldelete"), 2, gftp_text_delete, &gftp_text_locreq,
|
37
|
59 N_("Removes a local file"), NULL},
|
1
|
60 {N_("lls"), 2, gftp_text_ls, &gftp_text_locreq,
|
37
|
61 N_("Shows the directory listing for the current local directory"), NULL},
|
1
|
62 {N_("lmkdir"), 2, gftp_text_mkdir, &gftp_text_locreq,
|
37
|
63 N_("Creates a local directory"), NULL},
|
1
|
64 {N_("lpwd"), 2, gftp_text_pwd, &gftp_text_locreq,
|
37
|
65 N_("Show current local directory"), NULL},
|
1
|
66 {N_("lrename"), 3, gftp_text_rename, &gftp_text_locreq,
|
37
|
67 N_("Rename a local file"), NULL},
|
1
|
68 {N_("lrmdir"), 3, gftp_text_rmdir, &gftp_text_locreq,
|
37
|
69 N_("Remove a local directory"), NULL},
|
1
|
70 {N_("ls"), 2, gftp_text_ls, &gftp_text_remreq,
|
37
|
71 N_("Shows the directory listing for the current remote directory"), NULL},
|
1
|
72 {N_("mget"), 2, gftp_text_mget_file, NULL,
|
37
|
73 N_("Downloads remote file(s)"), NULL},
|
1
|
74 {N_("mkdir"), 2, gftp_text_mkdir, &gftp_text_remreq,
|
37
|
75 N_("Creates a remote directory"), NULL},
|
1
|
76 {N_("mput"), 2, gftp_text_mput_file, NULL,
|
37
|
77 N_("Uploads local file(s)"), NULL},
|
1
|
78 {N_("open"), 1, gftp_text_open, &gftp_text_remreq,
|
37
|
79 N_("Opens a connection to a remote site"), NULL},
|
1
|
80 {N_("put"), 2, gftp_text_mput_file, NULL,
|
37
|
81 N_("Uploads local file(s)"), NULL},
|
1
|
82 {N_("pwd"), 2, gftp_text_pwd, &gftp_text_remreq,
|
37
|
83 N_("Show current remote directory"), NULL},
|
1
|
84 {N_("quit"), 1, gftp_text_quit, NULL,
|
37
|
85 N_("Exit from gFTP"), NULL},
|
1
|
86 {N_("rename"), 2, gftp_text_rename, &gftp_text_remreq,
|
37
|
87 N_("Rename a remote file"), NULL},
|
1
|
88 {N_("rmdir"), 2, gftp_text_rmdir, &gftp_text_remreq,
|
37
|
89 N_("Remove a remote directory"), NULL},
|
1
|
90 {N_("set"), 1, gftp_text_set, NULL,
|
37
|
91 N_("Show configuration file variables. You can also set variables by set var=val"), gftp_text_set_show_subhelp},
|
1
|
92 {NULL, 0, NULL, NULL, NULL}};
|
|
93
|
|
94 int
|
|
95 main (int argc, char **argv)
|
|
96 {
|
|
97 gftp_request * request;
|
|
98 size_t len, cmdlen;
|
|
99 char *pos, *stpos;
|
|
100 int i;
|
|
101 #ifdef HAVE_LIBREADLINE
|
|
102 char *tempstr, prompt[20];
|
|
103 #else
|
|
104 char tempstr[512];
|
|
105 #endif
|
|
106
|
|
107 #ifdef HAVE_GETTEXT
|
|
108 setlocale (LC_ALL, "");
|
|
109 bindtextdomain ("gftp", LOCALE_DIR);
|
|
110 textdomain ("gftp");
|
|
111 #endif
|
|
112
|
|
113 signal (SIGCHLD, sig_child);
|
|
114 signal (SIGPIPE, SIG_IGN);
|
|
115
|
|
116 gftp_read_config_file (argv, 0);
|
|
117
|
|
118 /* SSH doesn't support reading the password with askpass via the command
|
|
119 line */
|
|
120 ssh_use_askpass = sshv2_use_sftp_subsys = 0;
|
|
121
|
|
122 if (gftp_parse_command_line (&argc, &argv) != 0)
|
|
123 exit (0);
|
|
124
|
|
125 gftp_text_remreq = gftp_request_new ();
|
|
126 gftp_text_remreq->logging_function = gftp_text_log;
|
|
127
|
|
128 gftp_text_locreq = gftp_request_new ();
|
|
129 gftp_text_locreq->logging_function = gftp_text_log;
|
|
130 gftp_protocols[GFTP_LOCAL_NUM].init (gftp_text_locreq);
|
|
131 if (startup_directory != NULL && *startup_directory != '\0')
|
|
132 gftp_set_directory (gftp_text_locreq, startup_directory);
|
|
133 gftp_connect (gftp_text_locreq);
|
|
134
|
|
135 gftp_text_log (gftp_logging_misc, NULL, "%s, Copyright (C) 1998-2002 Brian Masney <", version);
|
|
136 gftp_text_log (gftp_logging_recv, NULL, "masneyb@gftp.org");
|
|
137 gftp_text_log (gftp_logging_misc, NULL, _(">.\nIf you have any questions, comments, or suggestions about this program, please feel free to email them to me. You can always find out the latest news about gFTP from my website at http://www.gftp.org/\n"));
|
|
138 gftp_text_log (gftp_logging_misc, NULL, "\n");
|
|
139 gftp_text_log (gftp_logging_misc, NULL, _("gFTP comes with ABSOLUTELY NO WARRANTY; for details, see the COPYING file. This is free software, and you are welcome to redistribute it under certain conditions; for details, see the COPYING file\n"));
|
|
140 gftp_text_log (gftp_logging_misc, NULL, "\n");
|
|
141
|
|
142 if (argc == 3 && strcmp (argv[1], "-d") == 0)
|
|
143 {
|
|
144 if ((pos = strrchr (argv[2], '/')) != NULL)
|
|
145 *pos = '\0';
|
|
146 gftp_text_open (gftp_text_remreq, argv[2], NULL);
|
|
147
|
|
148 if (pos != NULL)
|
|
149 *pos = '/';
|
|
150
|
|
151 gftp_text_mget_file (gftp_text_remreq, pos + 1, NULL);
|
|
152 exit (0);
|
|
153 }
|
|
154 else if (argc == 2)
|
|
155 gftp_text_open (gftp_text_remreq, argv[1], NULL);
|
|
156
|
|
157 #ifdef HAVE_LIBREADLINE
|
|
158 g_snprintf (prompt, sizeof (prompt), "%sftp%s> ", COLOR_BLUE, COLOR_DEFAULT);
|
|
159 while ((tempstr = readline (prompt)))
|
|
160 #else
|
|
161 printf ("%sftp%s> ", COLOR_BLUE, COLOR_DEFAULT);
|
|
162 while (fgets (tempstr, sizeof (tempstr), stdin) != NULL)
|
|
163 #endif
|
|
164 {
|
|
165 len = strlen (tempstr);
|
|
166 if (tempstr[len - 1] == '\n')
|
|
167 tempstr[--len] = '\0';
|
|
168 if (tempstr[len - 1] == '\r')
|
|
169 tempstr[--len] = '\0';
|
|
170
|
|
171 for (stpos = tempstr; *stpos == ' '; stpos++);
|
|
172
|
|
173 for (pos = tempstr + len - 1;
|
|
174 (*pos == ' ' || *pos == '\t') && pos > tempstr;
|
|
175 pos--)
|
|
176 *pos = '\0';
|
|
177
|
|
178 if (*stpos == '\0')
|
|
179 {
|
|
180 #ifndef HAVE_LIBREADLINE
|
|
181 printf ("%sftp%s> ", COLOR_BLUE, COLOR_DEFAULT);
|
|
182 #endif
|
|
183 continue;
|
|
184 }
|
|
185
|
|
186 #ifdef HAVE_LIBREADLINE
|
|
187 add_history (tempstr);
|
|
188 #endif
|
|
189
|
|
190 if ((pos = strchr (stpos, ' ')) != NULL)
|
|
191 *pos = '\0';
|
|
192 cmdlen = strlen (stpos);
|
|
193
|
|
194 for (i=0; gftp_text_methods[i].command != NULL; i++)
|
|
195 {
|
|
196 if (strcmp (gftp_text_methods[i].command, stpos) == 0)
|
|
197 break;
|
|
198 else if (cmdlen >= gftp_text_methods[i].minlen &&
|
|
199 strncmp (gftp_text_methods[i].command, stpos, cmdlen) == 0)
|
|
200 break;
|
|
201 }
|
|
202
|
|
203 if (pos != NULL)
|
|
204 pos++;
|
|
205 else
|
|
206 pos = "";
|
|
207
|
|
208 if (gftp_text_methods[i].command != NULL)
|
|
209 {
|
|
210 if (gftp_text_methods[i].request != NULL)
|
|
211 request = *gftp_text_methods[i].request;
|
|
212 else
|
|
213 request = NULL;
|
|
214
|
|
215 if (gftp_text_methods[i].func (request, pos, NULL) == 0)
|
|
216 break;
|
|
217 }
|
|
218 else
|
|
219 gftp_text_log (gftp_logging_error, NULL,
|
|
220 _("Error: Command not recognized\n"));
|
|
221
|
|
222 #ifdef HAVE_LIBREADLINE
|
|
223 free (tempstr);
|
|
224 #else
|
|
225 printf ("%sftp%s> ", COLOR_BLUE, COLOR_DEFAULT);
|
|
226 #endif
|
|
227 }
|
|
228
|
|
229 if (logfd != NULL)
|
|
230 fclose (logfd);
|
|
231 gftp_text_quit (NULL, NULL, NULL);
|
|
232
|
|
233 return (0);
|
|
234 }
|
|
235
|
|
236
|
|
237 void
|
|
238 gftp_text_log (gftp_logging_level level, void *ptr, const char *string, ...)
|
|
239 {
|
|
240 char tempstr[512], *stpos, *endpos;
|
|
241 va_list argp;
|
|
242 int sw;
|
|
243
|
|
244 g_return_if_fail (string != NULL);
|
|
245
|
|
246 switch (level)
|
|
247 {
|
|
248 case gftp_logging_send:
|
|
249 printf ("%s", COLOR_GREEN);
|
|
250 break;
|
|
251 case gftp_logging_recv:
|
|
252 printf ("%s", COLOR_YELLOW);
|
|
253 break;
|
|
254 case gftp_logging_error:
|
|
255 printf ("%s", COLOR_RED);
|
|
256 break;
|
|
257 default:
|
|
258 printf ("%s", COLOR_DEFAULT);
|
|
259 break;
|
|
260 }
|
|
261
|
|
262 va_start (argp, string);
|
|
263 g_vsnprintf (tempstr, sizeof (tempstr), string, argp);
|
|
264 va_end (argp);
|
|
265
|
|
266 if (logfd != NULL)
|
|
267 {
|
|
268 fwrite (tempstr, 1, strlen (tempstr), logfd);
|
|
269 if (ferror (logfd))
|
|
270 {
|
|
271 fclose (logfd);
|
|
272 logfd = NULL;
|
|
273 }
|
|
274 else
|
|
275 fflush (logfd);
|
|
276 }
|
|
277
|
|
278 sw = gftp_text_get_win_size ();
|
|
279 stpos = tempstr;
|
|
280 endpos = tempstr + 1;
|
|
281 do
|
|
282 {
|
|
283 if (strlen (stpos) <= sw)
|
|
284 {
|
|
285 printf ("%s", stpos);
|
|
286 break;
|
|
287 }
|
|
288 for (endpos = stpos + sw - 1; *endpos != ' ' && endpos > stpos; endpos--);
|
|
289 if (endpos != stpos)
|
|
290 {
|
|
291 *endpos = '\0';
|
|
292 }
|
|
293 printf ("%s\n", stpos);
|
|
294 stpos = endpos + 1;
|
|
295 }
|
|
296 while (stpos != endpos);
|
|
297
|
|
298 printf ("%s", COLOR_DEFAULT);
|
|
299 }
|
|
300
|
|
301
|
|
302 int
|
|
303 gftp_text_open (gftp_request * request, char *command, gpointer *data)
|
|
304 {
|
|
305 char tempstr[255], *pos;
|
|
306
|
|
307 if (GFTP_IS_CONNECTED (request))
|
|
308 {
|
|
309 gftp_disconnect (request);
|
|
310 }
|
|
311
|
|
312 if (*command == '\0')
|
|
313 {
|
|
314 gftp_text_log (gftp_logging_error, NULL,
|
|
315 _("usage: open [[ftp://][user:pass@]ftp-site[:port][/directory]]\n"));
|
|
316 return (1);
|
|
317 }
|
|
318
|
|
319 if (gftp_parse_url (request, command) < 0)
|
|
320 {
|
|
321 gftp_text_log (gftp_logging_error, NULL,
|
|
322 _("Could not parse URL %s\n"), command);
|
|
323 return (1);
|
|
324 }
|
|
325
|
|
326 if (strcmp (GFTP_GET_USERNAME (request), "anonymous") == 0)
|
|
327 {
|
|
328 if ((pos = gftp_text_ask_question ("Username [anonymous]", 1, tempstr,
|
|
329 sizeof (tempstr))) != NULL)
|
|
330 {
|
|
331 gftp_set_username (request, pos);
|
|
332 if (request->password)
|
|
333 {
|
|
334 g_free (request->password);
|
|
335 request->password = NULL;
|
|
336 }
|
|
337 }
|
|
338 }
|
|
339
|
|
340 if (strcmp (GFTP_GET_USERNAME (request), "anonymous") != 0 &&
|
|
341 (request->password == NULL || *request->password == '\0'))
|
|
342 {
|
|
343 if ((pos = gftp_text_ask_question ("Password", 0, tempstr,
|
|
344 sizeof (tempstr))) == NULL)
|
|
345 return (1);
|
|
346 gftp_set_password (request, pos);
|
|
347 }
|
|
348
|
|
349 gftp_connect (request);
|
|
350 return (1);
|
|
351 }
|
|
352
|
|
353
|
|
354 int
|
|
355 gftp_text_close (gftp_request * request, char *command, gpointer *data)
|
|
356 {
|
|
357 gftp_disconnect (request);
|
|
358 return (1);
|
|
359 }
|
|
360
|
|
361
|
|
362 int
|
|
363 gftp_text_about (gftp_request * request, char *command, gpointer *data)
|
|
364 {
|
|
365 char *str;
|
|
366
|
|
367 gftp_text_log (gftp_logging_misc, NULL,
|
|
368 "%s. Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org>\n", version);
|
|
369
|
|
370 str = _("Translated by");
|
|
371 if (strcmp (str, "Translated by") != 0)
|
|
372 gftp_text_log (gftp_logging_misc, NULL, "%s\n", str);
|
|
373 return (1);
|
|
374 }
|
|
375
|
|
376
|
|
377 int
|
|
378 gftp_text_quit (gftp_request * request, char *command, gpointer *data)
|
|
379 {
|
|
380 gftp_clear_cache_files ();
|
|
381 if (configuration_changed)
|
|
382 gftp_write_config_file ();
|
|
383 return (0);
|
|
384 }
|
|
385
|
|
386
|
|
387 int
|
|
388 gftp_text_pwd (gftp_request * request, char *command, gpointer *data)
|
|
389 {
|
|
390 if (!GFTP_IS_CONNECTED (request))
|
|
391 {
|
|
392 gftp_text_log (gftp_logging_error, NULL,
|
|
393 _("Error: Not connected to a remote site\n"));
|
|
394 return (1);
|
|
395 }
|
|
396 gftp_text_log (gftp_logging_misc, NULL, "%s\n", request->directory);
|
|
397 return (1);
|
|
398 }
|
|
399
|
|
400
|
|
401 int
|
|
402 gftp_text_cd (gftp_request * request, char *command, gpointer *data)
|
|
403 {
|
|
404 char *newdir = NULL;
|
|
405
|
|
406 if (!GFTP_IS_CONNECTED (request))
|
|
407 {
|
|
408 gftp_text_log (gftp_logging_error, NULL,
|
|
409 _("Error: Not connected to a remote site\n"));
|
|
410 return (1);
|
|
411 }
|
|
412 else if (*command == '\0')
|
|
413 {
|
|
414 gftp_text_log (gftp_logging_error, NULL, _("usage: chdir <directory>\n"));
|
|
415 return (1);
|
|
416 }
|
|
417 else if (request->protonum == GFTP_LOCAL_NUM &&
|
|
418 (newdir = expand_path (command)) == NULL)
|
|
419 {
|
|
420 gftp_text_log (gftp_logging_error, NULL, _("usage: chdir <directory>\n"));
|
|
421 return (1);
|
|
422 }
|
|
423
|
|
424 gftp_set_directory (request, newdir != NULL ? newdir : command);
|
|
425
|
|
426 if (newdir != NULL)
|
|
427 g_free (newdir);
|
|
428
|
|
429 return (1);
|
|
430 }
|
|
431
|
|
432
|
|
433 int
|
|
434 gftp_text_mkdir (gftp_request * request, char *command, gpointer *data)
|
|
435 {
|
|
436 if (!GFTP_IS_CONNECTED (request))
|
|
437 {
|
|
438 gftp_text_log (gftp_logging_error, NULL,
|
|
439 _("Error: Not connected to a remote site\n"));
|
|
440 return (1);
|
|
441 }
|
|
442
|
|
443 if (*command == '\0')
|
|
444 {
|
|
445 gftp_text_log (gftp_logging_error, NULL,
|
|
446 _("usage: mkdir <new directory>\n"));
|
|
447 }
|
|
448 else
|
|
449 {
|
|
450 gftp_make_directory (request, command);
|
|
451 }
|
|
452 return (1);
|
|
453 }
|
|
454
|
|
455
|
|
456 int
|
|
457 gftp_text_rmdir (gftp_request * request, char *command, gpointer *data)
|
|
458 {
|
|
459 if (!GFTP_IS_CONNECTED (request))
|
|
460 {
|
|
461 gftp_text_log (gftp_logging_error, NULL,
|
|
462 _("Error: Not connected to a remote site\n"));
|
|
463 return (1);
|
|
464 }
|
|
465
|
|
466 if (*command == '\0')
|
|
467 {
|
|
468 gftp_text_log (gftp_logging_error, NULL, _("usage: rmdir <directory>\n"));
|
|
469 }
|
|
470 else
|
|
471 {
|
|
472 gftp_remove_directory (request, command);
|
|
473 }
|
|
474 return (1);
|
|
475 }
|
|
476
|
|
477
|
|
478 int
|
|
479 gftp_text_delete (gftp_request * request, char *command, gpointer *data)
|
|
480 {
|
|
481 if (!GFTP_IS_CONNECTED (request))
|
|
482 {
|
|
483 gftp_text_log (gftp_logging_error, NULL,
|
|
484 _("Error: Not connected to a remote site\n"));
|
|
485 return (1);
|
|
486 }
|
|
487
|
|
488 if (*command == '\0')
|
|
489 {
|
|
490 gftp_text_log (gftp_logging_error, NULL,_("usage: delete <file>\n"));
|
|
491 }
|
|
492 else
|
|
493 {
|
|
494 gftp_remove_file (request, command);
|
|
495 }
|
|
496 return (1);
|
|
497 }
|
|
498
|
|
499
|
|
500 int
|
|
501 gftp_text_rename (gftp_request * request, char *command, gpointer *data)
|
|
502 {
|
|
503 char *pos;
|
|
504
|
|
505 if (!GFTP_IS_CONNECTED (request))
|
|
506 {
|
|
507 gftp_text_log (gftp_logging_error, NULL,
|
|
508 _("Error: Not connected to a remote site\n"));
|
|
509 return (1);
|
|
510 }
|
|
511
|
|
512 if ((pos = strchr (command, ' ')) != NULL)
|
|
513 *pos++ = '\0';
|
|
514
|
|
515 if (*command == '\0' || pos == NULL || *pos == '\0')
|
|
516 {
|
|
517 gftp_text_log (gftp_logging_error, NULL,
|
|
518 _("usage: rename <old name> <new name>\n"));
|
|
519 }
|
|
520 else
|
|
521 {
|
|
522 gftp_rename_file (request, command, pos);
|
|
523 }
|
|
524 return (1);
|
|
525 }
|
|
526
|
|
527
|
|
528 int
|
|
529 gftp_text_chmod (gftp_request * request, char *command, gpointer *data)
|
|
530 {
|
|
531 char *pos;
|
|
532
|
|
533 if (!GFTP_IS_CONNECTED (request))
|
|
534 {
|
|
535 gftp_text_log (gftp_logging_error, NULL,
|
|
536 _("Error: Not connected to a remote site\n"));
|
|
537 return (1);
|
|
538 }
|
|
539
|
|
540 if ((pos = strchr (command, ' ')) != NULL)
|
|
541 *pos++ = '\0';
|
|
542
|
|
543 if (*command == '\0' || pos == NULL || *pos == '\0')
|
|
544 {
|
|
545 gftp_text_log (gftp_logging_error, NULL,
|
|
546 _("usage: chmod <mode> <file>\n"));
|
|
547 }
|
|
548 else
|
|
549 {
|
|
550 gftp_chmod (request, pos, strtol (command, NULL, 10));
|
|
551 }
|
|
552 return (1);
|
|
553 }
|
|
554
|
|
555
|
|
556 int
|
|
557 gftp_text_ls (gftp_request * request, char *command, gpointer *data)
|
|
558 {
|
37
|
559 GList * files, * templist, * delitem;
|
1
|
560 char *color, buf[20], *filespec;
|
37
|
561 int sortcol, sortasds;
|
|
562 gftp_file * fle;
|
1
|
563 time_t curtime;
|
|
564
|
|
565 time (&curtime);
|
|
566 if (!GFTP_IS_CONNECTED (request))
|
|
567 {
|
|
568 gftp_text_log (gftp_logging_error, NULL,
|
|
569 _("Error: Not connected to a remote site\n"));
|
|
570 return (1);
|
|
571 }
|
|
572
|
|
573 filespec = *command != '\0' ? command : NULL;
|
|
574 if (gftp_list_files (request) != 0)
|
|
575 return (1);
|
|
576
|
37
|
577 files = NULL;
|
|
578 fle = g_malloc0 (sizeof (*fle));
|
|
579 while (gftp_get_next_file (request, NULL, fle) > 0)
|
1
|
580 {
|
37
|
581 if (strcmp (fle->file, ".") == 0)
|
1
|
582 {
|
37
|
583 gftp_file_destroy (fle);
|
1
|
584 continue;
|
|
585 }
|
37
|
586 files = g_list_prepend (files, fle);
|
|
587 fle = g_malloc0 (sizeof (*fle));
|
|
588 }
|
|
589 g_free (fle);
|
1
|
590
|
58
|
591 if (files == NULL)
|
|
592 return (1);
|
|
593
|
37
|
594 if (request == gftp_text_locreq)
|
|
595 {
|
|
596 sortcol = local_sortcol;
|
|
597 sortasds = local_sortasds;
|
|
598 }
|
|
599 else
|
|
600 {
|
|
601 sortcol = remote_sortcol;
|
|
602 sortasds = remote_sortasds;
|
|
603 }
|
|
604
|
|
605 files = gftp_sort_filelist (files, sortcol, sortasds);
|
|
606 delitem = NULL;
|
|
607 for (templist = files; templist != NULL; templist = templist->next)
|
|
608 {
|
|
609 if (delitem != NULL)
|
|
610 {
|
|
611 fle = delitem->data;
|
|
612 gftp_file_destroy (fle);
|
|
613 g_free (fle);
|
|
614 delitem = NULL;
|
|
615 }
|
|
616
|
|
617 fle = templist->data;
|
|
618
|
|
619 if (*fle->attribs == 'd')
|
1
|
620 color = COLOR_BLUE;
|
37
|
621 else if (*fle->attribs == 'l')
|
1
|
622 color = COLOR_WHITE;
|
37
|
623 else if (strchr (fle->attribs, 'x') != NULL)
|
1
|
624 color = COLOR_GREEN;
|
|
625 else
|
|
626 color = COLOR_DEFAULT;
|
|
627
|
37
|
628 if (curtime > fle->datetime + 6 * 30 * 24 * 60 * 60 ||
|
|
629 curtime < fle->datetime - 60 * 60)
|
|
630 strftime (buf, sizeof (buf), "%b %d %Y", localtime (&fle->datetime));
|
1
|
631 else
|
37
|
632 strftime (buf, sizeof (buf), "%b %d %H:%M", localtime (&fle->datetime));
|
1
|
633
|
16
|
634 #if defined (_LARGEFILE_SOURCE)
|
|
635 printf ("%s %8s %8s %10lld %s %s%s%s\n",
|
|
636 #else
|
|
637 printf ("%s %8s %8s %10ld %s %s%s%s\n",
|
|
638 #endif
|
37
|
639 fle->attribs, fle->user, fle->group,
|
|
640 fle->size, buf, color, fle->file, COLOR_DEFAULT);
|
|
641 delitem = templist;
|
1
|
642 }
|
|
643 gftp_end_transfer (request);
|
37
|
644
|
|
645 if (delitem != NULL)
|
|
646 {
|
|
647 fle = delitem->data;
|
|
648 gftp_file_destroy (fle);
|
|
649 g_free (fle);
|
|
650 delitem = NULL;
|
|
651 }
|
|
652
|
|
653 if (files != NULL)
|
|
654 g_list_free (files);
|
|
655
|
1
|
656 return (1);
|
|
657 }
|
|
658
|
|
659
|
|
660 int
|
|
661 gftp_text_binary (gftp_request * request, char *command, gpointer *data)
|
|
662 {
|
|
663 if (!GFTP_IS_CONNECTED (gftp_text_remreq))
|
|
664 {
|
|
665 gftp_text_log (gftp_logging_error, NULL,
|
|
666 _("Error: Not connected to a remote site\n"));
|
|
667 return (1);
|
|
668 }
|
|
669
|
|
670 gftp_set_data_type (gftp_text_remreq, GFTP_TYPE_BINARY);
|
|
671 gftp_set_data_type (gftp_text_locreq, GFTP_TYPE_BINARY);
|
|
672 return (1);
|
|
673 }
|
|
674
|
|
675
|
|
676 int
|
|
677 gftp_text_ascii (gftp_request * request, char *command, gpointer *data)
|
|
678 {
|
|
679 if (!GFTP_IS_CONNECTED (gftp_text_remreq))
|
|
680 {
|
|
681 gftp_text_log (gftp_logging_error, NULL,
|
|
682 _("Error: Not connected to a remote site\n"));
|
|
683 return (1);
|
|
684 }
|
|
685
|
|
686 gftp_set_data_type (gftp_text_remreq, GFTP_TYPE_ASCII);
|
|
687 gftp_set_data_type (gftp_text_locreq, GFTP_TYPE_ASCII);
|
|
688 return (1);
|
|
689 }
|
|
690
|
|
691
|
|
692 int
|
|
693 gftp_text_mget_file (gftp_request * request, char *command, gpointer *data)
|
|
694 {
|
|
695 gftp_transfer * transfer;
|
|
696 gftp_file * fle;
|
|
697
|
|
698 if (!GFTP_IS_CONNECTED (gftp_text_remreq))
|
|
699 {
|
|
700 gftp_text_log (gftp_logging_error, NULL,
|
|
701 _("Error: Not connected to a remote site\n"));
|
|
702 return (1);
|
|
703 }
|
|
704
|
|
705 if (*command == '\0')
|
|
706 {
|
|
707 gftp_text_log (gftp_logging_error, NULL, _("usage: mget <filespec>\n"));
|
|
708 return (1);
|
|
709 }
|
|
710
|
|
711 transfer = g_malloc0 (sizeof (*transfer));
|
|
712 transfer->fromreq = gftp_text_remreq;
|
|
713 transfer->toreq = gftp_text_locreq;
|
|
714 transfer->transfer_direction = GFTP_DIRECTION_DOWNLOAD;
|
|
715
|
|
716 /* FIXME - ask whether to resume/skip/overwrite */
|
|
717 if (gftp_list_files (transfer->fromreq) != 0)
|
|
718 {
|
|
719 transfer->fromreq = transfer->toreq = NULL;
|
|
720 free_tdata (transfer);
|
|
721 return (1);
|
|
722 }
|
|
723 fle = g_malloc0 (sizeof (*fle));
|
|
724 while (gftp_get_next_file (transfer->fromreq, command, fle) > 0)
|
|
725 {
|
|
726 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0)
|
|
727 {
|
|
728 gftp_file_destroy (fle);
|
|
729 continue;
|
|
730 }
|
|
731 transfer->files = g_list_append (transfer->files, fle);
|
|
732 fle = g_malloc (sizeof (*fle));
|
|
733 }
|
|
734 g_free (fle);
|
|
735 gftp_end_transfer (transfer->fromreq);
|
|
736
|
|
737 if (transfer->files == NULL)
|
|
738 {
|
|
739 transfer->fromreq = transfer->toreq = NULL;
|
|
740 free_tdata (transfer);
|
|
741 return (1);
|
|
742 }
|
|
743
|
|
744 if (gftp_get_all_subdirs (transfer, NULL) != 0)
|
|
745 {
|
|
746 transfer->fromreq = transfer->toreq = NULL;
|
|
747 free_tdata (transfer);
|
|
748 return (1);
|
|
749 }
|
|
750
|
|
751 if (transfer->files == NULL)
|
|
752 {
|
|
753 transfer->fromreq = transfer->toreq = NULL;
|
|
754 free_tdata (transfer);
|
|
755 return (1);
|
|
756 }
|
|
757
|
|
758 gftp_text_transfer_files (transfer);
|
|
759 transfer->fromreq = transfer->toreq = NULL;
|
|
760 free_tdata (transfer);
|
|
761 return (1);
|
|
762 }
|
|
763
|
|
764
|
|
765 int
|
|
766 gftp_text_mput_file (gftp_request * request, char *command, gpointer *data)
|
|
767 {
|
|
768 gftp_transfer * transfer;
|
|
769 gftp_file * fle;
|
|
770
|
|
771 if (!GFTP_IS_CONNECTED (gftp_text_remreq))
|
|
772 {
|
|
773 gftp_text_log (gftp_logging_error, NULL,
|
|
774 _("Error: Not connected to a remote site\n"));
|
|
775 return (1);
|
|
776 }
|
|
777
|
|
778 if (*command == '\0')
|
|
779 {
|
|
780 gftp_text_log (gftp_logging_error, NULL, _("usage: mput <filespec>\n"));
|
|
781 return (1);
|
|
782 }
|
|
783
|
|
784 transfer = g_malloc0 (sizeof (*transfer));
|
|
785 transfer->fromreq = gftp_text_locreq;
|
|
786 transfer->toreq = gftp_text_remreq;
|
|
787 transfer->transfer_direction = GFTP_DIRECTION_UPLOAD;
|
|
788
|
|
789 if (gftp_list_files (transfer->fromreq) != 0)
|
|
790 {
|
|
791 transfer->fromreq = transfer->toreq = NULL;
|
|
792 free_tdata (transfer);
|
|
793 return (1);
|
|
794 }
|
|
795 fle = g_malloc (sizeof (*fle));
|
|
796 while (gftp_get_next_file (transfer->fromreq, command, fle) > 0)
|
|
797 {
|
|
798 if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0)
|
|
799 {
|
|
800 gftp_file_destroy (fle);
|
|
801 continue;
|
|
802 }
|
|
803 transfer->files = g_list_append (transfer->files, fle);
|
|
804 fle = g_malloc (sizeof (*fle));
|
|
805 }
|
|
806 g_free (fle);
|
|
807 gftp_end_transfer (transfer->fromreq);
|
|
808
|
|
809 if (transfer->files == NULL)
|
|
810 {
|
|
811 transfer->fromreq = transfer->toreq = NULL;
|
|
812 free_tdata (transfer);
|
|
813 return (1);
|
|
814 }
|
|
815
|
|
816 if (gftp_get_all_subdirs (transfer, NULL) != 0)
|
|
817 {
|
|
818 transfer->fromreq = transfer->toreq = NULL;
|
|
819 free_tdata (transfer);
|
|
820 return (1);
|
|
821 }
|
|
822
|
|
823 if (transfer->files == NULL)
|
|
824 {
|
|
825 transfer->fromreq = transfer->toreq = NULL;
|
|
826 free_tdata (transfer);
|
|
827 return (1);
|
|
828 }
|
|
829
|
|
830 gftp_text_transfer_files (transfer);
|
|
831 transfer->fromreq = transfer->toreq = NULL;
|
|
832 free_tdata (transfer);
|
|
833 return (1);
|
|
834 }
|
|
835
|
|
836
|
|
837 int
|
|
838 gftp_text_transfer_files (gftp_transfer * transfer)
|
|
839 {
|
|
840 char *tempstr, buf[8192], *progress = "|/-\\";
|
|
841 struct timeval updatetime;
|
|
842 long fromsize, total;
|
|
843 gftp_file * curfle;
|
|
844 int i, j, sw, tot;
|
|
845 ssize_t num_read;
|
|
846
|
|
847 for (transfer->curfle = transfer->files;
|
|
848 transfer->curfle != NULL;
|
|
849 transfer->curfle = transfer->curfle->next)
|
|
850 {
|
|
851 curfle = transfer->curfle->data;
|
|
852 if (curfle->transfer_action == GFTP_TRANS_ACTION_SKIP)
|
|
853 continue;
|
|
854
|
|
855 if (curfle->isdir && transfer->toreq->mkdir != NULL)
|
|
856 {
|
|
857 transfer->toreq->mkdir (transfer->toreq, curfle->destfile);
|
|
858 continue;
|
|
859 }
|
|
860
|
|
861 if (maxkbs > 0)
|
|
862 {
|
|
863 gftp_text_log (gftp_logging_misc, NULL,
|
|
864 _("File transfer will be throttled to %.2f KB/s\n"),
|
|
865 maxkbs);
|
|
866 }
|
|
867
|
|
868 transfer->curtrans = curfle->startsize;
|
58
|
869 fromsize = gftp_transfer_file (transfer->fromreq, curfle->file, -1,
|
1
|
870 curfle->startsize, transfer->toreq, curfle->destfile,
|
58
|
871 -1, curfle->startsize);
|
1
|
872 if (fromsize < 0)
|
|
873 return (1);
|
|
874
|
|
875 gettimeofday (&transfer->starttime, NULL);
|
|
876 memcpy (&transfer->lasttime, &transfer->starttime,
|
|
877 sizeof (transfer->lasttime));
|
|
878 memset (&updatetime, 0, sizeof (updatetime));
|
|
879
|
|
880 total = 0;
|
|
881 i = 0;
|
|
882 while (!cancel && (num_read = gftp_get_next_file_chunk (transfer->fromreq,
|
|
883 buf, sizeof (buf))) > 0)
|
|
884 {
|
|
885 printf ("\r%c ", progress[i++]);
|
|
886 fflush (stdout);
|
|
887 if (progress[i] == '\0')
|
|
888 i = 0;
|
|
889
|
|
890 total += num_read;
|
|
891 gftp_text_calc_kbs (transfer, num_read);
|
|
892 if (transfer->lasttime.tv_sec - updatetime.tv_sec >= 1 || total >= fromsize)
|
|
893 {
|
|
894 sw = gftp_text_get_win_size () - 20;
|
|
895 tot = (float) total / (float) fromsize * (float) sw;
|
|
896
|
|
897 if (tot > sw)
|
|
898 tot = sw;
|
|
899 printf ("[");
|
|
900 for (j=0; j<tot; j++)
|
|
901 printf ("=");
|
|
902 for (j=0; j<sw-tot; j++)
|
|
903 printf (" ");
|
|
904 printf ("] @ %.2fKB/s", transfer->kbs);
|
|
905 fflush (stdout);
|
|
906 memcpy (&updatetime, &transfer->lasttime, sizeof (updatetime));
|
|
907 }
|
|
908
|
|
909 if (GFTP_GET_DATA_TYPE (transfer->fromreq) == GFTP_TYPE_ASCII)
|
|
910 tempstr = gftp_convert_ascii (buf, &num_read, 1);
|
|
911 else
|
|
912 tempstr = buf;
|
|
913
|
|
914 if (gftp_put_next_file_chunk (transfer->toreq, tempstr, num_read) < 0)
|
|
915 {
|
|
916 num_read = -1;
|
|
917 break;
|
|
918 }
|
|
919
|
|
920 /* We don't have to free tempstr for a download because new memory is
|
|
921 not allocated for it in that case */
|
|
922 if (GFTP_GET_DATA_TYPE (transfer->fromreq) ==
|
|
923 GFTP_TYPE_ASCII && !transfer->transfer_direction)
|
|
924 g_free (tempstr);
|
|
925 }
|
|
926 printf ("\n");
|
|
927
|
|
928 if (num_read < 0)
|
|
929 {
|
|
930 gftp_text_log (gftp_logging_misc, NULL,
|
|
931 _("Could not download %s\n"), curfle->file);
|
|
932 gftp_disconnect (transfer->fromreq);
|
|
933 gftp_disconnect (transfer->toreq);
|
|
934 }
|
|
935 else
|
|
936 {
|
|
937 gftp_text_log (gftp_logging_misc, NULL,
|
|
938 _("Successfully transferred %s\n"), curfle->file);
|
|
939 gftp_end_transfer (transfer->fromreq);
|
|
940 gftp_end_transfer (transfer->toreq);
|
|
941 }
|
|
942
|
|
943 }
|
|
944 return (1);
|
|
945 }
|
|
946
|
|
947
|
|
948 int
|
|
949 gftp_text_help (gftp_request * request, char *command, gpointer *data)
|
|
950 {
|
37
|
951 int i, j, ele, numrows, numcols = 6, handled;
|
|
952 char *pos;
|
1
|
953
|
|
954 if (command != NULL && *command != '\0')
|
|
955 {
|
37
|
956 for (pos = command; *pos != ' ' && *pos != '\0'; pos++);
|
|
957 if (*pos == ' ')
|
|
958 {
|
|
959 *pos++ = '\0';
|
|
960 if (*pos == '\0')
|
|
961 pos = NULL;
|
|
962 }
|
|
963 else
|
|
964 pos = NULL;
|
|
965
|
1
|
966 for (i=0; gftp_text_methods[i].command != NULL; i++)
|
|
967 {
|
|
968 if (strcmp (gftp_text_methods[i].command, command) == 0)
|
|
969 break;
|
|
970 }
|
|
971
|
|
972 if (gftp_text_methods[i].cmd_description != NULL)
|
37
|
973 {
|
|
974 if (pos != NULL && gftp_text_methods[i].subhelp_func != NULL)
|
|
975 handled = gftp_text_methods[i].subhelp_func (pos);
|
|
976 else
|
|
977 handled = 0;
|
|
978
|
|
979 if (!handled)
|
|
980 printf ("%s\n", _(gftp_text_methods[i].cmd_description));
|
|
981 }
|
1
|
982 else
|
|
983 *command = '\0';
|
|
984 }
|
|
985
|
|
986 if (command == NULL || *command == '\0')
|
|
987 {
|
|
988 numrows = number_commands / numcols;
|
|
989 if (number_commands % numcols != 0)
|
|
990 numrows++;
|
|
991
|
|
992 printf (_("Supported commands:\n\n"));
|
|
993 for (i=0; i<numrows; i++)
|
|
994 {
|
|
995 printf (" ");
|
|
996 for (j=0; j<numcols; j++)
|
|
997 {
|
|
998 ele = i + j * numrows;
|
|
999 if (ele >= number_commands)
|
|
1000 break;
|
|
1001 printf ("%-10s", gftp_text_methods[ele].command);
|
|
1002 }
|
|
1003 printf ("\n");
|
|
1004 }
|
|
1005
|
|
1006 printf ("\n");
|
|
1007 }
|
|
1008 return (1);
|
|
1009 }
|
|
1010
|
|
1011
|
|
1012 int
|
|
1013 gftp_text_set (gftp_request * request, char *command, gpointer *data)
|
|
1014 {
|
|
1015 char *pos, *backpos;
|
|
1016 int i;
|
|
1017
|
|
1018 if (command == NULL || *command == '\0')
|
|
1019 {
|
|
1020 for (i=0; config_file_vars[i].key != NULL; i++)
|
|
1021 {
|
37
|
1022 if (!(config_file_vars[i].ports_shown & GFTP_PORT_TEXT))
|
|
1023 continue;
|
|
1024
|
1
|
1025 switch (config_file_vars[i].type)
|
|
1026 {
|
|
1027 case CONFIG_CHARTEXT:
|
|
1028 printf ("%s = %s\n", config_file_vars[i].key,
|
|
1029 *(char **) config_file_vars[i].var);
|
|
1030 break;
|
|
1031 case CONFIG_INTTEXT:
|
|
1032 case CONFIG_CHECKBOX:
|
|
1033 printf ("%s = %d\n", config_file_vars[i].key,
|
|
1034 *(int *) config_file_vars[i].var);
|
|
1035 break;
|
|
1036 case CONFIG_FLOATTEXT:
|
|
1037 printf ("%s = %.2f\n", config_file_vars[i].key,
|
|
1038 *(float *) config_file_vars[i].var);
|
|
1039 break;
|
|
1040 }
|
|
1041 }
|
|
1042 }
|
|
1043 else
|
|
1044 {
|
|
1045 if ((pos = strchr (command, '=')) == NULL)
|
|
1046 {
|
|
1047 gftp_text_log (gftp_logging_error, NULL,
|
|
1048 _("usage: set [variable = value]\n"));
|
|
1049 return (1);
|
|
1050 }
|
|
1051 *pos = '\0';
|
|
1052
|
|
1053 for (backpos = pos - 1;
|
|
1054 (*backpos == ' ' || *backpos == '\t') && backpos > command;
|
|
1055 backpos--)
|
|
1056 *backpos = '\0';
|
|
1057 for (++pos; *pos == ' ' || *pos == '\t'; pos++);
|
|
1058
|
|
1059 for (i=0; config_file_vars[i].key != NULL; i++)
|
|
1060 {
|
|
1061 if (strcmp (config_file_vars[i].key, command) == 0)
|
|
1062 break;
|
37
|
1063
|
1
|
1064 }
|
|
1065
|
|
1066 if (config_file_vars[i].key == NULL)
|
|
1067 {
|
|
1068 gftp_text_log (gftp_logging_error, NULL,
|
|
1069 _("Error: Variable %s is not a valid configuration variable.\n"), command);
|
|
1070 return (1);
|
|
1071 }
|
|
1072
|
37
|
1073 if (!(config_file_vars[i].ports_shown & GFTP_PORT_TEXT))
|
|
1074 {
|
|
1075 gftp_text_log (gftp_logging_error, NULL,
|
|
1076 _("Error: Variable %s is not available in the text port of gFTP\n"), command);
|
|
1077 return (1);
|
|
1078 }
|
|
1079
|
1
|
1080 configuration_changed = 1;
|
|
1081 switch (config_file_vars[i].type)
|
|
1082 {
|
|
1083 case CONFIG_CHARTEXT:
|
|
1084 if (*(char **) config_file_vars[i].var != NULL)
|
|
1085 g_free (*(char **) config_file_vars[i].var);
|
|
1086 *(char **) config_file_vars[i].var = g_strconcat (pos, NULL);
|
|
1087 break;
|
|
1088 case CONFIG_CHECKBOX:
|
|
1089 *(int *) config_file_vars[i].var = *pos == '1' ? 1 : 0;
|
|
1090 break;
|
|
1091 case CONFIG_INTTEXT:
|
|
1092 *(int *) config_file_vars[i].var = strtol (pos, NULL, 10);
|
|
1093 break;
|
|
1094 case CONFIG_FLOATTEXT:
|
|
1095 *(float *) config_file_vars[i].var = strtod (pos, NULL);
|
|
1096 break;
|
|
1097 default:
|
|
1098 gftp_text_log (gftp_logging_error, NULL,
|
|
1099 _("Error: You cannot change this variable\n"));
|
|
1100 break;
|
|
1101 }
|
|
1102 }
|
|
1103
|
|
1104 return (1);
|
|
1105 }
|
|
1106
|
|
1107
|
37
|
1108 int
|
|
1109 gftp_text_clear (gftp_request * request, char *command, gpointer *data)
|
|
1110 {
|
|
1111 if (strcasecmp (command, "cache") == 0)
|
|
1112 gftp_clear_cache_files ();
|
|
1113 else
|
|
1114 gftp_text_log (gftp_logging_error, NULL, "Invalid argument\n");
|
|
1115 return (1);
|
|
1116 }
|
|
1117
|
|
1118
|
1
|
1119 char *
|
|
1120 gftp_text_ask_question (const char *question, int echo, char *buf, size_t size)
|
|
1121 {
|
|
1122 struct termios term, oldterm;
|
|
1123 sigset_t sig, sigsave;
|
|
1124 char *pos, *termname;
|
|
1125 FILE *infd, *outfd;
|
|
1126
|
|
1127 if (!echo)
|
|
1128 {
|
|
1129 sigemptyset (&sig);
|
|
1130 sigaddset (&sig, SIGINT);
|
|
1131 sigaddset (&sig, SIGTSTP);
|
|
1132 sigprocmask (SIG_BLOCK, &sig, &sigsave);
|
|
1133
|
|
1134 termname = ctermid (NULL);
|
|
1135 if ((infd = fopen (termname, "r+")) == NULL)
|
|
1136 {
|
|
1137
|
|
1138 gftp_text_log (gftp_logging_error, NULL,
|
|
1139 "Cannot open controlling terminal %s\n", termname);
|
|
1140 return (NULL);
|
|
1141 }
|
|
1142 outfd = infd;
|
|
1143
|
|
1144 tcgetattr (0, &term);
|
|
1145 oldterm = term;
|
|
1146 term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
|
|
1147 tcsetattr (fileno (infd), TCSAFLUSH, &term);
|
|
1148 }
|
|
1149 else
|
|
1150 {
|
|
1151 infd = stdin;
|
|
1152 outfd = stdout;
|
|
1153 }
|
|
1154
|
|
1155 fprintf (outfd, "%s%s%s: ", COLOR_BLUE, question, COLOR_DEFAULT);
|
|
1156
|
|
1157 if (fgets (buf, size, infd) == NULL)
|
|
1158 return (NULL);
|
|
1159 buf[size - 1] = '\0';
|
|
1160
|
|
1161 if (!echo)
|
|
1162 {
|
|
1163 fprintf (outfd, "\n");
|
|
1164 tcsetattr (fileno (infd), TCSAFLUSH, &oldterm);
|
|
1165 fclose (outfd);
|
|
1166 sigprocmask (SIG_SETMASK, &sigsave, NULL);
|
|
1167 }
|
|
1168
|
|
1169 for (pos = buf + strlen (buf) - 1; *pos == ' ' || *pos == '\r' ||
|
|
1170 *pos == '\n'; pos--);
|
|
1171 *(pos+1) = '\0';
|
|
1172
|
|
1173 for (pos = buf; *pos == ' '; pos++);
|
|
1174 if (*pos == '\0')
|
|
1175 return (NULL);
|
|
1176
|
|
1177 return (pos);
|
|
1178 }
|
|
1179
|
|
1180
|
|
1181 int
|
|
1182 gftp_text_get_win_size (void)
|
|
1183 {
|
|
1184 struct winsize size;
|
|
1185 int ret;
|
|
1186
|
|
1187 if (ioctl (0, TIOCGWINSZ, (char *) &size) < 0)
|
|
1188 ret = 80;
|
|
1189 else
|
|
1190 ret = size.ws_col;
|
|
1191 return (ret);
|
|
1192 }
|
|
1193
|
|
1194
|
|
1195 void
|
|
1196 gftp_text_calc_kbs (gftp_transfer * tdata, ssize_t num_read)
|
|
1197 {
|
|
1198 unsigned long waitusecs;
|
|
1199 double difftime, curkbs;
|
|
1200 gftp_file * tempfle;
|
|
1201 struct timeval tv;
|
|
1202 unsigned long toadd;
|
|
1203
|
|
1204 gettimeofday (&tv, NULL);
|
|
1205
|
|
1206 tempfle = tdata->curfle->data;
|
|
1207 tdata->trans_bytes += num_read;
|
|
1208 tdata->curtrans += num_read;
|
|
1209 tdata->stalled = 0;
|
|
1210
|
|
1211 difftime = (tv.tv_sec - tdata->starttime.tv_sec) + ((double) (tv.tv_usec - tdata->starttime.tv_usec) / 1000000.0);
|
|
1212 if (difftime == 0)
|
|
1213 tdata->kbs = (double) tdata->trans_bytes / 1024.0;
|
|
1214 else
|
|
1215 tdata->kbs = (double) tdata->trans_bytes / 1024.0 / difftime;
|
|
1216
|
|
1217 difftime = (tv.tv_sec - tdata->lasttime.tv_sec) + ((double) (tv.tv_usec - tdata->lasttime.tv_usec) / 1000000.0);
|
|
1218
|
|
1219 if (difftime <= 0)
|
|
1220 curkbs = (double) (num_read / 1024.0);
|
|
1221 else
|
|
1222 curkbs = (double) (num_read / 1024.0 / difftime);
|
|
1223
|
|
1224 if (tdata->fromreq->maxkbs > 0 &&
|
|
1225 curkbs > tdata->fromreq->maxkbs)
|
|
1226 {
|
|
1227 waitusecs = (double) num_read / 1024.0 / tdata->fromreq->maxkbs * 1000000.0 - difftime;
|
|
1228
|
|
1229 if (waitusecs > 0)
|
|
1230 {
|
|
1231 difftime += ((double) waitusecs / 1000000.0);
|
|
1232 usleep (waitusecs);
|
|
1233 }
|
|
1234 }
|
|
1235
|
|
1236 /* I don't call gettimeofday (&tdata->lasttime) here because this will use
|
|
1237 less system resources. This will be close enough for what we need */
|
|
1238 difftime += tdata->lasttime.tv_usec / 1000000.0;
|
|
1239 toadd = (long) difftime;
|
|
1240 difftime -= toadd;
|
|
1241 tdata->lasttime.tv_sec += toadd;
|
|
1242 tdata->lasttime.tv_usec = difftime * 1000000.0;
|
|
1243 }
|
|
1244
|
|
1245
|
|
1246 void
|
|
1247 sig_child (int signo)
|
|
1248 {
|
|
1249 }
|
|
1250
|
|
1251
|
37
|
1252 int
|
|
1253 gftp_text_set_show_subhelp (char *topic)
|
|
1254 {
|
|
1255 int i;
|
|
1256
|
|
1257 for (i=0; config_file_vars[i].key != NULL; i++)
|
|
1258 {
|
|
1259 if (strcmp (topic, config_file_vars[i].key) == 0)
|
|
1260 {
|
|
1261 printf ("%s\n", config_file_vars[i].comment);
|
|
1262 return (1);
|
|
1263 }
|
|
1264 }
|
|
1265
|
|
1266 return (0);
|
|
1267 }
|
|
1268
|
|
1269
|
|
1270 int
|
|
1271 gftp_text_clear_show_subhelp (char *topic)
|
|
1272 {
|
|
1273 if (strcmp (topic, "cache") == 0)
|
|
1274 {
|
|
1275 printf (_("Clear the directory cache\n"));
|
|
1276 return (1);
|
|
1277 }
|
|
1278
|
|
1279 return (0);
|
|
1280 }
|
|
1281
|
|
1282
|
1
|
1283 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR)
|
|
1284
|
|
1285 struct hostent *
|
|
1286 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop)
|
|
1287 {
|
|
1288 struct hostent *hent;
|
|
1289
|
|
1290 if ((hent = gethostbyname (name)) == NULL)
|
|
1291 {
|
|
1292 if (h_errnop)
|
|
1293 *h_errnop = h_errno;
|
|
1294 }
|
|
1295 else
|
|
1296 {
|
|
1297 *result_buf = *hent;
|
|
1298 hent = result_buf;
|
|
1299 }
|
|
1300 return (hent);
|
|
1301 }
|
|
1302
|
56
|
1303 #endif /* HAVE_GETADDRINFO */
|
|
1304
|
1
|
1305
|
|
1306 struct servent *
|
|
1307 r_getservbyname (const char *name, const char *proto,
|
|
1308 struct servent *result_buf, int *h_errnop)
|
|
1309 {
|
|
1310 struct servent *sent;
|
|
1311
|
|
1312 if ((sent = getservbyname (name, proto)) == NULL)
|
|
1313 {
|
|
1314 if (h_errnop)
|
|
1315 *h_errnop = h_errno;
|
|
1316 }
|
|
1317 else
|
|
1318 {
|
|
1319 *result_buf = *sent;
|
|
1320 sent = result_buf;
|
|
1321 }
|
|
1322 return (sent);
|
|
1323 }
|
|
1324
|