2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
21 * In PSFTP our selects are synchronous, so these functions are
24 int uxsel_input_add(int fd, int rwx) { return 0; }
25 void uxsel_input_remove(int id) { }
27 char *x_get_default(const char *key)
29 return NULL; /* this is a stub */
32 void platform_get_x11_auth(char *display, int *protocol,
33 unsigned char *data, int *datalen)
35 /* Do nothing, therefore no auth. */
39 * Default settings that are specific to PSFTP.
41 char *platform_default_s(const char *name)
46 int platform_default_i(const char *name, int def)
51 FontSpec platform_default_fontspec(const char *name)
58 Filename platform_default_filename(const char *name)
61 if (!strcmp(name, "LogFileName"))
62 strcpy(ret.path, "putty.log");
69 * Stubs for the GUI feedback mechanism in Windows PSCP.
71 void gui_update_stats(char *name, unsigned long size,
72 int percentage, unsigned long elapsed,
73 unsigned long done, unsigned long eta,
74 unsigned long ratebs) {}
75 void gui_send_errcount(int list, int errs) {}
76 void gui_send_char(int is_stderr, int c) {}
77 void gui_enable(char *arg) {}
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, unsigned long *size,
124 unsigned long *mtime, unsigned long *atime)
129 fd = open(name, O_RDONLY);
136 if (size || mtime || atime) {
138 if (fstat(fd, &statbuf) < 0) {
139 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
140 memset(&statbuf, 0, sizeof(statbuf));
144 *size = statbuf.st_size;
147 *mtime = statbuf.st_mtime;
150 *atime = statbuf.st_atime;
156 int read_from_file(RFile *f, void *buffer, int length)
158 return read(f->fd, buffer, length);
161 void close_rfile(RFile *f)
172 WFile *open_new_file(char *name)
177 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
183 ret->name = dupstr(name);
188 int write_to_file(WFile *f, void *buffer, int length)
190 char *p = (char *)buffer;
193 /* Keep trying until we've really written as much as we can. */
195 int ret = write(f->fd, p, length);
211 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
221 /* Closes and frees the WFile */
222 void close_wfile(WFile *f)
229 int file_type(char *name)
233 if (stat(name, &statbuf) < 0) {
235 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
236 return FILE_TYPE_NONEXISTENT;
239 if (S_ISREG(statbuf.st_mode))
240 return FILE_TYPE_FILE;
242 if (S_ISDIR(statbuf.st_mode))
243 return FILE_TYPE_DIRECTORY;
245 return FILE_TYPE_WEIRD;
252 DirHandle *open_directory(char *name)
261 ret = snew(DirHandle);
266 char *read_filename(DirHandle *dir)
271 de = readdir(dir->dir);
274 } while ((de->d_name[0] == '.' &&
275 (de->d_name[1] == '\0' ||
276 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
278 return dupstr(de->d_name);
281 void close_directory(DirHandle *dir)
287 int test_wildcard(char *name, int cmdline)
291 if (stat(name, &statbuf) == 0) {
292 return WCTYPE_FILENAME;
293 } else if (cmdline) {
295 * On Unix, we never need to parse wildcards coming from
296 * the command line, because the shell will have expanded
297 * them into a filename list already.
299 return WCTYPE_NONEXISTENT;
302 int ret = WCTYPE_NONEXISTENT;
304 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
305 if (globbed.gl_pathc > 0)
306 ret = WCTYPE_WILDCARD;
315 * Actually return matching file names for a local wildcard.
317 struct WildcardMatcher {
321 WildcardMatcher *begin_wildcard_matching(char *name) {
322 WildcardMatcher *ret = snew(WildcardMatcher);
324 if (glob(name, 0, NULL, &ret->globbed) < 0) {
333 char *wildcard_get_filename(WildcardMatcher *dir) {
334 if (dir->i < dir->globbed.gl_pathc) {
335 return dupstr(dir->globbed.gl_pathv[dir->i++]);
339 void finish_wildcard_matching(WildcardMatcher *dir) {
340 globfree(&dir->globbed);
344 int vet_filename(char *name)
346 if (strchr(name, '/'))
349 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
355 int create_directory(char *name)
357 return mkdir(name, 0777) == 0;
360 char *dir_file_cat(char *dir, char *file)
362 return dupcat(dir, "/", file, NULL);
366 * Do a select() between all currently active network fds and
369 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
371 fd_set rset, wset, xset;
372 int i, fdcount, fdsize, *fdlist;
373 int fd, fdstate, rwx, ret, maxfd;
374 long now = GETTICKCOUNT();
377 fdcount = fdsize = 0;
381 /* Count the currently active fds. */
383 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
384 fd = next_fd(&fdstate, &rwx)) i++;
386 if (i < 1 && !no_fds_ok)
387 return -1; /* doom */
389 /* Expand the fdlist buffer if necessary. */
392 fdlist = sresize(fdlist, fdsize, int);
401 * Add all currently open fds to the select sets, and store
402 * them in fdlist as well.
405 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
406 fd = next_fd(&fdstate, &rwx)) {
407 fdlist[fdcount++] = fd;
409 FD_SET_MAX(fd, maxfd, rset);
411 FD_SET_MAX(fd, maxfd, wset);
413 FD_SET_MAX(fd, maxfd, xset);
417 FD_SET_MAX(0, maxfd, rset);
421 struct timeval tv, *ptv;
423 if (run_timers(now, &next)) {
424 ticks = next - GETTICKCOUNT();
426 ticks = 1; /* just in case */
427 tv.tv_sec = ticks / 1000;
428 tv.tv_usec = ticks % 1000 * 1000;
433 ret = select(maxfd, &rset, &wset, &xset, ptv);
437 now = GETTICKCOUNT();
438 } while (ret < 0 && errno != EINTR);
446 for (i = 0; i < fdcount; i++) {
449 * We must process exceptional notifications before
450 * ordinary readability ones, or we may go straight
451 * past the urgent marker.
453 if (FD_ISSET(fd, &xset))
454 select_result(fd, 4);
455 if (FD_ISSET(fd, &rset))
456 select_result(fd, 1);
457 if (FD_ISSET(fd, &wset))
458 select_result(fd, 2);
463 return FD_ISSET(0, &rset) ? 1 : 0;
467 * Wait for some network data and process it.
469 int ssh_sftp_loop_iteration(void)
471 return ssh_sftp_do_select(FALSE, FALSE);
475 * Read a PSFTP command line from stdin.
477 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
480 int buflen, bufsize, ret;
482 fputs(prompt, stdout);
486 buflen = bufsize = 0;
489 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
491 printf("connection died\n");
492 return NULL; /* woop woop */
495 if (buflen >= bufsize) {
496 bufsize = buflen + 512;
497 buf = sresize(buf, bufsize, char);
499 ret = read(0, buf+buflen, 1);
505 /* eof on stdin; no error, but no answer either */
509 if (buf[buflen++] == '\n') {
510 /* we have a full line */
518 * Main program: do platform-specific initialisation and then call
521 int main(int argc, char *argv[])
524 return psftp_main(argc, argv);