2 * Copyright (C) 2002 Cyrus Patel <cyp@fb14.uni-mainz.de>
3 * (C) 2007 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License 2.1 as published by the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 // This was originally Mozilla code, titled ParseFTPList.cpp
21 // Original version of this file can currently be found at: http://mxr.mozilla.org/mozilla1.8/source/netwerk/streamconv/converters/ParseFTPList.cpp
25 #include "FTPDirectoryParser.h"
29 // On Windows, use the threadsafe *_r functions provided by pthread.
30 #elif OS(WINDOWS) && (USE(PTHREADS) || HAVE(PTHREAD_H))
34 #include <wtf/ASCIICType.h>
40 #if PLATFORM(QT) && defined(Q_WS_WIN32)
42 // Replacement for gmtime_r() which is not available on MinGW.
43 // We use this on Win32 Qt platform for portability.
44 struct tm gmtimeQt(const QDateTime& input)
48 QDate date(input.date());
49 result.tm_year = date.year() - 1900;
50 result.tm_mon = date.month();
51 result.tm_mday = date.day();
52 result.tm_wday = date.dayOfWeek();
53 result.tm_yday = date.dayOfYear();
55 QTime time(input.time());
56 result.tm_sec = time.second();
57 result.tm_min = time.minute();
58 result.tm_hour = time.hour();
63 static struct tm *gmtimeQt(const time_t *const timep, struct tm *result)
65 const QDateTime dt(QDateTime::fromTime_t(*timep));
66 *result = WebCore::gmtimeQt(dt);
70 #define gmtime_r(x, y) gmtimeQt(x, y)
71 #elif OS(WINDOWS) && !defined(gmtime_r)
72 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
73 #define gmtime_r(x, y) gmtime_s((y), (x))
75 #define gmtime_r(x,y) (gmtime(x)?(*(y)=*gmtime(x),(y)):0)
79 static inline FTPEntryType ParsingFailed(ListState& state)
81 if (state.parsedOne || state.listStyle) /* junk if we fail to parse */
82 return FTPJunkEntry; /* this time but had previously parsed sucessfully */
83 return FTPMiscEntry; /* its part of a comment or error message */
86 FTPEntryType parseOneFTPLine(const char* line, ListState& state, ListResult& result)
95 /* carry buffer is only valid from one line to the next */
96 unsigned int carry_buf_len = state.carryBufferLength;
97 state.carryBufferLength = 0;
101 /* strip leading whitespace */
102 while (*line == ' ' || *line == '\t')
105 /* line is terminated at first '\0' or '\n' */
106 const char* p = line;
107 while (*p && *p != '\n')
111 if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
114 /* DON'T strip trailing whitespace. */
118 static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
119 const char *tokens[16]; /* 16 is more than enough */
120 unsigned int toklen[WTF_ARRAY_LENGTH(tokens)];
121 unsigned int linelen_sans_wsp; // line length sans whitespace
122 unsigned int numtoks = 0;
123 unsigned int tokmarker = 0; /* extra info for lstyle handler */
124 unsigned int month_num = 0;
128 if (carry_buf_len) /* VMS long filename carryover buffer */
130 tokens[0] = state.carryBuffer;
131 toklen[0] = carry_buf_len;
135 unsigned int pos = 0;
136 while (pos < linelen && numtoks < WTF_ARRAY_LENGTH(tokens))
138 while (pos < linelen &&
139 (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
143 tokens[numtoks] = &line[pos];
144 while (pos < linelen &&
145 (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
147 if (tokens[numtoks] != &line[pos])
149 toklen[numtoks] = (&line[pos] - tokens[numtoks]);
156 return ParsingFailed(state);
158 linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
159 if (numtoks == WTF_ARRAY_LENGTH(tokens))
162 while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
164 linelen_sans_wsp = pos;
167 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
168 #if defined(SUPPORT_EPLF)
169 /* EPLF handling must come somewhere before /bin/dls handling. */
170 if (!lstyle && (!state.listStyle || state.listStyle == 'E'))
172 if (*line == '+' && linelen > 4 && numtoks >= 2)
175 while (pos < (linelen-1))
179 result.type = FTPDirectoryEntry; /* its a dir */
181 result.type = FTPFileEntry; /* its a file */
184 if (isASCIIDigit(line[pos]))
186 while (pos < linelen && isASCIIDigit(line[pos]))
188 if (pos < linelen && line[pos] == ',')
190 unsigned long long seconds = 0;
192 sscanf(p + 1, "%I64u", &seconds);
194 sscanf(p + 1, "%llu", &seconds);
196 time_t t = static_cast<time_t>(seconds);
198 // FIXME: This code has the year 2038 bug
199 gmtime_r(&t, &result.modifiedTime);
200 result.modifiedTime.tm_year += 1900;
206 if (isASCIIDigit(line[pos]))
208 while (pos < linelen && isASCIIDigit(line[pos]))
210 if (pos < linelen && line[pos] == ',')
211 result.fileSize = String(p + 1, &line[pos] - p + 1);
214 else if (isASCIIAlpha(*p)) /* 'i'/'up' or unknown "fact" (property) */
216 while (pos < linelen && *++p != ',')
219 else if (*p != '\t' || (p+1) != tokens[1])
221 break; /* its not EPLF after all */
225 state.parsedOne = true;
226 state.listStyle = lstyle = 'E';
228 p = &(line[linelen_sans_wsp]);
229 result.filename = tokens[1];
230 result.filenameLength = p - tokens[1];
232 if (!result.type) /* access denied */
234 result.type = FTPFileEntry; /* is assuming 'f'ile correct? */
235 return FTPJunkEntry; /* NO! junk it. */
239 if (pos >= (linelen-1) || line[pos] != ',')
242 } /* while (pos < linelen) */
244 } /* if (*line == '+' && linelen > 4 && numtoks >= 2) */
245 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'E')) */
246 #endif /* SUPPORT_EPLF */
248 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
250 #if defined(SUPPORT_VMS)
251 if (!lstyle && (!state.listStyle || state.listStyle == 'V'))
252 { /* try VMS Multinet/UCX/CMS server */
254 * Legal characters in a VMS file/dir spec are [A-Z0-9$.-_~].
255 * '$' cannot begin a filename and `-' cannot be used as the first
256 * or last character. '.' is only valid as a directory separator
257 * and <file>.<type> separator. A canonical filename spec might look
258 * like this: DISK$VOL:[DIR1.DIR2.DIR3]FILE.TYPE;123
259 * All VMS FTP servers LIST in uppercase.
261 * We need to be picky about this in order to support
262 * multi-line listings correctly.
264 if (!state.parsedOne &&
265 (numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
266 memcmp(tokens[0], "Directory", 9)==0 )))
268 /* If no dirstyle has been detected yet, and this line is a
269 * VMS list's dirname, then turn on VMS dirstyle.
270 * eg "ACA:[ANONYMOUS]", "DISK$FTP:[ANONYMOUS]", "SYS$ANONFTP:"
282 while (pos > 0 && p[pos] != '[')
285 if (p[pos] == '-' || p[pos] == '$')
287 if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
288 (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
291 else if (p[pos] != '.' && p[pos] != '~' &&
292 !isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
294 else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
300 if (p[pos] != ':' || p[pos+1] != '[')
304 if (pos > 0 && p[pos] == ':')
309 if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
310 p[pos] != '~' && !isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
312 else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
317 state.listStyle = 'V';
318 return FTPJunkEntry; /* its junk */
323 else if ((tokens[0][toklen[0]-1]) != ';')
325 if (numtoks == 1 && (state.listStyle == 'V' && !carry_buf_len))
327 else if (numtoks < 4)
329 else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
331 else if ((&line[linelen] - tokens[1]) >= 22 &&
332 memcmp(tokens[1], "insufficient privilege", 22) == 0)
334 else if (numtoks != 4 && numtoks != 6)
336 else if (numtoks == 6 && (
337 toklen[5] < 4 || *tokens[5] != '(' || /* perms */
338 (tokens[5][toklen[5]-1]) != ')' ))
340 else if ( (toklen[2] == 10 || toklen[2] == 11) &&
341 (tokens[2][toklen[2]-5]) == '-' &&
342 (tokens[2][toklen[2]-9]) == '-' &&
343 (((toklen[3]==4 || toklen[3]==5 || toklen[3]==7 || toklen[3]==8) &&
344 (tokens[3][toklen[3]-3]) == ':' ) ||
345 ((toklen[3]==10 || toklen[3]==11 ) &&
346 (tokens[3][toklen[3]-3]) == '.' )
347 ) && /* time in [H]H:MM[:SS[.CC]] format */
348 isASCIIDigit(*tokens[1]) && /* size */
349 isASCIIDigit(*tokens[2]) && /* date */
350 isASCIIDigit(*tokens[3]) /* time */
359 * LOGIN.COM;2 1 4-NOV-1994 04:09 [ANONYMOUS] (RWE,RWE,,)
360 * PUB.DIR;1 1 27-JAN-1994 14:46 [ANONYMOUS] (RWE,RWE,RE,RWE)
361 * README.FTP;1 %RMS-E-PRV, insufficient privilege or file protection violation
362 * ROUSSOS.DIR;1 1 27-JAN-1994 14:48 [CS,ROUSSOS] (RWE,RWE,RE,R)
363 * S67-50903.JPG;1 328 22-SEP-1998 16:19 [ANONYMOUS] (RWED,RWED,,)
365 * CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
367 * [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
369 * FOO.BAR;1 4 5-MAR-1993 18:09:01.12
370 * Long filename example:
371 * THIS-IS-A-LONG-VMS-FILENAME.AND-THIS-IS-A-LONG-VMS-FILETYPE\r\n
372 * 213[/nnn] 29-JAN-1996 03:33[:nn] [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
377 if (*p == '[' && toklen[0] >= 4) /* CMU style */
384 while (lstyle && pos < toklen[0] && *p != ']')
386 if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
387 *p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
392 if (lstyle && pos < (toklen[0]-1))
394 /* ']' was found and there is at least one character after it */
398 tokmarker = pos; /* length of leading "[DIR1.DIR2.etc]" */
400 /* not a CMU style listing */
404 while (lstyle && pos < toklen[0] && *p != ';')
406 if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
407 *p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
409 else if (isASCIIAlpha(*p) && *p != toASCIIUpper(*p))
414 if (lstyle && *p == ';')
416 if (pos == 0 || pos == (toklen[0]-1))
418 for (pos++;lstyle && pos < toklen[0];pos++)
420 if (!isASCIIDigit(tokens[0][pos]))
424 pos = (p - tokens[0]); /* => fnlength sans ";####" */
425 pos -= tokmarker; /* => fnlength sans "[DIR1.DIR2.etc]" */
426 p = &(tokens[0][tokmarker]); /* offset of basename */
428 if (!lstyle || pos == 0 || pos > 80) /* VMS filenames can't be longer than that */
432 else if (numtoks == 1)
434 /* if VMS has been detected and there is only one token and that
435 * token was a VMS filename then this is a multiline VMS LIST entry.
437 if (pos >= (sizeof(state.carryBuffer)-1))
438 pos = (sizeof(state.carryBuffer)-1); /* shouldn't happen */
439 memcpy( state.carryBuffer, p, pos );
440 state.carryBufferLength = pos;
441 return FTPJunkEntry; /* tell caller to treat as junk */
443 else if (isASCIIDigit(*tokens[1])) /* not no-privs message */
445 for (pos = 0; lstyle && pos < (toklen[1]); pos++)
447 if (!isASCIIDigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
450 if (lstyle && numtoks > 4) /* Multinet or UCX but not CMU */
452 for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
454 p = &(tokens[5][pos]);
455 if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
460 } /* passed initial tests */
461 } /* else if ((tokens[0][toklen[0]-1]) != ';') */
465 state.parsedOne = true;
466 state.listStyle = lstyle;
468 if (isASCIIDigit(*tokens[1])) /* not permission denied etc */
470 /* strip leading directory name */
471 if (*tokens[0] == '[') /* CMU server */
484 while (pos < toklen[0] && (tokens[0][pos]) != ';')
487 result.caseSensitive = true;
488 result.type = FTPFileEntry;
489 result.filename = tokens[0];
490 result.filenameLength = pos;
494 p = &(tokens[0][pos-4]);
495 if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
497 result.filenameLength -= 4;
498 result.type = FTPDirectoryEntry;
502 if (result.type != FTPDirectoryEntry)
504 /* #### or used/allocated form. If used/allocated form, then
505 * 'used' is the size in bytes if and only if 'used'<=allocated.
506 * If 'used' is size in bytes then it can be > 2^32
507 * If 'used' is not size in bytes then it is size in blocks.
510 while (pos < toklen[1] && (tokens[1][pos]) != '/')
514 * I've never seen size come back in bytes, its always in blocks, and
515 * the following test fails. So, always perform the "size in blocks".
516 * I'm leaving the "size in bytes" code if'd out in case we ever need
520 if (pos < toklen[1] && ( (pos<<1) > (toklen[1]-1) ||
521 (strtoul(tokens[1], (char **)0, 10) >
522 strtoul(tokens[1]+pos+1, (char **)0, 10)) ))
523 { /* size is in bytes */
524 if (pos > (sizeof(result.fe_size)-1))
525 pos = sizeof(result.fe_size)-1;
526 memcpy( result.fe_size, tokens[1], pos );
527 result.fe_size[pos] = '\0';
529 else /* size is in blocks */
532 /* size requires multiplication by blocksize.
534 * We could assume blocksize is 512 (like Lynx does) and
535 * shift by 9, but that might not be right. Even if it
536 * were, doing that wouldn't reflect what the file's
537 * real size was. The sanest thing to do is not use the
538 * LISTing's filesize, so we won't (like ftpmirror).
540 * ulltoa(((unsigned long long)fsz)<<9, result.fe_size, 10);
542 * A block is always 512 bytes on OpenVMS, compute size.
543 * So its rounded up to the next block, so what, its better
544 * than not showing the size at all.
545 * A block is always 512 bytes on OpenVMS, compute size.
546 * So its rounded up to the next block, so what, its better
547 * than not showing the size at all.
549 uint64_t size = strtoul(tokens[1], NULL, 10) * 512;
550 result.fileSize = String::number(size);
553 } /* if (result.type != FTPDirectoryEntry) */
559 tbuf[1] = toASCIILower(p[1]);
560 tbuf[2] = toASCIILower(p[2]);
562 for (pos = 0; pos < (12*3); pos+=3)
564 if (tbuf[0] == month_names[pos+0] &&
565 tbuf[1] == month_names[pos+1] &&
566 tbuf[2] == month_names[pos+2])
572 result.modifiedTime.tm_mon = month_num;
573 result.modifiedTime.tm_mday = atoi(tokens[2]);
574 result.modifiedTime.tm_year = atoi(p+4); // NSPR wants year as XXXX
580 result.modifiedTime.tm_sec = atoi(p+3);
581 result.modifiedTime.tm_hour = atoi(tokens[3]);
582 result.modifiedTime.tm_min = atoi(p);
586 } /* if (isASCIIDigit(*tokens[1])) */
588 return FTPJunkEntry; /* junk */
590 } /* if (lstyle == 'V') */
591 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'V')) */
594 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
596 #if defined(SUPPORT_CMS)
597 /* Virtual Machine/Conversational Monitor System (IBM Mainframe) */
598 if (!lstyle && (!state.listStyle || state.listStyle == 'C')) /* VM/CMS */
600 /* LISTing according to mirror.pl
601 * Filename FileType Fm Format Lrecl Records Blocks Date Time
602 * LASTING GLOBALV A1 V 41 21 1 9/16/91 15:10:32
603 * J43401 NETLOG A0 V 77 1 1 9/12/91 12:36:04
604 * PROFILE EXEC A1 V 17 3 1 9/12/91 12:39:07
605 * DIRUNIX SCRIPT A1 V 77 1216 17 1/04/93 20:30:47
606 * MAIL PROFILE A2 F 80 1 1 10/14/92 16:12:27
607 * BADY2K TEXT A0 V 1 1 1 1/03/102 10:11:12
608 * AUTHORS A1 DIR - - - 9/20/99 10:31:11
610 * LISTing from vm.marist.edu and vm.sc.edu
611 * 220-FTPSERVE IBM VM Level 420 at VM.MARIST.EDU, 04:58:12 EDT WEDNESDAY 2002-07-10
612 * AUTHORS DIR - - - 1999-09-20 10:31:11 -
613 * HARRINGTON DIR - - - 1997-02-12 15:33:28 -
614 * PICS DIR - - - 2000-10-12 15:43:23 -
615 * SYSFILE DIR - - - 2000-07-20 17:48:01 -
616 * WELCNVT EXEC V 72 9 1 1999-09-20 17:16:18 -
617 * WELCOME EREADME F 80 21 1 1999-12-27 16:19:00 -
618 * WELCOME README V 82 21 1 1999-12-27 16:19:04 -
619 * README ANONYMOU V 71 26 1 1997-04-02 12:33:20 TCP291
620 * README ANONYOLD V 71 15 1 1995-08-25 16:04:27 TCP291
622 if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
624 for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
627 if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
628 (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
630 if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
631 (tokens[pos+5][5]) == ':' )
634 if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
635 (toklen[pos+4] >= 7 && toklen[pos+4] <= 9 &&
636 p[((p[1]!='/')?(2):(1))] == '/' &&
637 p[((p[1]!='/')?(5):(4))] == '/'))
638 /* Y2K bugs possible ("7/06/102" or "13/02/101") */
640 if ( (*tokens[pos+1] == '-' &&
641 *tokens[pos+2] == '-' &&
642 *tokens[pos+3] == '-') ||
643 (isASCIIDigit(*tokens[pos+1]) &&
644 isASCIIDigit(*tokens[pos+2]) &&
645 isASCIIDigit(*tokens[pos+3])) )
653 } /* for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) */
654 } /* if (numtoks >= 7) */
656 /* extra checking if first pass */
657 if (lstyle && !state.listStyle)
659 for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
661 if (isASCIIAlpha(*p) && toASCIIUpper(*p) != *p)
664 for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
666 if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
668 for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
670 if (!isASCIIDigit(*p))
675 for (pos = 0, p = tokens[tokmarker+4];
676 lstyle && pos < toklen[tokmarker+4]; pos++, p++)
680 /* There may be Y2K bugs in the date. Don't simplify to
681 * pos != (len-3) && pos != (len-6) like time is done.
683 if ((tokens[tokmarker+4][1]) == '/')
685 if (pos != 1 && pos != 4)
688 else if (pos != 2 && pos != 5)
691 else if (*p != '-' && !isASCIIDigit(*p))
693 else if (*p == '-' && pos != 4 && pos != 7)
696 for (pos = 0, p = tokens[tokmarker+5];
697 lstyle && pos < toklen[tokmarker+5]; pos++, p++)
699 if (*p != ':' && !isASCIIDigit(*p))
701 else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
702 && pos != (toklen[tokmarker+5]-6))
709 state.parsedOne = true;
710 state.listStyle = lstyle;
712 p = tokens[tokmarker+4];
713 if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */
715 result.modifiedTime.tm_year = atoi(p+0) - 1900;
716 result.modifiedTime.tm_mon = atoi(p+5) - 1;
717 result.modifiedTime.tm_mday = atoi(p+8);
719 else /* oldstyle: [M]M/DD/YY format */
721 pos = toklen[tokmarker+4];
722 result.modifiedTime.tm_mon = atoi(p) - 1;
723 result.modifiedTime.tm_mday = atoi((p+pos)-5);
724 result.modifiedTime.tm_year = atoi((p+pos)-2);
725 if (result.modifiedTime.tm_year < 70)
726 result.modifiedTime.tm_year += 100;
729 p = tokens[tokmarker+5];
730 pos = toklen[tokmarker+5];
731 result.modifiedTime.tm_hour = atoi(p);
732 result.modifiedTime.tm_min = atoi((p+pos)-5);
733 result.modifiedTime.tm_sec = atoi((p+pos)-2);
735 result.caseSensitive = true;
736 result.filename = tokens[0];
737 result.filenameLength = toklen[0];
738 result.type = FTPFileEntry;
740 p = tokens[tokmarker];
741 if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
742 result.type = FTPDirectoryEntry;
744 if ((/*newstyle*/ toklen[tokmarker+4] == 10 && tokmarker > 1) ||
745 (/*oldstyle*/ toklen[tokmarker+4] != 10 && tokmarker > 2))
746 { /* have a filetype column */
748 p = &(tokens[0][toklen[0]]);
749 memcpy( &dot, &p, sizeof(dot) ); /* NASTY! */
752 for (pos = 0; pos < toklen[1]; pos++)
754 result.filenameLength += 1 + toklen[1];
758 * files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable
759 if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
763 /* VM/CMS LISTings have no usable filesize field.
764 * Have to use the 'SIZE' command for that.
768 } /* if (lstyle == 'C' && (!state.listStyle || state.listStyle == lstyle)) */
772 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
774 #if defined(SUPPORT_DOS) /* WinNT DOS dirstyle */
775 if (!lstyle && (!state.listStyle || state.listStyle == 'W'))
778 * "10-23-00 01:27PM <DIR> veronist"
779 * "06-15-00 07:37AM <DIR> zoe"
780 * "07-14-00 01:35PM 2094926 canprankdesk.tif"
781 * "07-21-00 01:19PM 95077 Jon Kauffman Enjoys the Good Life.jpg"
782 * "07-21-00 01:19PM 52275 Name Plate.jpg"
783 * "07-14-00 01:38PM 2250540 Valentineoffprank-HiRes.jpg"
785 if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 &&
786 (*tokens[2] == '<' || isASCIIDigit(*tokens[2])) )
789 if ( isASCIIDigit(p[0]) && isASCIIDigit(p[1]) && p[2]=='-' &&
790 isASCIIDigit(p[3]) && isASCIIDigit(p[4]) && p[5]=='-' &&
791 isASCIIDigit(p[6]) && isASCIIDigit(p[7]) )
794 if ( isASCIIDigit(p[0]) && isASCIIDigit(p[1]) && p[2]==':' &&
795 isASCIIDigit(p[3]) && isASCIIDigit(p[4]) &&
796 (p[5]=='A' || p[5]=='P') && p[6]=='M')
799 if (!state.listStyle)
802 /* <DIR> or <JUNCTION> */
803 if (*p != '<' || p[toklen[2]-1] != '>')
805 for (pos = 1; (lstyle && pos < toklen[2]); pos++)
807 if (!isASCIIDigit(*++p))
818 state.parsedOne = true;
819 state.listStyle = lstyle;
821 p = &(line[linelen]); /* line end */
822 result.caseSensitive = true;
823 result.filename = tokens[3];
824 result.filenameLength = p - tokens[3];
825 result.type = FTPDirectoryEntry;
827 if (*tokens[2] != '<') /* not <DIR> or <JUNCTION> */
829 // try to handle correctly spaces at the beginning of the filename
830 // filesize (token[2]) must end at offset 38
831 if (tokens[2] + toklen[2] - line == 38) {
832 result.filename = &(line[39]);
833 result.filenameLength = p - result.filename;
835 result.type = FTPFileEntry;
837 result.fileSize = String(tokens[2], pos);
840 // try to handle correctly spaces at the beginning of the filename
841 // token[2] must begin at offset 24, the length is 5 or 10
842 // token[3] must begin at offset 39 or higher
843 if (tokens[2] - line == 24 && (toklen[2] == 5 || toklen[2] == 10) &&
844 tokens[3] - line >= 39) {
845 result.filename = &(line[39]);
846 result.filenameLength = p - result.filename;
849 if ((tokens[2][1]) != 'D') /* not <DIR> */
851 result.type = FTPJunkEntry; /* unknown until junc for sure */
852 if (result.filenameLength > 4)
855 for (pos = result.filenameLength - 4; pos > 0; pos--)
857 if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
858 (p[1] == '=' || p[1] == '-'))
860 result.type = FTPLinkEntry;
861 result.filenameLength = p - result.filename;
862 result.linkname = p + 4;
863 result.linknameLength = &(line[linelen])
873 result.modifiedTime.tm_mon = atoi(tokens[0]+0);
874 if (result.modifiedTime.tm_mon != 0)
876 result.modifiedTime.tm_mon--;
877 result.modifiedTime.tm_mday = atoi(tokens[0]+3);
878 result.modifiedTime.tm_year = atoi(tokens[0]+6);
879 /* if year has only two digits then assume that
881 80-99 is 1980-1999 */
882 if (result.modifiedTime.tm_year < 80)
883 result.modifiedTime.tm_year += 2000;
884 else if (result.modifiedTime.tm_year < 100)
885 result.modifiedTime.tm_year += 1900;
888 result.modifiedTime.tm_hour = atoi(tokens[1]+0);
889 result.modifiedTime.tm_min = atoi(tokens[1]+3);
890 if ((tokens[1][5]) == 'P' && result.modifiedTime.tm_hour < 12)
891 result.modifiedTime.tm_hour += 12;
893 /* the caller should do this (if dropping "." and ".." is desired)
894 if (result.type == FTPDirectoryEntry && result.filename[0] == '.' &&
895 (result.filenameLength == 1 || (result.filenameLength == 2 &&
896 result.filename[1] == '.')))
901 } /* if (lstyle == 'W' && (!state.listStyle || state.listStyle == lstyle)) */
902 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'W')) */
905 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
907 #if defined(SUPPORT_OS2)
908 if (!lstyle && (!state.listStyle || state.listStyle == 'O')) /* OS/2 test */
910 /* 220 server IBM TCP/IP for OS/2 - FTP Server ver 23:04:36 on Jan 15 1997 ready.
911 * fixed position, space padded columns. I have only a vague idea
912 * of what the contents between col 18 and 34 might be: All I can infer
913 * is that there may be attribute flags in there and there may be
917 *0123456789012345678901234567890123456789012345678901234567890123456789
918 *----- size -------|??????????????? MM-DD-YY| HH:MM| nnnnnnnnn....
919 * 0 DIR 04-11-95 16:26 .
920 * 0 DIR 04-11-95 16:26 ..
921 * 0 DIR 04-11-95 16:26 ADDRESS
922 * 612 RHSA 07-28-95 16:45 air_tra1.bag
923 * 195 A 08-09-95 10:23 Alfa1.bag
924 * 0 RHS DIR 04-11-95 16:26 ATTACH
925 * 372 A 08-09-95 10:26 Aussie_1.bag
926 * 310992 06-28-94 09:56 INSTALL.EXE
928 * 01234567890123456789012345678901234567890123456789
929 * dirlist from the mirror.pl project, col positions from Mozilla.
931 p = &(line[toklen[0]]);
932 /* \s(\d\d-\d\d-\d\d)\s+(\d\d:\d\d)\s */
933 if (numtoks >= 4 && toklen[0] <= 18 && isASCIIDigit(*tokens[0]) &&
934 (linelen - toklen[0]) >= (53-18) &&
935 p[18-18] == ' ' && p[34-18] == ' ' &&
936 p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' ' &&
937 p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' ' &&
938 isASCIIDigit(p[35-18]) && isASCIIDigit(p[36-18]) &&
939 isASCIIDigit(p[38-18]) && isASCIIDigit(p[39-18]) &&
940 isASCIIDigit(p[41-18]) && isASCIIDigit(p[42-18]) &&
941 isASCIIDigit(p[46-18]) && isASCIIDigit(p[47-18]) &&
942 isASCIIDigit(p[49-18]) && isASCIIDigit(p[50-18])
945 lstyle = 'O'; /* OS/2 */
946 if (!state.listStyle)
948 for (pos = 1; lstyle && pos < toklen[0]; pos++)
950 if (!isASCIIDigit(tokens[0][pos]))
958 state.parsedOne = true;
959 state.listStyle = lstyle;
961 p = &(line[toklen[0]]);
963 result.caseSensitive = true;
964 result.filename = &p[53-18];
965 result.filenameLength = (&(line[linelen_sans_wsp]))
967 result.type = FTPFileEntry;
969 /* I don't have a real listing to determine exact pos, so scan. */
970 for (pos = (18-18); pos < ((35-18)-4); pos++)
972 if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
973 p[pos+2] == 'I' && p[pos+3] == 'R')
975 result.type = FTPDirectoryEntry;
980 if (result.type != FTPDirectoryEntry)
983 result.fileSize = String(tokens[0], pos);
986 result.modifiedTime.tm_mon = atoi(&p[35-18]) - 1;
987 result.modifiedTime.tm_mday = atoi(&p[38-18]);
988 result.modifiedTime.tm_year = atoi(&p[41-18]);
989 if (result.modifiedTime.tm_year < 80)
990 result.modifiedTime.tm_year += 100;
991 result.modifiedTime.tm_hour = atoi(&p[46-18]);
992 result.modifiedTime.tm_min = atoi(&p[49-18]);
994 /* the caller should do this (if dropping "." and ".." is desired)
995 if (result.type == FTPDirectoryEntry && result.filename[0] == '.' &&
996 (result.filenameLength == 1 || (result.filenameLength == 2 &&
997 result.filename[1] == '.')))
1002 } /* if (lstyle == 'O') */
1004 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'O')) */
1007 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1009 #if defined(SUPPORT_LSL)
1010 if (!lstyle && (!state.listStyle || state.listStyle == 'U')) /* /bin/ls & co. */
1012 /* UNIX-style listing, without inum and without blocks
1013 * "-rw-r--r-- 1 root other 531 Jan 29 03:26 README"
1014 * "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc"
1015 * "dr-xr-xr-x 2 root 512 Apr 8 1994 etc"
1016 * "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin"
1017 * Also produced by Microsoft's FTP servers for Windows:
1018 * "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z"
1019 * "d--------- 1 owner group 0 May 9 19:45 Softlib"
1020 * Also WFTPD for MSDOS:
1021 * "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp"
1022 * Hellsoft for NetWare:
1023 * "d[RWCEMFA] supervisor 512 Jan 16 18:53 login"
1024 * "-[RWCEMFA] rhesus 214059 Oct 20 15:27 cx.exe"
1025 * Newer Hellsoft for NetWare: (netlab2.usu.edu)
1026 * - [RWCEAFMS] NFAUUser 192 Apr 27 15:21 HEADER.html
1027 * d [RWCEAFMS] jrd 512 Jul 11 03:01 allupdates
1028 * Also NetPresenz for the Mac:
1029 * "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit"
1030 * "drwxrwxr-x folder 2 May 10 1996 network"
1031 * Protected directory:
1032 * "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming"
1033 * uid/gid instead of username/groupname:
1034 * "drwxr-xr-x 2 0 0 512 May 28 22:17 etc"
1037 bool isOldHellsoft = false;
1041 /* there are two perm formats (Hellsoft/NetWare and *IX strmode(3)).
1042 * Scan for size column only if the perm format is one or the other.
1044 if (toklen[0] == 1 || (tokens[0][1]) == '[')
1046 if (*tokens[0] == 'd' || *tokens[0] == '-')
1055 if ((pos == 9 || pos == 10) &&
1056 (*p == '[' && p[pos-1] == ']') &&
1057 (p[1] == 'R' || p[1] == '-') &&
1058 (p[2] == 'W' || p[2] == '-') &&
1059 (p[3] == 'C' || p[3] == '-') &&
1060 (p[4] == 'E' || p[4] == '-'))
1062 /* rest is FMA[S] or AFM[S] */
1063 lstyle = 'U'; /* very likely one of the NetWare servers */
1064 if (toklen[0] == 10)
1065 isOldHellsoft = true;
1069 else if ((toklen[0] == 10 || toklen[0] == 11)
1070 && strchr("-bcdlpsw?DFam", *tokens[0]))
1072 p = &(tokens[0][1]);
1073 if ((p[0] == 'r' || p[0] == '-') &&
1074 (p[1] == 'w' || p[1] == '-') &&
1075 (p[3] == 'r' || p[3] == '-') &&
1076 (p[4] == 'w' || p[4] == '-') &&
1077 (p[6] == 'r' || p[6] == '-') &&
1078 (p[7] == 'w' || p[7] == '-'))
1079 /* 'x'/p[9] can be S|s|x|-|T|t or implementation specific */
1081 lstyle = 'U'; /* very likely /bin/ls */
1085 if (lstyle == 'U') /* first token checks out */
1088 for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
1090 /* scan for: (\d+)\s+([A-Z][a-z][a-z])\s+
1091 * (\d\d\d\d|\d\:\d\d|\d\d\:\d\d|\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
1094 if (isASCIIDigit(*tokens[pos]) /* size */
1096 && toklen[pos+1] == 3 && isASCIIAlpha(*tokens[pos+1]) &&
1097 isASCIIAlpha(tokens[pos+1][1]) && isASCIIAlpha(tokens[pos+1][2])
1099 && isASCIIDigit(*tokens[pos+2]) &&
1100 (toklen[pos+2] == 1 ||
1101 (toklen[pos+2] == 2 && isASCIIDigit(tokens[pos+2][1])))
1102 && toklen[pos+3] >= 4 && isASCIIDigit(*tokens[pos+3])
1103 /* (\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
1104 && (toklen[pos+3] <= 5 || (
1105 (toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
1106 (tokens[pos+3][toklen[pos+3]-3]) == ':'))
1107 && isASCIIDigit(tokens[pos+3][toklen[pos+3]-2])
1108 && isASCIIDigit(tokens[pos+3][toklen[pos+3]-1])
1111 ((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
1112 isASCIIDigit(tokens[pos+3][1]) &&
1113 isASCIIDigit(tokens[pos+3][2]) )
1114 /* (\d\:\d\d|\d\:\d\d\:\d\d) */
1115 || ((toklen[pos+3] == 4 || toklen[pos+3] == 7) &&
1116 (tokens[pos+3][1]) == ':' &&
1117 isASCIIDigit(tokens[pos+3][2]) && isASCIIDigit(tokens[pos+3][3]))
1118 /* (\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
1119 || ((toklen[pos+3] == 5 || toklen[pos+3] == 8) &&
1120 isASCIIDigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
1121 isASCIIDigit(tokens[pos+3][3]) && isASCIIDigit(tokens[pos+3][4]))
1125 lstyle = 'U'; /* assume /bin/ls or variant format */
1128 /* check that size is numeric */
1129 p = tokens[tokmarker];
1130 for (unsigned int i = 0; lstyle && i < toklen[tokmarker]; ++i)
1132 if (!isASCIIDigit(*p++))
1138 p = tokens[tokmarker+1];
1139 for (unsigned int i = 0; i < (12*3); i+=3)
1141 if (p[0] == month_names[i+0] &&
1142 p[1] == month_names[i+1] &&
1143 p[2] == month_names[i+2])
1147 if (month_num >= 12)
1150 } /* relative position test */
1151 } /* for (pos = (numtoks-5); !lstyle && pos > 1; pos--) */
1152 } /* if (lstyle == 'U') */
1156 state.parsedOne = true;
1157 state.listStyle = lstyle;
1159 result.caseSensitive = false;
1160 result.type = FTPJunkEntry;
1161 if (*tokens[0] == 'd' || *tokens[0] == 'D')
1162 result.type = FTPDirectoryEntry;
1163 else if (*tokens[0] == 'l')
1164 result.type = FTPLinkEntry;
1165 else if (*tokens[0] == '-' || *tokens[0] == 'F')
1166 result.type = FTPFileEntry; /* (hopefully a regular file) */
1168 if (result.type != FTPDirectoryEntry)
1170 pos = toklen[tokmarker];
1171 result.fileSize = String(tokens[tokmarker], pos);
1174 result.modifiedTime.tm_mon = month_num;
1175 result.modifiedTime.tm_mday = atoi(tokens[tokmarker+2]);
1176 if (result.modifiedTime.tm_mday == 0)
1177 result.modifiedTime.tm_mday++;
1179 p = tokens[tokmarker+3];
1180 pos = (unsigned int)atoi(p);
1181 if (p[1] == ':') /* one digit hour */
1183 if (p[2] != ':') /* year */
1185 result.modifiedTime.tm_year = pos;
1189 result.modifiedTime.tm_hour = pos;
1190 result.modifiedTime.tm_min = atoi(p+3);
1192 result.modifiedTime.tm_sec = atoi(p+6);
1196 time_t now = time(NULL);
1197 state.now = now * 1000000.0;
1199 // FIXME: This code has the year 2038 bug
1200 gmtime_r(&now, &state.nowFTPTime);
1201 state.nowFTPTime.tm_year += 1900;
1204 result.modifiedTime.tm_year = state.nowFTPTime.tm_year;
1205 if ( (( state.nowFTPTime.tm_mon << 5) + state.nowFTPTime.tm_mday) <
1206 ((result.modifiedTime.tm_mon << 5) + result.modifiedTime.tm_mday) )
1207 result.modifiedTime.tm_year--;
1211 // there is exactly 1 space between filename and previous token in all
1212 // outputs except old Hellsoft
1214 result.filename = tokens[tokmarker+3] + toklen[tokmarker+3] + 1;
1216 result.filename = tokens[tokmarker+4];
1218 result.filenameLength = (&(line[linelen]))
1219 - (result.filename);
1221 if (result.type == FTPLinkEntry && result.filenameLength > 4)
1223 /* First try to use result.fe_size to find " -> " sequence.
1224 This can give proper result for cases like "aaa -> bbb -> ccc". */
1225 unsigned int fileSize = result.fileSize.toUInt();
1227 if (result.filenameLength > (fileSize + 4) &&
1228 strncmp(result.filename + result.filenameLength - fileSize - 4, " -> ", 4) == 0)
1230 result.linkname = result.filename + (result.filenameLength - fileSize);
1231 result.linknameLength = (&(line[linelen])) - (result.linkname);
1232 result.filenameLength -= fileSize + 4;
1236 /* Search for sequence " -> " from the end for case when there are
1237 more occurrences. F.e. if ftpd returns "a -> b -> c" assume
1238 "a -> b" as a name. Powerusers can remove unnecessary parts
1239 manually but there is no way to follow the link when some
1240 essential part is missing. */
1241 p = result.filename + (result.filenameLength - 5);
1242 for (pos = (result.filenameLength - 5); pos > 0; pos--)
1244 if (strncmp(p, " -> ", 4) == 0)
1246 result.linkname = p + 4;
1247 result.linknameLength = (&(line[linelen]))
1248 - (result.linkname);
1249 result.filenameLength = pos;
1257 #if defined(SUPPORT_LSLF) /* some (very rare) servers return ls -lF */
1258 if (result.filenameLength > 1)
1260 p = result.filename[result.filenameLength-1];
1263 if (*p == '/') result.filenameLength--; /* directory */
1264 } else if (pos == 'l') {
1265 if (*p == '@') result.filenameLength--; /* symlink */
1266 } else if (pos == 'f') {
1267 if (*p == '*') result.filenameLength--; /* executable */
1268 } else if (*p == '=' || *p == '%' || *p == '|') {
1269 result.filenameLength--; /* socket, whiteout, fifo */
1274 /* the caller should do this (if dropping "." and ".." is desired)
1275 if (result.type == FTPDirectoryEntry && result.filename[0] == '.' &&
1276 (result.filenameLength == 1 || (result.filenameLength == 2 &&
1277 result.filename[1] == '.')))
1278 return FTPJunkEntry;
1283 } /* if (lstyle == 'U') */
1285 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'U')) */
1288 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1290 #if defined(SUPPORT_W16) /* 16bit Windows */
1291 if (!lstyle && (!state.listStyle || state.listStyle == 'w'))
1292 { /* old SuperTCP suite FTP server for Win3.1 */
1293 /* old NetManage Chameleon TCP/IP suite FTP server for Win3.1 */
1295 * SuperTCP dirlist from the mirror.pl project
1296 * mon/day/year separator may be '/' or '-'.
1297 * . <DIR> 11-16-94 17:16
1298 * .. <DIR> 11-16-94 17:16
1299 * INSTALL <DIR> 11-16-94 17:17
1300 * CMT <DIR> 11-21-94 10:17
1301 * DESIGN1.DOC 11264 05-11-95 14:20
1302 * README.TXT 1045 05-10-95 11:01
1303 * WPKIT1.EXE 960338 06-21-95 17:01
1304 * CMT.CSV 0 07-06-95 14:56
1306 * Chameleon dirlist guessed from lynx
1307 * . <DIR> Nov 16 1994 17:16
1308 * .. <DIR> Nov 16 1994 17:16
1309 * INSTALL <DIR> Nov 16 1994 17:17
1310 * CMT <DIR> Nov 21 1994 10:17
1311 * DESIGN1.DOC 11264 May 11 1995 14:20 A
1312 * README.TXT 1045 May 10 1995 11:01
1313 * WPKIT1.EXE 960338 Jun 21 1995 17:01 R
1314 * CMT.CSV 0 Jul 06 1995 14:56 RHA
1316 if (numtoks >= 4 && toklen[0] < 13 &&
1317 ((toklen[1] == 5 && *tokens[1] == '<') || isASCIIDigit(*tokens[1])) )
1320 && (toklen[2] == 8 || toklen[2] == 9)
1321 && (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
1322 ((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
1323 && (toklen[3] == 4 || toklen[3] == 5)
1324 && (tokens[3][toklen[3]-3]) == ':'
1325 && isASCIIDigit(tokens[2][0]) && isASCIIDigit(tokens[2][1])
1326 && isASCIIDigit(tokens[2][3]) && isASCIIDigit(tokens[2][4])
1327 && isASCIIDigit(tokens[2][6]) && isASCIIDigit(tokens[2][7])
1328 && (toklen[2] < 9 || isASCIIDigit(tokens[2][8]))
1329 && isASCIIDigit(tokens[3][toklen[3]-1]) && isASCIIDigit(tokens[3][toklen[3]-2])
1330 && isASCIIDigit(tokens[3][toklen[3]-4]) && isASCIIDigit(*tokens[3])
1335 else if ((numtoks == 6 || numtoks == 7)
1336 && toklen[2] == 3 && toklen[3] == 2
1337 && toklen[4] == 4 && toklen[5] == 5
1338 && (tokens[5][2]) == ':'
1339 && isASCIIAlpha(tokens[2][0]) && isASCIIAlpha(tokens[2][1])
1340 && isASCIIAlpha(tokens[2][2])
1341 && isASCIIDigit(tokens[3][0]) && isASCIIDigit(tokens[3][1])
1342 && isASCIIDigit(tokens[4][0]) && isASCIIDigit(tokens[4][1])
1343 && isASCIIDigit(tokens[4][2]) && isASCIIDigit(tokens[4][3])
1344 && isASCIIDigit(tokens[5][0]) && isASCIIDigit(tokens[5][1])
1345 && isASCIIDigit(tokens[5][3]) && isASCIIDigit(tokens[5][4])
1346 /* could also check that (&(tokens[5][5]) - tokens[2]) == 17 */
1351 if (lstyle && state.listStyle != lstyle) /* first time */
1354 if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
1355 p[2] != 'I' || p[3] != 'R' || p[4] != '>')
1357 for (pos = 0; lstyle && pos < toklen[1]; pos++)
1359 if (!isASCIIDigit(*p++))
1363 } /* if (first time) */
1364 } /* if (numtoks == ...) */
1368 state.parsedOne = true;
1369 state.listStyle = lstyle;
1371 result.caseSensitive = true;
1372 result.filename = tokens[0];
1373 result.filenameLength = toklen[0];
1374 result.type = FTPDirectoryEntry;
1377 if (isASCIIDigit(*p))
1379 result.type = FTPFileEntry;
1381 result.fileSize = String(p, pos);
1385 if (toklen[2] == 3) /* Chameleon */
1387 tbuf[0] = toASCIIUpper(p[0]);
1388 tbuf[1] = toASCIILower(p[1]);
1389 tbuf[2] = toASCIILower(p[2]);
1390 for (pos = 0; pos < (12*3); pos+=3)
1392 if (tbuf[0] == month_names[pos+0] &&
1393 tbuf[1] == month_names[pos+1] &&
1394 tbuf[2] == month_names[pos+2])
1396 result.modifiedTime.tm_mon = pos/3;
1397 result.modifiedTime.tm_mday = atoi(tokens[3]);
1398 result.modifiedTime.tm_year = atoi(tokens[4]) - 1900;
1402 pos = 5; /* Chameleon toknum of date field */
1406 result.modifiedTime.tm_mon = atoi(p+0)-1;
1407 result.modifiedTime.tm_mday = atoi(p+3);
1408 result.modifiedTime.tm_year = atoi(p+6);
1409 if (result.modifiedTime.tm_year < 80) /* SuperTCP */
1410 result.modifiedTime.tm_year += 100;
1412 pos = 3; /* SuperTCP toknum of date field */
1415 result.modifiedTime.tm_hour = atoi(tokens[pos]);
1416 result.modifiedTime.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
1418 /* the caller should do this (if dropping "." and ".." is desired)
1419 if (result.type == FTPDirectoryEntry && result.filename[0] == '.' &&
1420 (result.filenameLength == 1 || (result.filenameLength == 2 &&
1421 result.filename[1] == '.')))
1422 return FTPJunkEntry;
1426 } /* (lstyle == 'w') */
1428 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'w')) */
1431 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1433 #if defined(SUPPORT_DLS) /* dls -dtR */
1435 (state.listStyle == 'D' || (!state.listStyle && state.numLines == 1)))
1436 /* /bin/dls lines have to be immediately recognizable (first line) */
1438 /* I haven't seen an FTP server that delivers a /bin/dls listing,
1439 * but can infer the format from the lynx and mirror.pl projects.
1440 * Both formats are supported.
1443 * README 763 Information about this server\0
1448 * pub/ = Public area\0
1450 * morgan 14 -> ../real/morgan\0
1451 * TIMIT.mostlikely.Z\0
1455 * filename: ^(\S*)\s+
1456 * size: (\-|\=|\d+)\s+
1457 * month/day: ((\w\w\w\s+\d+|\d+\s+\w\w\w)\s+
1458 * time/year: (\d+:\d+|\d\d\d\d))\s+
1461 * README 763 Jul 11 21:05 Information about this server
1462 * bin/ - Apr 28 1994
1463 * etc/ = 11 Jul 21:04
1464 * ls-lR 0 6 Aug 17:14
1465 * ls-lR.Z 3 05 Sep 1994
1466 * pub/ = Jul 11 21:04 Public area
1467 * usr/ - Sep 7 09:39
1468 * morgan 14 Apr 18 09:39 -> ../real/morgan
1469 * TIMIT.mostlikely.Z
1470 * 79215 Jul 11 21:04
1472 if (!state.listStyle && line[linelen-1] == ':' &&
1473 linelen >= 2 && toklen[numtoks-1] != 1)
1475 /* code in mirror.pl suggests that a listing may be preceded
1476 * by a PWD line in the form "/some/dir/names/here:"
1477 * but does not necessarily begin with '/'. *sigh*
1481 while (pos < (linelen-1))
1483 /* illegal (or extremely unusual) chars in a dirspec */
1484 if (*p == '<' || *p == '|' || *p == '>' ||
1485 *p == '?' || *p == '*' || *p == '\\')
1487 if (*p == '/' && pos < (linelen-2) && p[1] == '/')
1492 if (pos == (linelen-1))
1494 state.listStyle = 'D';
1495 return FTPJunkEntry;
1499 if (!lstyle && numtoks >= 2)
1501 pos = 22; /* pos of (\d+|-|=) if this is not part of a multiline */
1502 if (state.listStyle && carry_buf_len) /* first is from previous line */
1503 pos = toklen[1]-1; /* and is 'as-is' (may contain whitespace) */
1508 if ((*p == '-' || *p == '=' || isASCIIDigit(*p)) &&
1509 ((linelen == (pos+1)) ||
1510 (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
1516 while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
1519 if ((tokens[pos]+toklen[pos]) == (&line[23]))
1525 if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
1527 if (toklen[tokmarker] != 1 ||
1528 (tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
1533 for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++)
1535 if (!isASCIIDigit(tokens[tokmarker][pos]))
1539 if (lstyle && !state.listStyle) /* first time */
1541 /* scan for illegal (or incredibly unusual) chars in fname */
1542 for (p = tokens[0]; lstyle &&
1543 p < &(tokens[tokmarker-1][toklen[tokmarker-1]]); p++)
1545 if (*p == '<' || *p == '|' || *p == '>' ||
1546 *p == '?' || *p == '*' || *p == '/' || *p == '\\')
1551 } /* size token found */
1552 } /* expected chars behind expected size token */
1553 } /* if (linelen > pos) */
1554 } /* if (!lstyle && numtoks >= 2) */
1556 if (!lstyle && state.listStyle == 'D' && !carry_buf_len)
1558 /* the filename of a multi-line entry can be identified
1559 * correctly only if dls format had been previously established.
1560 * This should always be true because there should be entries
1561 * for '.' and/or '..' and/or CWD that precede the rest of the
1565 if (pos > (sizeof(state.carryBuffer)-1))
1566 pos = sizeof(state.carryBuffer)-1;
1567 memcpy( state.carryBuffer, line, pos );
1568 state.carryBufferLength = pos;
1569 return FTPJunkEntry;
1574 state.parsedOne = true;
1575 state.listStyle = lstyle;
1577 p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
1578 result.filename = tokens[0];
1579 result.filenameLength = p - tokens[0];
1580 result.type = FTPFileEntry;
1582 if (result.filename[result.filenameLength-1] == '/')
1584 if (result.linknameLength == 1)
1585 result.type = FTPJunkEntry;
1588 result.filenameLength--;
1589 result.type = FTPDirectoryEntry;
1592 else if (isASCIIDigit(*tokens[tokmarker]))
1594 pos = toklen[tokmarker];
1595 result.fileSize = String(tokens[tokmarker], pos);
1598 if ((tokmarker+3) < numtoks &&
1599 (&(tokens[numtoks-1][toklen[numtoks-1]]) -
1600 tokens[tokmarker+1]) >= (1+1+3+1+4) )
1602 pos = (tokmarker+3);
1606 if ((pos == 4 || pos == 5)
1607 && isASCIIDigit(*p) && isASCIIDigit(p[pos-1]) && isASCIIDigit(p[pos-2])
1608 && ((pos == 5 && p[2] == ':') ||
1609 (pos == 4 && (isASCIIDigit(p[1]) || p[1] == ':')))
1612 month_num = tokmarker+1; /* assumed position of month field */
1613 pos = tokmarker+2; /* assumed position of mday field */
1614 if (isASCIIDigit(*tokens[month_num])) /* positions are reversed */
1619 p = tokens[month_num];
1620 if (isASCIIDigit(*tokens[pos])
1621 && (toklen[pos] == 1 ||
1622 (toklen[pos] == 2 && isASCIIDigit(tokens[pos][1])))
1623 && toklen[month_num] == 3
1624 && isASCIIAlpha(*p) && isASCIIAlpha(p[1]) && isASCIIAlpha(p[2]) )
1626 pos = atoi(tokens[pos]);
1627 if (pos > 0 && pos <= 31)
1629 result.modifiedTime.tm_mday = pos;
1631 for (pos = 0; pos < (12*3); pos+=3)
1633 if (p[0] == month_names[pos+0] &&
1634 p[1] == month_names[pos+1] &&
1635 p[2] == month_names[pos+2])
1640 result.modifiedTime.tm_mday = 0;
1642 result.modifiedTime.tm_mon = month_num - 1;
1645 if (result.modifiedTime.tm_mday)
1647 tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */
1648 p = tokens[tokmarker];
1652 result.modifiedTime.tm_year = pos-1900;
1657 result.modifiedTime.tm_hour = pos;
1658 result.modifiedTime.tm_min = atoi(p+3);
1661 time_t now = time(NULL);
1662 state.now = now * 1000000.0;
1664 // FIXME: This code has the year 2038 bug
1665 gmtime_r(&now, &state.nowFTPTime);
1666 state.nowFTPTime.tm_year += 1900;
1668 result.modifiedTime.tm_year = state.nowFTPTime.tm_year;
1669 if ( (( state.nowFTPTime.tm_mon << 4) + state.nowFTPTime.tm_mday) <
1670 ((result.modifiedTime.tm_mon << 4) + result.modifiedTime.tm_mday) )
1671 result.modifiedTime.tm_year--;
1672 } /* got year or time */
1673 } /* got month/mday */
1674 } /* may have year or time */
1675 } /* enough remaining to possibly have date/time */
1677 if (numtoks > (tokmarker+2))
1681 if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
1683 p = &(tokens[numtoks-1][toklen[numtoks-1]]);
1684 result.type = FTPLinkEntry;
1685 result.linkname = tokens[pos+1];
1686 result.linknameLength = p - result.linkname;
1687 if (result.linknameLength > 1 &&
1688 result.linkname[result.linknameLength-1] == '/')
1689 result.linknameLength--;
1691 } /* if (numtoks > (tokmarker+2)) */
1693 /* the caller should do this (if dropping "." and ".." is desired)
1694 if (result.type == FTPDirectoryEntry && result.filename[0] == '.' &&
1695 (result.filenameLength == 1 || (result.filenameLength == 2 &&
1696 result.filename[1] == '.')))
1697 return FTPJunkEntry;
1702 } /* if (lstyle == 'D') */
1703 } /* if (!lstyle && (!state.listStyle || state.listStyle == 'D')) */
1706 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1708 } /* if (linelen > 0) */
1710 return ParsingFailed(state);
1713 } // namespace WebCore
1715 #endif // ENABLE(FTPDIR)