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
);
205 ret
->name
= dupstr(name
);
209 if (fstat(fd
, &statbuf
) < 0) {
210 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
211 memset(&statbuf
, 0, sizeof(statbuf
));
214 *size
= uint64_make((statbuf
.st_size
>> 16) >> 16,
221 int write_to_file(WFile
*f
, void *buffer
, int length
)
223 char *p
= (char *)buffer
;
226 /* Keep trying until we've really written as much as we can. */
228 int ret
= write(f
->fd
, p
, length
);
244 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
254 /* Closes and frees the WFile */
255 void close_wfile(WFile
*f
)
262 /* Seek offset bytes through file, from whence, where whence is
263 FROM_START, FROM_CURRENT, or FROM_END */
264 int seek_file(WFile
*f
, uint64 offset
, int whence
)
269 fileofft
= (((off_t
) offset
.hi
<< 16) << 16) + offset
.lo
;
273 lseek_whence
= SEEK_SET
;
276 lseek_whence
= SEEK_CUR
;
279 lseek_whence
= SEEK_END
;
285 return lseek(f
->fd
, fileofft
, lseek_whence
) >= 0 ?
0 : -1;
288 uint64
get_file_posn(WFile
*f
)
293 fileofft
= lseek(f
->fd
, (off_t
) 0, SEEK_CUR
);
295 ret
= uint64_make((fileofft
>> 16) >> 16, fileofft
);
300 int file_type(char *name
)
304 if (stat(name
, &statbuf
) < 0) {
306 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
307 return FILE_TYPE_NONEXISTENT
;
310 if (S_ISREG(statbuf
.st_mode
))
311 return FILE_TYPE_FILE
;
313 if (S_ISDIR(statbuf
.st_mode
))
314 return FILE_TYPE_DIRECTORY
;
316 return FILE_TYPE_WEIRD
;
323 DirHandle
*open_directory(char *name
)
332 ret
= snew(DirHandle
);
337 char *read_filename(DirHandle
*dir
)
342 de
= readdir(dir
->dir
);
345 } while ((de
->d_name
[0] == '.' &&
346 (de
->d_name
[1] == '\0' ||
347 (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))));
349 return dupstr(de
->d_name
);
352 void close_directory(DirHandle
*dir
)
358 int test_wildcard(char *name
, int cmdline
)
362 if (stat(name
, &statbuf
) == 0) {
363 return WCTYPE_FILENAME
;
364 } else if (cmdline
) {
366 * On Unix, we never need to parse wildcards coming from
367 * the command line, because the shell will have expanded
368 * them into a filename list already.
370 return WCTYPE_NONEXISTENT
;
373 int ret
= WCTYPE_NONEXISTENT
;
375 if (glob(name
, GLOB_ERR
, NULL
, &globbed
) == 0) {
376 if (globbed
.gl_pathc
> 0)
377 ret
= WCTYPE_WILDCARD
;
386 * Actually return matching file names for a local wildcard.
388 struct WildcardMatcher
{
392 WildcardMatcher
*begin_wildcard_matching(char *name
) {
393 WildcardMatcher
*ret
= snew(WildcardMatcher
);
395 if (glob(name
, 0, NULL
, &ret
->globbed
) < 0) {
404 char *wildcard_get_filename(WildcardMatcher
*dir
) {
405 if (dir
->i
< dir
->globbed
.gl_pathc
) {
406 return dupstr(dir
->globbed
.gl_pathv
[dir
->i
++]);
410 void finish_wildcard_matching(WildcardMatcher
*dir
) {
411 globfree(&dir
->globbed
);
415 int vet_filename(char *name
)
417 if (strchr(name
, '/'))
420 if (name
[0] == '.' && (!name
[1] || (name
[1] == '.' && !name
[2])))
426 int create_directory(char *name
)
428 return mkdir(name
, 0777) == 0;
431 char *dir_file_cat(char *dir
, char *file
)
433 return dupcat(dir
, "/", file
, NULL
);
437 * Do a select() between all currently active network fds and
440 static int ssh_sftp_do_select(int include_stdin
, int no_fds_ok
)
442 fd_set rset
, wset
, xset
;
443 int i
, fdcount
, fdsize
, *fdlist
;
444 int fd
, fdstate
, rwx
, ret
, maxfd
;
445 long now
= GETTICKCOUNT();
448 fdcount
= fdsize
= 0;
452 /* Count the currently active fds. */
454 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
455 fd
= next_fd(&fdstate
, &rwx
)) i
++;
457 if (i
< 1 && !no_fds_ok
)
458 return -1; /* doom */
460 /* Expand the fdlist buffer if necessary. */
463 fdlist
= sresize(fdlist
, fdsize
, int);
472 * Add all currently open fds to the select sets, and store
473 * them in fdlist as well.
476 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
477 fd
= next_fd(&fdstate
, &rwx
)) {
478 fdlist
[fdcount
++] = fd
;
480 FD_SET_MAX(fd
, maxfd
, rset
);
482 FD_SET_MAX(fd
, maxfd
, wset
);
484 FD_SET_MAX(fd
, maxfd
, xset
);
488 FD_SET_MAX(0, maxfd
, rset
);
492 struct timeval tv
, *ptv
;
494 if (run_timers(now
, &next
)) {
495 ticks
= next
- GETTICKCOUNT();
497 ticks
= 1; /* just in case */
498 tv
.tv_sec
= ticks
/ 1000;
499 tv
.tv_usec
= ticks
% 1000 * 1000;
504 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
508 long newnow
= GETTICKCOUNT();
510 * Check to see whether the system clock has
511 * changed massively during the select.
513 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
515 * If so, look at the elapsed time in the
516 * select and use it to compute a new
519 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
520 /* So we'd like GETTICKCOUNT to have returned othernow,
521 * but instead it return newnow. Hence ... */
522 tickcount_offset
+= othernow
- newnow
;
528 } while (ret
< 0 && errno
!= EINTR
);
536 for (i
= 0; i
< fdcount
; i
++) {
539 * We must process exceptional notifications before
540 * ordinary readability ones, or we may go straight
541 * past the urgent marker.
543 if (FD_ISSET(fd
, &xset
))
544 select_result(fd
, 4);
545 if (FD_ISSET(fd
, &rset
))
546 select_result(fd
, 1);
547 if (FD_ISSET(fd
, &wset
))
548 select_result(fd
, 2);
553 return FD_ISSET(0, &rset
) ?
1 : 0;
557 * Wait for some network data and process it.
559 int ssh_sftp_loop_iteration(void)
561 return ssh_sftp_do_select(FALSE
, FALSE
);
565 * Read a PSFTP command line from stdin.
567 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
570 int buflen
, bufsize
, ret
;
572 fputs(prompt
, stdout
);
576 buflen
= bufsize
= 0;
579 ret
= ssh_sftp_do_select(TRUE
, no_fds_ok
);
581 printf("connection died\n");
582 return NULL
; /* woop woop */
585 if (buflen
>= bufsize
) {
586 bufsize
= buflen
+ 512;
587 buf
= sresize(buf
, bufsize
, char);
589 ret
= read(0, buf
+buflen
, 1);
595 /* eof on stdin; no error, but no answer either */
599 if (buf
[buflen
++] == '\n') {
600 /* we have a full line */
608 * Main program: do platform-specific initialisation and then call
611 int main(int argc
, char *argv
[])
614 return psftp_main(argc
, argv
);