1/*
2 * Copyright (C) 2002 Cyrus Patel <cyp@fb14.uni-mainz.de>
3 *           (C) 2007 Apple Inc. All rights reserved.
4 *
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.
8 *
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.
13 *
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.
18 */
19
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
22
23#include "config.h"
24#if ENABLE(FTPDIR)
25#include "FTPDirectoryParser.h"
26
27#if PLATFORM(QT)
28#include <QDateTime>
29// On Windows, use the threadsafe *_r functions provided by pthread.
30#elif OS(WINDOWS) && (USE(PTHREADS) || HAVE(PTHREAD_H))
31#include <pthread.h>
32#endif
33
34#include <wtf/ASCIICType.h>
35#include <stdio.h>
36
37using namespace WTF;
38
39namespace WebCore {
40#if PLATFORM(QT) && defined(Q_WS_WIN32)
41
42// Replacement for gmtime_r() which is not available on MinGW.
43// We use this on Win32 Qt platform for portability.
44struct tm gmtimeQt(const QDateTime& input)
45{
46    tm result;
47
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();
54
55    QTime time(input.time());
56    result.tm_sec = time.second();
57    result.tm_min = time.minute();
58    result.tm_hour = time.hour();
59
60    return result;
61}
62
63static struct tm *gmtimeQt(const time_t *const timep, struct tm *result)
64{
65    const QDateTime dt(QDateTime::fromTime_t(*timep));
66    *result = WebCore::gmtimeQt(dt);
67    return result;
68}
69
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))
74#else /* !_MSC_VER */
75#define gmtime_r(x,y) (gmtime(x)?(*(y)=*gmtime(x),(y)):0)
76#endif
77#endif
78
79static inline FTPEntryType ParsingFailed(ListState& state)
80{
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 */
84}
85
86FTPEntryType parseOneFTPLine(const char* line, ListState& state, ListResult& result)
87{
88  result.clear();
89
90  if (!line)
91    return FTPJunkEntry;
92
93  state.numLines++;
94
95  /* carry buffer is only valid from one line to the next */
96  unsigned int carry_buf_len = state.carryBufferLength;
97  state.carryBufferLength = 0;
98
99  unsigned linelen = 0;
100
101  /* strip leading whitespace */
102  while (*line == ' ' || *line == '\t')
103    line++;
104
105  /* line is terminated at first '\0' or '\n' */
106  const char* p = line;
107  while (*p && *p != '\n')
108    p++;
109  linelen = p - line;
110
111  if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
112    linelen--;
113
114  /* DON'T strip trailing whitespace. */
115
116  if (linelen > 0)
117  {
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;
125    char tbuf[4];
126    int lstyle = 0;
127
128    if (carry_buf_len) /* VMS long filename carryover buffer */
129    {
130      tokens[0] = state.carryBuffer;
131      toklen[0] = carry_buf_len;
132      numtoks++;
133    }
134
135    unsigned int pos = 0;
136    while (pos < linelen && numtoks < WTF_ARRAY_LENGTH(tokens))
137    {
138      while (pos < linelen &&
139            (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
140        pos++;
141      if (pos < linelen)
142      {
143        tokens[numtoks] = &line[pos];
144        while (pos < linelen &&
145           (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
146          pos++;
147        if (tokens[numtoks] != &line[pos])
148        {
149          toklen[numtoks] = (&line[pos] - tokens[numtoks]);
150          numtoks++;
151        }
152      }
153    }
154
155    if (!numtoks)
156      return ParsingFailed(state);
157
158    linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
159    if (numtoks == WTF_ARRAY_LENGTH(tokens))
160    {
161      pos = linelen;
162      while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
163        pos--;
164      linelen_sans_wsp = pos;
165    }
166
167    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
168#if defined(SUPPORT_EPLF)
169    /* EPLF handling must come somewhere before /bin/dls handling. */
170    if (!lstyle && (!state.listStyle || state.listStyle == 'E'))
171    {
172      if (*line == '+' && linelen > 4 && numtoks >= 2)
173      {
174        pos = 1;
175        while (pos < (linelen-1))
176        {
177          p = &line[pos++];
178          if (*p == '/')
179            result.type = FTPDirectoryEntry; /* its a dir */
180          else if (*p == 'r')
181            result.type = FTPFileEntry; /* its a file */
182          else if (*p == 'm')
183          {
184            if (isASCIIDigit(line[pos]))
185            {
186              while (pos < linelen && isASCIIDigit(line[pos]))
187                pos++;
188              if (pos < linelen && line[pos] == ',')
189              {
190                unsigned long long seconds = 0;
191#if OS(WINDOWS)
192                sscanf(p + 1, "%I64u", &seconds);
193#else
194                sscanf(p + 1, "%llu", &seconds);
195#endif
196                time_t t = static_cast<time_t>(seconds);
197
198                // FIXME: This code has the year 2038 bug
199                gmtime_r(&t, &result.modifiedTime);
200                result.modifiedTime.tm_year += 1900;
201              }
202            }
203          }
204          else if (*p == 's')
205          {
206            if (isASCIIDigit(line[pos]))
207            {
208              while (pos < linelen && isASCIIDigit(line[pos]))
209                pos++;
210              if (pos < linelen && line[pos] == ',')
211                result.fileSize = String(p + 1, &line[pos] - p + 1);
212            }
213          }
214          else if (isASCIIAlpha(*p)) /* 'i'/'up' or unknown "fact" (property) */
215          {
216            while (pos < linelen && *++p != ',')
217              pos++;
218          }
219          else if (*p != '\t' || (p+1) != tokens[1])
220          {
221            break; /* its not EPLF after all */
222          }
223          else
224          {
225            state.parsedOne = true;
226            state.listStyle = lstyle = 'E';
227
228            p = &(line[linelen_sans_wsp]);
229            result.filename = tokens[1];
230            result.filenameLength = p - tokens[1];
231
232            if (!result.type) /* access denied */
233            {
234              result.type = FTPFileEntry; /* is assuming 'f'ile correct? */
235              return FTPJunkEntry;            /* NO! junk it. */
236            }
237            return result.type;
238          }
239          if (pos >= (linelen-1) || line[pos] != ',')
240            break;
241          pos++;
242        } /* while (pos < linelen) */
243        result.clear();
244      } /* if (*line == '+' && linelen > 4 && numtoks >= 2) */
245    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'E')) */
246#endif /* SUPPORT_EPLF */
247
248    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
249
250#if defined(SUPPORT_VMS)
251    if (!lstyle && (!state.listStyle || state.listStyle == 'V'))
252    {                          /* try VMS Multinet/UCX/CMS server */
253      /*
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.
260       *
261       * We need to be picky about this in order to support
262       * multi-line listings correctly.
263      */
264      if (!state.parsedOne &&
265          (numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
266                            memcmp(tokens[0], "Directory", 9)==0 )))
267      {
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:"
271        */
272        p = tokens[0];
273        pos = toklen[0];
274        if (numtoks == 2)
275        {
276          p = tokens[1];
277          pos = toklen[1];
278        }
279        pos--;
280        if (pos >= 3)
281        {
282          while (pos > 0 && p[pos] != '[')
283          {
284            pos--;
285            if (p[pos] == '-' || p[pos] == '$')
286            {
287              if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
288                  (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
289                break;
290            }
291            else if (p[pos] != '.' && p[pos] != '~' &&
292                     !isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
293              break;
294            else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
295              break;
296          }
297          if (pos > 0)
298          {
299            pos--;
300            if (p[pos] != ':' || p[pos+1] != '[')
301              pos = 0;
302          }
303        }
304        if (pos > 0 && p[pos] == ':')
305        {
306          while (pos > 0)
307          {
308            pos--;
309            if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
310                p[pos] != '~' && !isASCIIDigit(p[pos]) && !isASCIIAlpha(p[pos]))
311              break;
312            else if (isASCIIAlpha(p[pos]) && p[pos] != toASCIIUpper(p[pos]))
313              break;
314          }
315          if (pos == 0)
316          {
317            state.listStyle = 'V';
318            return FTPJunkEntry; /* its junk */
319          }
320        }
321        /* fallthrough */
322      }
323      else if ((tokens[0][toklen[0]-1]) != ';')
324      {
325        if (numtoks == 1 && (state.listStyle == 'V' && !carry_buf_len))
326          lstyle = 'V';
327        else if (numtoks < 4)
328          ;
329        else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
330          lstyle = 'V';
331        else if ((&line[linelen] - tokens[1]) >= 22 &&
332                  memcmp(tokens[1], "insufficient privilege", 22) == 0)
333          lstyle = 'V';
334        else if (numtoks != 4 && numtoks != 6)
335          ;
336        else if (numtoks == 6 && (
337                 toklen[5] < 4 || *tokens[5] != '(' ||        /* perms */
338                           (tokens[5][toklen[5]-1]) != ')'  ))
339          ;
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 */
351                )
352        {
353          lstyle = 'V';
354        }
355        if (lstyle == 'V')
356        {
357          /*
358          * MultiNet FTP:
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,,)
364          * UCX FTP:
365          *   CII-MANUAL.TEX;1  213/216  29-JAN-1996 03:33:12  [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
366          * CMU/VMS-IP FTP
367          *   [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
368          * TCPware FTP
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,,)
373          */
374          tokmarker = 0;
375          p = tokens[0];
376          pos = 0;
377          if (*p == '[' && toklen[0] >= 4) /* CMU style */
378          {
379            if (p[1] != ']')
380            {
381              p++;
382              pos++;
383            }
384            while (lstyle && pos < toklen[0] && *p != ']')
385            {
386              if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
387                  *p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
388                lstyle = 0;
389              pos++;
390              p++;
391            }
392            if (lstyle && pos < (toklen[0]-1))
393            {
394              /* ']' was found and there is at least one character after it */
395              ASSERT(*p == ']');
396              pos++;
397              p++;
398              tokmarker = pos; /* length of leading "[DIR1.DIR2.etc]" */
399            } else {
400              /* not a CMU style listing */
401              lstyle = 0;
402            }
403          }
404          while (lstyle && pos < toklen[0] && *p != ';')
405          {
406            if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
407                *p != '~' && !isASCIIDigit(*p) && !isASCIIAlpha(*p))
408              lstyle = 0;
409            else if (isASCIIAlpha(*p) && *p != toASCIIUpper(*p))
410              lstyle = 0;
411            p++;
412            pos++;
413          }
414          if (lstyle && *p == ';')
415          {
416            if (pos == 0 || pos == (toklen[0]-1))
417              lstyle = 0;
418            for (pos++;lstyle && pos < toklen[0];pos++)
419            {
420              if (!isASCIIDigit(tokens[0][pos]))
421                lstyle = 0;
422            }
423          }
424          pos = (p - tokens[0]); /* => fnlength sans ";####" */
425          pos -= tokmarker;      /* => fnlength sans "[DIR1.DIR2.etc]" */
426          p = &(tokens[0][tokmarker]); /* offset of basename */
427
428          if (!lstyle || pos == 0 || pos > 80) /* VMS filenames can't be longer than that */
429          {
430            lstyle = 0;
431          }
432          else if (numtoks == 1)
433          {
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.
436            */
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 */
442          }
443          else if (isASCIIDigit(*tokens[1])) /* not no-privs message */
444          {
445            for (pos = 0; lstyle && pos < (toklen[1]); pos++)
446            {
447              if (!isASCIIDigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
448                lstyle = 0;
449            }
450            if (lstyle && numtoks > 4) /* Multinet or UCX but not CMU */
451            {
452              for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
453              {
454                p = &(tokens[5][pos]);
455                if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
456                  lstyle = 0;
457              }
458            }
459          }
460        } /* passed initial tests */
461      } /* else if ((tokens[0][toklen[0]-1]) != ';') */
462
463      if (lstyle == 'V')
464      {
465        state.parsedOne = true;
466        state.listStyle = lstyle;
467
468        if (isASCIIDigit(*tokens[1]))  /* not permission denied etc */
469        {
470          /* strip leading directory name */
471          if (*tokens[0] == '[') /* CMU server */
472          {
473            pos = toklen[0]-1;
474            p = tokens[0]+1;
475            while (*p != ']')
476            {
477              p++;
478              pos--;
479            }
480            toklen[0] = --pos;
481            tokens[0] = ++p;
482          }
483          pos = 0;
484          while (pos < toklen[0] && (tokens[0][pos]) != ';')
485            pos++;
486
487          result.caseSensitive = true;
488          result.type = FTPFileEntry;
489          result.filename = tokens[0];
490          result.filenameLength = pos;
491
492          if (pos > 4)
493          {
494            p = &(tokens[0][pos-4]);
495            if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
496            {
497              result.filenameLength -= 4;
498              result.type = FTPDirectoryEntry;
499            }
500          }
501
502          if (result.type != FTPDirectoryEntry)
503          {
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.
508            */
509            pos = 0;
510            while (pos < toklen[1] && (tokens[1][pos]) != '/')
511              pos++;
512
513/*
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
517 * to re-instate it.
518*/
519#if 0
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';
528            }
529            else /* size is in blocks */
530#endif
531            {
532              /* size requires multiplication by blocksize.
533               *
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).
539               *
540               * ulltoa(((unsigned long long)fsz)<<9, result.fe_size, 10);
541               *
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.
548              */
549              uint64_t size = strtoul(tokens[1], NULL, 10) * 512;
550              result.fileSize = String::number(size);
551            }
552
553          } /* if (result.type != FTPDirectoryEntry) */
554
555          p = tokens[2] + 2;
556          if (*p == '-')
557            p++;
558          tbuf[0] = p[0];
559          tbuf[1] = toASCIILower(p[1]);
560          tbuf[2] = toASCIILower(p[2]);
561          month_num = 0;
562          for (pos = 0; pos < (12*3); pos+=3)
563          {
564            if (tbuf[0] == month_names[pos+0] &&
565                tbuf[1] == month_names[pos+1] &&
566                tbuf[2] == month_names[pos+2])
567              break;
568            month_num++;
569          }
570          if (month_num >= 12)
571            month_num = 0;
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
575
576          p = tokens[3] + 2;
577          if (*p == ':')
578            p++;
579          if (p[2] == ':')
580            result.modifiedTime.tm_sec = atoi(p+3);
581          result.modifiedTime.tm_hour = atoi(tokens[3]);
582          result.modifiedTime.tm_min  = atoi(p);
583
584          return result.type;
585
586        } /* if (isASCIIDigit(*tokens[1])) */
587
588        return FTPJunkEntry; /* junk */
589
590      } /* if (lstyle == 'V') */
591    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'V')) */
592#endif
593
594    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
595
596#if defined(SUPPORT_CMS)
597    /* Virtual Machine/Conversational Monitor System (IBM Mainframe) */
598    if (!lstyle && (!state.listStyle || state.listStyle == 'C'))  /* VM/CMS */
599    {
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
609       *
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
621      */
622      if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
623      {
624        for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
625        {
626          p = tokens[pos];
627          if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
628              (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
629          {
630            if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
631                                      (tokens[pos+5][5]) == ':'   )
632            {
633              p = tokens[pos+4];
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") */
639              {
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])) )
646                {
647                  lstyle = 'C';
648                  tokmarker = pos;
649                }
650              }
651            }
652          }
653        } /* for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) */
654      } /* if (numtoks >= 7) */
655
656      /* extra checking if first pass */
657      if (lstyle && !state.listStyle)
658      {
659        for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
660        {
661          if (isASCIIAlpha(*p) && toASCIIUpper(*p) != *p)
662            lstyle = 0;
663        }
664        for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
665        {
666          if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
667          {
668            for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
669            {
670              if (!isASCIIDigit(*p))
671                lstyle = 0;
672            }
673          }
674        }
675        for (pos = 0, p = tokens[tokmarker+4];
676             lstyle && pos < toklen[tokmarker+4]; pos++, p++)
677        {
678          if (*p == '/')
679          {
680            /* There may be Y2K bugs in the date. Don't simplify to
681             * pos != (len-3) && pos != (len-6) like time is done.
682            */
683            if ((tokens[tokmarker+4][1]) == '/')
684            {
685              if (pos != 1 && pos != 4)
686                lstyle = 0;
687            }
688            else if (pos != 2 && pos != 5)
689              lstyle = 0;
690          }
691          else if (*p != '-' && !isASCIIDigit(*p))
692            lstyle = 0;
693          else if (*p == '-' && pos != 4 && pos != 7)
694            lstyle = 0;
695        }
696        for (pos = 0, p = tokens[tokmarker+5];
697             lstyle && pos < toklen[tokmarker+5]; pos++, p++)
698        {
699          if (*p != ':' && !isASCIIDigit(*p))
700            lstyle = 0;
701          else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
702                             && pos != (toklen[tokmarker+5]-6))
703            lstyle = 0;
704        }
705      } /* initial if() */
706
707      if (lstyle == 'C')
708      {
709        state.parsedOne = true;
710        state.listStyle = lstyle;
711
712        p = tokens[tokmarker+4];
713        if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */
714        {
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);
718        }
719        else /* oldstyle: [M]M/DD/YY format */
720        {
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;
727        }
728
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);
734
735        result.caseSensitive = true;
736        result.filename = tokens[0];
737        result.filenameLength = toklen[0];
738        result.type  = FTPFileEntry;
739
740        p = tokens[tokmarker];
741        if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
742          result.type  = FTPDirectoryEntry;
743
744        if ((/*newstyle*/ toklen[tokmarker+4] == 10 && tokmarker > 1) ||
745            (/*oldstyle*/ toklen[tokmarker+4] != 10 && tokmarker > 2))
746        {                            /* have a filetype column */
747          char *dot;
748          p = &(tokens[0][toklen[0]]);
749          memcpy( &dot, &p, sizeof(dot) ); /* NASTY! */
750          *dot++ = '.';
751          p = tokens[1];
752          for (pos = 0; pos < toklen[1]; pos++)
753            *dot++ = *p++;
754          result.filenameLength += 1 + toklen[1];
755        }
756
757        /* oldstyle LISTING:
758         * files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable
759        if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
760          return FTPJunkEntry;
761        */
762
763        /* VM/CMS LISTings have no usable filesize field.
764         * Have to use the 'SIZE' command for that.
765        */
766        return result.type;
767
768      } /* if (lstyle == 'C' && (!state.listStyle || state.listStyle == lstyle)) */
769    } /* VM/CMS */
770#endif
771
772    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
773
774#if defined(SUPPORT_DOS) /* WinNT DOS dirstyle */
775    if (!lstyle && (!state.listStyle || state.listStyle == 'W'))
776    {
777      /*
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"
784      */
785      if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 &&
786          (*tokens[2] == '<' || isASCIIDigit(*tokens[2])) )
787      {
788        p = tokens[0];
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]) )
792        {
793          p = tokens[1];
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')
797          {
798            lstyle = 'W';
799            if (!state.listStyle)
800            {
801              p = tokens[2];
802              /* <DIR> or <JUNCTION> */
803              if (*p != '<' || p[toklen[2]-1] != '>')
804              {
805                for (pos = 1; (lstyle && pos < toklen[2]); pos++)
806                {
807                  if (!isASCIIDigit(*++p))
808                    lstyle = 0;
809                }
810              }
811            }
812          }
813        }
814      }
815
816      if (lstyle == 'W')
817      {
818        state.parsedOne = true;
819        state.listStyle = lstyle;
820
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;
826
827        if (*tokens[2] != '<') /* not <DIR> or <JUNCTION> */
828        {
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;
834          }
835          result.type = FTPFileEntry;
836          pos = toklen[2];
837          result.fileSize = String(tokens[2], pos);
838        }
839        else {
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;
847          }
848
849          if ((tokens[2][1]) != 'D') /* not <DIR> */
850          {
851            result.type = FTPJunkEntry; /* unknown until junc for sure */
852            if (result.filenameLength > 4)
853            {
854              p = result.filename;
855              for (pos = result.filenameLength - 4; pos > 0; pos--)
856              {
857                if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
858                    (p[1] == '=' || p[1] == '-'))
859                {
860                  result.type = FTPLinkEntry;
861                  result.filenameLength = p - result.filename;
862                  result.linkname = p + 4;
863                  result.linknameLength = &(line[linelen])
864                                     - result.linkname;
865                  break;
866                }
867                p++;
868              }
869            }
870          }
871        }
872
873        result.modifiedTime.tm_mon = atoi(tokens[0]+0);
874        if (result.modifiedTime.tm_mon != 0)
875        {
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
880               00-79 is 2000-2079
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;
886        }
887
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;
892
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] == '.')))
897          return FTPJunkEntry;
898        */
899
900        return result.type;
901      } /* if (lstyle == 'W' && (!state.listStyle || state.listStyle == lstyle)) */
902    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'W')) */
903#endif
904
905    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
906
907#if defined(SUPPORT_OS2)
908    if (!lstyle && (!state.listStyle || state.listStyle == 'O')) /* OS/2 test */
909    {
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
914      * a " DIR" in there.
915      *
916      *          1         2         3         4         5         6
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
927      *                            1         2         3         4
928      *                  01234567890123456789012345678901234567890123456789
929      * dirlist from the mirror.pl project, col positions from Mozilla.
930      */
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])
943      )
944      {
945        lstyle = 'O'; /* OS/2 */
946        if (!state.listStyle)
947        {
948          for (pos = 1; lstyle && pos < toklen[0]; pos++)
949          {
950            if (!isASCIIDigit(tokens[0][pos]))
951              lstyle = 0;
952          }
953        }
954      }
955
956      if (lstyle == 'O')
957      {
958        state.parsedOne = true;
959        state.listStyle = lstyle;
960
961        p = &(line[toklen[0]]);
962
963        result.caseSensitive = true;
964        result.filename = &p[53-18];
965        result.filenameLength = (&(line[linelen_sans_wsp]))
966                           - (result.filename);
967        result.type = FTPFileEntry;
968
969        /* I don't have a real listing to determine exact pos, so scan. */
970        for (pos = (18-18); pos < ((35-18)-4); pos++)
971        {
972          if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
973              p[pos+2] == 'I' && p[pos+3] == 'R')
974          {
975            result.type = FTPDirectoryEntry;
976            break;
977          }
978        }
979
980        if (result.type != FTPDirectoryEntry)
981        {
982          pos = toklen[0];
983          result.fileSize = String(tokens[0], pos);
984        }
985
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]);
993
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] == '.')))
998          return FTPJunkEntry;
999        */
1000
1001        return result.type;
1002      } /* if (lstyle == 'O') */
1003
1004    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'O')) */
1005#endif
1006
1007    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1008
1009#if defined(SUPPORT_LSL)
1010    if (!lstyle && (!state.listStyle || state.listStyle == 'U')) /* /bin/ls & co. */
1011    {
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"
1035      */
1036
1037      bool isOldHellsoft = false;
1038
1039      if (numtoks >= 6)
1040      {
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.
1043         */
1044        if (toklen[0] == 1 || (tokens[0][1]) == '[')
1045        {
1046          if (*tokens[0] == 'd' || *tokens[0] == '-')
1047          {
1048            pos = toklen[0]-1;
1049            p = tokens[0] + 1;
1050            if (pos == 0)
1051            {
1052              p = tokens[1];
1053              pos = toklen[1];
1054            }
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] == '-'))
1061            {
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;
1066            }
1067          }
1068        }
1069        else if ((toklen[0] == 10 || toklen[0] == 11)
1070                   && strchr("-bcdlpsw?DFam", *tokens[0]))
1071        {
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 */
1080          {
1081            lstyle = 'U'; /* very likely /bin/ls */
1082          }
1083        }
1084      }
1085      if (lstyle == 'U') /* first token checks out */
1086      {
1087        lstyle = 0;
1088        for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
1089        {
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)
1092           *  \s+(.+)$
1093          */
1094          if (isASCIIDigit(*tokens[pos]) /* size */
1095              /* (\w\w\w) */
1096           && toklen[pos+1] == 3 && isASCIIAlpha(*tokens[pos+1]) &&
1097              isASCIIAlpha(tokens[pos+1][1]) && isASCIIAlpha(tokens[pos+1][2])
1098              /* (\d|\d\d) */
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])
1109           && (
1110              /* (\d\d\d\d) */
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]))
1122              )
1123           )
1124          {
1125            lstyle = 'U'; /* assume /bin/ls or variant format */
1126            tokmarker = pos;
1127
1128            /* check that size is numeric */
1129            p = tokens[tokmarker];
1130            for (unsigned int i = 0; lstyle && i < toklen[tokmarker]; ++i)
1131            {
1132              if (!isASCIIDigit(*p++))
1133                lstyle = 0;
1134            }
1135            if (lstyle)
1136            {
1137              month_num = 0;
1138              p = tokens[tokmarker+1];
1139              for (unsigned int i = 0; i < (12*3); i+=3)
1140              {
1141                if (p[0] == month_names[i+0] &&
1142                    p[1] == month_names[i+1] &&
1143                    p[2] == month_names[i+2])
1144                  break;
1145                month_num++;
1146              }
1147              if (month_num >= 12)
1148                lstyle = 0;
1149            }
1150          } /* relative position test */
1151        } /* for (pos = (numtoks-5); !lstyle && pos > 1; pos--) */
1152      } /* if (lstyle == 'U') */
1153
1154      if (lstyle == 'U')
1155      {
1156        state.parsedOne = true;
1157        state.listStyle = lstyle;
1158
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) */
1167
1168        if (result.type != FTPDirectoryEntry)
1169        {
1170          pos = toklen[tokmarker];
1171          result.fileSize = String(tokens[tokmarker], pos);
1172        }
1173
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++;
1178
1179        p = tokens[tokmarker+3];
1180        pos = (unsigned int)atoi(p);
1181        if (p[1] == ':') /* one digit hour */
1182          p--;
1183        if (p[2] != ':') /* year */
1184        {
1185          result.modifiedTime.tm_year = pos;
1186        }
1187        else
1188        {
1189          result.modifiedTime.tm_hour = pos;
1190          result.modifiedTime.tm_min  = atoi(p+3);
1191          if (p[5] == ':')
1192            result.modifiedTime.tm_sec = atoi(p+6);
1193
1194          if (!state.now)
1195          {
1196            time_t now = time(NULL);
1197            state.now = now * 1000000.0;
1198
1199            // FIXME: This code has the year 2038 bug
1200            gmtime_r(&now, &state.nowFTPTime);
1201            state.nowFTPTime.tm_year += 1900;
1202          }
1203
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--;
1208
1209        } /* time/year */
1210
1211        // there is exactly 1 space between filename and previous token in all
1212        // outputs except old Hellsoft
1213        if (!isOldHellsoft)
1214          result.filename = tokens[tokmarker+3] + toklen[tokmarker+3] + 1;
1215        else
1216          result.filename = tokens[tokmarker+4];
1217
1218        result.filenameLength = (&(line[linelen]))
1219                           - (result.filename);
1220
1221        if (result.type == FTPLinkEntry && result.filenameLength > 4)
1222        {
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();
1226
1227          if (result.filenameLength > (fileSize + 4) &&
1228              strncmp(result.filename + result.filenameLength - fileSize - 4, " -> ", 4) == 0)
1229          {
1230            result.linkname = result.filename + (result.filenameLength - fileSize);
1231            result.linknameLength = (&(line[linelen])) - (result.linkname);
1232            result.filenameLength -= fileSize + 4;
1233          }
1234          else
1235          {
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--)
1243            {
1244              if (strncmp(p, " -> ", 4) == 0)
1245              {
1246                result.linkname = p + 4;
1247                result.linknameLength = (&(line[linelen]))
1248                                 - (result.linkname);
1249                result.filenameLength = pos;
1250                break;
1251              }
1252              p--;
1253            }
1254          }
1255        }
1256
1257#if defined(SUPPORT_LSLF) /* some (very rare) servers return ls -lF */
1258        if (result.filenameLength > 1)
1259        {
1260          p = result.filename[result.filenameLength-1];
1261          pos = result.type;
1262          if (pos == 'd') {
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 */
1270          }
1271        }
1272#endif
1273
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;
1279        */
1280
1281        return result.type;
1282
1283      } /* if (lstyle == 'U') */
1284
1285    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'U')) */
1286#endif
1287
1288    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1289
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 */
1294      /*
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
1305      *
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
1315      */
1316      if (numtoks >= 4 && toklen[0] < 13 &&
1317          ((toklen[1] == 5 && *tokens[1] == '<') || isASCIIDigit(*tokens[1])) )
1318      {
1319        if (numtoks == 4
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])
1331         )
1332        {
1333          lstyle = 'w';
1334        }
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 */
1347        )
1348        {
1349          lstyle = 'w';
1350        }
1351        if (lstyle && state.listStyle != lstyle) /* first time */
1352        {
1353          p = tokens[1];
1354          if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
1355                 p[2] != 'I' || p[3] != 'R' || p[4] != '>')
1356          {
1357            for (pos = 0; lstyle && pos < toklen[1]; pos++)
1358            {
1359              if (!isASCIIDigit(*p++))
1360                lstyle = 0;
1361            }
1362          } /* not <DIR> */
1363        } /* if (first time) */
1364      } /* if (numtoks == ...) */
1365
1366      if (lstyle == 'w')
1367      {
1368        state.parsedOne = true;
1369        state.listStyle = lstyle;
1370
1371        result.caseSensitive = true;
1372        result.filename = tokens[0];
1373        result.filenameLength = toklen[0];
1374        result.type = FTPDirectoryEntry;
1375
1376        p = tokens[1];
1377        if (isASCIIDigit(*p))
1378        {
1379          result.type = FTPFileEntry;
1380          pos = toklen[1];
1381          result.fileSize = String(p, pos);
1382        }
1383
1384        p = tokens[2];
1385        if (toklen[2] == 3) /* Chameleon */
1386        {
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)
1391          {
1392            if (tbuf[0] == month_names[pos+0] &&
1393                tbuf[1] == month_names[pos+1] &&
1394                tbuf[2] == month_names[pos+2])
1395            {
1396              result.modifiedTime.tm_mon = pos/3;
1397              result.modifiedTime.tm_mday = atoi(tokens[3]);
1398              result.modifiedTime.tm_year = atoi(tokens[4]) - 1900;
1399              break;
1400            }
1401          }
1402          pos = 5; /* Chameleon toknum of date field */
1403        }
1404        else
1405        {
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;
1411
1412          pos = 3; /* SuperTCP toknum of date field */
1413        }
1414
1415        result.modifiedTime.tm_hour = atoi(tokens[pos]);
1416        result.modifiedTime.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
1417
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;
1423        */
1424
1425        return result.type;
1426      } /* (lstyle == 'w') */
1427
1428    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'w'))  */
1429#endif
1430
1431    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1432
1433#if defined(SUPPORT_DLS) /* dls -dtR */
1434    if (!lstyle &&
1435       (state.listStyle == 'D' || (!state.listStyle && state.numLines == 1)))
1436       /* /bin/dls lines have to be immediately recognizable (first line) */
1437    {
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.
1441       *
1442       * Lynx says:
1443       * README              763  Information about this server\0
1444       * bin/                  -  \0
1445       * etc/                  =  \0
1446       * ls-lR                 0  \0
1447       * ls-lR.Z               3  \0
1448       * pub/                  =  Public area\0
1449       * usr/                  -  \0
1450       * morgan               14  -> ../real/morgan\0
1451       * TIMIT.mostlikely.Z\0
1452       *                   79215  \0
1453       *
1454       * mirror.pl says:
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+
1459       * rest:      (.+)
1460       *
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
1471      */
1472      if (!state.listStyle && line[linelen-1] == ':' &&
1473          linelen >= 2 && toklen[numtoks-1] != 1)
1474      {
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*
1478        */
1479        pos = 0;
1480        p = line;
1481        while (pos < (linelen-1))
1482        {
1483          /* illegal (or extremely unusual) chars in a dirspec */
1484          if (*p == '<' || *p == '|' || *p == '>' ||
1485              *p == '?' || *p == '*' || *p == '\\')
1486            break;
1487          if (*p == '/' && pos < (linelen-2) && p[1] == '/')
1488            break;
1489          pos++;
1490          p++;
1491        }
1492        if (pos == (linelen-1))
1493        {
1494          state.listStyle = 'D';
1495          return FTPJunkEntry;
1496        }
1497      }
1498
1499      if (!lstyle && numtoks >= 2)
1500      {
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) */
1504
1505        if (linelen > pos)
1506        {
1507          p = &line[pos];
1508          if ((*p == '-' || *p == '=' || isASCIIDigit(*p)) &&
1509              ((linelen == (pos+1)) ||
1510               (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
1511          {
1512            tokmarker = 1;
1513            if (!carry_buf_len)
1514            {
1515              pos = 1;
1516              while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
1517                pos++;
1518              tokmarker = 0;
1519              if ((tokens[pos]+toklen[pos]) == (&line[23]))
1520                tokmarker = pos;
1521            }
1522            if (tokmarker)
1523            {
1524              lstyle = 'D';
1525              if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
1526              {
1527                if (toklen[tokmarker] != 1 ||
1528                   (tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
1529                  lstyle = 0;
1530              }
1531              else
1532              {
1533                for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++)
1534                {
1535                  if (!isASCIIDigit(tokens[tokmarker][pos]))
1536                    lstyle = 0;
1537                }
1538              }
1539              if (lstyle && !state.listStyle) /* first time */
1540              {
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++)
1544                {
1545                  if (*p == '<' || *p == '|' || *p == '>' ||
1546                      *p == '?' || *p == '*' || *p == '/' || *p == '\\')
1547                    lstyle = 0;
1548                }
1549              }
1550
1551            } /* size token found */
1552          } /* expected chars behind expected size token */
1553        } /* if (linelen > pos) */
1554      } /* if (!lstyle && numtoks >= 2) */
1555
1556      if (!lstyle && state.listStyle == 'D' && !carry_buf_len)
1557      {
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
1562         * listing.
1563        */
1564        pos = linelen;
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;
1570      }
1571
1572      if (lstyle == 'D')
1573      {
1574        state.parsedOne = true;
1575        state.listStyle = lstyle;
1576
1577        p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
1578        result.filename = tokens[0];
1579        result.filenameLength = p - tokens[0];
1580        result.type  = FTPFileEntry;
1581
1582        if (result.filename[result.filenameLength-1] == '/')
1583        {
1584          if (result.linknameLength == 1)
1585            result.type = FTPJunkEntry;
1586          else
1587          {
1588            result.filenameLength--;
1589            result.type  = FTPDirectoryEntry;
1590          }
1591        }
1592        else if (isASCIIDigit(*tokens[tokmarker]))
1593        {
1594          pos = toklen[tokmarker];
1595          result.fileSize = String(tokens[tokmarker], pos);
1596        }
1597
1598        if ((tokmarker+3) < numtoks &&
1599              (&(tokens[numtoks-1][toklen[numtoks-1]]) -
1600               tokens[tokmarker+1]) >= (1+1+3+1+4) )
1601        {
1602          pos = (tokmarker+3);
1603          p = tokens[pos];
1604          pos = toklen[pos];
1605
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] == ':')))
1610             )
1611          {
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 */
1615            {
1616              month_num++;
1617              pos--;
1618            }
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])  )
1625            {
1626              pos = atoi(tokens[pos]);
1627              if (pos > 0 && pos <= 31)
1628              {
1629                result.modifiedTime.tm_mday = pos;
1630                month_num = 1;
1631                for (pos = 0; pos < (12*3); pos+=3)
1632                {
1633                  if (p[0] == month_names[pos+0] &&
1634                      p[1] == month_names[pos+1] &&
1635                      p[2] == month_names[pos+2])
1636                    break;
1637                  month_num++;
1638                }
1639                if (month_num > 12)
1640                  result.modifiedTime.tm_mday = 0;
1641                else
1642                  result.modifiedTime.tm_mon = month_num - 1;
1643              }
1644            }
1645            if (result.modifiedTime.tm_mday)
1646            {
1647              tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */
1648              p = tokens[tokmarker];
1649
1650              pos = atoi(p);
1651              if (pos > 24)
1652                result.modifiedTime.tm_year = pos-1900;
1653              else
1654              {
1655                if (p[1] == ':')
1656                  p--;
1657                result.modifiedTime.tm_hour = pos;
1658                result.modifiedTime.tm_min = atoi(p+3);
1659                if (!state.now)
1660                {
1661                  time_t now = time(NULL);
1662                  state.now = now * 1000000.0;
1663
1664                  // FIXME: This code has the year 2038 bug
1665                  gmtime_r(&now, &state.nowFTPTime);
1666                  state.nowFTPTime.tm_year += 1900;
1667                }
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 */
1676
1677        if (numtoks > (tokmarker+2))
1678        {
1679          pos = tokmarker+1;
1680          p = tokens[pos];
1681          if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
1682          {
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--;
1690          }
1691        } /* if (numtoks > (tokmarker+2)) */
1692
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;
1698        */
1699
1700        return result.type;
1701
1702      } /* if (lstyle == 'D') */
1703    } /* if (!lstyle && (!state.listStyle || state.listStyle == 'D')) */
1704#endif
1705
1706    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1707
1708  } /* if (linelen > 0) */
1709
1710  return ParsingFailed(state);
1711}
1712
1713} // namespace WebCore
1714
1715#endif // ENABLE(FTPDIR)
1716