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 uxsel_id *uxsel_input_add(int fd, int rwx) { return NULL; }
30 void uxsel_input_remove(uxsel_id *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)
63 if (!strcmp(name, "LogFileName"))
64 return filename_from_str("putty.log");
66 return filename_from_str("");
69 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
71 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
74 ret = cmdline_get_passwd_input(p, in, inlen);
76 ret = console_get_userpass_input(p, in, inlen);
81 * Set local current directory. Returns NULL on success, or else an
82 * error message which must be freed after printing.
84 char *psftp_lcd(char *dir)
87 return dupprintf("%s: chdir: %s", dir, strerror(errno));
93 * Get local current directory. Returns a string which must be
96 char *psftp_getcwd(void)
101 buffer = snewn(size, char);
103 ret = getcwd(buffer, size);
106 if (errno != ERANGE) {
108 return dupprintf("[cwd unavailable: %s]", strerror(errno));
111 * Otherwise, ERANGE was returned, meaning the buffer
115 buffer = sresize(buffer, size, char);
123 RFile *open_existing_file(const char *name, uint64 *size,
124 unsigned long *mtime, unsigned long *atime,
130 fd = open(name, O_RDONLY);
137 if (size || mtime || atime || perms) {
139 if (fstat(fd, &statbuf) < 0) {
140 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
141 memset(&statbuf, 0, sizeof(statbuf));
145 *size = uint64_make((statbuf.st_size >> 16) >> 16,
149 *mtime = statbuf.st_mtime;
152 *atime = statbuf.st_atime;
155 *perms = statbuf.st_mode;
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(const char *name, long perms)
182 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
183 (mode_t)(perms ? perms : 0666));
189 ret->name = dupstr(name);
195 WFile *open_existing_wfile(const char *name, uint64 *size)
200 fd = open(name, O_APPEND | O_WRONLY);
206 ret->name = dupstr(name);
210 if (fstat(fd, &statbuf) < 0) {
211 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
212 memset(&statbuf, 0, sizeof(statbuf));
215 *size = uint64_make((statbuf.st_size >> 16) >> 16,
222 int write_to_file(WFile *f, void *buffer, int length)
224 char *p = (char *)buffer;
227 /* Keep trying until we've really written as much as we can. */
229 int ret = write(f->fd, p, length);
245 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
255 /* Closes and frees the WFile */
256 void close_wfile(WFile *f)
263 /* Seek offset bytes through file, from whence, where whence is
264 FROM_START, FROM_CURRENT, or FROM_END */
265 int seek_file(WFile *f, uint64 offset, int whence)
270 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
274 lseek_whence = SEEK_SET;
277 lseek_whence = SEEK_CUR;
280 lseek_whence = SEEK_END;
286 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
289 uint64 get_file_posn(WFile *f)
294 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
296 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
301 int file_type(const char *name)
305 if (stat(name, &statbuf) < 0) {
307 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
308 return FILE_TYPE_NONEXISTENT;
311 if (S_ISREG(statbuf.st_mode))
312 return FILE_TYPE_FILE;
314 if (S_ISDIR(statbuf.st_mode))
315 return FILE_TYPE_DIRECTORY;
317 return FILE_TYPE_WEIRD;
324 DirHandle *open_directory(const char *name)
333 ret = snew(DirHandle);
338 char *read_filename(DirHandle *dir)
343 de = readdir(dir->dir);
346 } while ((de->d_name[0] == '.' &&
347 (de->d_name[1] == '\0' ||
348 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
350 return dupstr(de->d_name);
353 void close_directory(DirHandle *dir)
359 int test_wildcard(const char *name, int cmdline)
363 if (stat(name, &statbuf) == 0) {
364 return WCTYPE_FILENAME;
365 } else if (cmdline) {
367 * On Unix, we never need to parse wildcards coming from
368 * the command line, because the shell will have expanded
369 * them into a filename list already.
371 return WCTYPE_NONEXISTENT;
374 int ret = WCTYPE_NONEXISTENT;
376 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
377 if (globbed.gl_pathc > 0)
378 ret = WCTYPE_WILDCARD;
387 * Actually return matching file names for a local wildcard.
389 struct WildcardMatcher {
393 WildcardMatcher *begin_wildcard_matching(const char *name) {
394 WildcardMatcher *ret = snew(WildcardMatcher);
396 if (glob(name, 0, NULL, &ret->globbed) < 0) {
405 char *wildcard_get_filename(WildcardMatcher *dir) {
406 if (dir->i < dir->globbed.gl_pathc) {
407 return dupstr(dir->globbed.gl_pathv[dir->i++]);
411 void finish_wildcard_matching(WildcardMatcher *dir) {
412 globfree(&dir->globbed);
416 char *stripslashes(const char *str, int local)
421 * On Unix, we do the same thing regardless of the 'local'
424 p = strrchr(str, '/');
430 int vet_filename(const char *name)
432 if (strchr(name, '/'))
435 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
441 int create_directory(const char *name)
443 return mkdir(name, 0777) == 0;
446 char *dir_file_cat(const char *dir, const char *file)
448 return dupcat(dir, "/", file, NULL);
452 * Do a select() between all currently active network fds and
455 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
457 fd_set rset, wset, xset;
458 int i, fdcount, fdsize, *fdlist;
459 int fd, fdstate, rwx, ret, maxfd;
460 unsigned long now = GETTICKCOUNT();
464 fdcount = fdsize = 0;
468 /* Count the currently active fds. */
470 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
471 fd = next_fd(&fdstate, &rwx)) i++;
473 if (i < 1 && !no_fds_ok)
474 return -1; /* doom */
476 /* Expand the fdlist buffer if necessary. */
479 fdlist = sresize(fdlist, fdsize, int);
488 * Add all currently open fds to the select sets, and store
489 * them in fdlist as well.
492 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
493 fd = next_fd(&fdstate, &rwx)) {
494 fdlist[fdcount++] = fd;
496 FD_SET_MAX(fd, maxfd, rset);
498 FD_SET_MAX(fd, maxfd, wset);
500 FD_SET_MAX(fd, maxfd, xset);
504 FD_SET_MAX(0, maxfd, rset);
506 if (toplevel_callback_pending()) {
510 ret = select(maxfd, &rset, &wset, &xset, &tv);
512 run_toplevel_callbacks();
513 } else if (run_timers(now, &next)) {
520 now = GETTICKCOUNT();
521 if (now - then > next - then)
525 tv.tv_sec = ticks / 1000;
526 tv.tv_usec = ticks % 1000 * 1000;
527 ret = select(maxfd, &rset, &wset, &xset, &tv);
531 now = GETTICKCOUNT();
532 } while (ret < 0 && errno == EINTR);
534 ret = select(maxfd, &rset, &wset, &xset, NULL);
543 for (i = 0; i < fdcount; i++) {
546 * We must process exceptional notifications before
547 * ordinary readability ones, or we may go straight
548 * past the urgent marker.
550 if (FD_ISSET(fd, &xset))
551 select_result(fd, 4);
552 if (FD_ISSET(fd, &rset))
553 select_result(fd, 1);
554 if (FD_ISSET(fd, &wset))
555 select_result(fd, 2);
560 run_toplevel_callbacks();
562 return FD_ISSET(0, &rset) ? 1 : 0;
566 * Wait for some network data and process it.
568 int ssh_sftp_loop_iteration(void)
570 return ssh_sftp_do_select(FALSE, FALSE);
574 * Read a PSFTP command line from stdin.
576 char *ssh_sftp_get_cmdline(const char *prompt, int no_fds_ok)
579 int buflen, bufsize, ret;
581 fputs(prompt, stdout);
585 buflen = bufsize = 0;
588 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
590 printf("connection died\n");
592 return NULL; /* woop woop */
595 if (buflen >= bufsize) {
596 bufsize = buflen + 512;
597 buf = sresize(buf, bufsize, char);
599 ret = read(0, buf+buflen, 1);
606 /* eof on stdin; no error, but no answer either */
611 if (buf[buflen++] == '\n') {
612 /* we have a full line */
619 void frontend_net_error_pending(void) {}
622 * Main program: do platform-specific initialisation and then call
625 int main(int argc, char *argv[])
628 return psftp_main(argc, argv);