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)
58 return fontspec_new("");
61 Filename platform_default_filename(const char *name)
64 if (!strcmp(name, "LogFileName"))
65 strcpy(ret.path, "putty.log");
71 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
73 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
76 ret = cmdline_get_passwd_input(p, in, inlen);
78 ret = console_get_userpass_input(p, in, inlen);
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, uint64 *size,
126 unsigned long *mtime, unsigned long *atime,
132 fd = open(name, O_RDONLY);
139 if (size || mtime || atime || perms) {
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;
157 *perms = statbuf.st_mode;
163 int read_from_file(RFile *f, void *buffer, int length)
165 return read(f->fd, buffer, length);
168 void close_rfile(RFile *f)
179 WFile *open_new_file(char *name, long perms)
184 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
185 (mode_t)(perms ? perms : 0666));
191 ret->name = dupstr(name);
197 WFile *open_existing_wfile(char *name, uint64 *size)
202 fd = open(name, O_APPEND | O_WRONLY);
208 ret->name = dupstr(name);
212 if (fstat(fd, &statbuf) < 0) {
213 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
214 memset(&statbuf, 0, sizeof(statbuf));
217 *size = uint64_make((statbuf.st_size >> 16) >> 16,
224 int write_to_file(WFile *f, void *buffer, int length)
226 char *p = (char *)buffer;
229 /* Keep trying until we've really written as much as we can. */
231 int ret = write(f->fd, p, length);
247 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
257 /* Closes and frees the WFile */
258 void close_wfile(WFile *f)
265 /* Seek offset bytes through file, from whence, where whence is
266 FROM_START, FROM_CURRENT, or FROM_END */
267 int seek_file(WFile *f, uint64 offset, int whence)
272 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
276 lseek_whence = SEEK_SET;
279 lseek_whence = SEEK_CUR;
282 lseek_whence = SEEK_END;
288 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
291 uint64 get_file_posn(WFile *f)
296 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
298 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
303 int file_type(char *name)
307 if (stat(name, &statbuf) < 0) {
309 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
310 return FILE_TYPE_NONEXISTENT;
313 if (S_ISREG(statbuf.st_mode))
314 return FILE_TYPE_FILE;
316 if (S_ISDIR(statbuf.st_mode))
317 return FILE_TYPE_DIRECTORY;
319 return FILE_TYPE_WEIRD;
326 DirHandle *open_directory(char *name)
335 ret = snew(DirHandle);
340 char *read_filename(DirHandle *dir)
345 de = readdir(dir->dir);
348 } while ((de->d_name[0] == '.' &&
349 (de->d_name[1] == '\0' ||
350 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
352 return dupstr(de->d_name);
355 void close_directory(DirHandle *dir)
361 int test_wildcard(char *name, int cmdline)
365 if (stat(name, &statbuf) == 0) {
366 return WCTYPE_FILENAME;
367 } else if (cmdline) {
369 * On Unix, we never need to parse wildcards coming from
370 * the command line, because the shell will have expanded
371 * them into a filename list already.
373 return WCTYPE_NONEXISTENT;
376 int ret = WCTYPE_NONEXISTENT;
378 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
379 if (globbed.gl_pathc > 0)
380 ret = WCTYPE_WILDCARD;
389 * Actually return matching file names for a local wildcard.
391 struct WildcardMatcher {
395 WildcardMatcher *begin_wildcard_matching(char *name) {
396 WildcardMatcher *ret = snew(WildcardMatcher);
398 if (glob(name, 0, NULL, &ret->globbed) < 0) {
407 char *wildcard_get_filename(WildcardMatcher *dir) {
408 if (dir->i < dir->globbed.gl_pathc) {
409 return dupstr(dir->globbed.gl_pathv[dir->i++]);
413 void finish_wildcard_matching(WildcardMatcher *dir) {
414 globfree(&dir->globbed);
418 int vet_filename(char *name)
420 if (strchr(name, '/'))
423 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
429 int create_directory(char *name)
431 return mkdir(name, 0777) == 0;
434 char *dir_file_cat(char *dir, char *file)
436 return dupcat(dir, "/", file, NULL);
440 * Do a select() between all currently active network fds and
443 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
445 fd_set rset, wset, xset;
446 int i, fdcount, fdsize, *fdlist;
447 int fd, fdstate, rwx, ret, maxfd;
448 long now = GETTICKCOUNT();
451 fdcount = fdsize = 0;
455 /* Count the currently active fds. */
457 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
458 fd = next_fd(&fdstate, &rwx)) i++;
460 if (i < 1 && !no_fds_ok)
461 return -1; /* doom */
463 /* Expand the fdlist buffer if necessary. */
466 fdlist = sresize(fdlist, fdsize, int);
475 * Add all currently open fds to the select sets, and store
476 * them in fdlist as well.
479 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
480 fd = next_fd(&fdstate, &rwx)) {
481 fdlist[fdcount++] = fd;
483 FD_SET_MAX(fd, maxfd, rset);
485 FD_SET_MAX(fd, maxfd, wset);
487 FD_SET_MAX(fd, maxfd, xset);
491 FD_SET_MAX(0, maxfd, rset);
495 struct timeval tv, *ptv;
497 if (run_timers(now, &next)) {
498 ticks = next - GETTICKCOUNT();
500 ticks = 1; /* just in case */
501 tv.tv_sec = ticks / 1000;
502 tv.tv_usec = ticks % 1000 * 1000;
507 ret = select(maxfd, &rset, &wset, &xset, ptv);
511 long newnow = GETTICKCOUNT();
513 * Check to see whether the system clock has
514 * changed massively during the select.
516 if (newnow - now < 0 || newnow - now > next - now) {
518 * If so, look at the elapsed time in the
519 * select and use it to compute a new
522 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
523 /* So we'd like GETTICKCOUNT to have returned othernow,
524 * but instead it return newnow. Hence ... */
525 tickcount_offset += othernow - newnow;
531 } while (ret < 0 && errno != EINTR);
539 for (i = 0; i < fdcount; i++) {
542 * We must process exceptional notifications before
543 * ordinary readability ones, or we may go straight
544 * past the urgent marker.
546 if (FD_ISSET(fd, &xset))
547 select_result(fd, 4);
548 if (FD_ISSET(fd, &rset))
549 select_result(fd, 1);
550 if (FD_ISSET(fd, &wset))
551 select_result(fd, 2);
556 return FD_ISSET(0, &rset) ? 1 : 0;
560 * Wait for some network data and process it.
562 int ssh_sftp_loop_iteration(void)
564 return ssh_sftp_do_select(FALSE, FALSE);
568 * Read a PSFTP command line from stdin.
570 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
573 int buflen, bufsize, ret;
575 fputs(prompt, stdout);
579 buflen = bufsize = 0;
582 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
584 printf("connection died\n");
585 return NULL; /* woop woop */
588 if (buflen >= bufsize) {
589 bufsize = buflen + 512;
590 buf = sresize(buf, bufsize, char);
592 ret = read(0, buf+buflen, 1);
598 /* eof on stdin; no error, but no answer either */
602 if (buf[buflen++] == '\n') {
603 /* we have a full line */
611 * Main program: do platform-specific initialisation and then call
614 int main(int argc, char *argv[])
617 return psftp_main(argc, argv);