2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
26 * In PSFTP our selects are synchronous, so these functions are
29 int uxsel_input_add(int fd
, int rwx
) { return 0; }
30 void uxsel_input_remove(int id
) { }
32 char *x_get_default(const char *key
)
34 return NULL
; /* this is a stub */
37 void platform_get_x11_auth(struct X11Display
*display
, const Config
*cfg
)
39 /* Do nothing, therefore no auth. */
41 const int platform_uses_x11_unix_by_default
= TRUE
;
44 * Default settings that are specific to PSFTP.
46 char *platform_default_s(const char *name
)
51 int platform_default_i(const char *name
, int def
)
56 FontSpec
platform_default_fontspec(const char *name
)
63 Filename
platform_default_filename(const char *name
)
66 if (!strcmp(name
, "LogFileName"))
67 strcpy(ret
.path
, "putty.log");
73 char *get_ttymode(void *frontend
, const char *mode
) { return NULL
; }
75 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
78 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
80 ret
= console_get_userpass_input(p
, in
, inlen
);
85 * Set local current directory. Returns NULL on success, or else an
86 * error message which must be freed after printing.
88 char *psftp_lcd(char *dir
)
91 return dupprintf("%s: chdir: %s", dir
, strerror(errno
));
97 * Get local current directory. Returns a string which must be
100 char *psftp_getcwd(void)
105 buffer
= snewn(size
, char);
107 ret
= getcwd(buffer
, size
);
110 if (errno
!= ERANGE
) {
112 return dupprintf("[cwd unavailable: %s]", strerror(errno
));
115 * Otherwise, ERANGE was returned, meaning the buffer
119 buffer
= sresize(buffer
, size
, char);
127 RFile
*open_existing_file(char *name
, uint64
*size
,
128 unsigned long *mtime
, unsigned long *atime
)
133 fd
= open(name
, O_RDONLY
);
140 if (size
|| mtime
|| atime
) {
142 if (fstat(fd
, &statbuf
) < 0) {
143 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
144 memset(&statbuf
, 0, sizeof(statbuf
));
148 *size
= uint64_make((statbuf
.st_size
>> 16) >> 16,
152 *mtime
= statbuf
.st_mtime
;
155 *atime
= statbuf
.st_atime
;
161 int read_from_file(RFile
*f
, void *buffer
, int length
)
163 return read(f
->fd
, buffer
, length
);
166 void close_rfile(RFile
*f
)
177 WFile
*open_new_file(char *name
)
182 fd
= open(name
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
188 ret
->name
= dupstr(name
);
194 WFile
*open_existing_wfile(char *name
, uint64
*size
)
199 fd
= open(name
, O_APPEND
| O_WRONLY
);
208 if (fstat(fd
, &statbuf
) < 0) {
209 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
210 memset(&statbuf
, 0, sizeof(statbuf
));
213 *size
= uint64_make((statbuf
.st_size
>> 16) >> 16,
220 int write_to_file(WFile
*f
, void *buffer
, int length
)
222 char *p
= (char *)buffer
;
225 /* Keep trying until we've really written as much as we can. */
227 int ret
= write(f
->fd
, p
, length
);
243 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
253 /* Closes and frees the WFile */
254 void close_wfile(WFile
*f
)
261 /* Seek offset bytes through file, from whence, where whence is
262 FROM_START, FROM_CURRENT, or FROM_END */
263 int seek_file(WFile
*f
, uint64 offset
, int whence
)
268 fileofft
= (((off_t
) offset
.hi
<< 16) << 16) + offset
.lo
;
272 lseek_whence
= SEEK_SET
;
275 lseek_whence
= SEEK_CUR
;
278 lseek_whence
= SEEK_END
;
284 return lseek(f
->fd
, fileofft
, lseek_whence
) >= 0 ?
0 : -1;
287 uint64
get_file_posn(WFile
*f
)
292 fileofft
= lseek(f
->fd
, (off_t
) 0, SEEK_CUR
);
294 ret
= uint64_make((fileofft
>> 16) >> 16, fileofft
);
299 int file_type(char *name
)
303 if (stat(name
, &statbuf
) < 0) {
305 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
306 return FILE_TYPE_NONEXISTENT
;
309 if (S_ISREG(statbuf
.st_mode
))
310 return FILE_TYPE_FILE
;
312 if (S_ISDIR(statbuf
.st_mode
))
313 return FILE_TYPE_DIRECTORY
;
315 return FILE_TYPE_WEIRD
;
322 DirHandle
*open_directory(char *name
)
331 ret
= snew(DirHandle
);
336 char *read_filename(DirHandle
*dir
)
341 de
= readdir(dir
->dir
);
344 } while ((de
->d_name
[0] == '.' &&
345 (de
->d_name
[1] == '\0' ||
346 (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))));
348 return dupstr(de
->d_name
);
351 void close_directory(DirHandle
*dir
)
357 int test_wildcard(char *name
, int cmdline
)
361 if (stat(name
, &statbuf
) == 0) {
362 return WCTYPE_FILENAME
;
363 } else if (cmdline
) {
365 * On Unix, we never need to parse wildcards coming from
366 * the command line, because the shell will have expanded
367 * them into a filename list already.
369 return WCTYPE_NONEXISTENT
;
372 int ret
= WCTYPE_NONEXISTENT
;
374 if (glob(name
, GLOB_ERR
, NULL
, &globbed
) == 0) {
375 if (globbed
.gl_pathc
> 0)
376 ret
= WCTYPE_WILDCARD
;
385 * Actually return matching file names for a local wildcard.
387 struct WildcardMatcher
{
391 WildcardMatcher
*begin_wildcard_matching(char *name
) {
392 WildcardMatcher
*ret
= snew(WildcardMatcher
);
394 if (glob(name
, 0, NULL
, &ret
->globbed
) < 0) {
403 char *wildcard_get_filename(WildcardMatcher
*dir
) {
404 if (dir
->i
< dir
->globbed
.gl_pathc
) {
405 return dupstr(dir
->globbed
.gl_pathv
[dir
->i
++]);
409 void finish_wildcard_matching(WildcardMatcher
*dir
) {
410 globfree(&dir
->globbed
);
414 int vet_filename(char *name
)
416 if (strchr(name
, '/'))
419 if (name
[0] == '.' && (!name
[1] || (name
[1] == '.' && !name
[2])))
425 int create_directory(char *name
)
427 return mkdir(name
, 0777) == 0;
430 char *dir_file_cat(char *dir
, char *file
)
432 return dupcat(dir
, "/", file
, NULL
);
436 * Do a select() between all currently active network fds and
439 static int ssh_sftp_do_select(int include_stdin
, int no_fds_ok
)
441 fd_set rset
, wset
, xset
;
442 int i
, fdcount
, fdsize
, *fdlist
;
443 int fd
, fdstate
, rwx
, ret
, maxfd
;
444 long now
= GETTICKCOUNT();
447 fdcount
= fdsize
= 0;
451 /* Count the currently active fds. */
453 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
454 fd
= next_fd(&fdstate
, &rwx
)) i
++;
456 if (i
< 1 && !no_fds_ok
)
457 return -1; /* doom */
459 /* Expand the fdlist buffer if necessary. */
462 fdlist
= sresize(fdlist
, fdsize
, int);
471 * Add all currently open fds to the select sets, and store
472 * them in fdlist as well.
475 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
476 fd
= next_fd(&fdstate
, &rwx
)) {
477 fdlist
[fdcount
++] = fd
;
479 FD_SET_MAX(fd
, maxfd
, rset
);
481 FD_SET_MAX(fd
, maxfd
, wset
);
483 FD_SET_MAX(fd
, maxfd
, xset
);
487 FD_SET_MAX(0, maxfd
, rset
);
491 struct timeval tv
, *ptv
;
493 if (run_timers(now
, &next
)) {
494 ticks
= next
- GETTICKCOUNT();
496 ticks
= 1; /* just in case */
497 tv
.tv_sec
= ticks
/ 1000;
498 tv
.tv_usec
= ticks
% 1000 * 1000;
503 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
507 long newnow
= GETTICKCOUNT();
509 * Check to see whether the system clock has
510 * changed massively during the select.
512 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
514 * If so, look at the elapsed time in the
515 * select and use it to compute a new
518 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
519 /* So we'd like GETTICKCOUNT to have returned othernow,
520 * but instead it return newnow. Hence ... */
521 tickcount_offset
+= othernow
- newnow
;
527 } while (ret
< 0 && errno
!= EINTR
);
535 for (i
= 0; i
< fdcount
; i
++) {
538 * We must process exceptional notifications before
539 * ordinary readability ones, or we may go straight
540 * past the urgent marker.
542 if (FD_ISSET(fd
, &xset
))
543 select_result(fd
, 4);
544 if (FD_ISSET(fd
, &rset
))
545 select_result(fd
, 1);
546 if (FD_ISSET(fd
, &wset
))
547 select_result(fd
, 2);
552 return FD_ISSET(0, &rset
) ?
1 : 0;
556 * Wait for some network data and process it.
558 int ssh_sftp_loop_iteration(void)
560 return ssh_sftp_do_select(FALSE
, FALSE
);
564 * Read a PSFTP command line from stdin.
566 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
569 int buflen
, bufsize
, ret
;
571 fputs(prompt
, stdout
);
575 buflen
= bufsize
= 0;
578 ret
= ssh_sftp_do_select(TRUE
, no_fds_ok
);
580 printf("connection died\n");
581 return NULL
; /* woop woop */
584 if (buflen
>= bufsize
) {
585 bufsize
= buflen
+ 512;
586 buf
= sresize(buf
, bufsize
, char);
588 ret
= read(0, buf
+buflen
, 1);
594 /* eof on stdin; no error, but no answer either */
598 if (buf
[buflen
++] == '\n') {
599 /* we have a full line */
607 * Main program: do platform-specific initialisation and then call
610 int main(int argc
, char *argv
[])
613 return psftp_main(argc
, argv
);