2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
21 * In PSFTP our selects are synchronous, so these functions are
24 int uxsel_input_add(int fd
, int rwx
) { return 0; }
25 void uxsel_input_remove(int id
) { }
27 char *x_get_default(const char *key
)
29 return NULL
; /* this is a stub */
32 void platform_get_x11_auth(char *display
, int *protocol
,
33 unsigned char *data
, int *datalen
)
35 /* Do nothing, therefore no auth. */
39 * Default settings that are specific to PSFTP.
41 char *platform_default_s(const char *name
)
46 int platform_default_i(const char *name
, int def
)
51 FontSpec
platform_default_fontspec(const char *name
)
58 Filename
platform_default_filename(const char *name
)
61 if (!strcmp(name
, "LogFileName"))
62 strcpy(ret
.path
, "putty.log");
68 char *get_ttymode(void *frontend
, const char *mode
) { return NULL
; }
71 * Stubs for the GUI feedback mechanism in Windows PSCP.
73 void gui_update_stats(char *name
, unsigned long size
,
74 int percentage
, unsigned long elapsed
,
75 unsigned long done
, unsigned long eta
,
76 unsigned long ratebs
) {}
77 void gui_send_errcount(int list
, int errs
) {}
78 void gui_send_char(int is_stderr
, int c
) {}
79 void gui_enable(char *arg
) {}
83 * Set local current directory. Returns NULL on success, or else an
84 * error message which must be freed after printing.
86 char *psftp_lcd(char *dir
)
89 return dupprintf("%s: chdir: %s", dir
, strerror(errno
));
95 * Get local current directory. Returns a string which must be
98 char *psftp_getcwd(void)
103 buffer
= snewn(size
, char);
105 ret
= getcwd(buffer
, size
);
108 if (errno
!= ERANGE
) {
110 return dupprintf("[cwd unavailable: %s]", strerror(errno
));
113 * Otherwise, ERANGE was returned, meaning the buffer
117 buffer
= sresize(buffer
, size
, char);
125 RFile
*open_existing_file(char *name
, unsigned long *size
,
126 unsigned long *mtime
, unsigned long *atime
)
131 fd
= open(name
, O_RDONLY
);
138 if (size
|| mtime
|| atime
) {
140 if (fstat(fd
, &statbuf
) < 0) {
141 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
142 memset(&statbuf
, 0, sizeof(statbuf
));
146 *size
= statbuf
.st_size
;
149 *mtime
= statbuf
.st_mtime
;
152 *atime
= statbuf
.st_atime
;
158 int read_from_file(RFile
*f
, void *buffer
, int length
)
160 return read(f
->fd
, buffer
, length
);
163 void close_rfile(RFile
*f
)
174 WFile
*open_new_file(char *name
)
179 fd
= open(name
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
185 ret
->name
= dupstr(name
);
190 int write_to_file(WFile
*f
, void *buffer
, int length
)
192 char *p
= (char *)buffer
;
195 /* Keep trying until we've really written as much as we can. */
197 int ret
= write(f
->fd
, p
, length
);
213 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
223 /* Closes and frees the WFile */
224 void close_wfile(WFile
*f
)
231 int file_type(char *name
)
235 if (stat(name
, &statbuf
) < 0) {
237 fprintf(stderr
, "%s: stat: %s\n", name
, strerror(errno
));
238 return FILE_TYPE_NONEXISTENT
;
241 if (S_ISREG(statbuf
.st_mode
))
242 return FILE_TYPE_FILE
;
244 if (S_ISDIR(statbuf
.st_mode
))
245 return FILE_TYPE_DIRECTORY
;
247 return FILE_TYPE_WEIRD
;
254 DirHandle
*open_directory(char *name
)
263 ret
= snew(DirHandle
);
268 char *read_filename(DirHandle
*dir
)
273 de
= readdir(dir
->dir
);
276 } while ((de
->d_name
[0] == '.' &&
277 (de
->d_name
[1] == '\0' ||
278 (de
->d_name
[1] == '.' && de
->d_name
[2] == '\0'))));
280 return dupstr(de
->d_name
);
283 void close_directory(DirHandle
*dir
)
289 int test_wildcard(char *name
, int cmdline
)
293 if (stat(name
, &statbuf
) == 0) {
294 return WCTYPE_FILENAME
;
295 } else if (cmdline
) {
297 * On Unix, we never need to parse wildcards coming from
298 * the command line, because the shell will have expanded
299 * them into a filename list already.
301 return WCTYPE_NONEXISTENT
;
304 int ret
= WCTYPE_NONEXISTENT
;
306 if (glob(name
, GLOB_ERR
, NULL
, &globbed
) == 0) {
307 if (globbed
.gl_pathc
> 0)
308 ret
= WCTYPE_WILDCARD
;
317 * Actually return matching file names for a local wildcard.
319 struct WildcardMatcher
{
323 WildcardMatcher
*begin_wildcard_matching(char *name
) {
324 WildcardMatcher
*ret
= snew(WildcardMatcher
);
326 if (glob(name
, 0, NULL
, &ret
->globbed
) < 0) {
335 char *wildcard_get_filename(WildcardMatcher
*dir
) {
336 if (dir
->i
< dir
->globbed
.gl_pathc
) {
337 return dupstr(dir
->globbed
.gl_pathv
[dir
->i
++]);
341 void finish_wildcard_matching(WildcardMatcher
*dir
) {
342 globfree(&dir
->globbed
);
346 int vet_filename(char *name
)
348 if (strchr(name
, '/'))
351 if (name
[0] == '.' && (!name
[1] || (name
[1] == '.' && !name
[2])))
357 int create_directory(char *name
)
359 return mkdir(name
, 0777) == 0;
362 char *dir_file_cat(char *dir
, char *file
)
364 return dupcat(dir
, "/", file
, NULL
);
368 * Do a select() between all currently active network fds and
371 static int ssh_sftp_do_select(int include_stdin
, int no_fds_ok
)
373 fd_set rset
, wset
, xset
;
374 int i
, fdcount
, fdsize
, *fdlist
;
375 int fd
, fdstate
, rwx
, ret
, maxfd
;
376 long now
= GETTICKCOUNT();
379 fdcount
= fdsize
= 0;
383 /* Count the currently active fds. */
385 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
386 fd
= next_fd(&fdstate
, &rwx
)) i
++;
388 if (i
< 1 && !no_fds_ok
)
389 return -1; /* doom */
391 /* Expand the fdlist buffer if necessary. */
394 fdlist
= sresize(fdlist
, fdsize
, int);
403 * Add all currently open fds to the select sets, and store
404 * them in fdlist as well.
407 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
408 fd
= next_fd(&fdstate
, &rwx
)) {
409 fdlist
[fdcount
++] = fd
;
411 FD_SET_MAX(fd
, maxfd
, rset
);
413 FD_SET_MAX(fd
, maxfd
, wset
);
415 FD_SET_MAX(fd
, maxfd
, xset
);
419 FD_SET_MAX(0, maxfd
, rset
);
423 struct timeval tv
, *ptv
;
425 if (run_timers(now
, &next
)) {
426 ticks
= next
- GETTICKCOUNT();
428 ticks
= 1; /* just in case */
429 tv
.tv_sec
= ticks
/ 1000;
430 tv
.tv_usec
= ticks
% 1000 * 1000;
435 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
439 long newnow
= GETTICKCOUNT();
441 * Check to see whether the system clock has
442 * changed massively during the select.
444 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
446 * If so, look at the elapsed time in the
447 * select and use it to compute a new
450 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
451 /* So we'd like GETTICKCOUNT to have returned othernow,
452 * but instead it return newnow. Hence ... */
453 tickcount_offset
+= othernow
- newnow
;
459 } while (ret
< 0 && errno
!= EINTR
);
467 for (i
= 0; i
< fdcount
; i
++) {
470 * We must process exceptional notifications before
471 * ordinary readability ones, or we may go straight
472 * past the urgent marker.
474 if (FD_ISSET(fd
, &xset
))
475 select_result(fd
, 4);
476 if (FD_ISSET(fd
, &rset
))
477 select_result(fd
, 1);
478 if (FD_ISSET(fd
, &wset
))
479 select_result(fd
, 2);
484 return FD_ISSET(0, &rset
) ?
1 : 0;
488 * Wait for some network data and process it.
490 int ssh_sftp_loop_iteration(void)
492 return ssh_sftp_do_select(FALSE
, FALSE
);
496 * Read a PSFTP command line from stdin.
498 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
501 int buflen
, bufsize
, ret
;
503 fputs(prompt
, stdout
);
507 buflen
= bufsize
= 0;
510 ret
= ssh_sftp_do_select(TRUE
, no_fds_ok
);
512 printf("connection died\n");
513 return NULL
; /* woop woop */
516 if (buflen
>= bufsize
) {
517 bufsize
= buflen
+ 512;
518 buf
= sresize(buf
, bufsize
, char);
520 ret
= read(0, buf
+buflen
, 1);
526 /* eof on stdin; no error, but no answer either */
530 if (buf
[buflen
++] == '\n') {
531 /* we have a full line */
539 * Main program: do platform-specific initialisation and then call
542 int main(int argc
, char *argv
[])
545 return psftp_main(argc
, argv
);