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, Conf *conf)
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,
134 fd = open(name, O_RDONLY);
141 if (size || mtime || atime || perms) {
143 if (fstat(fd, &statbuf) < 0) {
144 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
145 memset(&statbuf, 0, sizeof(statbuf));
149 *size = uint64_make((statbuf.st_size >> 16) >> 16,
153 *mtime = statbuf.st_mtime;
156 *atime = statbuf.st_atime;
159 *perms = statbuf.st_mode;
165 int read_from_file(RFile *f, void *buffer, int length)
167 return read(f->fd, buffer, length);
170 void close_rfile(RFile *f)
181 WFile *open_new_file(char *name, long perms)
186 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
187 (mode_t)(perms ? perms : 0666));
193 ret->name = dupstr(name);
199 WFile *open_existing_wfile(char *name, uint64 *size)
204 fd = open(name, O_APPEND | O_WRONLY);
210 ret->name = dupstr(name);
214 if (fstat(fd, &statbuf) < 0) {
215 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
216 memset(&statbuf, 0, sizeof(statbuf));
219 *size = uint64_make((statbuf.st_size >> 16) >> 16,
226 int write_to_file(WFile *f, void *buffer, int length)
228 char *p = (char *)buffer;
231 /* Keep trying until we've really written as much as we can. */
233 int ret = write(f->fd, p, length);
249 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
259 /* Closes and frees the WFile */
260 void close_wfile(WFile *f)
267 /* Seek offset bytes through file, from whence, where whence is
268 FROM_START, FROM_CURRENT, or FROM_END */
269 int seek_file(WFile *f, uint64 offset, int whence)
274 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
278 lseek_whence = SEEK_SET;
281 lseek_whence = SEEK_CUR;
284 lseek_whence = SEEK_END;
290 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
293 uint64 get_file_posn(WFile *f)
298 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
300 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
305 int file_type(char *name)
309 if (stat(name, &statbuf) < 0) {
311 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
312 return FILE_TYPE_NONEXISTENT;
315 if (S_ISREG(statbuf.st_mode))
316 return FILE_TYPE_FILE;
318 if (S_ISDIR(statbuf.st_mode))
319 return FILE_TYPE_DIRECTORY;
321 return FILE_TYPE_WEIRD;
328 DirHandle *open_directory(char *name)
337 ret = snew(DirHandle);
342 char *read_filename(DirHandle *dir)
347 de = readdir(dir->dir);
350 } while ((de->d_name[0] == '.' &&
351 (de->d_name[1] == '\0' ||
352 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
354 return dupstr(de->d_name);
357 void close_directory(DirHandle *dir)
363 int test_wildcard(char *name, int cmdline)
367 if (stat(name, &statbuf) == 0) {
368 return WCTYPE_FILENAME;
369 } else if (cmdline) {
371 * On Unix, we never need to parse wildcards coming from
372 * the command line, because the shell will have expanded
373 * them into a filename list already.
375 return WCTYPE_NONEXISTENT;
378 int ret = WCTYPE_NONEXISTENT;
380 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
381 if (globbed.gl_pathc > 0)
382 ret = WCTYPE_WILDCARD;
391 * Actually return matching file names for a local wildcard.
393 struct WildcardMatcher {
397 WildcardMatcher *begin_wildcard_matching(char *name) {
398 WildcardMatcher *ret = snew(WildcardMatcher);
400 if (glob(name, 0, NULL, &ret->globbed) < 0) {
409 char *wildcard_get_filename(WildcardMatcher *dir) {
410 if (dir->i < dir->globbed.gl_pathc) {
411 return dupstr(dir->globbed.gl_pathv[dir->i++]);
415 void finish_wildcard_matching(WildcardMatcher *dir) {
416 globfree(&dir->globbed);
420 int vet_filename(char *name)
422 if (strchr(name, '/'))
425 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
431 int create_directory(char *name)
433 return mkdir(name, 0777) == 0;
436 char *dir_file_cat(char *dir, char *file)
438 return dupcat(dir, "/", file, NULL);
442 * Do a select() between all currently active network fds and
445 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
447 fd_set rset, wset, xset;
448 int i, fdcount, fdsize, *fdlist;
449 int fd, fdstate, rwx, ret, maxfd;
450 long now = GETTICKCOUNT();
453 fdcount = fdsize = 0;
457 /* Count the currently active fds. */
459 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
460 fd = next_fd(&fdstate, &rwx)) i++;
462 if (i < 1 && !no_fds_ok)
463 return -1; /* doom */
465 /* Expand the fdlist buffer if necessary. */
468 fdlist = sresize(fdlist, fdsize, int);
477 * Add all currently open fds to the select sets, and store
478 * them in fdlist as well.
481 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
482 fd = next_fd(&fdstate, &rwx)) {
483 fdlist[fdcount++] = fd;
485 FD_SET_MAX(fd, maxfd, rset);
487 FD_SET_MAX(fd, maxfd, wset);
489 FD_SET_MAX(fd, maxfd, xset);
493 FD_SET_MAX(0, maxfd, rset);
497 struct timeval tv, *ptv;
499 if (run_timers(now, &next)) {
500 ticks = next - GETTICKCOUNT();
502 ticks = 1; /* just in case */
503 tv.tv_sec = ticks / 1000;
504 tv.tv_usec = ticks % 1000 * 1000;
509 ret = select(maxfd, &rset, &wset, &xset, ptv);
513 long newnow = GETTICKCOUNT();
515 * Check to see whether the system clock has
516 * changed massively during the select.
518 if (newnow - now < 0 || newnow - now > next - now) {
520 * If so, look at the elapsed time in the
521 * select and use it to compute a new
524 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
525 /* So we'd like GETTICKCOUNT to have returned othernow,
526 * but instead it return newnow. Hence ... */
527 tickcount_offset += othernow - newnow;
533 } while (ret < 0 && errno != EINTR);
541 for (i = 0; i < fdcount; i++) {
544 * We must process exceptional notifications before
545 * ordinary readability ones, or we may go straight
546 * past the urgent marker.
548 if (FD_ISSET(fd, &xset))
549 select_result(fd, 4);
550 if (FD_ISSET(fd, &rset))
551 select_result(fd, 1);
552 if (FD_ISSET(fd, &wset))
553 select_result(fd, 2);
558 return FD_ISSET(0, &rset) ? 1 : 0;
562 * Wait for some network data and process it.
564 int ssh_sftp_loop_iteration(void)
566 return ssh_sftp_do_select(FALSE, FALSE);
570 * Read a PSFTP command line from stdin.
572 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
575 int buflen, bufsize, ret;
577 fputs(prompt, stdout);
581 buflen = bufsize = 0;
584 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
586 printf("connection died\n");
587 return NULL; /* woop woop */
590 if (buflen >= bufsize) {
591 bufsize = buflen + 512;
592 buf = sresize(buf, bufsize, char);
594 ret = read(0, buf+buflen, 1);
600 /* eof on stdin; no error, but no answer either */
604 if (buf[buflen++] == '\n') {
605 /* we have a full line */
613 * Main program: do platform-specific initialisation and then call
616 int main(int argc, char *argv[])
619 return psftp_main(argc, argv);