2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
24 * In PSFTP our selects are synchronous, so these functions are
27 int uxsel_input_add(int fd, int rwx) { return 0; }
28 void uxsel_input_remove(int id) { }
30 char *x_get_default(const char *key)
32 return NULL; /* this is a stub */
35 void platform_get_x11_auth(char *display, int *protocol,
36 unsigned char *data, int *datalen)
38 /* Do nothing, therefore no auth. */
42 * Default settings that are specific to PSFTP.
44 char *platform_default_s(const char *name)
49 int platform_default_i(const char *name, int def)
54 FontSpec platform_default_fontspec(const char *name)
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; }
74 * Stubs for the GUI feedback mechanism in Windows PSCP.
76 void gui_update_stats(char *name, unsigned long size,
77 int percentage, unsigned long elapsed,
78 unsigned long done, unsigned long eta,
79 unsigned long ratebs) {}
80 void gui_send_errcount(int list, int errs) {}
81 void gui_send_char(int is_stderr, int c) {}
82 void gui_enable(char *arg) {}
86 * Set local current directory. Returns NULL on success, or else an
87 * error message which must be freed after printing.
89 char *psftp_lcd(char *dir)
92 return dupprintf("%s: chdir: %s", dir, strerror(errno));
98 * Get local current directory. Returns a string which must be
101 char *psftp_getcwd(void)
106 buffer = snewn(size, char);
108 ret = getcwd(buffer, size);
111 if (errno != ERANGE) {
113 return dupprintf("[cwd unavailable: %s]", strerror(errno));
116 * Otherwise, ERANGE was returned, meaning the buffer
120 buffer = sresize(buffer, size, char);
128 RFile *open_existing_file(char *name, unsigned long *size,
129 unsigned long *mtime, unsigned long *atime)
134 fd = open(name, O_RDONLY);
141 if (size || mtime || atime) {
143 if (fstat(fd, &statbuf) < 0) {
144 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
145 memset(&statbuf, 0, sizeof(statbuf));
149 *size = statbuf.st_size;
152 *mtime = statbuf.st_mtime;
155 *atime = statbuf.st_atime;
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)
182 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
188 ret->name = dupstr(name);
193 int write_to_file(WFile *f, void *buffer, int length)
195 char *p = (char *)buffer;
198 /* Keep trying until we've really written as much as we can. */
200 int ret = write(f->fd, p, length);
216 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
226 /* Closes and frees the WFile */
227 void close_wfile(WFile *f)
234 int file_type(char *name)
238 if (stat(name, &statbuf) < 0) {
240 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
241 return FILE_TYPE_NONEXISTENT;
244 if (S_ISREG(statbuf.st_mode))
245 return FILE_TYPE_FILE;
247 if (S_ISDIR(statbuf.st_mode))
248 return FILE_TYPE_DIRECTORY;
250 return FILE_TYPE_WEIRD;
257 DirHandle *open_directory(char *name)
266 ret = snew(DirHandle);
271 char *read_filename(DirHandle *dir)
276 de = readdir(dir->dir);
279 } while ((de->d_name[0] == '.' &&
280 (de->d_name[1] == '\0' ||
281 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
283 return dupstr(de->d_name);
286 void close_directory(DirHandle *dir)
292 int test_wildcard(char *name, int cmdline)
296 if (stat(name, &statbuf) == 0) {
297 return WCTYPE_FILENAME;
298 } else if (cmdline) {
300 * On Unix, we never need to parse wildcards coming from
301 * the command line, because the shell will have expanded
302 * them into a filename list already.
304 return WCTYPE_NONEXISTENT;
307 int ret = WCTYPE_NONEXISTENT;
309 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
310 if (globbed.gl_pathc > 0)
311 ret = WCTYPE_WILDCARD;
320 * Actually return matching file names for a local wildcard.
322 struct WildcardMatcher {
326 WildcardMatcher *begin_wildcard_matching(char *name) {
327 WildcardMatcher *ret = snew(WildcardMatcher);
329 if (glob(name, 0, NULL, &ret->globbed) < 0) {
338 char *wildcard_get_filename(WildcardMatcher *dir) {
339 if (dir->i < dir->globbed.gl_pathc) {
340 return dupstr(dir->globbed.gl_pathv[dir->i++]);
344 void finish_wildcard_matching(WildcardMatcher *dir) {
345 globfree(&dir->globbed);
349 int vet_filename(char *name)
351 if (strchr(name, '/'))
354 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
360 int create_directory(char *name)
362 return mkdir(name, 0777) == 0;
365 char *dir_file_cat(char *dir, char *file)
367 return dupcat(dir, "/", file, NULL);
371 * Do a select() between all currently active network fds and
374 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
376 fd_set rset, wset, xset;
377 int i, fdcount, fdsize, *fdlist;
378 int fd, fdstate, rwx, ret, maxfd;
379 long now = GETTICKCOUNT();
382 fdcount = fdsize = 0;
386 /* Count the currently active fds. */
388 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
389 fd = next_fd(&fdstate, &rwx)) i++;
391 if (i < 1 && !no_fds_ok)
392 return -1; /* doom */
394 /* Expand the fdlist buffer if necessary. */
397 fdlist = sresize(fdlist, fdsize, int);
406 * Add all currently open fds to the select sets, and store
407 * them in fdlist as well.
410 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
411 fd = next_fd(&fdstate, &rwx)) {
412 fdlist[fdcount++] = fd;
414 FD_SET_MAX(fd, maxfd, rset);
416 FD_SET_MAX(fd, maxfd, wset);
418 FD_SET_MAX(fd, maxfd, xset);
422 FD_SET_MAX(0, maxfd, rset);
426 struct timeval tv, *ptv;
428 if (run_timers(now, &next)) {
429 ticks = next - GETTICKCOUNT();
431 ticks = 1; /* just in case */
432 tv.tv_sec = ticks / 1000;
433 tv.tv_usec = ticks % 1000 * 1000;
438 ret = select(maxfd, &rset, &wset, &xset, ptv);
442 long newnow = GETTICKCOUNT();
444 * Check to see whether the system clock has
445 * changed massively during the select.
447 if (newnow - now < 0 || newnow - now > next - now) {
449 * If so, look at the elapsed time in the
450 * select and use it to compute a new
453 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
454 /* So we'd like GETTICKCOUNT to have returned othernow,
455 * but instead it return newnow. Hence ... */
456 tickcount_offset += othernow - newnow;
462 } while (ret < 0 && errno != EINTR);
470 for (i = 0; i < fdcount; i++) {
473 * We must process exceptional notifications before
474 * ordinary readability ones, or we may go straight
475 * past the urgent marker.
477 if (FD_ISSET(fd, &xset))
478 select_result(fd, 4);
479 if (FD_ISSET(fd, &rset))
480 select_result(fd, 1);
481 if (FD_ISSET(fd, &wset))
482 select_result(fd, 2);
487 return FD_ISSET(0, &rset) ? 1 : 0;
491 * Wait for some network data and process it.
493 int ssh_sftp_loop_iteration(void)
495 return ssh_sftp_do_select(FALSE, FALSE);
499 * Read a PSFTP command line from stdin.
501 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
504 int buflen, bufsize, ret;
506 fputs(prompt, stdout);
510 buflen = bufsize = 0;
513 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
515 printf("connection died\n");
516 return NULL; /* woop woop */
519 if (buflen >= bufsize) {
520 bufsize = buflen + 512;
521 buf = sresize(buf, bufsize, char);
523 ret = read(0, buf+buflen, 1);
529 /* eof on stdin; no error, but no answer either */
533 if (buf[buflen++] == '\n') {
534 /* we have a full line */
542 * Main program: do platform-specific initialisation and then call
545 int main(int argc, char *argv[])
548 return psftp_main(argc, argv);