Change the semantics of 'FontSpec' so that it's a dynamically
[u/mdw/putty] / unix / uxsftp.c
CommitLineData
876eefd4 1/*
2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
3 */
4
5#include <sys/time.h>
6#include <sys/types.h>
7#include <sys/stat.h>
18609086 8#include <stdlib.h>
876eefd4 9#include <fcntl.h>
10#include <dirent.h>
11#include <unistd.h>
12#include <utime.h>
13#include <errno.h>
14#include <assert.h>
9c77ddf6 15#include <glob.h>
154c73d5 16#ifndef HAVE_NO_SYS_SELECT_H
17#include <sys/select.h>
18#endif
876eefd4 19
20#include "putty.h"
8def70c3 21#include "ssh.h"
876eefd4 22#include "psftp.h"
0ac1920c 23#include "int64.h"
876eefd4 24
25/*
26 * In PSFTP our selects are synchronous, so these functions are
27 * empty stubs.
28 */
29int uxsel_input_add(int fd, int rwx) { return 0; }
30void uxsel_input_remove(int id) { }
31
32char *x_get_default(const char *key)
33{
34 return NULL; /* this is a stub */
35}
36
4a693cfc 37void platform_get_x11_auth(struct X11Display *display, Conf *conf)
876eefd4 38{
39 /* Do nothing, therefore no auth. */
40}
8def70c3 41const int platform_uses_x11_unix_by_default = TRUE;
876eefd4 42
43/*
44 * Default settings that are specific to PSFTP.
45 */
46char *platform_default_s(const char *name)
47{
48 return NULL;
49}
50
51int platform_default_i(const char *name, int def)
52{
53 return def;
54}
55
ae62eaeb 56FontSpec *platform_default_fontspec(const char *name)
876eefd4 57{
ae62eaeb 58 return fontspec_new("");
876eefd4 59}
60
61Filename platform_default_filename(const char *name)
62{
63 Filename ret;
64 if (!strcmp(name, "LogFileName"))
65 strcpy(ret.path, "putty.log");
66 else
67 *ret.path = '\0';
68 return ret;
69}
70
c6ccd5c2 71char *get_ttymode(void *frontend, const char *mode) { return NULL; }
72
edd0cb8a 73int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
74{
75 int ret;
76 ret = cmdline_get_passwd_input(p, in, inlen);
77 if (ret == -1)
78 ret = console_get_userpass_input(p, in, inlen);
79 return ret;
80}
81
876eefd4 82/*
876eefd4 83 * Set local current directory. Returns NULL on success, or else an
84 * error message which must be freed after printing.
85 */
86char *psftp_lcd(char *dir)
87{
88 if (chdir(dir) < 0)
89 return dupprintf("%s: chdir: %s", dir, strerror(errno));
90 else
91 return NULL;
92}
93
94/*
95 * Get local current directory. Returns a string which must be
96 * freed.
97 */
98char *psftp_getcwd(void)
99{
100 char *buffer, *ret;
101 int size = 256;
102
103 buffer = snewn(size, char);
104 while (1) {
105 ret = getcwd(buffer, size);
106 if (ret != NULL)
107 return ret;
108 if (errno != ERANGE) {
109 sfree(buffer);
110 return dupprintf("[cwd unavailable: %s]", strerror(errno));
111 }
112 /*
113 * Otherwise, ERANGE was returned, meaning the buffer
114 * wasn't big enough.
115 */
116 size = size * 3 / 2;
117 buffer = sresize(buffer, size, char);
118 }
119}
120
121struct RFile {
122 int fd;
123};
124
0ac1920c 125RFile *open_existing_file(char *name, uint64 *size,
ee07dce4 126 unsigned long *mtime, unsigned long *atime,
127 long *perms)
876eefd4 128{
129 int fd;
130 RFile *ret;
131
132 fd = open(name, O_RDONLY);
133 if (fd < 0)
134 return NULL;
135
136 ret = snew(RFile);
137 ret->fd = fd;
138
ee07dce4 139 if (size || mtime || atime || perms) {
876eefd4 140 struct stat statbuf;
141 if (fstat(fd, &statbuf) < 0) {
142 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
143 memset(&statbuf, 0, sizeof(statbuf));
144 }
145
7bb18feb 146 if (size)
147 *size = uint64_make((statbuf.st_size >> 16) >> 16,
148 statbuf.st_size);
0ac1920c 149
876eefd4 150 if (mtime)
151 *mtime = statbuf.st_mtime;
152
153 if (atime)
154 *atime = statbuf.st_atime;
ee07dce4 155
156 if (perms)
157 *perms = statbuf.st_mode;
876eefd4 158 }
159
160 return ret;
161}
162
163int read_from_file(RFile *f, void *buffer, int length)
164{
165 return read(f->fd, buffer, length);
166}
167
168void close_rfile(RFile *f)
169{
170 close(f->fd);
171 sfree(f);
172}
173
174struct WFile {
175 int fd;
176 char *name;
177};
178
ee07dce4 179WFile *open_new_file(char *name, long perms)
876eefd4 180{
181 int fd;
182 WFile *ret;
183
ee07dce4 184 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
185 (mode_t)(perms ? perms : 0666));
876eefd4 186 if (fd < 0)
187 return NULL;
188
189 ret = snew(WFile);
190 ret->fd = fd;
191 ret->name = dupstr(name);
192
193 return ret;
194}
195
0ac1920c 196
197WFile *open_existing_wfile(char *name, uint64 *size)
198{
199 int fd;
200 WFile *ret;
201
202 fd = open(name, O_APPEND | O_WRONLY);
203 if (fd < 0)
204 return NULL;
205
206 ret = snew(WFile);
207 ret->fd = fd;
bb8bd6e9 208 ret->name = dupstr(name);
0ac1920c 209
210 if (size) {
211 struct stat statbuf;
212 if (fstat(fd, &statbuf) < 0) {
213 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
214 memset(&statbuf, 0, sizeof(statbuf));
215 }
216
7bb18feb 217 *size = uint64_make((statbuf.st_size >> 16) >> 16,
218 statbuf.st_size);
0ac1920c 219 }
220
221 return ret;
222}
223
876eefd4 224int write_to_file(WFile *f, void *buffer, int length)
225{
226 char *p = (char *)buffer;
227 int so_far = 0;
228
229 /* Keep trying until we've really written as much as we can. */
230 while (length > 0) {
231 int ret = write(f->fd, p, length);
232
233 if (ret < 0)
234 return ret;
235
236 if (ret == 0)
237 break;
238
239 p += ret;
240 length -= ret;
241 so_far += ret;
242 }
243
244 return so_far;
245}
246
247void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
248{
249 struct utimbuf ut;
250
251 ut.actime = atime;
252 ut.modtime = mtime;
253
254 utime(f->name, &ut);
255}
256
257/* Closes and frees the WFile */
258void close_wfile(WFile *f)
259{
260 close(f->fd);
261 sfree(f->name);
262 sfree(f);
263}
264
0ac1920c 265/* Seek offset bytes through file, from whence, where whence is
266 FROM_START, FROM_CURRENT, or FROM_END */
267int seek_file(WFile *f, uint64 offset, int whence)
268{
269 off_t fileofft;
270 int lseek_whence;
271
7bb18feb 272 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
0ac1920c 273
274 switch (whence) {
275 case FROM_START:
276 lseek_whence = SEEK_SET;
277 break;
278 case FROM_CURRENT:
279 lseek_whence = SEEK_CUR;
280 break;
281 case FROM_END:
282 lseek_whence = SEEK_END;
283 break;
284 default:
285 return -1;
286 }
287
288 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
289}
290
291uint64 get_file_posn(WFile *f)
292{
293 off_t fileofft;
294 uint64 ret;
295
296 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
297
7bb18feb 298 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
0ac1920c 299
300 return ret;
301}
302
876eefd4 303int file_type(char *name)
304{
305 struct stat statbuf;
306
307 if (stat(name, &statbuf) < 0) {
308 if (errno != ENOENT)
309 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
310 return FILE_TYPE_NONEXISTENT;
311 }
312
313 if (S_ISREG(statbuf.st_mode))
314 return FILE_TYPE_FILE;
315
316 if (S_ISDIR(statbuf.st_mode))
317 return FILE_TYPE_DIRECTORY;
318
319 return FILE_TYPE_WEIRD;
320}
321
322struct DirHandle {
323 DIR *dir;
324};
325
326DirHandle *open_directory(char *name)
327{
328 DIR *dir;
329 DirHandle *ret;
330
331 dir = opendir(name);
332 if (!dir)
333 return NULL;
334
335 ret = snew(DirHandle);
336 ret->dir = dir;
337 return ret;
338}
339
340char *read_filename(DirHandle *dir)
341{
342 struct dirent *de;
343
344 do {
345 de = readdir(dir->dir);
346 if (de == NULL)
347 return NULL;
348 } while ((de->d_name[0] == '.' &&
349 (de->d_name[1] == '\0' ||
350 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
351
352 return dupstr(de->d_name);
353}
354
355void close_directory(DirHandle *dir)
356{
357 closedir(dir->dir);
358 sfree(dir);
359}
360
361int test_wildcard(char *name, int cmdline)
362{
876eefd4 363 struct stat statbuf;
364
9c77ddf6 365 if (stat(name, &statbuf) == 0) {
876eefd4 366 return WCTYPE_FILENAME;
9c77ddf6 367 } else if (cmdline) {
368 /*
369 * On Unix, we never need to parse wildcards coming from
370 * the command line, because the shell will have expanded
371 * them into a filename list already.
372 */
373 return WCTYPE_NONEXISTENT;
374 } else {
375 glob_t globbed;
376 int ret = WCTYPE_NONEXISTENT;
377
378 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
379 if (globbed.gl_pathc > 0)
380 ret = WCTYPE_WILDCARD;
381 globfree(&globbed);
382 }
383
384 return ret;
385 }
876eefd4 386}
387
388/*
9c77ddf6 389 * Actually return matching file names for a local wildcard.
876eefd4 390 */
391struct WildcardMatcher {
9c77ddf6 392 glob_t globbed;
393 int i;
876eefd4 394};
9c77ddf6 395WildcardMatcher *begin_wildcard_matching(char *name) {
396 WildcardMatcher *ret = snew(WildcardMatcher);
397
398 if (glob(name, 0, NULL, &ret->globbed) < 0) {
399 sfree(ret);
400 return NULL;
401 }
402
403 ret->i = 0;
404
405 return ret;
406}
407char *wildcard_get_filename(WildcardMatcher *dir) {
408 if (dir->i < dir->globbed.gl_pathc) {
409 return dupstr(dir->globbed.gl_pathv[dir->i++]);
410 } else
411 return NULL;
412}
413void finish_wildcard_matching(WildcardMatcher *dir) {
414 globfree(&dir->globbed);
415 sfree(dir);
416}
876eefd4 417
e9d14678 418int vet_filename(char *name)
419{
420 if (strchr(name, '/'))
421 return FALSE;
422
423 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
424 return FALSE;
425
426 return TRUE;
427}
428
876eefd4 429int create_directory(char *name)
430{
431 return mkdir(name, 0777) == 0;
432}
433
434char *dir_file_cat(char *dir, char *file)
435{
436 return dupcat(dir, "/", file, NULL);
437}
438
439/*
39934deb 440 * Do a select() between all currently active network fds and
441 * optionally stdin.
876eefd4 442 */
65857773 443static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
876eefd4 444{
445 fd_set rset, wset, xset;
446 int i, fdcount, fdsize, *fdlist;
447 int fd, fdstate, rwx, ret, maxfd;
39934deb 448 long now = GETTICKCOUNT();
876eefd4 449
450 fdlist = NULL;
451 fdcount = fdsize = 0;
452
39934deb 453 do {
876eefd4 454
39934deb 455 /* Count the currently active fds. */
456 i = 0;
457 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
458 fd = next_fd(&fdstate, &rwx)) i++;
876eefd4 459
65857773 460 if (i < 1 && !no_fds_ok)
39934deb 461 return -1; /* doom */
876eefd4 462
39934deb 463 /* Expand the fdlist buffer if necessary. */
464 if (i > fdsize) {
465 fdsize = i + 16;
466 fdlist = sresize(fdlist, fdsize, int);
467 }
876eefd4 468
39934deb 469 FD_ZERO(&rset);
470 FD_ZERO(&wset);
471 FD_ZERO(&xset);
472 maxfd = 0;
876eefd4 473
39934deb 474 /*
475 * Add all currently open fds to the select sets, and store
476 * them in fdlist as well.
477 */
478 fdcount = 0;
479 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
480 fd = next_fd(&fdstate, &rwx)) {
481 fdlist[fdcount++] = fd;
482 if (rwx & 1)
483 FD_SET_MAX(fd, maxfd, rset);
484 if (rwx & 2)
485 FD_SET_MAX(fd, maxfd, wset);
486 if (rwx & 4)
487 FD_SET_MAX(fd, maxfd, xset);
488 }
489
490 if (include_stdin)
491 FD_SET_MAX(0, maxfd, rset);
492
493 do {
494 long next, ticks;
495 struct timeval tv, *ptv;
496
497 if (run_timers(now, &next)) {
498 ticks = next - GETTICKCOUNT();
499 if (ticks <= 0)
500 ticks = 1; /* just in case */
501 tv.tv_sec = ticks / 1000;
502 tv.tv_usec = ticks % 1000 * 1000;
503 ptv = &tv;
504 } else {
505 ptv = NULL;
506 }
507 ret = select(maxfd, &rset, &wset, &xset, ptv);
508 if (ret == 0)
509 now = next;
2ac3322e 510 else {
511 long newnow = GETTICKCOUNT();
512 /*
513 * Check to see whether the system clock has
514 * changed massively during the select.
515 */
516 if (newnow - now < 0 || newnow - now > next - now) {
517 /*
518 * If so, look at the elapsed time in the
519 * select and use it to compute a new
520 * tickcount_offset.
521 */
522 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
523 /* So we'd like GETTICKCOUNT to have returned othernow,
524 * but instead it return newnow. Hence ... */
525 tickcount_offset += othernow - newnow;
526 now = othernow;
527 } else {
528 now = newnow;
529 }
530 }
39934deb 531 } while (ret < 0 && errno != EINTR);
532 } while (ret == 0);
876eefd4 533
534 if (ret < 0) {
535 perror("select");
536 exit(1);
537 }
538
539 for (i = 0; i < fdcount; i++) {
540 fd = fdlist[i];
541 /*
542 * We must process exceptional notifications before
543 * ordinary readability ones, or we may go straight
544 * past the urgent marker.
545 */
546 if (FD_ISSET(fd, &xset))
547 select_result(fd, 4);
548 if (FD_ISSET(fd, &rset))
549 select_result(fd, 1);
550 if (FD_ISSET(fd, &wset))
551 select_result(fd, 2);
552 }
553
554 sfree(fdlist);
555
39934deb 556 return FD_ISSET(0, &rset) ? 1 : 0;
557}
558
559/*
560 * Wait for some network data and process it.
561 */
562int ssh_sftp_loop_iteration(void)
563{
65857773 564 return ssh_sftp_do_select(FALSE, FALSE);
39934deb 565}
566
567/*
568 * Read a PSFTP command line from stdin.
569 */
65857773 570char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
39934deb 571{
572 char *buf;
573 int buflen, bufsize, ret;
574
575 fputs(prompt, stdout);
576 fflush(stdout);
577
578 buf = NULL;
579 buflen = bufsize = 0;
580
581 while (1) {
65857773 582 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
39934deb 583 if (ret < 0) {
584 printf("connection died\n");
585 return NULL; /* woop woop */
586 }
587 if (ret > 0) {
588 if (buflen >= bufsize) {
589 bufsize = buflen + 512;
590 buf = sresize(buf, bufsize, char);
591 }
592 ret = read(0, buf+buflen, 1);
593 if (ret < 0) {
594 perror("read");
595 return NULL;
596 }
597 if (ret == 0) {
598 /* eof on stdin; no error, but no answer either */
599 return NULL;
600 }
601
602 if (buf[buflen++] == '\n') {
603 /* we have a full line */
604 return buf;
605 }
606 }
607 }
876eefd4 608}
609
610/*
611 * Main program: do platform-specific initialisation and then call
612 * psftp_main().
613 */
614int main(int argc, char *argv[])
615{
616 uxsel_init();
617 return psftp_main(argc, argv);
618}