1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* Get a directory listing using the ftp protocol
Copyright (C) 1997,2002 Free Software Foundation, Inc.
Written by Miles Bader <miles@gnu.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <unistd.h>
#include <string.h>
#include <error.h>
#include <argp.h>
#include <time.h>
#include <netdb.h>
#include <version.h>
#include <ftpconn.h>
#define COPY_SZ 65536
const char *argp_program_version = STANDARD_HURD_VERSION (ftpdir);
static struct argp_option options[] =
{
{"user", 'u', "USER",0, "User to login as on ftp server"},
{"password", 'p', "PWD", 0, "USER's password"},
{"account", 'a', "ACCT",0, "Account to login as"},
{"separator",'S', "SEP", 0, "String to separate multiple listings"},
{"prefix", 'P', "PFX", 0, "String to proceed listings; the first and second"
" occurances of %s are replace by HOST and DIR"},
{"host", 'h', "HOST",0, "Use HOST as a default host"},
{"debug", 'D', 0, 0, "Turn on debugging output for ftp connections"},
{"intepret", 'i', 0, 0, "Parse the directory output"},
{0, 0}
};
static char *args_doc = "[([HOST:]DIR | HOST:)...]";
static char *doc = "Get a directory listing over ftp from HOST:DIR."
"\vIf HOST is not supplied in an argument any default value set by --host is"
" used; if DIR is not supplied, the default directory of HOST is used."
"\nIf multiple DIRs are supplied on the command line, each listing is"
" prefixed by a newline (or SEP) and a line containing HOST:DIR: (or PFX).";
/* customization hooks. */
static struct ftp_conn_hooks conn_hooks = { 0 };
static void
cntl_debug (struct ftp_conn *conn, int type, const char *txt)
{
char *type_str;
switch (type)
{
case FTP_CONN_CNTL_DEBUG_CMD: type_str = "."; break;
case FTP_CONN_CNTL_DEBUG_REPLY: type_str = "="; break;
default: type_str = "?"; break;
}
fprintf (stderr, "%s%s\n", type_str, txt);
}
struct ftpdir_host
{
char *name;
struct ftp_conn_params params;
struct ftp_conn *conn;
struct ftpdir_host *next;
};
/* Return an ftp connection for the host NAME using PARAMS, and add an entry
for it to *HOSTS. If a connection already exists in HOSTS, it is returned
instead of making a new one. If an error occurrs, a message is printed and
0 is returned. */
static struct ftpdir_host *
get_host_conn (char *name, struct ftp_conn_params *params,
struct ftpdir_host **hosts)
{
error_t err;
struct ftpdir_host *h;
struct hostent *he;
for (h = *hosts; h; h = h->next)
if (strcmp (h->name, name) == 0)
return h;
he = gethostbyname (name);
if (! he)
{
error (0, 0, "%s: %s", name, hstrerror (h_errno));
return 0;
}
for (h = *hosts; h; h = h->next)
if (he->h_addrtype == h->params.addr_type
&& he->h_length == h->params.addr_len
&& bcmp (he->h_addr_list[0], h->params.addr, he->h_length) == 0)
return h;
h = malloc (sizeof (struct ftpdir_host));
if (! h)
{
error (0, ENOMEM, "%s", name);
return 0;
}
h->params = *params;
h->params.addr = malloc (he->h_length);
h->name = strdup (he->h_name);
if (!h->name || !h->params.addr)
err = ENOMEM;
else
{
bcopy (he->h_addr_list[0], h->params.addr, he->h_length);
h->params.addr_len = he->h_length;
h->params.addr_type = he->h_addrtype;
err = ftp_conn_create (&h->params, &conn_hooks, &h->conn);
}
if (err)
{
error (0, err, "%s", he->h_name);
if (h->name)
free (h->name);
if (h->params.addr)
free (h->params.addr);
free (h);
return 0;
}
h->next = *hosts;
*hosts = h;
return h;
}
static int
ftpdir (char *dir, struct ftpdir_host *host)
{
int data;
int rd;
error_t err;
static void *copy_buf = 0;
struct ftp_conn *conn = host->conn;
char *host_name = host->name;
err = ftp_conn_start_dir (conn, dir, &data);
if (err)
{
error (0, err, "%s:%s", host_name, dir);
return err;
}
if (! copy_buf)
{
copy_buf = valloc (COPY_SZ);
if (! copy_buf)
error (12, ENOMEM, "Cannot allocate copy buffer");
}
while ((rd = read (data, copy_buf, COPY_SZ)) > 0)
do
{
int wr = write (1, copy_buf, rd);
if (wr < 0)
error (13, errno, "stdout");
rd -= wr;
}
while (rd > 0);
if (rd != 0)
{
error (0, errno, "%s:%s", host_name, dir);
return errno;
}
close (data);
err = ftp_conn_finish_transfer (conn);
if (err)
{
error (0, err, "%s:%s", host_name, dir);
return err;
}
return 0;
}
static error_t
pdirent (const char *name, const struct stat *st, const char *symlink_target,
void *hook)
{
char timebuf[20];
strftime (timebuf, sizeof timebuf, "%Y-%m-%d %H:%M", localtime (&st->st_mtime));
printf ("%6o %2d %5d %5d %6lld %s %s\n",
st->st_mode, st->st_nlink, st->st_uid, st->st_gid, st->st_size,
timebuf, name);
if (symlink_target)
printf (" -> %s\n",
symlink_target);
return 0;
}
static error_t
ftpdir2 (char *dir, struct ftpdir_host *host)
{
error_t err = ftp_conn_get_stats (host->conn, dir, 1, pdirent, 0);
if (err == ENOTDIR)
err = ftp_conn_get_stats (host->conn, dir, 0, pdirent, 0);
if (err)
error (0, err, "%s:%s", host->name, dir);
return err;
}
int
main (int argc, char **argv)
{
struct ftpdir_host *hosts = 0;
char *default_host = 0;
int interpret = 0;
struct ftp_conn_params params = { 0 };
char *sep = "\n";
char *pfx = "%s:%s:\n";
int use_pfx = 0;
int errs = 0;
/* Parse our options... */
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ARGP_KEY_ARG:
{
char *host, *dir;
if (state->next < state->argc)
use_pfx = 1; /* Multiple arguments. */
dir = index (arg, ':');
if (dir)
{
host = arg;
*dir++ = '\0';
if (*host == '\0')
/* An argument of `:' */
host = default_host;
}
else
{
host = default_host;
dir = arg;
}
if (host)
{
struct ftpdir_host *h = get_host_conn (host, ¶ms, &hosts);
if (h)
{
if (state->arg_num > 0)
fputs (sep, stdout);
if (use_pfx)
printf (pfx, h->name, dir);
if ((use_pfx && *pfx) || (state->arg_num > 0 && *sep))
fflush (stdout);
if (interpret)
errs |= ftpdir2 (dir, h);
else
errs |= ftpdir (dir, h);
}
errs = 1;
}
else
{
error (0, 0, "%s: No default host", arg);
errs = 1;
}
}
break;
case ARGP_KEY_NO_ARGS:
if (default_host)
{
struct ftpdir_host *h =
get_host_conn (default_host, ¶ms, &hosts);
if (h)
errs |= ftpdir (0, h);
}
else
{
error (0, 0, "No default host");
errs = 1;
}
break;
return EINVAL;
case 'u': params.user = arg; break;
case 'p': params.pass = arg; break;
case 'a': params.acct = arg; break;
case 'h': default_host = arg; break;
case 'D': conn_hooks.cntl_debug = cntl_debug; break;
case 'P': pfx = arg; use_pfx = 1; break;
case 'S': sep = arg; break;
case 'i': interpret = 1; break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
struct argp argp = {options, parse_opt, args_doc, doc};
argp_parse (&argp, argc, argv, 0, 0, 0);
if (errs)
exit (10);
else
exit (0);
}
|