uint64_decimal() incorrectly output 0 as "" instead of "0". This only affected
[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>
15
16#include "putty.h"
17#include "psftp.h"
18
19/*
20 * In PSFTP our selects are synchronous, so these functions are
21 * empty stubs.
22 */
23int uxsel_input_add(int fd, int rwx) { return 0; }
24void uxsel_input_remove(int id) { }
25
26char *x_get_default(const char *key)
27{
28 return NULL; /* this is a stub */
29}
30
31void platform_get_x11_auth(char *display, int *protocol,
32 unsigned char *data, int *datalen)
33{
34 /* Do nothing, therefore no auth. */
35}
36
37/*
38 * Default settings that are specific to PSFTP.
39 */
40char *platform_default_s(const char *name)
41{
42 return NULL;
43}
44
45int platform_default_i(const char *name, int def)
46{
47 return def;
48}
49
50FontSpec platform_default_fontspec(const char *name)
51{
52 FontSpec ret;
53 *ret.name = '\0';
54 return ret;
55}
56
57Filename platform_default_filename(const char *name)
58{
59 Filename ret;
60 if (!strcmp(name, "LogFileName"))
61 strcpy(ret.path, "putty.log");
62 else
63 *ret.path = '\0';
64 return ret;
65}
66
67/*
68 * Stubs for the GUI feedback mechanism in Windows PSCP.
69 */
70void gui_update_stats(char *name, unsigned long size,
71 int percentage, unsigned long elapsed,
72 unsigned long done, unsigned long eta,
73 unsigned long ratebs) {}
74void gui_send_errcount(int list, int errs) {}
75void gui_send_char(int is_stderr, int c) {}
76void gui_enable(char *arg) {}
77
78
79/*
80 * Set local current directory. Returns NULL on success, or else an
81 * error message which must be freed after printing.
82 */
83char *psftp_lcd(char *dir)
84{
85 if (chdir(dir) < 0)
86 return dupprintf("%s: chdir: %s", dir, strerror(errno));
87 else
88 return NULL;
89}
90
91/*
92 * Get local current directory. Returns a string which must be
93 * freed.
94 */
95char *psftp_getcwd(void)
96{
97 char *buffer, *ret;
98 int size = 256;
99
100 buffer = snewn(size, char);
101 while (1) {
102 ret = getcwd(buffer, size);
103 if (ret != NULL)
104 return ret;
105 if (errno != ERANGE) {
106 sfree(buffer);
107 return dupprintf("[cwd unavailable: %s]", strerror(errno));
108 }
109 /*
110 * Otherwise, ERANGE was returned, meaning the buffer
111 * wasn't big enough.
112 */
113 size = size * 3 / 2;
114 buffer = sresize(buffer, size, char);
115 }
116}
117
118struct RFile {
119 int fd;
120};
121
122RFile *open_existing_file(char *name, unsigned long *size,
123 unsigned long *mtime, unsigned long *atime)
124{
125 int fd;
126 RFile *ret;
127
128 fd = open(name, O_RDONLY);
129 if (fd < 0)
130 return NULL;
131
132 ret = snew(RFile);
133 ret->fd = fd;
134
135 if (size || mtime || atime) {
136 struct stat statbuf;
137 if (fstat(fd, &statbuf) < 0) {
138 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
139 memset(&statbuf, 0, sizeof(statbuf));
140 }
141
142 if (size)
143 *size = statbuf.st_size;
144
145 if (mtime)
146 *mtime = statbuf.st_mtime;
147
148 if (atime)
149 *atime = statbuf.st_atime;
150 }
151
152 return ret;
153}
154
155int read_from_file(RFile *f, void *buffer, int length)
156{
157 return read(f->fd, buffer, length);
158}
159
160void close_rfile(RFile *f)
161{
162 close(f->fd);
163 sfree(f);
164}
165
166struct WFile {
167 int fd;
168 char *name;
169};
170
171WFile *open_new_file(char *name)
172{
173 int fd;
174 WFile *ret;
175
176 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
177 if (fd < 0)
178 return NULL;
179
180 ret = snew(WFile);
181 ret->fd = fd;
182 ret->name = dupstr(name);
183
184 return ret;
185}
186
187int write_to_file(WFile *f, void *buffer, int length)
188{
189 char *p = (char *)buffer;
190 int so_far = 0;
191
192 /* Keep trying until we've really written as much as we can. */
193 while (length > 0) {
194 int ret = write(f->fd, p, length);
195
196 if (ret < 0)
197 return ret;
198
199 if (ret == 0)
200 break;
201
202 p += ret;
203 length -= ret;
204 so_far += ret;
205 }
206
207 return so_far;
208}
209
210void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
211{
212 struct utimbuf ut;
213
214 ut.actime = atime;
215 ut.modtime = mtime;
216
217 utime(f->name, &ut);
218}
219
220/* Closes and frees the WFile */
221void close_wfile(WFile *f)
222{
223 close(f->fd);
224 sfree(f->name);
225 sfree(f);
226}
227
228int file_type(char *name)
229{
230 struct stat statbuf;
231
232 if (stat(name, &statbuf) < 0) {
233 if (errno != ENOENT)
234 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
235 return FILE_TYPE_NONEXISTENT;
236 }
237
238 if (S_ISREG(statbuf.st_mode))
239 return FILE_TYPE_FILE;
240
241 if (S_ISDIR(statbuf.st_mode))
242 return FILE_TYPE_DIRECTORY;
243
244 return FILE_TYPE_WEIRD;
245}
246
247struct DirHandle {
248 DIR *dir;
249};
250
251DirHandle *open_directory(char *name)
252{
253 DIR *dir;
254 DirHandle *ret;
255
256 dir = opendir(name);
257 if (!dir)
258 return NULL;
259
260 ret = snew(DirHandle);
261 ret->dir = dir;
262 return ret;
263}
264
265char *read_filename(DirHandle *dir)
266{
267 struct dirent *de;
268
269 do {
270 de = readdir(dir->dir);
271 if (de == NULL)
272 return NULL;
273 } while ((de->d_name[0] == '.' &&
274 (de->d_name[1] == '\0' ||
275 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
276
277 return dupstr(de->d_name);
278}
279
280void close_directory(DirHandle *dir)
281{
282 closedir(dir->dir);
283 sfree(dir);
284}
285
286int test_wildcard(char *name, int cmdline)
287{
288 /*
289 * On Unix, we currently don't support local wildcards at all.
290 * We will have to do so (FIXME) once PSFTP starts implementing
291 * mput, but until then we can assume `cmdline' is always set.
292 */
293 struct stat statbuf;
294
295 assert(cmdline);
296 if (stat(name, &statbuf) < 0)
297 return WCTYPE_NONEXISTENT;
298 else
299 return WCTYPE_FILENAME;
300}
301
302/*
303 * Actually return matching file names for a local wildcard. FIXME:
304 * we currently don't support this at all.
305 */
306struct WildcardMatcher {
307 int x;
308};
309WildcardMatcher *begin_wildcard_matching(char *name) { return NULL; }
310char *wildcard_get_filename(WildcardMatcher *dir) { return NULL; }
311void finish_wildcard_matching(WildcardMatcher *dir) {}
312
313int create_directory(char *name)
314{
315 return mkdir(name, 0777) == 0;
316}
317
318char *dir_file_cat(char *dir, char *file)
319{
320 return dupcat(dir, "/", file, NULL);
321}
322
323/*
324 * Wait for some network data and process it.
325 */
326int ssh_sftp_loop_iteration(void)
327{
328 fd_set rset, wset, xset;
329 int i, fdcount, fdsize, *fdlist;
330 int fd, fdstate, rwx, ret, maxfd;
331
332 fdlist = NULL;
333 fdcount = fdsize = 0;
334
335 /* Count the currently active fds. */
336 i = 0;
337 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
338 fd = next_fd(&fdstate, &rwx)) i++;
339
340 if (i < 1)
341 return -1; /* doom */
342
343 /* Expand the fdlist buffer if necessary. */
344 if (i > fdsize) {
345 fdsize = i + 16;
346 fdlist = sresize(fdlist, fdsize, int);
347 }
348
349 FD_ZERO(&rset);
350 FD_ZERO(&wset);
351 FD_ZERO(&xset);
352 maxfd = 0;
353
354 /*
355 * Add all currently open fds to the select sets, and store
356 * them in fdlist as well.
357 */
358 fdcount = 0;
359 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
360 fd = next_fd(&fdstate, &rwx)) {
361 fdlist[fdcount++] = fd;
362 if (rwx & 1)
363 FD_SET_MAX(fd, maxfd, rset);
364 if (rwx & 2)
365 FD_SET_MAX(fd, maxfd, wset);
366 if (rwx & 4)
367 FD_SET_MAX(fd, maxfd, xset);
368 }
369
370 do {
371 ret = select(maxfd, &rset, &wset, &xset, NULL);
372 } while (ret < 0 && errno == EINTR);
373
374 if (ret < 0) {
375 perror("select");
376 exit(1);
377 }
378
379 for (i = 0; i < fdcount; i++) {
380 fd = fdlist[i];
381 /*
382 * We must process exceptional notifications before
383 * ordinary readability ones, or we may go straight
384 * past the urgent marker.
385 */
386 if (FD_ISSET(fd, &xset))
387 select_result(fd, 4);
388 if (FD_ISSET(fd, &rset))
389 select_result(fd, 1);
390 if (FD_ISSET(fd, &wset))
391 select_result(fd, 2);
392 }
393
394 sfree(fdlist);
395
396 return 0;
397}
398
399/*
400 * Main program: do platform-specific initialisation and then call
401 * psftp_main().
402 */
403int main(int argc, char *argv[])
404{
405 uxsel_init();
406 return psftp_main(argc, argv);
407}