Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1013)

Side by Side Diff: net/ftp/ftp_directory_parser.cc

Issue 28298: Directory listing code for new Portable FTP(Patch SET-2) (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/ftp/ftp_directory_parser.h ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 //
7 // The contents of this file are subject to the Mozilla Public License Version
8 // 1.1 (the "License"); you may not use this file except in compliance with
9 // the License. You may obtain a copy of the License at
10 // http://www.mozilla.org/MPL/
11 //
12 // Software distributed under the License is distributed on an "AS IS" basis,
13 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 // for the specific language governing rights and limitations under the
15 // License.
16 //
17 // The Original Code is mozilla.org Code.
18 //
19 // The Initial Developer of the Original Code is
20 // Cyrus Patel <cyp@fb14.uni-mainz.de>.
21 // Portions created by the Initial Developer are Copyright (C) 2002
22 // the Initial Developer. All Rights Reserved.
23 //
24 // Contributor(s):
25 // Doug Turner <dougt@netscape.com>
26 //
27 // Alternatively, the contents of this file may be used under the terms of
28 // either the GNU General Public License Version 2 or later (the "GPL"), or
29 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 // in which case the provisions of the GPL or the LGPL are applicable instead
31 // of those above. If you wish to allow use of your version of this file only
32 // under the terms of either the GPL or the LGPL, and not to allow others to
33 // use your version of this file under the terms of the MPL, indicate your
34 // decision by deleting the provisions above and replace them with the notice
35 // and other provisions required by the GPL or the LGPL. If you do not delete
36 // the provisions above, a recipient may use your version of this file under
37 // the terms of any one of the MPL, the GPL or the LGPL.
38
39 // Derived from:
40 // mozilla/netwerk/streamconv/converters/ParseFTPList.cpp revision 1.10
41
42 #include "net/ftp/ftp_directory_parser.h"
43
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <time.h>
48
49 #include "build/build_config.h"
50 #include "base/basictypes.h"
51 #include "base/string_util.h"
52
53 // #undef anything you don't want to support
54 #define SUPPORT_LSL // /bin/ls -l and dozens of variations therof
55 #define SUPPORT_DLS // /bin/dls format (very, Very, VERY rare)
56 #define SUPPORT_EPLF // Extraordinarily Pathetic List Format
57 #define SUPPORT_DOS // WinNT server in 'site dirstyle' dos
58 #define SUPPORT_VMS // VMS (all: MultiNet, UCX, CMU-IP)
59 #define SUPPORT_CMS // IBM VM/CMS,VM/ESA (z/VM and LISTING forms)
60 #define SUPPORT_OS2 // IBM TCP/IP for OS/2 - FTP Server
61 #define SUPPORT_W16 // win16 hosts: SuperTCP or NetManage Chameleon
62
63 namespace net {
64
65 LineType ParseFTPLine(const char *line,
66 struct ListState *state,
67 struct ListResult *result) {
68 unsigned int carry_buf_len;
69 unsigned int linelen, pos;
70 const char *p;
71
72 if (!line || !state || !result)
73 return FTP_TYPE_JUNK;
74
75 memset(result, 0, sizeof(*result));
76
77 state->numlines++;
78
79 // Carry buffer is only valid from one line to the next.
80 carry_buf_len = state->carry_buf_len;
81 state->carry_buf_len = 0;
82
83 linelen = 0;
84
85 // Strip leading whitespace.
86 while (*line == ' ' || *line == '\t')
87 line++;
88
89 // Line is terminated at first '\0' or '\n'.
90 p = line;
91 while (*p && *p != '\n')
92 p++;
93 linelen = p - line;
94
95 if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
96 linelen--;
97
98 // DON'T strip trailing whitespace.
99 if (linelen > 0) {
100 static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
101 const char *tokens[16];
102 unsigned int toklen[(sizeof(tokens)/sizeof(tokens[0]))];
103 unsigned int linelen_sans_wsp;
104 unsigned int numtoks = 0;
105 unsigned int tokmarker = 0;
106 unsigned int month_num = 0;
107 char tbuf[4];
108 int lstyle = 0;
109
110 // VMS long filename carryover buffer.
111 if (carry_buf_len) {
112 tokens[0] = state->carry_buf;
113 toklen[0] = carry_buf_len;
114 numtoks++;
115 }
116
117 pos = 0;
118 while (pos < linelen && numtoks < (sizeof(tokens)/sizeof(tokens[0]))) {
119 while (pos < linelen &&
120 (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
121 pos++;
122 if (pos < linelen) {
123 tokens[numtoks] = &line[pos];
124 while (pos < linelen &&
125 (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
126 pos++;
127 if (tokens[numtoks] != &line[pos]) {
128 toklen[numtoks] = (&line[pos] - tokens[numtoks]);
129 numtoks++;
130 }
131 }
132 }
133 linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
134 if (numtoks == (sizeof(tokens)/sizeof(tokens[0]))) {
135 pos = linelen;
136 while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
137 pos--;
138 linelen_sans_wsp = pos;
139 }
140
141 #if defined(SUPPORT_EPLF)
142 // EPLF handling must come somewhere before /bin/dls handling.
143 if (!lstyle && (!state->lstyle || state->lstyle == 'E')) {
144 if (*line == '+' && linelen > 4 && numtoks >= 2) {
145 pos = 1;
146 while (pos < (linelen-1)) {
147 p = &line[pos++];
148 if (*p == '/')
149 result->fe_type = FTP_TYPE_DIRECTORY; // Its a dir.
150 else if (*p == 'r')
151 result->fe_type = FTP_TYPE_FILE;
152 else if (*p == 'm') {
153 if (isdigit(line[pos])) {
154 while (pos < linelen && isdigit(line[pos]))
155 pos++;
156 if (pos < linelen && line[pos] == ',') {
157 uint64 seconds = 0;
158 seconds = StringToInt64(p + 1);
159 time_t tim = static_cast<time_t>(seconds);
160 // TODO(ibrar): Yet thought required here
161 #if defined(OS_WIN)
162 gmtime_s(&result->fe_time, &tim);
163 #elif defined(OS_POSIX)
164 gmtime_r(&tim, &result->fe_time);
165 #endif
166 }
167 }
168 } else if (*p == 's') {
169 if (isdigit(line[pos])) {
170 while (pos < linelen && isdigit(line[pos]))
171 pos++;
172 if (pos < linelen && line[pos] == ',' &&
173 ((&line[pos]) - (p+1)) <
174 static_cast<int>(sizeof(result->fe_size) - 1)) {
175 memcpy(result->fe_size, p + 1,
176 (unsigned)(&line[pos] - (p + 1)));
177 result->fe_size[(&line[pos] - (p+1))] = '\0';
178 }
179 }
180 } else if (isalpha(*p)) {
181 while (pos < linelen && *++p != ',')
182 pos++;
183 } else if (*p != '\t' || (p + 1) != tokens[1]) {
184 break; // Its not EPLF after all.
185 } else {
186 state->parsed_one = 1;
187 state->lstyle = lstyle = 'E';
188
189 p = &(line[linelen_sans_wsp]);
190 result->fe_fname = tokens[1];
191 result->fe_fnlen = p - tokens[1];
192
193 // Access denied.
194 if (!result->fe_type) {
195 result->fe_type = FTP_TYPE_FILE;
196 // is assuming 'f'ile correct?
197 return FTP_TYPE_JUNK; // NO! junk it.
198 }
199 return result->fe_type;
200 }
201 if (pos >= (linelen-1) || line[pos] != ',')
202 break;
203 pos++;
204 } // while (pos < linelen)
205 memset(result, 0, sizeof(*result));
206 } // if (*line == '+' && linelen > 4 && numtoks >= 2).
207 } // if (!lstyle && (!state->lstyle || state->lstyle == 'E')).
208 #endif // SUPPORT_EPLF
209
210 #if defined(SUPPORT_VMS)
211 // try VMS Multinet/UCX/CMS server
212 if (!lstyle && (!state->lstyle || state->lstyle == 'V')) {
213 // Legal characters in a VMS file/dir spec are [A-Z0-9$.-_~].
214 // '$' cannot begin a filename and `-' cannot be used as the first
215 // or last character. '.' is only valid as a directory separator
216 // and <file>.<type> separator. A canonical filename spec might look
217 // like this: DISK$VOL:[DIR1.DIR2.DIR3]FILE.TYPE;123
218 // All VMS FTP servers LIST in uppercase.
219 // We need to be picky about this in order to support
220 // multi-line listings correctly.
221 if ((!state->parsed_one &&
222 numtoks == 1) || (numtoks == 2 && toklen[0] == 9 &&
223 memcmp(tokens[0], "Directory", 9) == 0)) {
224 // If no dirstyle has been detected yet, and this line is a
225 // VMS list's dirname, then turn on VMS dirstyle.
226 // eg "ACA:[ANONYMOUS]", "DISK$FTP:[ANONYMOUS]", "SYS$ANONFTP:"
227 p = tokens[0];
228 pos = toklen[0];
229 if (numtoks == 2) {
230 p = tokens[1];
231 pos = toklen[1];
232 }
233 pos--;
234 if (pos >= 3) {
235 while (pos > 0 && p[pos] != '[') {
236 pos--;
237 if (p[pos] == '-' || p[pos] == '$') {
238 if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
239 (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
240 break;
241 } else if (p[pos] != '.' && p[pos] != '~' &&
242 !isdigit(p[pos]) && !isalpha(p[pos])) {
243 break;
244 } else if (isalpha(p[pos]) && p[pos] != toupper(p[pos])) {
245 break;
246 }
247 }
248 if (pos > 0) {
249 pos--;
250 if (p[pos] != ':' || p[pos+1] != '[')
251 pos = 0;
252 }
253 }
254 if (pos > 0 && p[pos] == ':') {
255 while (pos > 0) {
256 pos--;
257 if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
258 p[pos] != '~' && !isdigit(p[pos]) && !isalpha(p[pos]))
259 break;
260 else if (isalpha(p[pos]) && p[pos] != toupper(p[pos]))
261 break;
262 }
263 if (pos == 0) {
264 state->lstyle = 'V';
265 return FTP_TYPE_JUNK; // Its junk.
266 }
267 }
268 // fallthrough
269 } else if ((tokens[0][toklen[0]-1]) != ';') {
270 if (numtoks == 1 && (state->lstyle == 'V' && !carry_buf_len))
271 lstyle = 'V';
272 else if (numtoks < 4);
273 else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
274 lstyle = 'V';
275 else if ((&line[linelen] - tokens[1]) >= 22 &&
276 memcmp(tokens[1], "insufficient privilege", 22) == 0)
277 lstyle = 'V';
278 else if (numtoks != 4 && numtoks != 6) {
279 } else if (numtoks == 6 && (
280 toklen[5] < 4 || *tokens[5] != '(' ||
281 (tokens[5][toklen[5]-1]) != ')')) {
282 } else if ((toklen[2] == 10 || toklen[2] == 11) &&
283 (tokens[2][toklen[2]-5]) == '-' &&
284 (tokens[2][toklen[2]-9]) == '-' &&
285 (((toklen[3] == 4 || toklen[3] == 5 || toklen[3] == 7 ||
286 toklen[3] == 8) &&
287 (tokens[3][toklen[3]-3]) == ':') ||
288 ((toklen[3] == 10 || toklen[3] == 11) &&
289 (tokens[3][toklen[3]-3]) == '.')) &&
290 // Time in [H]H:MM[:SS[.CC]] format.
291 isdigit(*tokens[1]) && // size
292 isdigit(*tokens[2]) && // date
293 isdigit(*tokens[3]) // time
294 ) {
295 lstyle = 'V';
296 }
297 if (lstyle == 'V') {
298 // MultiNet FTP:
299 // LOGIN.COM;2 1 4-NOV-1994 04:09 [ANONYMOUS] (RWE,RWE,,)
300 // PUB.DIR;1 1 27-JAN-1994 14:46 [ANONYMOUS] (RWE,RWE,RE,RWE)
301 // README.FTP;1 %RMS-E-PRV, insufficient privilege or file
302 // protection violation
303 // ROUSSOS.DIR;1 1 27-JAN-1994 14:48 [CS,ROUSSOS] (RWE,RWE,RE,R)
304 // S67-50903.JPG;1 328 22-SEP-1998 16:19 [ANONYMOUS] (RWED,RWED,,)
305 // UCX FTP:
306 // CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12[ANONYMOU,ANONYMOUS]
307 // (RWED,RWED,,)
308 // CMU/VMS-IP FTP
309 // [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
310 // TCPware FTP
311 // FOO.BAR;1 4 5-MAR-1993 18:09:01.12
312 // Long filename example:
313 // THIS-IS-A-LONG-VMS-FILENAME.AND-THIS-IS-A-LONG-VMS-FILETYPE\r\n
314 // 213[/nnn] 29-JAN-1996 03:33[:nn] [ANONYMOU,ANONYMOUS] (RWED,RWED, ,)
315 tokmarker = 0;
316 p = tokens[0];
317 pos = 0;
318 if (*p == '[' && toklen[0] >= 4) {
319 if (p[1] != ']') {
320 p++;
321 pos++;
322 }
323 while (lstyle && pos < toklen[0] && *p != ']') {
324 if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
325 *p != '~' && !isdigit(*p) && !isalpha(*p))
326 lstyle = 0;
327 pos++;
328 p++;
329 }
330 if (lstyle && pos < (toklen[0]-1) && *p == ']') {
331 pos++;
332 p++;
333 tokmarker = pos; // length of leading "[DIR1.DIR2.etc]"
334 }
335 }
336 while (lstyle && pos < toklen[0] && *p != ';') {
337 if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
338 *p != '~' && !isdigit(*p) && !isalpha(*p))
339 lstyle = 0;
340 else if (isalpha(*p) && *p != toupper(*p))
341 lstyle = 0;
342 p++;
343 pos++;
344 }
345 if (lstyle && *p == ';') {
346 if (pos == 0 || pos == (toklen[0]-1))
347 lstyle = 0;
348 for (pos++;lstyle && pos < toklen[0];pos++) {
349 if (!isdigit(tokens[0][pos]))
350 lstyle = 0;
351 }
352 }
353 pos = (p - tokens[0]); // => fnlength sans ";####"
354 pos -= tokmarker; // => fnlength sans "[DIR1.DIR2.etc]"
355 p = &(tokens[0][tokmarker]); // offset of basename.
356
357 if (!lstyle || pos > 80) {
358 // VMS filenames can't be longer than that.
359 lstyle = 0;
360 } else if (numtoks == 1) {
361 // If VMS has been detected and there is only one token and
362 // that token was a VMS filename then this is a multiline
363 // VMS LIST entry.
364 if (pos >= (sizeof(state->carry_buf) - 1))
365 pos = (sizeof(state->carry_buf) - 1); // Shouldn't happen.
366 memcpy(state->carry_buf, p, pos);
367 state->carry_buf_len = pos;
368 return FTP_TYPE_JUNK; // Tell caller to treat as junk.
369 } else if (isdigit(*tokens[1])) { // not no-privs message.
370 for (pos = 0; lstyle && pos < (toklen[1]); pos++) {
371 if (!isdigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
372 lstyle = 0;
373 }
374 if (lstyle && numtoks > 4) { // Multinet or UCX but not CMU.
375 for (pos = 1; lstyle && pos < (toklen[5]-1); pos++) {
376 p = &(tokens[5][pos]);
377 if (*p != 'R' && *p != 'W' && *p != 'E' &&
378 *p != 'D' && *p != ',')
379 lstyle = 0;
380 }
381 }
382 }
383 } // passed initial tests
384 } // else if ((tokens[0][toklen[0]-1]) != ';')
385
386 if (lstyle == 'V') {
387 state->parsed_one = 1;
388 state->lstyle = lstyle;
389 if (isdigit(*tokens[1])) { // not permission denied etc
390 // strip leading directory name
391 if (*tokens[0] == '[') { // CMU server
392 pos = toklen[0] - 1;
393 p = tokens[0] + 1;
394 while (*p != ']') {
395 p++;
396 pos--;
397 }
398 toklen[0] = --pos;
399 tokens[0] = ++p;
400 }
401 pos = 0;
402 while (pos < toklen[0] && (tokens[0][pos]) != ';')
403 pos++;
404
405 result->fe_cinfs = 1;
406 result->fe_type = FTP_TYPE_FILE;
407 result->fe_fname = tokens[0];
408 result->fe_fnlen = pos;
409
410 if (pos > 4) {
411 p = &(tokens[0][pos-4]);
412 if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R') {
413 result->fe_fnlen -= 4;
414 result->fe_type = FTP_TYPE_DIRECTORY;
415 }
416 }
417
418 if (result->fe_type != FTP_TYPE_DIRECTORY) {
419 // #### or used/allocated form. If used/allocated form, then
420 // 'used' is the size in bytes if and only if 'used'<=allocated.
421 // If 'used' is size in bytes then it can be > 2^32
422 // If 'used' is not size in bytes then it is size in blocks.
423 pos = 0;
424 while (pos < toklen[1] && (tokens[1][pos]) != '/')
425 pos++;
426 // size requires multiplication by blocksize.
427 //
428 // We could assume blocksize is 512 (like Lynx does) and
429 // shift by 9, but that might not be right. Even if it
430 // were, doing that wouldn't reflect what the file's
431 // real size was. The sanest thing to do is not use the
432 // LISTing's filesize, so we won't (like ftpmirror).
433 //
434 // ulltoa(((uint64)fsz)<<9, result->fe_size, 10);
435 //
436 // A block is always 512 bytes on OpenVMS, compute size.
437 // So its rounded up to the next block, so what, its better
438 // than not showing the size at all.
439 // A block is always 512 bytes on OpenVMS, compute size.
440 // So its rounded up to the next block, so what, its better
441 // than not showing the size at all.
442 uint64 size = strtoul(tokens[1], NULL, 10) * 512;
443 #if defined(OS_WIN)
444 sprintf_s(result->fe_size, sizeof(result->fe_size), "%lld", size);
445 #elif defined(OS_POSIX)
446 snprintf(result->fe_size, sizeof(result->fe_size), "%lld", size);
447 #endif
448 } // if (result->fe_type != FTP_TYPE_DIRECTORY)
449
450 p = tokens[2] + 2;
451 if (*p == '-')
452 p++;
453 tbuf[0] = p[0];
454 tbuf[1] = tolower(p[1]);
455 tbuf[2] = tolower(p[2]);
456 month_num = 0;
457 for (pos = 0; pos < (12*3); pos += 3) {
458 if (tbuf[0] == month_names[pos+0] &&
459 tbuf[1] == month_names[pos + 1] &&
460 tbuf[2] == month_names[pos + 2])
461 break;
462 month_num++;
463 }
464 if (month_num >= 12)
465 month_num = 0;
466 result->fe_time.tm_mon = month_num;
467 result->fe_time.tm_mday = StringToInt(tokens[2]);
468 result->fe_time.tm_year = StringToInt(p + 4);
469 // NSPR wants year as XXXX
470
471 p = tokens[3] + 2;
472 if (*p == ':')
473 p++;
474 if (p[2] == ':')
475 result->fe_time.tm_sec = StringToInt(p + 3);
476 result->fe_time.tm_hour = StringToInt(tokens[3]);
477 result->fe_time.tm_min = StringToInt(p);
478 return result->fe_type;
479 } // if (isdigit(*tokens[1]))
480 return FTP_TYPE_JUNK; // junk
481 } // if (lstyle == 'V')
482 } // if (!lstyle && (!state->lstyle || state->lstyle == 'V'))
483 #endif // SUPPORT_VMS
484
485 #if defined(SUPPORT_CMS)
486 // Virtual Machine/Conversational Monitor System (IBM Mainframe)
487 if (!lstyle && (!state->lstyle || state->lstyle == 'C')) { // VM/CMS
488 // LISTing according to mirror.pl
489 // Filename FileType Fm Format Lrecl Records Blocks Date Time
490 // LASTING GLOBALV A1 V 41 21 1 9/16/91 15:10:32
491 // J43401 NETLOG A0 V 77 1 1 9/12/91 12:36:04
492 // PROFILE EXEC A1 V 17 3 1 9/12/91 12:39:07
493 // DIRUNIX SCRIPT A1 V 77 1216 17 1/04/93 20:30:47
494 // MAIL PROFILE A2 F 80 1 1 10/14/92 16:12:27
495 // BADY2K TEXT A0 V 1 1 1 1/03/102 10:11:12
496 // AUTHORS A1 DIR - - - 9/20/99 10:31:11
497 //
498 // LISTing from vm.marist.edu and vm.sc.edu
499 // 220-FTPSERVE IBM VM Level 420 at VM.MARIST.EDU, 04:58:12 EDT WEDNESDAY 2002-07-10
500 // AUTHORS DIR - - - 1999-09-20 10:31:11 -
501 // HARRINGTON DIR - - - 1997-02-12 15:33:28 -
502 // PICS DIR - - - 2000-10-12 15:43:23 -
503 // SYSFILE DIR - - - 2000-07-20 17:48:01 -
504 // WELCNVT EXEC V 72 9 1 1999-09-20 17:16:18 -
505 // WELCOME EREADME F 80 21 1 1999-12-27 16:19:00 -
506 // WELCOME README V 82 21 1 1999-12-27 16:19:04 -
507 // README ANONYMOU V 71 26 1 1997-04-02 12:33:20 TCP291
508 // README ANONYOLD V 71 15 1 1995-08-25 16:04:27 TCP291
509 if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16) {
510 for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) {
511 p = tokens[pos];
512 if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
513 (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R')) {
514 if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
515 (tokens[pos+5][5]) == ':') {
516 p = tokens[pos+4];
517 if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
518 (toklen[pos+4] >= 7 && toklen[pos+4] <= 9 &&
519 p[((p[1] != '/')?(2):(1))] == '/' &&
520 p[((p[1] != '/')?(5):(4))] == '/')) {
521 // Y2K bugs possible ("7/06/102" or "13/02/101")
522 if ((*tokens[pos+1] == '-' &&
523 *tokens[pos+2] == '-' &&
524 *tokens[pos+3] == '-') ||
525 (isdigit(*tokens[pos+1]) &&
526 isdigit(*tokens[pos+2]) &&
527 isdigit(*tokens[pos+3]))) {
528 lstyle = 'C';
529 tokmarker = pos;
530 } // if ((*tokens[pos+1] == '-' &&
531 } // if ((toklen[pos+4] == 10
532 } // toklen[pos+5] == 8
533 } // if ((toklen[pos] == 1 && (*p == 'F'
534 } // for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
535 } // if (numtoks >= 7)
536 // extra checking if first pass
537 if (lstyle && !state->lstyle) {
538 for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++) {
539 if (isalpha(*p) && toupper(*p) != *p)
540 lstyle = 0;
541 }
542 for (pos = tokmarker+1; pos <= tokmarker+3; pos++) {
543 if (!(toklen[pos] == 1 && *tokens[pos] == '-')) {
544 for (p = tokens[pos]; lstyle && p < (tokens[pos] + toklen[pos]);
545 p++) {
546 if (!isdigit(*p))
547 lstyle = 0;
548 } // for (p = tokens[pos]; lstyle
549 } // if (!(toklen[pos] == 1
550 } // for (pos = tokmarker+1;
551 for (pos = 0, p = tokens[tokmarker+4];
552 lstyle && pos < toklen[tokmarker+4]; pos++, p++) {
553 if (*p == '/') {
554 // There may be Y2K bugs in the date. Don't simplify to
555 // pos != (len-3) && pos != (len-6) like time is done.
556 if ((tokens[tokmarker + 4][1]) == '/') {
557 if (pos != 1 && pos != 4)
558 lstyle = 0;
559 } else if (pos != 2 && pos != 5) {
560 lstyle = 0;
561 }
562 } else if (*p != '-' && !isdigit(*p)) {
563 lstyle = 0;
564 } else if (*p == '-' && pos != 4 && pos != 7) {
565 lstyle = 0;
566 }
567 }
568 for (pos = 0, p = tokens[tokmarker+5];
569 lstyle && pos < toklen[tokmarker+5]; pos++, p++) {
570 if (*p != ':' && !isdigit(*p))
571 lstyle = 0;
572 else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
573 && pos != (toklen[tokmarker+5]-6))
574 lstyle = 0;
575 }
576 } // if (lstyle && !state->lstyle)
577
578 if (lstyle == 'C') {
579 state->parsed_one = 1;
580 state->lstyle = lstyle;
581
582 p = tokens[tokmarker+4];
583 if (toklen[tokmarker+4] == 10) { // newstyle: YYYY-MM-DD format
584 result->fe_time.tm_year = StringToInt(p + 0) - 1900;
585 result->fe_time.tm_mon = StringToInt(p + 5) - 1;
586 result->fe_time.tm_mday = StringToInt(p + 8);
587 } else { // oldstyle: [M]M/DD/YY format
588 pos = toklen[tokmarker + 4];
589 result->fe_time.tm_mon = StringToInt(p) - 1;
590 result->fe_time.tm_mday = StringToInt((p + pos)-5);
591 result->fe_time.tm_year = StringToInt((p + pos)-2);
592 if (result->fe_time.tm_year < 70)
593 result->fe_time.tm_year += 100;
594 }
595 p = tokens[tokmarker + 5];
596 pos = toklen[tokmarker + 5];
597 result->fe_time.tm_hour = StringToInt(p);
598 result->fe_time.tm_min = StringToInt((p + pos) - 5);
599 result->fe_time.tm_sec = StringToInt((p + pos) - 2);
600
601 result->fe_cinfs = 1;
602 result->fe_fname = tokens[0];
603 result->fe_fnlen = toklen[0];
604 result->fe_type = FTP_TYPE_FILE;
605
606 p = tokens[tokmarker];
607 if (toklen[tokmarker] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R')
608 result->fe_type = FTP_TYPE_DIRECTORY;
609
610 if ((toklen[tokmarker+4] == 10 && tokmarker > 1) ||
611 (toklen[tokmarker+4] != 10 && tokmarker > 2)) {
612 // have a filetype column
613 char *dot;
614 p = &(tokens[0][toklen[0]]);
615 memcpy(&dot, &p, sizeof(dot)); // NASTY
616 *dot++ = '.';
617 p = tokens[1];
618 for (pos = 0; pos < toklen[1]; pos++)
619 *dot++ = *p++;
620 result->fe_fnlen += 1 + toklen[1];
621 }
622 // oldstyle LISTING:
623 // files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable
624 // if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
625 // return FTP_TYPE_JUNK;
626
627 // VM/CMS LISTings have no usable filesize field.
628 // Have to use the 'SIZE' command for that.
629 return result->fe_type;
630 } // if (lstyle == 'C' && (!state->lstyle || state->lstyle == lstyle))
631 } // VM/CMS
632 #endif // SUPPORT_CMS
633
634 #if defined(SUPPORT_DOS) // WinNT DOS dirstyle
635 if (!lstyle && (!state->lstyle || state->lstyle == 'W')) {
636 // "10-23-00 01:27PM <DIR> veronist"
637 // "06-15-00 07:37AM <DIR> zoe"
638 // "07-14-00 01:35PM 2094926 canprankdesk.tif"
639 // "07-21-00 01:19PM 95077 Jon Kauffman Enjoys the Good Life.jpg"
640 // "07-21-00 01:19PM 52275 Name Plate.jpg"
641 // "07-14-00 01:38PM 2250540 Valentineoffprank-HiRes.jpg"
642 if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 &&
643 (*tokens[2] == '<' || isdigit(*tokens[2]))) {
644 p = tokens[0];
645 if (isdigit(p[0]) && isdigit(p[1]) && p[2] == '-' &&
646 isdigit(p[3]) && isdigit(p[4]) && p[5] == '-' &&
647 isdigit(p[6]) && isdigit(p[7])) {
648 p = tokens[1];
649 if (isdigit(p[0]) && isdigit(p[1]) && p[2] == ':' &&
650 isdigit(p[3]) && isdigit(p[4]) &&
651 (p[5] == 'A' || p[5] == 'P') && p[6] == 'M') {
652 lstyle = 'W';
653 if (!state->lstyle) {
654 p = tokens[2];
655 // <DIR> or <JUNCTION>
656 if (*p != '<' || p[toklen[2] - 1] != '>') {
657 for (pos = 1; (lstyle && pos < toklen[2]); pos++) {
658 if (!isdigit(*++p))
659 lstyle = 0;
660 }
661 }
662 }
663 }
664 }
665 }
666 if (lstyle == 'W') {
667 state->parsed_one = 1;
668 state->lstyle = lstyle;
669
670 p = &(line[linelen_sans_wsp]); // line end sans wsp
671 result->fe_cinfs = 1;
672 result->fe_fname = tokens[3];
673 result->fe_fnlen = p - tokens[3];
674 result->fe_type = FTP_TYPE_DIRECTORY;
675
676 if (*tokens[2] != '<') { // not <DIR> or <JUNCTION>
677 result->fe_type = FTP_TYPE_FILE;
678 pos = toklen[2];
679 while (pos > (sizeof(result->fe_size) - 1))
680 pos = (sizeof(result->fe_size) - 1);
681 memcpy(result->fe_size, tokens[2], pos);
682 result->fe_size[pos] = '\0';
683 } else if ((tokens[2][1]) != 'D') { // not <DIR>
684 result->fe_type = FTP_TYPE_JUNK; // unknown until junc for sure
685 if (result->fe_fnlen > 4) {
686 p = result->fe_fname;
687 for (pos = result->fe_fnlen - 4; pos > 0; pos--) {
688 if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
689 (p[1] == '=' || p[1] == '-')) {
690 result->fe_type = FTP_TYPE_SYMLINK;
691 result->fe_fnlen = p - result->fe_fname;
692 result->fe_lname = p + 4;
693 result->fe_lnlen = &(line[linelen_sans_wsp])
694 - result->fe_lname;
695 break;
696 }
697 p++;
698 }
699 }
700 }
701
702 result->fe_time.tm_mon = StringToInt(tokens[0]+0);
703 if (result->fe_time.tm_mon != 0) {
704 result->fe_time.tm_mon--;
705 result->fe_time.tm_mday = StringToInt(tokens[0]+3);
706 result->fe_time.tm_year = StringToInt(tokens[0]+6);
707 if (result->fe_time.tm_year < 80)
708 result->fe_time.tm_year += 100;
709 }
710
711 result->fe_time.tm_hour = StringToInt(tokens[1]+0);
712 result->fe_time.tm_min = StringToInt(tokens[1]+3);
713 if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12)
714 result->fe_time.tm_hour += 12;
715
716 // the caller should do this (if dropping "." and ".." is desired)
717 // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0]
718 // == '.' &&
719 // (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
720 // result->fe_fname[1] == '.')))
721 // return FTP_TYPE_JUNK;
722
723 return result->fe_type;
724 } // if (lstyle == 'W' && (!state->lstyle || state->lstyle == lstyle))
725 } // if (!lstyle && (!state->lstyle || state->lstyle == 'W'))
726 #endif
727
728 #if defined(SUPPORT_OS2)
729 if (!lstyle && (!state->lstyle || state->lstyle == 'O')) { // OS/2 test
730 // 220 server IBM TCP/IP for OS/2 - FTP Server ver 23:04:36 on
731 // Jan 15 1997 ready.
732 // fixed position, space padded columns. I have only a vague idea
733 // of what the contents between col 18 and 34 might be: All I can infer
734 // is that there may be attribute flags in there and there may be
735 // a " DIR" in there.
736 //
737 // 1 2 3 4 5 6
738 // 0123456789012345678901234567890123456789012345678901234567890123456789
739 // ----- size -------|??????????????? MM-DD-YY| HH:MM| nnnnnnnnn....
740 // 0 DIR 04-11-95 16:26 .
741 // 0 DIR 04-11-95 16:26 ..
742 // 0 DIR 04-11-95 16:26 ADDRESS
743 // 612 RHSA 07-28-95 16:45 air_tra1.bag
744 // 195 A 08-09-95 10:23 Alfa1.bag
745 // 0 RHS DIR 04-11-95 16:26 ATTACH
746 // 372 A 08-09-95 10:26 Aussie_1.bag
747 // 310992 06-28-94 09:56 INSTALL.EXE
748 // 1 2 3 4
749 // 01234567890123456789012345678901234567890123456789
750 // dirlist from the mirror.pl project, col positions from Mozilla.
751
752 p = &(line[toklen[0]]);
753 // \s(\d\d-\d\d-\d\d)\s+(\d\d:\d\d)\s
754 if (numtoks >= 4 && toklen[0] <= 18 && isdigit(*tokens[0]) &&
755 (linelen - toklen[0]) >= (53-18) &&
756 p[18-18] == ' ' && p[34-18] == ' ' &&
757 p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' ' &&
758 p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' ' &&
759 isdigit(p[35-18]) && isdigit(p[36-18]) &&
760 isdigit(p[38-18]) && isdigit(p[39-18]) &&
761 isdigit(p[41-18]) && isdigit(p[42-18]) &&
762 isdigit(p[46-18]) && isdigit(p[47-18]) &&
763 isdigit(p[49-18]) && isdigit(p[50-18])) {
764 lstyle = 'O'; // OS/2
765 if (!state->lstyle) {
766 for (pos = 1; lstyle && pos < toklen[0]; pos++) {
767 if (!isdigit(tokens[0][pos]))
768 lstyle = 0;
769 }
770 }
771 }
772
773 if (lstyle == 'O') {
774 state->parsed_one = 1;
775 state->lstyle = lstyle;
776
777 p = &(line[toklen[0]]);
778
779 result->fe_cinfs = 1;
780 result->fe_fname = &p[53-18];
781 result->fe_fnlen = (&(line[linelen_sans_wsp]))
782 - (result->fe_fname);
783 result->fe_type = FTP_TYPE_FILE;
784
785 // I don't have a real listing to determine exact pos, so scan.
786 for (pos = (18-18); pos < ((35-18)-4); pos++) {
787 if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
788 p[pos+2] == 'I' && p[pos+3] == 'R') {
789 result->fe_type = FTP_TYPE_DIRECTORY;
790 break;
791 }
792 }
793
794 if (result->fe_type != FTP_TYPE_DIRECTORY) {
795 pos = toklen[0];
796 if (pos > (sizeof(result->fe_size)-1))
797 pos = (sizeof(result->fe_size)-1);
798 memcpy(result->fe_size, tokens[0], pos);
799 result->fe_size[pos] = '\0';
800 }
801
802 result->fe_time.tm_mon = StringToInt(&p[35-18]) - 1;
803 result->fe_time.tm_mday = StringToInt(&p[38-18]);
804 result->fe_time.tm_year = StringToInt(&p[41-18]);
805 if (result->fe_time.tm_year < 80)
806 result->fe_time.tm_year += 100;
807 result->fe_time.tm_hour = StringToInt(&p[46-18]);
808 result->fe_time.tm_min = StringToInt(&p[49-18]);
809
810 // The caller should do this (if dropping "." and ".." is desired)
811 // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0] == ' .' &&
812 // (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
813 // result->fe_fname[1] == '.')))
814 // return FTP_TYPE_JUNK;
815
816 return result->fe_type;
817 } // if (lstyle == 'O')
818 } // if (!lstyle && (!state->lstyle || state->lstyle == 'O'))
819 #endif // SUPPORT_OS2
820
821 #if defined(SUPPORT_LSL)
822 if (!lstyle && (!state->lstyle || state->lstyle == 'U')) { // /bin/ls & co.
823 // UNIX-style listing, without inum and without blocks
824 // "-rw-r--r-- 1 root other 531 Jan 29 03:26 README"
825 // "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc"
826 // "dr-xr-xr-x 2 root 512 Apr 8 1994 etc"
827 // "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin"
828 // Also produced by Microsoft's FTP servers for Windows:
829 // "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z"
830 // "d--------- 1 owner group 0 May 9 19:45 Softlib"
831 // Also WFTPD for MSDOS:
832 // "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp"
833 // Hellsoft for NetWare:
834 // "d[RWCEMFA] supervisor 512 Jan 16 18:53 login"
835 // "-[RWCEMFA] rhesus 214059 Oct 20 15:27 cx.exe"
836 // Newer Hellsoft for NetWare: (netlab2.usu.edu)
837 // - [RWCEAFMS] NFAUUser 192 Apr 27 15:21 HEADER.html
838 // d [RWCEAFMS] jrd 512 Jul 11 03:01 allupdates
839 // Also NetPresenz for the Mac:
840 // "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit"
841 // "drwxrwxr-x folder 2 May 10 1996 network"
842 // Protected directory:
843 // "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming"
844 // uid/gid instead of username/groupname:
845 // "drwxr-xr-x 2 0 0 512 May 28 22:17 etc"
846
847 if (numtoks >= 6) {
848 // there are two perm formats (Hellsoft/NetWare and *IX strmode(3)).
849 // Scan for size column only if the perm format is one or the other.
850 if (toklen[0] == 1 || (tokens[0][1]) == '[') {
851 if (*tokens[0] == FTP_TYPE_DIRECTORY || *tokens[0] == '-') {
852 pos = toklen[0]-1;
853 p = tokens[0] + 1;
854 if (pos == 0) {
855 p = tokens[1];
856 pos = toklen[1];
857 }
858 if ((pos == 9 || pos == 10) &&
859 (*p == '[' && p[pos-1] == ']') &&
860 (p[1] == 'R' || p[1] == '-') &&
861 (p[2] == 'W' || p[2] == '-') &&
862 (p[3] == 'C' || p[3] == '-') &&
863 (p[4] == 'E' || p[4] == '-')) {
864 // rest is FMA[S] or AFM[S]
865 lstyle = 'U'; // very likely one of the NetWare servers
866 }
867 }
868 } else if ((toklen[0] == 10 || toklen[0] == 11) &&
869 strchr("-bcdlpsw?DFam", *tokens[0])) {
870 p = &(tokens[0][1]);
871 if ((p[0] == 'r' || p[0] == '-') &&
872 (p[1] == 'w' || p[1] == '-') &&
873 (p[3] == 'r' || p[3] == '-') &&
874 (p[4] == 'w' || p[4] == '-') &&
875 (p[6] == 'r' || p[6] == '-') &&
876 (p[7] == 'w' || p[7] == '-')) {
877 // 'x'/p[9] can be S|s|x|-|T|t or implementation specific
878 lstyle = 'U'; // very likely /bin/ls
879 }
880 }
881 }
882 if (lstyle == 'U') { // first token checks out
883 lstyle = 0;
884 for (pos = (numtoks-5); !lstyle && pos > 1; pos--) {
885 // scan for: (\d+)\s+([A-Z][a-z][a-z])\s+
886 // (\d\d\d\d|\d\:\d\d|\d\d\:\d\d|\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
887 // \s+(.+)$
888 if (isdigit(*tokens[pos]) // size
889 // (\w\w\w)
890 && toklen[pos+1] == 3 && isalpha(*tokens[pos+1]) &&
891 isalpha(tokens[pos+1][1]) && isalpha(tokens[pos+1][2])
892 // (\d|\d\d)
893 && isdigit(*tokens[pos+2]) &&
894 (toklen[pos+2] == 1 ||
895 (toklen[pos+2] == 2 && isdigit(tokens[pos+2][1])))
896 && toklen[pos+3] >= 4 && isdigit(*tokens[pos+3])
897 // (\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
898 && (toklen[pos+3] <= 5 || (
899 (toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
900 (tokens[pos+3][toklen[pos+3]-3]) == ':'))
901 && isdigit(tokens[pos+3][toklen[pos+3]-2])
902 && isdigit(tokens[pos+3][toklen[pos+3]-1])
903 && (
904 // (\d\d\d\d)
905 ((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
906 isdigit(tokens[pos+3][1]) &&
907 isdigit(tokens[pos+3][2]) )
908 // (\d\:\d\d|\d\:\d\d\:\d\d)
909 || ((toklen[pos+3] == 4 || toklen[pos+3] == 7) &&
910 (tokens[pos+3][1]) == ':' &&
911 isdigit(tokens[pos+3][2]) && isdigit(tokens[pos+3][3]))
912 // (\d\d\:\d\d|\d\d\:\d\d\:\d\d)
913 || ((toklen[pos+3] == 5 || toklen[pos+3] == 8) &&
914 isdigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
915 isdigit(tokens[pos+3][3]) && isdigit(tokens[pos+3][4])))) {
916 lstyle = 'U'; // assume /bin/ls or variant format
917 tokmarker = pos;
918 // check that size is numeric
919 p = tokens[tokmarker];
920 unsigned int i;
921 for (i = 0; i < toklen[tokmarker]; i++) {
922 if (!isdigit(*p++)) {
923 lstyle = 0;
924 break;
925 }
926 }
927 if (lstyle) {
928 month_num = 0;
929 p = tokens[tokmarker+1];
930 for (i = 0; i < (12*3); i+=3) {
931 if (p[0] == month_names[i+0] &&
932 p[1] == month_names[i+1] &&
933 p[2] == month_names[i+2])
934 break;
935 month_num++;
936 }
937 if (month_num >= 12)
938 lstyle = 0;
939 }
940 } // relative position test
941 } // for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
942 } // if (lstyle == 'U')
943
944 if (lstyle == 'U') {
945 state->parsed_one = 1;
946 state->lstyle = lstyle;
947
948 result->fe_cinfs = 0;
949 result->fe_type = FTP_TYPE_JUNK;
950 if (*tokens[0] == 'd' || *tokens[0] == 'D')
951 result->fe_type = FTP_TYPE_DIRECTORY;
952 else if (*tokens[0] == 'l')
953 result->fe_type = FTP_TYPE_SYMLINK;
954 else if (*tokens[0] == '-' || *tokens[0] == 'F')
955 result->fe_type = FTP_TYPE_FILE; // (hopefully a regular file)
956
957 if (result->fe_type != FTP_TYPE_DIRECTORY) {
958 pos = toklen[tokmarker];
959 if (pos > (sizeof(result->fe_size)-1))
960 pos = (sizeof(result->fe_size)-1);
961 memcpy(result->fe_size, tokens[tokmarker], pos);
962 result->fe_size[pos] = '\0';
963 }
964
965 result->fe_time.tm_mon = month_num;
966 result->fe_time.tm_mday = StringToInt(tokens[tokmarker+2]);
967 if (result->fe_time.tm_mday == 0)
968 result->fe_time.tm_mday++;
969
970 p = tokens[tokmarker+3];
971 pos = (unsigned int)StringToInt(p);
972 if (p[1] == ':') // one digit hour
973 p--;
974 if (p[2] != ':') { // year
975 result->fe_time.tm_year = pos;
976 } else {
977 result->fe_time.tm_hour = pos;
978 result->fe_time.tm_min = StringToInt(p+3);
979 if (p[5] == ':')
980 result->fe_time.tm_sec = StringToInt(p+6);
981
982 if (!state->now_time) {
983 time_t now = time(NULL);
984 state->now_time = now * 1000000;
985 // TODO(ibrar): Yet to give another thouth
986 #if defined(OS_WIN)
987 gmtime_s(&state->now_tm, &now);
988 #elif defined(OS_POSIX)
989 gmtime_r(&now, &state->now_tm);
990 #endif
991 }
992
993 result->fe_time.tm_year = state->now_tm.tm_year;
994 if ( (( state->now_tm.tm_mon << 5) + state->now_tm.tm_mday) <
995 ((result->fe_time.tm_mon << 5) + result->fe_time.tm_mday) )
996 result->fe_time.tm_year--;
997 } // time/year
998
999 result->fe_fname = tokens[tokmarker+4];
1000 result->fe_fnlen = (&(line[linelen_sans_wsp]))
1001 - (result->fe_fname);
1002
1003 if (result->fe_type == FTP_TYPE_SYMLINK && result->fe_fnlen > 4) {
1004 p = result->fe_fname + 1;
1005 for (pos = 1; pos < (result->fe_fnlen - 4); pos++) {
1006 if (*p == ' ' && p[1] == '-' && p[2] == '>' && p[3] == ' ') {
1007 result->fe_lname = p + 4;
1008 result->fe_lnlen = (&(line[linelen_sans_wsp]))
1009 - (result->fe_lname);
1010 result->fe_fnlen = pos;
1011 break;
1012 }
1013 p++;
1014 }
1015 }
1016
1017 #if defined(SUPPORT_LSLF) // some (very rare) servers return ls -lF
1018 if (result->fe_fnlen > 1) {
1019 p = result->fe_fname[result->fe_fnlen-1];
1020 pos = result->fe_type;
1021 if (pos == FTP_TYPE_DIRECTORY) {
1022 if (*p == '/') result->fe_fnlen--; // directory
1023 } else if (pos == FTP_TYPE_SYMLINK) {
1024 if (*p == '@') result->fe_fnlen--; // symlink
1025 } else if (pos == FTP_TYPE_FILE) {
1026 if (*p == '*') result->fe_fnlen--; // executable
1027 } else if (*p == '=' || *p == '%' || *p == '|') {
1028 result->fe_fnlen--; // socket, whiteout, fifo
1029 }
1030 }
1031 #endif // SUPPORT_LSLF
1032 // the caller should do this (if dropping "." and ".." is desired)
1033 // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0] == ' .' &&
1034 // (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1035 // result->fe_fname[1] == '.')))
1036 // return FTP_TYPE_JUNK;
1037 return result->fe_type;
1038 } // if (lstyle == 'U')
1039 } // if (!lstyle && (!state->lstyle || state->lstyle == 'U'))
1040 #endif // SUPPORT_LSL
1041
1042 #if defined(SUPPORT_W16) // 16bit Windows
1043 // old SuperTCP suite FTP server for Win3.1
1044 if (!lstyle && (!state->lstyle || state->lstyle == 'w')) {
1045 // old NetManage Chameleon TCP/IP suite FTP server for Win3.1
1046 // SuperTCP dirlist from the mirror.pl project
1047 // mon/day/year separator may be '/' or '-'.
1048 // . <DIR> 11-16-94 17:16
1049 // .. <DIR> 11-16-94 17:16
1050 // INSTALL <DIR> 11-16-94 17:17
1051 // CMT <DIR> 11-21-94 10:17
1052 // DESIGN1.DOC 11264 05-11-95 14:20
1053 // README.TXT 1045 05-10-95 11:01
1054 // WPKIT1.EXE 960338 06-21-95 17:01
1055 // CMT.CSV 0 07-06-95 14:56
1056 //
1057 // Chameleon dirlist guessed from lynx
1058 // . <DIR> Nov 16 1994 17:16
1059 // .. <DIR> Nov 16 1994 17:16
1060 // INSTALL <DIR> Nov 16 1994 17:17
1061 // CMT <DIR> Nov 21 1994 10:17
1062 // DESIGN1.DOC 11264 May 11 1995 14:20 A
1063 // README.TXT 1045 May 10 1995 11:01
1064 // WPKIT1.EXE 960338 Jun 21 1995 17:01 R
1065 // CMT.CSV 0 Jul 06 1995 14:56 RHA
1066 if (numtoks >= 4 && toklen[0] < 13 &&
1067 ((toklen[1] == 5 && *tokens[1] == '<') || isdigit(*tokens[1]))) {
1068 if (numtoks == 4
1069 && (toklen[2] == 8 || toklen[2] == 9)
1070 && (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
1071 ((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
1072 && (toklen[3] == 4 || toklen[3] == 5)
1073 && (tokens[3][toklen[3]-3]) == ':'
1074 && isdigit(tokens[2][0]) && isdigit(tokens[2][1])
1075 && isdigit(tokens[2][3]) && isdigit(tokens[2][4])
1076 && isdigit(tokens[2][6]) && isdigit(tokens[2][7])
1077 && (toklen[2] < 9 || isdigit(tokens[2][8]))
1078 && isdigit(tokens[3][toklen[3]-1])
1079 && isdigit(tokens[3][toklen[3]-2])
1080 && isdigit(tokens[3][toklen[3]-4]) && isdigit(*tokens[3])) {
1081 lstyle = 'w';
1082 } else if ((numtoks == 6 || numtoks == 7)
1083 && toklen[2] == 3 && toklen[3] == 2
1084 && toklen[4] == 4 && toklen[5] == 5
1085 && (tokens[5][2]) == ':'
1086 && isalpha(tokens[2][0]) && isalpha(tokens[2][1])
1087 && isalpha(tokens[2][2])
1088 && isdigit(tokens[3][0]) && isdigit(tokens[3][1])
1089 && isdigit(tokens[4][0]) && isdigit(tokens[4][1])
1090 && isdigit(tokens[4][2]) && isdigit(tokens[4][3])
1091 && isdigit(tokens[5][0]) && isdigit(tokens[5][1])
1092 && isdigit(tokens[5][3]) && isdigit(tokens[5][4])) {
1093 // could also check that (&(tokens[5][5]) - tokens[2]) == 17
1094 lstyle = 'w';
1095 }
1096 if (lstyle && state->lstyle != lstyle) { // first time
1097 p = tokens[1];
1098 if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
1099 p[2] != 'I' || p[3] != 'R' || p[4] != '>') {
1100 for (pos = 0; lstyle && pos < toklen[1]; pos++) {
1101 if (!isdigit(*p++))
1102 lstyle = 0;
1103 }
1104 } // not <DIR>
1105 } // if (first time)
1106 } // if (numtoks == ...)
1107
1108 if (lstyle == 'w') {
1109 state->parsed_one = 1;
1110 state->lstyle = lstyle;
1111
1112 result->fe_cinfs = 1;
1113 result->fe_fname = tokens[0];
1114 result->fe_fnlen = toklen[0];
1115 result->fe_type = FTP_TYPE_DIRECTORY;
1116
1117 p = tokens[1];
1118 if (isdigit(*p)) {
1119 result->fe_type = FTP_TYPE_FILE;
1120 pos = toklen[1];
1121 if (pos > (sizeof(result->fe_size) - 1))
1122 pos = sizeof(result->fe_size) - 1;
1123 memcpy(result->fe_size, p, pos);
1124 result->fe_size[pos] = '\0';
1125 }
1126
1127 p = tokens[2];
1128 if (toklen[2] == 3) { // Chameleon
1129 tbuf[0] = toupper(p[0]);
1130 tbuf[1] = tolower(p[1]);
1131 tbuf[2] = tolower(p[2]);
1132 for (pos = 0; pos < (12*3); pos+=3) {
1133 if (tbuf[0] == month_names[pos+0] &&
1134 tbuf[1] == month_names[pos+1] &&
1135 tbuf[2] == month_names[pos+2]) {
1136 result->fe_time.tm_mon = pos/3;
1137 result->fe_time.tm_mday = StringToInt(tokens[3]);
1138 result->fe_time.tm_year = StringToInt(tokens[4]) - 1900;
1139 break;
1140 }
1141 }
1142 pos = 5; // Chameleon toknum of date field
1143 } else {
1144 result->fe_time.tm_mon = StringToInt(p+0)-1;
1145 result->fe_time.tm_mday = StringToInt(p+3);
1146 result->fe_time.tm_year = StringToInt(p+6);
1147 if (result->fe_time.tm_year < 80) // SuperTCP
1148 result->fe_time.tm_year += 100;
1149
1150 pos = 3; // SuperTCP toknum of date field
1151 }
1152
1153 result->fe_time.tm_hour = StringToInt(tokens[pos]);
1154 result->fe_time.tm_min = StringToInt(&(tokens[pos][toklen[pos]-2]));
1155
1156 // the caller should do this (if dropping "." and ".." is desired)
1157 // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0] == ' .' &&
1158 // (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1159 // result->fe_fname[1] == '.')))
1160 // return FTP_TYPE_JUNK;
1161
1162 return result->fe_type;
1163 } // (lstyle == 'w')
1164 } // if (!lstyle && (!state->lstyle || state->lstyle == 'w'))
1165 #endif // SUPPORT_W16
1166
1167 #if defined(SUPPORT_DLS) // dls -dtR
1168 if (!lstyle &&
1169 (state->lstyle == 'D' || (!state->lstyle && state->numlines == 1))) {
1170 // /bin/dls lines have to be immediately recognizable (first line)
1171 // I haven't seen an FTP server that delivers a /bin/dls listing,
1172 // but can infer the format from the lynx and mirror.pl projects.
1173 // Both formats are supported.
1174 //
1175 // Lynx says:
1176 // README 763 Information about this server\0
1177 // bin/ - \0
1178 // etc/ = \0
1179 // ls-lR 0 \0
1180 // ls-lR.Z 3 \0
1181 // pub/ = Public area\0
1182 // usr/ - \0
1183 // morgan 14 -> ../real/morgan\0
1184 // TIMIT.mostlikely.Z\0
1185 // 79215 \0
1186 //
1187 // mirror.pl says:
1188 // filename: ^(\S*)\s+
1189 // size: (\-|\=|\d+)\s+
1190 // month/day: ((\w\w\w\s+\d+|\d+\s+\w\w\w)\s+
1191 // time/year: (\d+:\d+|\d\d\d\d))\s+
1192 // rest: (.+)
1193 //
1194 // README 763 Jul 11 21:05 Information about this server
1195 // bin/ - Apr 28 1994
1196 // etc/ = 11 Jul 21:04
1197 // ls-lR 0 6 Aug 17:14
1198 // ls-lR.Z 3 05 Sep 1994
1199 // pub/ = Jul 11 21:04 Public area
1200 // usr/ - Sep 7 09:39
1201 // morgan 14 Apr 18 09:39 -> ../real/morgan
1202 // TIMIT.mostlikely.Z
1203 // 79215 Jul 11 21:04
1204 if (!state->lstyle && line[linelen-1] == ':' &&
1205 linelen >= 2 && toklen[numtoks-1] != 1) {
1206 // code in mirror.pl suggests that a listing may be preceded
1207 // by a PWD line in the form "/some/dir/names/here:"
1208 // but does not necessarily begin with '/'. *sigh*
1209 pos = 0;
1210 p = line;
1211 while (pos < (linelen-1)) {
1212 // illegal (or extremely unusual) chars in a dirspec
1213 if (*p == '<' || *p == '|' || *p == '>' ||
1214 *p == '?' || *p == '*' || *p == '\\')
1215 break;
1216 if (*p == '/' && pos < (linelen-2) && p[1] == '/')
1217 break;
1218 pos++;
1219 p++;
1220 }
1221 if (pos == (linelen-1)) {
1222 state->lstyle = 'D';
1223 return FTP_TYPE_JUNK;
1224 }
1225 }
1226
1227 if (!lstyle && numtoks >= 2) {
1228 pos = 22; // pos of (\d+|-|=) if this is not part of a multiline
1229 if (state->lstyle && carry_buf_len) // first is from previous line
1230 pos = toklen[1]-1; // and is 'as-is' (may contain whitespace)
1231 if (linelen > pos) {
1232 p = &line[pos];
1233 if ((*p == '-' || *p == '=' || isdigit(*p)) &&
1234 ((linelen == (pos+1)) ||
1235 (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' '))) {
1236 tokmarker = 1;
1237 if (!carry_buf_len) {
1238 pos = 1;
1239 while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
1240 pos++;
1241 tokmarker = 0;
1242 if ((tokens[pos]+toklen[pos]) == (&line[23]))
1243 tokmarker = pos;
1244 }
1245 if (tokmarker) {
1246 lstyle = 'D';
1247 if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=') {
1248 if (toklen[tokmarker] != 1 ||
1249 (tokens[tokmarker - 1][toklen[tokmarker - 1] - 1]) != '/')
1250 lstyle = 0;
1251 } else {
1252 for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++) {
1253 if (!isdigit(tokens[tokmarker][pos]))
1254 lstyle = 0;
1255 }
1256 }
1257 if (lstyle && !state->lstyle) { // first time
1258 // scan for illegal (or incredibly unusual) chars in fname
1259 for (p = tokens[0]; lstyle &&
1260 p < &(tokens[tokmarker - 1][toklen[tokmarker - 1]]); p++) {
1261 if (*p == '<' || *p == '|' || *p == '>' ||
1262 *p == '?' || *p == '*' || *p == '/' || *p == '\\')
1263 lstyle = 0;
1264 }
1265 }
1266 } // size token found
1267 } // expected chars behind expected size token
1268 } // if (linelen > pos)
1269 } // if (!lstyle && numtoks >= 2)
1270
1271 if (!lstyle && state->lstyle == 'D' && !carry_buf_len) {
1272 // the filename of a multi-line entry can be identified
1273 // correctly only if dls format had been previously established.
1274 // This should always be true because there should be entries
1275 // for '.' and/or '..' and/or CWD that precede the rest of the
1276 // listing.
1277 pos = linelen;
1278 if (pos > (sizeof(state->carry_buf) - 1))
1279 pos = sizeof(state->carry_buf) - 1;
1280 memcpy(state->carry_buf, line, pos);
1281 state->carry_buf_len = pos;
1282 return FTP_TYPE_JUNK;
1283 }
1284
1285 if (lstyle == 'D') {
1286 state->parsed_one = 1;
1287 state->lstyle = lstyle;
1288
1289 p = &(tokens[tokmarker-1][toklen[tokmarker - 1]]);
1290 result->fe_fname = tokens[0];
1291 result->fe_fnlen = p - tokens[0];
1292 result->fe_type = FTP_TYPE_FILE;
1293
1294 if (result->fe_fname[result->fe_fnlen - 1] == '/') {
1295 if (result->fe_lnlen == 1) {
1296 result->fe_type = FTP_TYPE_JUNK;
1297 } else {
1298 result->fe_fnlen--;
1299 result->fe_type = FTP_TYPE_DIRECTORY;
1300 }
1301 } else if (isdigit(*tokens[tokmarker])) {
1302 pos = toklen[tokmarker];
1303 if (pos > (sizeof(result->fe_size) - 1))
1304 pos = sizeof(result->fe_size) - 1;
1305 memcpy(result->fe_size, tokens[tokmarker], pos);
1306 result->fe_size[pos] = '\0';
1307 }
1308
1309 if ((tokmarker+3) < numtoks &&
1310 (&(tokens[numtoks - 1][toklen[numtoks-1]]) -
1311 tokens[tokmarker + 1]) >= (1 + 1 + 3 + 1 + 4)) {
1312 pos = (tokmarker + 3);
1313 p = tokens[pos];
1314 pos = toklen[pos];
1315
1316 if ((pos == 4 || pos == 5) && isdigit(*p) &&
1317 isdigit(p[pos-1]) && isdigit(p[pos - 2])
1318 && ((pos == 5 && p[2] == ':') ||
1319 (pos == 4 && (isdigit(p[1]) || p[1] == ':')))) {
1320 month_num = tokmarker + 1; // assumed position of month field
1321 pos = tokmarker + 2; // assumed position of mday field
1322 if (isdigit(*tokens[month_num])) { // positions are reversed
1323 month_num++;
1324 pos--;
1325 }
1326 p = tokens[month_num];
1327 if (isdigit(*tokens[pos])
1328 && (toklen[pos] == 1 ||
1329 (toklen[pos] == 2 && isdigit(tokens[pos][1])))
1330 && toklen[month_num] == 3
1331 && isalpha(*p) && isalpha(p[1]) && isalpha(p[2])) {
1332 pos = StringToInt(tokens[pos]);
1333 if (pos > 0 && pos <= 31) {
1334 result->fe_time.tm_mday = pos;
1335 month_num = 1;
1336 for (pos = 0; pos < (12*3); pos += 3) {
1337 if (p[0] == month_names[pos + 0] &&
1338 p[1] == month_names[pos + 1] &&
1339 p[2] == month_names[pos + 2])
1340 break;
1341 month_num++;
1342 }
1343 if (month_num > 12)
1344 result->fe_time.tm_mday = 0;
1345 else
1346 result->fe_time.tm_mon = month_num - 1;
1347 }
1348 }
1349 if (result->fe_time.tm_mday) {
1350 tokmarker += 3; // skip mday/mon/yrtime (to find " -> ")
1351 p = tokens[tokmarker];
1352
1353 pos = StringToInt(p);
1354 if (pos > 24) {
1355 result->fe_time.tm_year = pos - 1900;
1356 } else {
1357 if (p[1] == ':')
1358 p--;
1359 result->fe_time.tm_hour = pos;
1360 result->fe_time.tm_min = StringToInt(p + 3);
1361 if (!state->now_time) {
1362 time_t now = time(NULL);
1363 state->now_time = now * 1000000;
1364 // TODO(ibrar): Yet to give anther thought.
1365 #if defined(OS_WIN)
1366 gmtime_s(&state->now_tm, &now);
1367 #elif defined(OS_POSIX)
1368 gmtime_r(&now, &state->now_tm);
1369 #endif
1370 }
1371 result->fe_time.tm_year = state->now_tm.tm_year;
1372 if ((( state->now_tm.tm_mon << 4) +
1373 state->now_tm.tm_mday) <
1374 ((result->fe_time.tm_mon << 4) +
1375 result->fe_time.tm_mday))
1376 result->fe_time.tm_year--;
1377 } // got year or time
1378 } // got month/mday
1379 } // may have year or time
1380 } // enough remaining to possibly have date/time
1381
1382 if (numtoks > (tokmarker+2)) {
1383 pos = tokmarker+1;
1384 p = tokens[pos];
1385 if (toklen[pos] == 2 && *p == '-' && p[1] == '>') {
1386 p = &(tokens[numtoks - 1][toklen[numtoks - 1]]);
1387 result->fe_type = FTP_TYPE_SYMLINK;
1388 result->fe_lname = tokens[pos + 1];
1389 result->fe_lnlen = p - result->fe_lname;
1390 if (result->fe_lnlen > 1 &&
1391 result->fe_lname[result->fe_lnlen -1] == '/')
1392 result->fe_lnlen--;
1393 }
1394 } // if (numtoks > (tokmarker+2))
1395
1396 // The caller should do this (if dropping "." and ".." is desired)
1397 // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0] == ' .' &&
1398 // (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1399 // result->fe_fname[1] == '.')))
1400 // return FTP_TYPE_JUNK;
1401 return result->fe_type;
1402 } // if (lstyle == 'D')
1403 } // if (!lstyle && (!state->lstyle || state->lstyle == 'D'))
1404 #endif
1405 } // if (linelen > 0)
1406
1407 if (state->parsed_one || state->lstyle) // junk if we fail to parse
1408 return FTP_TYPE_JUNK; // this time but had previously parsed successfully
1409 return FTP_TYPE_COMMENT; // its part of a comment or error message
1410 }
1411
1412 } // namespace net
OLDNEW
« no previous file with comments | « net/ftp/ftp_directory_parser.h ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698