2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
25 * In PSFTP our selects are synchronous, so these functions are
28 int uxsel_input_add(int fd
, int rwx
) { return 0; }
29 void uxsel_input_remove(int id
) { }
31 char *x_get_default(const char *key
)
33 return NULL
; /* this is a stub */
36 void platform_get_x11_auth(char *display
, int *protocol
,
37 unsigned char *data
, int *datalen
)
39 /* Do nothing, therefore no auth. */
43 * Default settings that are specific to PSFTP.
45 char *platform_default_s(const char *name
)
50 int platform_default_i(const char *name
, int def
)
55 FontSpec
platform_default_fontspec(const char *name
)
62 Filename
platform_default_filename(const char *name
)
65 if (!strcmp(name
, "LogFileName"))
66 strcpy(ret
.path
, "putty.log");
72 char *get_ttymode(void *frontend
, const char *mode
) { return NULL
; }
74 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
77 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
79 ret
= console_get_userpass_input(p
, in
, inlen
);
84 * Set local current directory. Returns NULL on success, or else an
85 * error message which must be freed after printing.
87 char *psftp_lcd(char *dir
)
90 return dupprintf("%s: chdir: %s", dir
, strerror(errno
));
96 * Get local current directory. Returns a string which must be
99 char *psftp_getcwd(void)
104 buffer
= snewn(size
, char);
106 ret
= getcwd(buffer
, size
);
109 if (errno
!= ERANGE
) {
111 return dupprintf("[cwd unavailable: %s]", strerror(errno
));
114 * Otherwise, ERANGE was returned, meaning the buffer
118 buffer
= sresize(buffer
, size
, char);
126 RFile
*open_existing_file(char *name
, uint64
*size
,
127 unsigned long *mtime
, unsigned long *atime
)
132 fd
= open(name
, O_RDONLY
);
139 if (size
|| mtime
|| atime
) {
141 if (fstat(fd
, &statbuf
) < 0) {
142 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
143 memset(&statbuf
, 0, sizeof(statbuf
));
147 *size
= uint64_make((statbuf
.st_size
>> 16) >> 16,
151 *mtime
= statbuf
.st_mtime
;
154 *atime
= statbuf
.st_atime
;
160 int read_from_file(RFile
*f
, void *buffer
, int length
)
162 return read(f
->fd
, buffer
, length
);
165 void close_rfile(RFile
*f
)
176 WFile
*open_new_file(char *name
)
181 fd
= open(name
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
187 ret
->name
= dupstr(name
);
193 WFile
*open_existing_wfile(char *name
, uint64
*size
)
198 fd
= open(name
, O_APPEND
| O_WRONLY
);
207 if (fstat(fd
, &statbuf
) < 0) {
208 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
209 memset(&statbuf
, 0, sizeof(statbuf
));
212 *size
= uint64_make((statbuf
.st_size
>> 16) >> 16,
219 int write_to_file(WFile
*f
, void *buffer
, int length
)
221 char *p
= (char *)buffer
;
224 /* Keep trying until we've really written as much as we can. */
226 int ret
= write(f
->fd
, p
, length
);
242 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
252 /* Closes and frees the WFile */
253 void close_wfile(WFile
*f
)
260 /* Seek offset bytes through file, from whence, where whence is
261 FROM_START, FROM_CURRENT, or FROM_END */
262 int seek_file(WFile
*f
, uint64 offset
, int whence
)
267 fileofft
= (((off_t
) offset
.hi
<< 16) << 16) + offset
.lo
;
271 lseek_whence
= SEEK_SET
;
274 lseek_whence
= SEEK_CUR
;
277 lseek_whence
= SEEK_END
;
283 return lseek(f
->fd
, fileofft
, lseek_whence
) >= 0 ?
0 : -1;
286 uint64
get_file_posn(WFile
*f
)
291 fileofft
= lseek(f
->fd
, (off_t
) 0, SEEK_CUR
);
293 ret
= uint64_make((fileofft
>> 16) >> 16, fileofft
);
298 int file_type(char *name
)
302 if (stat(name
, &statbuf
) < 0) {
304 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
305 return FILE_TYPE_NONEXISTENT
;
308 if (S_ISREG(statbuf
.st_mode
))
309 return FILE_TYPE_FILE
;
311 if (S_ISDIR(statbuf
.st_mode
))
312 return FILE_TYPE_DIRECTORY
;
314 return FILE_TYPE_WEIRD
;
321 DirHandle
*open_directory(char *name
)
330 ret
= snew(DirHandle
);
335 char *read_filename(DirHandle
*dir
)
340 de
= readdir(dir
->dir
);
343 } while ((de
->d_name
[0] == '.' &&
344 (de
->d_name
[1] == '\0' ||
345 (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))));
347 return dupstr(de
->d_name
);
350 void close_directory(DirHandle
*dir
)
356 int test_wildcard(char *name
, int cmdline
)
360 if (stat(name
, &statbuf
) == 0) {
361 return WCTYPE_FILENAME
;
362 } else if (cmdline
) {
364 * On Unix, we never need to parse wildcards coming from
365 * the command line, because the shell will have expanded
366 * them into a filename list already.
368 return WCTYPE_NONEXISTENT
;
371 int ret
= WCTYPE_NONEXISTENT
;
373 if (glob(name
, GLOB_ERR
, NULL
, &globbed
) == 0) {
374 if (globbed
.gl_pathc
> 0)
375 ret
= WCTYPE_WILDCARD
;
384 * Actually return matching file names for a local wildcard.
386 struct WildcardMatcher
{
390 WildcardMatcher
*begin_wildcard_matching(char *name
) {
391 WildcardMatcher
*ret
= snew(WildcardMatcher
);
393 if (glob(name
, 0, NULL
, &ret
->globbed
) < 0) {
402 char *wildcard_get_filename(WildcardMatcher
*dir
) {
403 if (dir
->i
< dir
->globbed
.gl_pathc
) {
404 return dupstr(dir
->globbed
.gl_pathv
[dir
->i
++]);
408 void finish_wildcard_matching(WildcardMatcher
*dir
) {
409 globfree(&dir
->globbed
);
413 int vet_filename(char *name
)
415 if (strchr(name
, '/'))
418 if (name
[0] == '.' && (!name
[1] || (name
[1] == '.' && !name
[2])))
424 int create_directory(char *name
)
426 return mkdir(name
, 0777) == 0;
429 char *dir_file_cat(char *dir
, char *file
)
431 return dupcat(dir
, "/", file
, NULL
);
435 * Do a select() between all currently active network fds and
438 static int ssh_sftp_do_select(int include_stdin
, int no_fds_ok
)
440 fd_set rset
, wset
, xset
;
441 int i
, fdcount
, fdsize
, *fdlist
;
442 int fd
, fdstate
, rwx
, ret
, maxfd
;
443 long now
= GETTICKCOUNT();
446 fdcount
= fdsize
= 0;
450 /* Count the currently active fds. */
452 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
453 fd
= next_fd(&fdstate
, &rwx
)) i
++;
455 if (i
< 1 && !no_fds_ok
)
456 return -1; /* doom */
458 /* Expand the fdlist buffer if necessary. */
461 fdlist
= sresize(fdlist
, fdsize
, int);
470 * Add all currently open fds to the select sets, and store
471 * them in fdlist as well.
474 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
475 fd
= next_fd(&fdstate
, &rwx
)) {
476 fdlist
[fdcount
++] = fd
;
478 FD_SET_MAX(fd
, maxfd
, rset
);
480 FD_SET_MAX(fd
, maxfd
, wset
);
482 FD_SET_MAX(fd
, maxfd
, xset
);
486 FD_SET_MAX(0, maxfd
, rset
);
490 struct timeval tv
, *ptv
;
492 if (run_timers(now
, &next
)) {
493 ticks
= next
- GETTICKCOUNT();
495 ticks
= 1; /* just in case */
496 tv
.tv_sec
= ticks
/ 1000;
497 tv
.tv_usec
= ticks
% 1000 * 1000;
502 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
506 long newnow
= GETTICKCOUNT();
508 * Check to see whether the system clock has
509 * changed massively during the select.
511 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
513 * If so, look at the elapsed time in the
514 * select and use it to compute a new
517 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
518 /* So we'd like GETTICKCOUNT to have returned othernow,
519 * but instead it return newnow. Hence ... */
520 tickcount_offset
+= othernow
- newnow
;
526 } while (ret
< 0 && errno
!= EINTR
);
534 for (i
= 0; i
< fdcount
; i
++) {
537 * We must process exceptional notifications before
538 * ordinary readability ones, or we may go straight
539 * past the urgent marker.
541 if (FD_ISSET(fd
, &xset
))
542 select_result(fd
, 4);
543 if (FD_ISSET(fd
, &rset
))
544 select_result(fd
, 1);
545 if (FD_ISSET(fd
, &wset
))
546 select_result(fd
, 2);
551 return FD_ISSET(0, &rset
) ?
1 : 0;
555 * Wait for some network data and process it.
557 int ssh_sftp_loop_iteration(void)
559 return ssh_sftp_do_select(FALSE
, FALSE
);
563 * Read a PSFTP command line from stdin.
565 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
568 int buflen
, bufsize
, ret
;
570 fputs(prompt
, stdout
);
574 buflen
= bufsize
= 0;
577 ret
= ssh_sftp_do_select(TRUE
, no_fds_ok
);
579 printf("connection died\n");
580 return NULL
; /* woop woop */
583 if (buflen
>= bufsize
) {
584 bufsize
= buflen
+ 512;
585 buf
= sresize(buf
, bufsize
, char);
587 ret
= read(0, buf
+buflen
, 1);
593 /* eof on stdin; no error, but no answer either */
597 if (buf
[buflen
++] == '\n') {
598 /* we have a full line */
606 * Main program: do platform-specific initialisation and then call
609 int main(int argc
, char *argv
[])
612 return psftp_main(argc
, argv
);