2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
20 * In PSFTP our selects are synchronous, so these functions are
23 int uxsel_input_add(int fd, int rwx) { return 0; }
24 void uxsel_input_remove(int id) { }
26 char *x_get_default(const char *key)
28 return NULL; /* this is a stub */
31 void platform_get_x11_auth(char *display, int *protocol,
32 unsigned char *data, int *datalen)
34 /* Do nothing, therefore no auth. */
37 const char platform_x11_best_transport[] = "unix";
40 * Default settings that are specific to PSFTP.
42 char *platform_default_s(const char *name)
47 int platform_default_i(const char *name, int def)
52 FontSpec platform_default_fontspec(const char *name)
59 Filename platform_default_filename(const char *name)
62 if (!strcmp(name, "LogFileName"))
63 strcpy(ret.path, "putty.log");
70 * Stubs for the GUI feedback mechanism in Windows PSCP.
72 void gui_update_stats(char *name, unsigned long size,
73 int percentage, unsigned long elapsed,
74 unsigned long done, unsigned long eta,
75 unsigned long ratebs) {}
76 void gui_send_errcount(int list, int errs) {}
77 void gui_send_char(int is_stderr, int c) {}
78 void gui_enable(char *arg) {}
82 * Set local current directory. Returns NULL on success, or else an
83 * error message which must be freed after printing.
85 char *psftp_lcd(char *dir)
88 return dupprintf("%s: chdir: %s", dir, strerror(errno));
94 * Get local current directory. Returns a string which must be
97 char *psftp_getcwd(void)
102 buffer = snewn(size, char);
104 ret = getcwd(buffer, size);
107 if (errno != ERANGE) {
109 return dupprintf("[cwd unavailable: %s]", strerror(errno));
112 * Otherwise, ERANGE was returned, meaning the buffer
116 buffer = sresize(buffer, size, char);
124 RFile *open_existing_file(char *name, unsigned long *size,
125 unsigned long *mtime, unsigned long *atime)
130 fd = open(name, O_RDONLY);
137 if (size || mtime || atime) {
139 if (fstat(fd, &statbuf) < 0) {
140 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
141 memset(&statbuf, 0, sizeof(statbuf));
145 *size = statbuf.st_size;
148 *mtime = statbuf.st_mtime;
151 *atime = statbuf.st_atime;
157 int read_from_file(RFile *f, void *buffer, int length)
159 return read(f->fd, buffer, length);
162 void close_rfile(RFile *f)
173 WFile *open_new_file(char *name)
178 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
184 ret->name = dupstr(name);
189 int write_to_file(WFile *f, void *buffer, int length)
191 char *p = (char *)buffer;
194 /* Keep trying until we've really written as much as we can. */
196 int ret = write(f->fd, p, length);
212 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
222 /* Closes and frees the WFile */
223 void close_wfile(WFile *f)
230 int file_type(char *name)
234 if (stat(name, &statbuf) < 0) {
236 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
237 return FILE_TYPE_NONEXISTENT;
240 if (S_ISREG(statbuf.st_mode))
241 return FILE_TYPE_FILE;
243 if (S_ISDIR(statbuf.st_mode))
244 return FILE_TYPE_DIRECTORY;
246 return FILE_TYPE_WEIRD;
253 DirHandle *open_directory(char *name)
262 ret = snew(DirHandle);
267 char *read_filename(DirHandle *dir)
272 de = readdir(dir->dir);
275 } while ((de->d_name[0] == '.' &&
276 (de->d_name[1] == '\0' ||
277 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
279 return dupstr(de->d_name);
282 void close_directory(DirHandle *dir)
288 int test_wildcard(char *name, int cmdline)
291 * On Unix, we currently don't support local wildcards at all.
292 * We will have to do so (FIXME) once PSFTP starts implementing
293 * mput, but until then we can assume `cmdline' is always set.
298 if (stat(name, &statbuf) < 0)
299 return WCTYPE_NONEXISTENT;
301 return WCTYPE_FILENAME;
305 * Actually return matching file names for a local wildcard. FIXME:
306 * we currently don't support this at all.
308 struct WildcardMatcher {
311 WildcardMatcher *begin_wildcard_matching(char *name) { return NULL; }
312 char *wildcard_get_filename(WildcardMatcher *dir) { return NULL; }
313 void finish_wildcard_matching(WildcardMatcher *dir) {}
315 int create_directory(char *name)
317 return mkdir(name, 0777) == 0;
320 char *dir_file_cat(char *dir, char *file)
322 return dupcat(dir, "/", file, NULL);
326 * Wait for some network data and process it.
328 int ssh_sftp_loop_iteration(void)
330 fd_set rset, wset, xset;
331 int i, fdcount, fdsize, *fdlist;
332 int fd, fdstate, rwx, ret, maxfd;
335 fdcount = fdsize = 0;
337 /* Count the currently active fds. */
339 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
340 fd = next_fd(&fdstate, &rwx)) i++;
343 return -1; /* doom */
345 /* Expand the fdlist buffer if necessary. */
348 fdlist = sresize(fdlist, fdsize, int);
357 * Add all currently open fds to the select sets, and store
358 * them in fdlist as well.
361 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
362 fd = next_fd(&fdstate, &rwx)) {
363 fdlist[fdcount++] = fd;
365 FD_SET_MAX(fd, maxfd, rset);
367 FD_SET_MAX(fd, maxfd, wset);
369 FD_SET_MAX(fd, maxfd, xset);
373 ret = select(maxfd, &rset, &wset, &xset, NULL);
374 } while (ret < 0 && errno == EINTR);
381 for (i = 0; i < fdcount; i++) {
384 * We must process exceptional notifications before
385 * ordinary readability ones, or we may go straight
386 * past the urgent marker.
388 if (FD_ISSET(fd, &xset))
389 select_result(fd, 4);
390 if (FD_ISSET(fd, &rset))
391 select_result(fd, 1);
392 if (FD_ISSET(fd, &wset))
393 select_result(fd, 2);
402 * Main program: do platform-specific initialisation and then call
405 int main(int argc, char *argv[])
408 return psftp_main(argc, argv);