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)
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, 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(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(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(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(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(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(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(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 int vet_filename(char *name)
418 if (strchr(name, '/'))
421 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
427 int create_directory(char *name)
429 return mkdir(name, 0777) == 0;
432 char *dir_file_cat(char *dir, char *file)
434 return dupcat(dir, "/", file, NULL);
438 * Do a select() between all currently active network fds and
441 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
443 fd_set rset, wset, xset;
444 int i, fdcount, fdsize, *fdlist;
445 int fd, fdstate, rwx, ret, maxfd;
446 unsigned long now = GETTICKCOUNT();
450 fdcount = fdsize = 0;
454 /* Count the currently active fds. */
456 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
457 fd = next_fd(&fdstate, &rwx)) i++;
459 if (i < 1 && !no_fds_ok)
460 return -1; /* doom */
462 /* Expand the fdlist buffer if necessary. */
465 fdlist = sresize(fdlist, fdsize, int);
474 * Add all currently open fds to the select sets, and store
475 * them in fdlist as well.
478 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
479 fd = next_fd(&fdstate, &rwx)) {
480 fdlist[fdcount++] = fd;
482 FD_SET_MAX(fd, maxfd, rset);
484 FD_SET_MAX(fd, maxfd, wset);
486 FD_SET_MAX(fd, maxfd, xset);
490 FD_SET_MAX(0, maxfd, rset);
492 if (toplevel_callback_pending()) {
496 ret = select(maxfd, &rset, &wset, &xset, &tv);
498 run_toplevel_callbacks();
499 } else if (run_timers(now, &next)) {
506 now = GETTICKCOUNT();
507 if (now - then > next - then)
511 tv.tv_sec = ticks / 1000;
512 tv.tv_usec = ticks % 1000 * 1000;
513 ret = select(maxfd, &rset, &wset, &xset, &tv);
517 now = GETTICKCOUNT();
518 } while (ret < 0 && errno == EINTR);
520 ret = select(maxfd, &rset, &wset, &xset, NULL);
529 for (i = 0; i < fdcount; i++) {
532 * We must process exceptional notifications before
533 * ordinary readability ones, or we may go straight
534 * past the urgent marker.
536 if (FD_ISSET(fd, &xset))
537 select_result(fd, 4);
538 if (FD_ISSET(fd, &rset))
539 select_result(fd, 1);
540 if (FD_ISSET(fd, &wset))
541 select_result(fd, 2);
546 run_toplevel_callbacks();
548 return FD_ISSET(0, &rset) ? 1 : 0;
552 * Wait for some network data and process it.
554 int ssh_sftp_loop_iteration(void)
556 return ssh_sftp_do_select(FALSE, FALSE);
560 * Read a PSFTP command line from stdin.
562 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
565 int buflen, bufsize, ret;
567 fputs(prompt, stdout);
571 buflen = bufsize = 0;
574 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
576 printf("connection died\n");
578 return NULL; /* woop woop */
581 if (buflen >= bufsize) {
582 bufsize = buflen + 512;
583 buf = sresize(buf, bufsize, char);
585 ret = read(0, buf+buflen, 1);
592 /* eof on stdin; no error, but no answer either */
597 if (buf[buflen++] == '\n') {
598 /* we have a full line */
605 void frontend_net_error_pending(void) {}
608 * Main program: do platform-specific initialisation and then call
611 int main(int argc, char *argv[])
614 return psftp_main(argc, argv);