2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
24 * In PSFTP our selects are synchronous, so these functions are
27 int uxsel_input_add(int fd, int rwx) { return 0; }
28 void uxsel_input_remove(int id) { }
30 char *x_get_default(const char *key)
32 return NULL; /* this is a stub */
35 void platform_get_x11_auth(char *display, int *protocol,
36 unsigned char *data, int *datalen)
38 /* Do nothing, therefore no auth. */
42 * Default settings that are specific to PSFTP.
44 char *platform_default_s(const char *name)
49 int platform_default_i(const char *name, int def)
54 FontSpec platform_default_fontspec(const char *name)
61 Filename platform_default_filename(const char *name)
64 if (!strcmp(name, "LogFileName"))
65 strcpy(ret.path, "putty.log");
71 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
73 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
76 ret = cmdline_get_passwd_input(p, in, inlen);
78 ret = console_get_userpass_input(p, in, inlen);
83 * Stubs for the GUI feedback mechanism in Windows PSCP.
85 void gui_update_stats(char *name, unsigned long size,
86 int percentage, unsigned long elapsed,
87 unsigned long done, unsigned long eta,
88 unsigned long ratebs) {}
89 void gui_send_errcount(int list, int errs) {}
90 void gui_send_char(int is_stderr, int c) {}
91 void gui_enable(char *arg) {}
95 * Set local current directory. Returns NULL on success, or else an
96 * error message which must be freed after printing.
98 char *psftp_lcd(char *dir)
101 return dupprintf("%s: chdir: %s", dir, strerror(errno));
107 * Get local current directory. Returns a string which must be
110 char *psftp_getcwd(void)
115 buffer = snewn(size, char);
117 ret = getcwd(buffer, size);
120 if (errno != ERANGE) {
122 return dupprintf("[cwd unavailable: %s]", strerror(errno));
125 * Otherwise, ERANGE was returned, meaning the buffer
129 buffer = sresize(buffer, size, char);
137 RFile *open_existing_file(char *name, unsigned long *size,
138 unsigned long *mtime, unsigned long *atime)
143 fd = open(name, O_RDONLY);
150 if (size || mtime || atime) {
152 if (fstat(fd, &statbuf) < 0) {
153 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
154 memset(&statbuf, 0, sizeof(statbuf));
158 *size = statbuf.st_size;
161 *mtime = statbuf.st_mtime;
164 *atime = statbuf.st_atime;
170 int read_from_file(RFile *f, void *buffer, int length)
172 return read(f->fd, buffer, length);
175 void close_rfile(RFile *f)
186 WFile *open_new_file(char *name)
191 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
197 ret->name = dupstr(name);
202 int write_to_file(WFile *f, void *buffer, int length)
204 char *p = (char *)buffer;
207 /* Keep trying until we've really written as much as we can. */
209 int ret = write(f->fd, p, length);
225 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
235 /* Closes and frees the WFile */
236 void close_wfile(WFile *f)
243 int file_type(char *name)
247 if (stat(name, &statbuf) < 0) {
249 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
250 return FILE_TYPE_NONEXISTENT;
253 if (S_ISREG(statbuf.st_mode))
254 return FILE_TYPE_FILE;
256 if (S_ISDIR(statbuf.st_mode))
257 return FILE_TYPE_DIRECTORY;
259 return FILE_TYPE_WEIRD;
266 DirHandle *open_directory(char *name)
275 ret = snew(DirHandle);
280 char *read_filename(DirHandle *dir)
285 de = readdir(dir->dir);
288 } while ((de->d_name[0] == '.' &&
289 (de->d_name[1] == '\0' ||
290 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
292 return dupstr(de->d_name);
295 void close_directory(DirHandle *dir)
301 int test_wildcard(char *name, int cmdline)
305 if (stat(name, &statbuf) == 0) {
306 return WCTYPE_FILENAME;
307 } else if (cmdline) {
309 * On Unix, we never need to parse wildcards coming from
310 * the command line, because the shell will have expanded
311 * them into a filename list already.
313 return WCTYPE_NONEXISTENT;
316 int ret = WCTYPE_NONEXISTENT;
318 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
319 if (globbed.gl_pathc > 0)
320 ret = WCTYPE_WILDCARD;
329 * Actually return matching file names for a local wildcard.
331 struct WildcardMatcher {
335 WildcardMatcher *begin_wildcard_matching(char *name) {
336 WildcardMatcher *ret = snew(WildcardMatcher);
338 if (glob(name, 0, NULL, &ret->globbed) < 0) {
347 char *wildcard_get_filename(WildcardMatcher *dir) {
348 if (dir->i < dir->globbed.gl_pathc) {
349 return dupstr(dir->globbed.gl_pathv[dir->i++]);
353 void finish_wildcard_matching(WildcardMatcher *dir) {
354 globfree(&dir->globbed);
358 int vet_filename(char *name)
360 if (strchr(name, '/'))
363 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
369 int create_directory(char *name)
371 return mkdir(name, 0777) == 0;
374 char *dir_file_cat(char *dir, char *file)
376 return dupcat(dir, "/", file, NULL);
380 * Do a select() between all currently active network fds and
383 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
385 fd_set rset, wset, xset;
386 int i, fdcount, fdsize, *fdlist;
387 int fd, fdstate, rwx, ret, maxfd;
388 long now = GETTICKCOUNT();
391 fdcount = fdsize = 0;
395 /* Count the currently active fds. */
397 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
398 fd = next_fd(&fdstate, &rwx)) i++;
400 if (i < 1 && !no_fds_ok)
401 return -1; /* doom */
403 /* Expand the fdlist buffer if necessary. */
406 fdlist = sresize(fdlist, fdsize, int);
415 * Add all currently open fds to the select sets, and store
416 * them in fdlist as well.
419 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
420 fd = next_fd(&fdstate, &rwx)) {
421 fdlist[fdcount++] = fd;
423 FD_SET_MAX(fd, maxfd, rset);
425 FD_SET_MAX(fd, maxfd, wset);
427 FD_SET_MAX(fd, maxfd, xset);
431 FD_SET_MAX(0, maxfd, rset);
435 struct timeval tv, *ptv;
437 if (run_timers(now, &next)) {
438 ticks = next - GETTICKCOUNT();
440 ticks = 1; /* just in case */
441 tv.tv_sec = ticks / 1000;
442 tv.tv_usec = ticks % 1000 * 1000;
447 ret = select(maxfd, &rset, &wset, &xset, ptv);
451 long newnow = GETTICKCOUNT();
453 * Check to see whether the system clock has
454 * changed massively during the select.
456 if (newnow - now < 0 || newnow - now > next - now) {
458 * If so, look at the elapsed time in the
459 * select and use it to compute a new
462 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
463 /* So we'd like GETTICKCOUNT to have returned othernow,
464 * but instead it return newnow. Hence ... */
465 tickcount_offset += othernow - newnow;
471 } while (ret < 0 && errno != EINTR);
479 for (i = 0; i < fdcount; i++) {
482 * We must process exceptional notifications before
483 * ordinary readability ones, or we may go straight
484 * past the urgent marker.
486 if (FD_ISSET(fd, &xset))
487 select_result(fd, 4);
488 if (FD_ISSET(fd, &rset))
489 select_result(fd, 1);
490 if (FD_ISSET(fd, &wset))
491 select_result(fd, 2);
496 return FD_ISSET(0, &rset) ? 1 : 0;
500 * Wait for some network data and process it.
502 int ssh_sftp_loop_iteration(void)
504 return ssh_sftp_do_select(FALSE, FALSE);
508 * Read a PSFTP command line from stdin.
510 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
513 int buflen, bufsize, ret;
515 fputs(prompt, stdout);
519 buflen = bufsize = 0;
522 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
524 printf("connection died\n");
525 return NULL; /* woop woop */
528 if (buflen >= bufsize) {
529 bufsize = buflen + 512;
530 buf = sresize(buf, bufsize, char);
532 ret = read(0, buf+buflen, 1);
538 /* eof on stdin; no error, but no answer either */
542 if (buf[buflen++] == '\n') {
543 /* we have a full line */
551 * Main program: do platform-specific initialisation and then call
554 int main(int argc, char *argv[])
557 return psftp_main(argc, argv);