2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
\r
5 #include <sys/time.h>
\r
6 #include <sys/types.h>
\r
7 #include <sys/stat.h>
\r
16 #ifndef HAVE_NO_SYS_SELECT_H
\r
17 #include <sys/select.h>
\r
26 * In PSFTP our selects are synchronous, so these functions are
\r
29 int uxsel_input_add(int fd, int rwx) { return 0; }
\r
30 void uxsel_input_remove(int id) { }
\r
32 char *x_get_default(const char *key)
\r
34 return NULL; /* this is a stub */
\r
37 void platform_get_x11_auth(struct X11Display *display, const Config *cfg)
\r
39 /* Do nothing, therefore no auth. */
\r
41 const int platform_uses_x11_unix_by_default = TRUE;
\r
44 * Default settings that are specific to PSFTP.
\r
46 char *platform_default_s(const char *name)
\r
51 int platform_default_i(const char *name, int def)
\r
56 FontSpec platform_default_fontspec(const char *name)
\r
63 Filename platform_default_filename(const char *name)
\r
66 if (!strcmp(name, "LogFileName"))
\r
67 strcpy(ret.path, "putty.log");
\r
73 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
\r
75 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
\r
78 ret = cmdline_get_passwd_input(p, in, inlen);
\r
80 ret = console_get_userpass_input(p, in, inlen);
\r
85 * Set local current directory. Returns NULL on success, or else an
\r
86 * error message which must be freed after printing.
\r
88 char *psftp_lcd(char *dir)
\r
91 return dupprintf("%s: chdir: %s", dir, strerror(errno));
\r
97 * Get local current directory. Returns a string which must be
\r
100 char *psftp_getcwd(void)
\r
102 char *buffer, *ret;
\r
105 buffer = snewn(size, char);
\r
107 ret = getcwd(buffer, size);
\r
110 if (errno != ERANGE) {
\r
112 return dupprintf("[cwd unavailable: %s]", strerror(errno));
\r
115 * Otherwise, ERANGE was returned, meaning the buffer
\r
116 * wasn't big enough.
\r
118 size = size * 3 / 2;
\r
119 buffer = sresize(buffer, size, char);
\r
127 RFile *open_existing_file(char *name, uint64 *size,
\r
128 unsigned long *mtime, unsigned long *atime)
\r
133 fd = open(name, O_RDONLY);
\r
140 if (size || mtime || atime) {
\r
141 struct stat statbuf;
\r
142 if (fstat(fd, &statbuf) < 0) {
\r
143 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
\r
144 memset(&statbuf, 0, sizeof(statbuf));
\r
148 *size = uint64_make((statbuf.st_size >> 16) >> 16,
\r
152 *mtime = statbuf.st_mtime;
\r
155 *atime = statbuf.st_atime;
\r
161 int read_from_file(RFile *f, void *buffer, int length)
\r
163 return read(f->fd, buffer, length);
\r
166 void close_rfile(RFile *f)
\r
177 WFile *open_new_file(char *name)
\r
182 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
\r
188 ret->name = dupstr(name);
\r
194 WFile *open_existing_wfile(char *name, uint64 *size)
\r
199 fd = open(name, O_APPEND | O_WRONLY);
\r
205 ret->name = dupstr(name);
\r
208 struct stat statbuf;
\r
209 if (fstat(fd, &statbuf) < 0) {
\r
210 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
\r
211 memset(&statbuf, 0, sizeof(statbuf));
\r
214 *size = uint64_make((statbuf.st_size >> 16) >> 16,
\r
221 int write_to_file(WFile *f, void *buffer, int length)
\r
223 char *p = (char *)buffer;
\r
226 /* Keep trying until we've really written as much as we can. */
\r
227 while (length > 0) {
\r
228 int ret = write(f->fd, p, length);
\r
244 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
\r
249 ut.modtime = mtime;
\r
251 utime(f->name, &ut);
\r
254 /* Closes and frees the WFile */
\r
255 void close_wfile(WFile *f)
\r
262 /* Seek offset bytes through file, from whence, where whence is
\r
263 FROM_START, FROM_CURRENT, or FROM_END */
\r
264 int seek_file(WFile *f, uint64 offset, int whence)
\r
269 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
\r
273 lseek_whence = SEEK_SET;
\r
276 lseek_whence = SEEK_CUR;
\r
279 lseek_whence = SEEK_END;
\r
285 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
\r
288 uint64 get_file_posn(WFile *f)
\r
293 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
\r
295 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
\r
300 int file_type(char *name)
\r
302 struct stat statbuf;
\r
304 if (stat(name, &statbuf) < 0) {
\r
305 if (errno != ENOENT)
\r
306 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
\r
307 return FILE_TYPE_NONEXISTENT;
\r
310 if (S_ISREG(statbuf.st_mode))
\r
311 return FILE_TYPE_FILE;
\r
313 if (S_ISDIR(statbuf.st_mode))
\r
314 return FILE_TYPE_DIRECTORY;
\r
316 return FILE_TYPE_WEIRD;
\r
323 DirHandle *open_directory(char *name)
\r
328 dir = opendir(name);
\r
332 ret = snew(DirHandle);
\r
337 char *read_filename(DirHandle *dir)
\r
342 de = readdir(dir->dir);
\r
345 } while ((de->d_name[0] == '.' &&
\r
346 (de->d_name[1] == '\0' ||
\r
347 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
\r
349 return dupstr(de->d_name);
\r
352 void close_directory(DirHandle *dir)
\r
354 closedir(dir->dir);
\r
358 int test_wildcard(char *name, int cmdline)
\r
360 struct stat statbuf;
\r
362 if (stat(name, &statbuf) == 0) {
\r
363 return WCTYPE_FILENAME;
\r
364 } else if (cmdline) {
\r
366 * On Unix, we never need to parse wildcards coming from
\r
367 * the command line, because the shell will have expanded
\r
368 * them into a filename list already.
\r
370 return WCTYPE_NONEXISTENT;
\r
373 int ret = WCTYPE_NONEXISTENT;
\r
375 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
\r
376 if (globbed.gl_pathc > 0)
\r
377 ret = WCTYPE_WILDCARD;
\r
378 globfree(&globbed);
\r
386 * Actually return matching file names for a local wildcard.
\r
388 struct WildcardMatcher {
\r
392 WildcardMatcher *begin_wildcard_matching(char *name) {
\r
393 WildcardMatcher *ret = snew(WildcardMatcher);
\r
395 if (glob(name, 0, NULL, &ret->globbed) < 0) {
\r
404 char *wildcard_get_filename(WildcardMatcher *dir) {
\r
405 if (dir->i < dir->globbed.gl_pathc) {
\r
406 return dupstr(dir->globbed.gl_pathv[dir->i++]);
\r
410 void finish_wildcard_matching(WildcardMatcher *dir) {
\r
411 globfree(&dir->globbed);
\r
415 int vet_filename(char *name)
\r
417 if (strchr(name, '/'))
\r
420 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
\r
426 int create_directory(char *name)
\r
428 return mkdir(name, 0777) == 0;
\r
431 char *dir_file_cat(char *dir, char *file)
\r
433 return dupcat(dir, "/", file, NULL);
\r
437 * Do a select() between all currently active network fds and
\r
438 * optionally stdin.
\r
440 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
\r
442 fd_set rset, wset, xset;
\r
443 int i, fdcount, fdsize, *fdlist;
\r
444 int fd, fdstate, rwx, ret, maxfd;
\r
445 long now = GETTICKCOUNT();
\r
448 fdcount = fdsize = 0;
\r
452 /* Count the currently active fds. */
\r
454 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
\r
455 fd = next_fd(&fdstate, &rwx)) i++;
\r
457 if (i < 1 && !no_fds_ok)
\r
458 return -1; /* doom */
\r
460 /* Expand the fdlist buffer if necessary. */
\r
463 fdlist = sresize(fdlist, fdsize, int);
\r
472 * Add all currently open fds to the select sets, and store
\r
473 * them in fdlist as well.
\r
476 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
\r
477 fd = next_fd(&fdstate, &rwx)) {
\r
478 fdlist[fdcount++] = fd;
\r
480 FD_SET_MAX(fd, maxfd, rset);
\r
482 FD_SET_MAX(fd, maxfd, wset);
\r
484 FD_SET_MAX(fd, maxfd, xset);
\r
488 FD_SET_MAX(0, maxfd, rset);
\r
492 struct timeval tv, *ptv;
\r
494 if (run_timers(now, &next)) {
\r
495 ticks = next - GETTICKCOUNT();
\r
497 ticks = 1; /* just in case */
\r
498 tv.tv_sec = ticks / 1000;
\r
499 tv.tv_usec = ticks % 1000 * 1000;
\r
504 ret = select(maxfd, &rset, &wset, &xset, ptv);
\r
508 long newnow = GETTICKCOUNT();
\r
510 * Check to see whether the system clock has
\r
511 * changed massively during the select.
\r
513 if (newnow - now < 0 || newnow - now > next - now) {
\r
515 * If so, look at the elapsed time in the
\r
516 * select and use it to compute a new
\r
517 * tickcount_offset.
\r
519 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
\r
520 /* So we'd like GETTICKCOUNT to have returned othernow,
\r
521 * but instead it return newnow. Hence ... */
\r
522 tickcount_offset += othernow - newnow;
\r
528 } while (ret < 0 && errno != EINTR);
\r
529 } while (ret == 0);
\r
536 for (i = 0; i < fdcount; i++) {
\r
539 * We must process exceptional notifications before
\r
540 * ordinary readability ones, or we may go straight
\r
541 * past the urgent marker.
\r
543 if (FD_ISSET(fd, &xset))
\r
544 select_result(fd, 4);
\r
545 if (FD_ISSET(fd, &rset))
\r
546 select_result(fd, 1);
\r
547 if (FD_ISSET(fd, &wset))
\r
548 select_result(fd, 2);
\r
553 return FD_ISSET(0, &rset) ? 1 : 0;
\r
557 * Wait for some network data and process it.
\r
559 int ssh_sftp_loop_iteration(void)
\r
561 return ssh_sftp_do_select(FALSE, FALSE);
\r
565 * Read a PSFTP command line from stdin.
\r
567 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
\r
570 int buflen, bufsize, ret;
\r
572 fputs(prompt, stdout);
\r
576 buflen = bufsize = 0;
\r
579 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
\r
581 printf("connection died\n");
\r
582 return NULL; /* woop woop */
\r
585 if (buflen >= bufsize) {
\r
586 bufsize = buflen + 512;
\r
587 buf = sresize(buf, bufsize, char);
\r
589 ret = read(0, buf+buflen, 1);
\r
595 /* eof on stdin; no error, but no answer either */
\r
599 if (buf[buflen++] == '\n') {
\r
600 /* we have a full line */
\r
608 * Main program: do platform-specific initialisation and then call
\r
611 int main(int argc, char *argv[])
\r
614 return psftp_main(argc, argv);
\r