2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
25 * In PSFTP our selects are synchronous, so these functions are
28 int uxsel_input_add(int fd, int rwx) { return 0; }
29 void uxsel_input_remove(int id) { }
31 char *x_get_default(const char *key)
33 return NULL; /* this is a stub */
36 void platform_get_x11_auth(char *display, int *protocol,
37 unsigned char *data, int *datalen)
39 /* Do nothing, therefore no auth. */
43 * Default settings that are specific to PSFTP.
45 char *platform_default_s(const char *name)
50 int platform_default_i(const char *name, int def)
55 FontSpec platform_default_fontspec(const char *name)
62 Filename platform_default_filename(const char *name)
65 if (!strcmp(name, "LogFileName"))
66 strcpy(ret.path, "putty.log");
72 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
74 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
77 ret = cmdline_get_passwd_input(p, in, inlen);
79 ret = console_get_userpass_input(p, in, inlen);
84 * Set local current directory. Returns NULL on success, or else an
85 * error message which must be freed after printing.
87 char *psftp_lcd(char *dir)
90 return dupprintf("%s: chdir: %s", dir, strerror(errno));
96 * Get local current directory. Returns a string which must be
99 char *psftp_getcwd(void)
104 buffer = snewn(size, char);
106 ret = getcwd(buffer, size);
109 if (errno != ERANGE) {
111 return dupprintf("[cwd unavailable: %s]", strerror(errno));
114 * Otherwise, ERANGE was returned, meaning the buffer
118 buffer = sresize(buffer, size, char);
126 RFile *open_existing_file(char *name, uint64 *size,
127 unsigned long *mtime, unsigned long *atime)
132 fd = open(name, O_RDONLY);
139 if (size || mtime || atime) {
141 if (fstat(fd, &statbuf) < 0) {
142 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
143 memset(&statbuf, 0, sizeof(statbuf));
147 if (sizeof(statbuf.st_size) == 8) {
148 size->hi = statbuf.st_size >> 32;
149 size->lo = (long) statbuf.st_size;
151 *size = uint64_make(0, statbuf.st_size);
156 *mtime = statbuf.st_mtime;
159 *atime = statbuf.st_atime;
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)
186 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
192 ret->name = dupstr(name);
198 WFile *open_existing_wfile(char *name, uint64 *size)
203 fd = open(name, O_APPEND | O_WRONLY);
212 if (fstat(fd, &statbuf) < 0) {
213 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
214 memset(&statbuf, 0, sizeof(statbuf));
217 if (sizeof(statbuf.st_size) == 8) {
218 size->hi = statbuf.st_size >> 32;
219 size->lo = (long) statbuf.st_size;
221 *size = uint64_make(0, statbuf.st_size);
228 int write_to_file(WFile *f, void *buffer, int length)
230 char *p = (char *)buffer;
233 /* Keep trying until we've really written as much as we can. */
235 int ret = write(f->fd, p, length);
251 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
261 /* Closes and frees the WFile */
262 void close_wfile(WFile *f)
269 /* Seek offset bytes through file, from whence, where whence is
270 FROM_START, FROM_CURRENT, or FROM_END */
271 int seek_file(WFile *f, uint64 offset, int whence)
276 if (sizeof(off_t) == 8) {
277 fileofft = ((off_t) offset.hi << 32) + offset.lo;
279 fileofft = offset.lo;
284 lseek_whence = SEEK_SET;
287 lseek_whence = SEEK_CUR;
290 lseek_whence = SEEK_END;
296 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
299 uint64 get_file_posn(WFile *f)
304 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
306 if (sizeof(off_t) == 8) {
307 ret.hi = fileofft >> 32;
308 ret.lo = (long) fileofft;
310 ret = uint64_make(0, fileofft);
316 int file_type(char *name)
320 if (stat(name, &statbuf) < 0) {
322 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
323 return FILE_TYPE_NONEXISTENT;
326 if (S_ISREG(statbuf.st_mode))
327 return FILE_TYPE_FILE;
329 if (S_ISDIR(statbuf.st_mode))
330 return FILE_TYPE_DIRECTORY;
332 return FILE_TYPE_WEIRD;
339 DirHandle *open_directory(char *name)
348 ret = snew(DirHandle);
353 char *read_filename(DirHandle *dir)
358 de = readdir(dir->dir);
361 } while ((de->d_name[0] == '.' &&
362 (de->d_name[1] == '\0' ||
363 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
365 return dupstr(de->d_name);
368 void close_directory(DirHandle *dir)
374 int test_wildcard(char *name, int cmdline)
378 if (stat(name, &statbuf) == 0) {
379 return WCTYPE_FILENAME;
380 } else if (cmdline) {
382 * On Unix, we never need to parse wildcards coming from
383 * the command line, because the shell will have expanded
384 * them into a filename list already.
386 return WCTYPE_NONEXISTENT;
389 int ret = WCTYPE_NONEXISTENT;
391 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
392 if (globbed.gl_pathc > 0)
393 ret = WCTYPE_WILDCARD;
402 * Actually return matching file names for a local wildcard.
404 struct WildcardMatcher {
408 WildcardMatcher *begin_wildcard_matching(char *name) {
409 WildcardMatcher *ret = snew(WildcardMatcher);
411 if (glob(name, 0, NULL, &ret->globbed) < 0) {
420 char *wildcard_get_filename(WildcardMatcher *dir) {
421 if (dir->i < dir->globbed.gl_pathc) {
422 return dupstr(dir->globbed.gl_pathv[dir->i++]);
426 void finish_wildcard_matching(WildcardMatcher *dir) {
427 globfree(&dir->globbed);
431 int vet_filename(char *name)
433 if (strchr(name, '/'))
436 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
442 int create_directory(char *name)
444 return mkdir(name, 0777) == 0;
447 char *dir_file_cat(char *dir, char *file)
449 return dupcat(dir, "/", file, NULL);
453 * Do a select() between all currently active network fds and
456 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
458 fd_set rset, wset, xset;
459 int i, fdcount, fdsize, *fdlist;
460 int fd, fdstate, rwx, ret, maxfd;
461 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);
508 struct timeval tv, *ptv;
510 if (run_timers(now, &next)) {
511 ticks = next - GETTICKCOUNT();
513 ticks = 1; /* just in case */
514 tv.tv_sec = ticks / 1000;
515 tv.tv_usec = ticks % 1000 * 1000;
520 ret = select(maxfd, &rset, &wset, &xset, ptv);
524 long newnow = GETTICKCOUNT();
526 * Check to see whether the system clock has
527 * changed massively during the select.
529 if (newnow - now < 0 || newnow - now > next - now) {
531 * If so, look at the elapsed time in the
532 * select and use it to compute a new
535 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
536 /* So we'd like GETTICKCOUNT to have returned othernow,
537 * but instead it return newnow. Hence ... */
538 tickcount_offset += othernow - newnow;
544 } while (ret < 0 && errno != EINTR);
552 for (i = 0; i < fdcount; i++) {
555 * We must process exceptional notifications before
556 * ordinary readability ones, or we may go straight
557 * past the urgent marker.
559 if (FD_ISSET(fd, &xset))
560 select_result(fd, 4);
561 if (FD_ISSET(fd, &rset))
562 select_result(fd, 1);
563 if (FD_ISSET(fd, &wset))
564 select_result(fd, 2);
569 return FD_ISSET(0, &rset) ? 1 : 0;
573 * Wait for some network data and process it.
575 int ssh_sftp_loop_iteration(void)
577 return ssh_sftp_do_select(FALSE, FALSE);
581 * Read a PSFTP command line from stdin.
583 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
586 int buflen, bufsize, ret;
588 fputs(prompt, stdout);
592 buflen = bufsize = 0;
595 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
597 printf("connection died\n");
598 return NULL; /* woop woop */
601 if (buflen >= bufsize) {
602 bufsize = buflen + 512;
603 buf = sresize(buf, bufsize, char);
605 ret = read(0, buf+buflen, 1);
611 /* eof on stdin; no error, but no answer either */
615 if (buf[buflen++] == '\n') {
616 /* we have a full line */
624 * Main program: do platform-specific initialisation and then call
627 int main(int argc, char *argv[])
630 return psftp_main(argc, argv);