Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/streamconv/converters/ParseFTPList.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "ParseFTPList.h"
7
#include <algorithm>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <ctype.h>
11
#include "plstr.h"
12
#include "nsDebug.h"
13
#include "prprf.h"
14
#include "nsUnicharUtils.h"
15
#include "mozilla/CheckedInt.h"
16
#include "mozilla/IntegerPrintfMacros.h"
17
#include "mozilla/TextUtils.h"
18
#include "mozilla/Sprintf.h"
19
20
/* ==================================================================== */
21
22
using mozilla::CheckedInt;
23
using mozilla::IsAsciiDigit;
24
using mozilla::IsAsciiAlpha;
25
using mozilla::IsAsciiLowercaseAlpha;
26
using mozilla::IsAsciiAlphanumeric;
27
28
static const int kMaxFTPListLen = 32768;
29
30
static inline int ParsingFailed(struct list_state *state)
31
0
{
32
0
  if (state->parsed_one || state->lstyle) /* junk if we fail to parse */
33
0
    return '?';      /* this time but had previously parsed successfully */
34
0
  return '"';        /* its part of a comment or error message */
35
0
}
36
37
void
38
FixupYear(PRExplodedTime* aTime)
39
0
{
40
0
  /* if year has only two digits then assume that
41
0
     00-79 is 2000-2079
42
0
     80-99 is 1980-1999 */
43
0
  if (aTime->tm_year < 80) {
44
0
    aTime->tm_year += 2000;
45
0
  } else if (aTime->tm_year < 100) {
46
0
    aTime->tm_year += 1900;
47
0
  }
48
0
}
49
50
int ParseFTPList(const char *line, struct list_state *state,
51
                 struct list_result *result, PRTimeParamFn timeParam,
52
                 NowTimeFn nowTimeFn)
53
0
{
54
0
  unsigned int carry_buf_len; /* copy of state->carry_buf_len */
55
0
  unsigned int pos;
56
0
  const char *p;
57
0
58
0
  if (!line || !state || !result)
59
0
    return 0;
60
0
61
0
  memset( result, 0, sizeof(*result) );
62
0
  state->numlines++;
63
0
64
0
  /* carry buffer is only valid from one line to the next */
65
0
  carry_buf_len = state->carry_buf_len;
66
0
  state->carry_buf_len = 0;
67
0
68
0
  /* strip leading whitespace */
69
0
  while (*line == ' ' || *line == '\t')
70
0
    line++;
71
0
72
0
  /* line is terminated at first '\0' or '\n' */
73
0
  p = line;
74
0
  while (*p && *p != '\n')
75
0
    p++;
76
0
  unsigned int linelen = p - line;
77
0
78
0
  if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
79
0
    linelen--;
80
0
81
0
  /* DON'T strip trailing whitespace. */
82
0
83
0
  if (linelen > kMaxFTPListLen) {
84
0
    return ParsingFailed(state);
85
0
  }
86
0
87
0
  if (linelen > 0)
88
0
  {
89
0
    static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
90
0
    const char *tokens[16]; /* 16 is more than enough */
91
0
    unsigned int toklen[(sizeof(tokens)/sizeof(tokens[0]))];
92
0
    unsigned int linelen_sans_wsp;  // line length sans whitespace
93
0
    unsigned int numtoks = 0;
94
0
    unsigned int tokmarker = 0; /* extra info for lstyle handler */
95
0
    unsigned int month_num = 0;
96
0
    char tbuf[4];
97
0
    int lstyle = 0;
98
0
99
0
    if (carry_buf_len) /* VMS long filename carryover buffer */
100
0
    {
101
0
      tokens[0] = state->carry_buf;
102
0
      toklen[0] = carry_buf_len;
103
0
      numtoks++;
104
0
    }
105
0
106
0
    pos = 0;
107
0
    while (pos < linelen && numtoks < (sizeof(tokens)/sizeof(tokens[0])) )
108
0
    {
109
0
      while (pos < linelen &&
110
0
            (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
111
0
        pos++;
112
0
      if (pos < linelen)
113
0
      {
114
0
        tokens[numtoks] = &line[pos];
115
0
        while (pos < linelen &&
116
0
           (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
117
0
          pos++;
118
0
        if (tokens[numtoks] != &line[pos])
119
0
        {
120
0
          toklen[numtoks] = (&line[pos] - tokens[numtoks]);
121
0
          numtoks++;
122
0
        }
123
0
      }
124
0
    }
125
0
126
0
    if (!numtoks)
127
0
      return ParsingFailed(state);
128
0
129
0
    linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
130
0
    if (numtoks == (sizeof(tokens)/sizeof(tokens[0])) )
131
0
    {
132
0
      pos = linelen;
133
0
      while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
134
0
        pos--;
135
0
      linelen_sans_wsp = pos;
136
0
    }
137
0
138
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
139
0
140
0
#if defined(SUPPORT_EPLF)
141
0
    /* EPLF handling must come somewhere before /bin/dls handling. */
142
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'E'))
143
0
    {
144
0
      if (*line == '+' && linelen > 4 && numtoks >= 2)
145
0
      {
146
0
        pos = 1;
147
0
        while (pos < (linelen-1))
148
0
        {
149
0
          p = &line[pos++];
150
0
          if (*p == '/')
151
0
            result->fe_type = 'd'; /* its a dir */
152
0
          else if (*p == 'r')
153
0
            result->fe_type = 'f'; /* its a file */
154
0
          else if (*p == 'm')
155
0
          {
156
0
            if (IsAsciiDigit(line[pos]))
157
0
            {
158
0
              while (pos < linelen && IsAsciiDigit(line[pos]))
159
0
                pos++;
160
0
              if (pos < linelen && line[pos] == ',')
161
0
              {
162
0
                PRTime t;
163
0
                PRTime seconds;
164
0
                PR_sscanf(p+1, "%llu", &seconds);
165
0
                t = seconds * PR_USEC_PER_SEC;
166
0
                PR_ExplodeTime(t, timeParam, &(result->fe_time) );
167
0
              }
168
0
            }
169
0
          }
170
0
          else if (*p == 's')
171
0
          {
172
0
            if (IsAsciiDigit(line[pos]))
173
0
            {
174
0
              while (pos < linelen && IsAsciiDigit(line[pos]))
175
0
                pos++;
176
0
              if (pos < linelen && line[pos] == ',' &&
177
0
                 ((&line[pos]) - (p+1)) < int(sizeof(result->fe_size)-1) )
178
0
              {
179
0
                memcpy( result->fe_size, p+1, (unsigned)(&line[pos] - (p+1)) );
180
0
                result->fe_size[(&line[pos] - (p+1))] = '\0';
181
0
              }
182
0
            }
183
0
          }
184
0
          else if (IsAsciiAlpha(*p)) /* 'i'/'up' or unknown "fact" (property) */
185
0
          {
186
0
            while (pos < linelen && *++p != ',')
187
0
              pos++;
188
0
          }
189
0
          else if (*p != '\t' || (p+1) != tokens[1])
190
0
          {
191
0
            break; /* its not EPLF after all */
192
0
          }
193
0
          else
194
0
          {
195
0
            state->parsed_one = 1;
196
0
            state->lstyle = lstyle = 'E';
197
0
198
0
            p = &(line[linelen_sans_wsp]);
199
0
            result->fe_fname = tokens[1];
200
0
            result->fe_fnlen = p - tokens[1];
201
0
202
0
            if (!result->fe_type) /* access denied */
203
0
            {
204
0
              result->fe_type = 'f'; /* is assuming 'f'ile correct? */
205
0
              return '?';            /* NO! junk it. */
206
0
            }
207
0
            return result->fe_type;
208
0
          }
209
0
          if (pos >= (linelen-1) || line[pos] != ',')
210
0
            break;
211
0
          pos++;
212
0
        } /* while (pos < linelen) */
213
0
        memset( result, 0, sizeof(*result) );
214
0
      } /* if (*line == '+' && linelen > 4 && numtoks >= 2) */
215
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'E')) */
216
0
#endif /* SUPPORT_EPLF */
217
0
218
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
219
0
220
0
#if defined(SUPPORT_VMS)
221
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'V'))
222
0
    {                          /* try VMS Multinet/UCX/CMS server */
223
0
      /*
224
0
       * Legal characters in a VMS file/dir spec are [A-Z0-9$.-_~].
225
0
       * '$' cannot begin a filename and `-' cannot be used as the first
226
0
       * or last character. '.' is only valid as a directory separator
227
0
       * and <file>.<type> separator. A canonical filename spec might look
228
0
       * like this: DISK$VOL:[DIR1.DIR2.DIR3]FILE.TYPE;123
229
0
       * All VMS FTP servers LIST in uppercase.
230
0
       *
231
0
       * We need to be picky about this in order to support
232
0
       * multi-line listings correctly.
233
0
      */
234
0
      if (!state->parsed_one &&
235
0
          (numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
236
0
                            memcmp(tokens[0], "Directory", 9)==0 )))
237
0
      {
238
0
        /* If no dirstyle has been detected yet, and this line is a
239
0
         * VMS list's dirname, then turn on VMS dirstyle.
240
0
         * eg "ACA:[ANONYMOUS]", "DISK$FTP:[ANONYMOUS]", "SYS$ANONFTP:"
241
0
        */
242
0
        p = tokens[0];
243
0
        pos = toklen[0];
244
0
        if (numtoks == 2)
245
0
        {
246
0
          p = tokens[1];
247
0
          pos = toklen[1];
248
0
        }
249
0
        pos--;
250
0
        if (pos >= 3)
251
0
        {
252
0
          while (pos > 0 && p[pos] != '[')
253
0
          {
254
0
            pos--;
255
0
            if (p[pos] == '-' || p[pos] == '$')
256
0
            {
257
0
              if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
258
0
                  (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
259
0
                break;
260
0
            }
261
0
            else if (p[pos] != '.' && p[pos] != '~' &&
262
0
                     !IsAsciiAlphanumeric(p[pos]))
263
0
              break;
264
0
            else if (IsAsciiLowercaseAlpha(p[pos]))
265
0
              break;
266
0
          }
267
0
          if (pos > 0)
268
0
          {
269
0
            pos--;
270
0
            if (p[pos] != ':' || p[pos+1] != '[')
271
0
              pos = 0;
272
0
          }
273
0
        }
274
0
        if (pos > 0 && p[pos] == ':')
275
0
        {
276
0
          while (pos > 0)
277
0
          {
278
0
            pos--;
279
0
            if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
280
0
                p[pos] != '~' && !IsAsciiAlphanumeric(p[pos]))
281
0
              break;
282
0
            else if (IsAsciiLowercaseAlpha(p[pos]))
283
0
              break;
284
0
          }
285
0
          if (pos == 0)
286
0
          {
287
0
            state->lstyle = 'V';
288
0
            return '?'; /* its junk */
289
0
          }
290
0
        }
291
0
        /* fallthrough */
292
0
      }
293
0
      else if ((tokens[0][toklen[0]-1]) != ';')
294
0
      {
295
0
        if (numtoks == 1 && (state->lstyle == 'V' && !carry_buf_len))
296
0
          lstyle = 'V';
297
0
        else if (numtoks < 4)
298
0
          ;
299
0
        else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
300
0
          lstyle = 'V';
301
0
        else if ((&line[linelen] - tokens[1]) >= 22 &&
302
0
                  memcmp(tokens[1], "insufficient privilege", 22) == 0)
303
0
          lstyle = 'V';
304
0
        else if (numtoks != 4 && numtoks != 6)
305
0
          ;
306
0
        else if (numtoks == 6 && (
307
0
                 toklen[5] < 4 || *tokens[5] != '(' ||        /* perms */
308
0
                           (tokens[5][toklen[5]-1]) != ')'  ))
309
0
          ;
310
0
        else if (  (toklen[2] == 10 || toklen[2] == 11) &&
311
0
                        (tokens[2][toklen[2]-5]) == '-' &&
312
0
                        (tokens[2][toklen[2]-9]) == '-' &&
313
0
        (((toklen[3]==4 || toklen[3]==5 || toklen[3]==7 || toklen[3]==8) &&
314
0
                        (tokens[3][toklen[3]-3]) == ':' ) ||
315
0
         ((toklen[3]==10 || toklen[3]==11 ) &&
316
0
                        (tokens[3][toklen[3]-3]) == '.' )
317
0
        ) &&  /* time in [H]H:MM[:SS[.CC]] format */
318
0
                                    IsAsciiDigit(*tokens[1]) && /* size */
319
0
                                    IsAsciiDigit(*tokens[2]) && /* date */
320
0
                                    IsAsciiDigit(*tokens[3])    /* time */
321
0
                )
322
0
        {
323
0
          lstyle = 'V';
324
0
        }
325
0
        if (lstyle == 'V')
326
0
        {
327
0
          /*
328
0
          * MultiNet FTP:
329
0
          *   LOGIN.COM;2                 1   4-NOV-1994 04:09 [ANONYMOUS] (RWE,RWE,,)
330
0
          *   PUB.DIR;1                   1  27-JAN-1994 14:46 [ANONYMOUS] (RWE,RWE,RE,RWE)
331
0
          *   README.FTP;1        %RMS-E-PRV, insufficient privilege or file protection violation
332
0
          *   ROUSSOS.DIR;1               1  27-JAN-1994 14:48 [CS,ROUSSOS] (RWE,RWE,RE,R)
333
0
          *   S67-50903.JPG;1           328  22-SEP-1998 16:19 [ANONYMOUS] (RWED,RWED,,)
334
0
          * UCX FTP:
335
0
          *   CII-MANUAL.TEX;1  213/216  29-JAN-1996 03:33:12  [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
336
0
          * CMU/VMS-IP FTP
337
0
          *   [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
338
0
          * TCPware FTP
339
0
          *   FOO.BAR;1 4 5-MAR-1993 18:09:01.12
340
0
          * Long filename example:
341
0
          *   THIS-IS-A-LONG-VMS-FILENAME.AND-THIS-IS-A-LONG-VMS-FILETYPE\r\n
342
0
          *                    213[/nnn]  29-JAN-1996 03:33[:nn]  [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
343
0
          */
344
0
          tokmarker = 0;
345
0
          p = tokens[0];
346
0
          pos = 0;
347
0
          if (*p == '[' && toklen[0] >= 4) /* CMU style */
348
0
          {
349
0
            if (p[1] != ']')
350
0
            {
351
0
              p++;
352
0
              pos++;
353
0
            }
354
0
            while (lstyle && pos < toklen[0] && *p != ']')
355
0
            {
356
0
              if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
357
0
                  *p != '~' && !IsAsciiAlphanumeric(*p))
358
0
                lstyle = 0;
359
0
              pos++;
360
0
              p++;
361
0
            }
362
0
            if (lstyle && pos < (toklen[0]-1))
363
0
            {
364
0
              /* ']' was found and there is at least one character after it */
365
0
              NS_ASSERTION(*p == ']', "unexpected state");
366
0
              pos++;
367
0
              p++;
368
0
              tokmarker = pos; /* length of leading "[DIR1.DIR2.etc]" */
369
0
            } else {
370
0
              /* not a CMU style listing */
371
0
              lstyle = 0;
372
0
            }
373
0
          }
374
0
          while (lstyle && pos < toklen[0] && *p != ';')
375
0
          {
376
0
            if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
377
0
                *p != '~' && !IsAsciiAlphanumeric(*p))
378
0
              lstyle = 0;
379
0
            else if (IsAsciiLowercaseAlpha(*p))
380
0
              lstyle = 0;
381
0
            p++;
382
0
            pos++;
383
0
          }
384
0
          if (lstyle && *p == ';')
385
0
          {
386
0
            if (pos == 0 || pos == (toklen[0]-1))
387
0
              lstyle = 0;
388
0
            for (pos++;lstyle && pos < toklen[0];pos++)
389
0
            {
390
0
              if (!IsAsciiDigit(tokens[0][pos]))
391
0
                lstyle = 0;
392
0
            }
393
0
          }
394
0
          pos = (p - tokens[0]); /* => fnlength sans ";####" */
395
0
          pos -= tokmarker;      /* => fnlength sans "[DIR1.DIR2.etc]" */
396
0
          p = &(tokens[0][tokmarker]); /* offset of basename */
397
0
398
0
          if (!lstyle || pos == 0 || pos > 80) /* VMS filenames can't be longer than that */
399
0
          {
400
0
            lstyle = 0;
401
0
          }
402
0
          else if (numtoks == 1)
403
0
          {
404
0
            /* if VMS has been detected and there is only one token and that
405
0
             * token was a VMS filename then this is a multiline VMS LIST entry.
406
0
            */
407
0
            if (pos >= (sizeof(state->carry_buf)-1))
408
0
              pos = (sizeof(state->carry_buf)-1); /* shouldn't happen */
409
0
            memcpy( state->carry_buf, p, pos );
410
0
            state->carry_buf_len = pos;
411
0
            return '?'; /* tell caller to treat as junk */
412
0
          }
413
0
          else if (IsAsciiDigit(*tokens[1])) /* not no-privs message */
414
0
          {
415
0
            for (pos = 0; lstyle && pos < (toklen[1]); pos++)
416
0
            {
417
0
              if (!IsAsciiDigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
418
0
                lstyle = 0;
419
0
            }
420
0
            if (lstyle && numtoks > 4) /* Multinet or UCX but not CMU */
421
0
            {
422
0
              for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
423
0
              {
424
0
                p = &(tokens[5][pos]);
425
0
                if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
426
0
                  lstyle = 0;
427
0
              }
428
0
            }
429
0
          }
430
0
        } /* passed initial tests */
431
0
      } /* else if ((tokens[0][toklen[0]-1]) != ';') */
432
0
433
0
      if (lstyle == 'V')
434
0
      {
435
0
        state->parsed_one = 1;
436
0
        state->lstyle = lstyle;
437
0
438
0
        if (IsAsciiDigit(*tokens[1]))  /* not permission denied etc */
439
0
        {
440
0
          /* strip leading directory name */
441
0
          if (*tokens[0] == '[') /* CMU server */
442
0
          {
443
0
            pos = toklen[0]-1;
444
0
            p = tokens[0]+1;
445
0
            while (*p != ']')
446
0
            {
447
0
              p++;
448
0
              pos--;
449
0
            }
450
0
            toklen[0] = --pos;
451
0
            tokens[0] = ++p;
452
0
          }
453
0
          pos = 0;
454
0
          while (pos < toklen[0] && (tokens[0][pos]) != ';')
455
0
            pos++;
456
0
457
0
          result->fe_cinfs = 1;
458
0
          result->fe_type = 'f';
459
0
          result->fe_fname = tokens[0];
460
0
          result->fe_fnlen = pos;
461
0
462
0
          if (pos > 4)
463
0
          {
464
0
            p = &(tokens[0][pos-4]);
465
0
            if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
466
0
            {
467
0
              result->fe_fnlen -= 4;
468
0
              result->fe_type = 'd';
469
0
            }
470
0
          }
471
0
472
0
          if (result->fe_type != 'd')
473
0
          {
474
0
            /* #### or used/allocated form. If used/allocated form, then
475
0
             * 'used' is the size in bytes if and only if 'used'<=allocated.
476
0
             * If 'used' is size in bytes then it can be > 2^32
477
0
             * If 'used' is not size in bytes then it is size in blocks.
478
0
            */
479
0
            pos = 0;
480
0
            while (pos < toklen[1] && (tokens[1][pos]) != '/')
481
0
              pos++;
482
0
483
0
            /*
484
0
             * On OpenVMS, the size is given in blocks. A block is 512
485
0
             * bytes. This can only approximate the size of the file,
486
0
             * but that's better than not showing a size at all.
487
0
             * numBlocks is clamped to UINT32_MAX to make 32-bit and
488
0
             * 64-bit builds return consistent results.
489
0
             */
490
0
            uint64_t numBlocks = strtoul(tokens[1], nullptr, 10);
491
0
            numBlocks = std::min(numBlocks, (uint64_t)UINT32_MAX);
492
0
            uint64_t fileSize = numBlocks * 512;
493
0
            SprintfLiteral(result->fe_size, "%" PRIu64, fileSize);
494
0
          } /* if (result->fe_type != 'd') */
495
0
496
0
          p = tokens[2] + 2;
497
0
          if (*p == '-')
498
0
            p++;
499
0
          tbuf[0] = p[0];
500
0
          tbuf[1] = ToLowerCaseASCII(p[1]);
501
0
          tbuf[2] = ToLowerCaseASCII(p[2]);
502
0
          month_num = 0;
503
0
          for (pos = 0; pos < (12*3); pos+=3)
504
0
          {
505
0
            if (tbuf[0] == month_names[pos+0] &&
506
0
                tbuf[1] == month_names[pos+1] &&
507
0
                tbuf[2] == month_names[pos+2])
508
0
              break;
509
0
            month_num++;
510
0
          }
511
0
          if (month_num >= 12)
512
0
            month_num = 0;
513
0
          result->fe_time.tm_month = month_num;
514
0
          result->fe_time.tm_mday = atoi(tokens[2]);
515
0
          result->fe_time.tm_year = atoi(p+4); // NSPR wants year as XXXX
516
0
517
0
          p = tokens[3] + 2;
518
0
          if (*p == ':')
519
0
            p++;
520
0
          if (p[2] == ':')
521
0
            result->fe_time.tm_sec = atoi(p+3);
522
0
          result->fe_time.tm_hour = atoi(tokens[3]);
523
0
          result->fe_time.tm_min  = atoi(p);
524
0
525
0
          return result->fe_type;
526
0
527
0
        } /* if (IsAsciiDigit(*tokens[1])) */
528
0
529
0
        return '?'; /* junk */
530
0
531
0
      } /* if (lstyle == 'V') */
532
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'V')) */
533
0
#endif
534
0
535
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
536
0
537
0
#if defined(SUPPORT_CMS)
538
0
    /* Virtual Machine/Conversational Monitor System (IBM Mainframe) */
539
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'C'))  /* VM/CMS */
540
0
    {
541
0
      /* LISTing according to mirror.pl
542
0
       * Filename FileType  Fm Format Lrecl  Records Blocks Date      Time
543
0
       * LASTING  GLOBALV   A1 V      41     21     1       9/16/91   15:10:32
544
0
       * J43401   NETLOG    A0 V      77     1      1       9/12/91   12:36:04
545
0
       * PROFILE  EXEC      A1 V      17     3      1       9/12/91   12:39:07
546
0
       * DIRUNIX  SCRIPT    A1 V      77     1216   17      1/04/93   20:30:47
547
0
       * MAIL     PROFILE   A2 F      80     1      1       10/14/92  16:12:27
548
0
       * BADY2K   TEXT      A0 V      1      1      1       1/03/102  10:11:12
549
0
       * AUTHORS            A1 DIR    -      -      -       9/20/99   10:31:11
550
0
       *
551
0
       * LISTing from vm.marist.edu and vm.sc.edu
552
0
       * 220-FTPSERVE IBM VM Level 420 at VM.MARIST.EDU, 04:58:12 EDT WEDNESDAY 2002-07-10
553
0
       * AUTHORS           DIR        -          -          - 1999-09-20 10:31:11 -
554
0
       * HARRINGTON        DIR        -          -          - 1997-02-12 15:33:28 -
555
0
       * PICS              DIR        -          -          - 2000-10-12 15:43:23 -
556
0
       * SYSFILE           DIR        -          -          - 2000-07-20 17:48:01 -
557
0
       * WELCNVT  EXEC     V         72          9          1 1999-09-20 17:16:18 -
558
0
       * WELCOME  EREADME  F         80         21          1 1999-12-27 16:19:00 -
559
0
       * WELCOME  README   V         82         21          1 1999-12-27 16:19:04 -
560
0
       * README   ANONYMOU V         71         26          1 1997-04-02 12:33:20 TCP291
561
0
       * README   ANONYOLD V         71         15          1 1995-08-25 16:04:27 TCP291
562
0
      */
563
0
      if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
564
0
      {
565
0
        for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
566
0
        {
567
0
          p = tokens[pos];
568
0
          if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
569
0
              (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
570
0
          {
571
0
            if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
572
0
                                      (tokens[pos+5][5]) == ':'   )
573
0
            {
574
0
              p = tokens[pos+4];
575
0
              if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
576
0
                  (toklen[pos+4] >= 7 && toklen[pos+4] <= 9 &&
577
0
                            p[((p[1]!='/')?(2):(1))] == '/' &&
578
0
                            p[((p[1]!='/')?(5):(4))] == '/'))
579
0
               /* Y2K bugs possible ("7/06/102" or "13/02/101") */
580
0
              {
581
0
                if ( (*tokens[pos+1] == '-' &&
582
0
                      *tokens[pos+2] == '-' &&
583
0
                      *tokens[pos+3] == '-')  ||
584
0
                      (IsAsciiDigit(*tokens[pos+1]) &&
585
0
                       IsAsciiDigit(*tokens[pos+2]) &&
586
0
                       IsAsciiDigit(*tokens[pos+3])) )
587
0
                {
588
0
                  lstyle = 'C';
589
0
                  tokmarker = pos;
590
0
                }
591
0
              }
592
0
            }
593
0
          }
594
0
        } /* for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) */
595
0
      } /* if (numtoks >= 7) */
596
0
597
0
      /* extra checking if first pass */
598
0
      if (lstyle && !state->lstyle)
599
0
      {
600
0
        for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
601
0
        {
602
0
          if (IsAsciiLowercaseAlpha(*p))
603
0
            lstyle = 0;
604
0
        }
605
0
        for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
606
0
        {
607
0
          if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
608
0
          {
609
0
            for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
610
0
            {
611
0
              if (!IsAsciiDigit(*p))
612
0
                lstyle = 0;
613
0
            }
614
0
          }
615
0
        }
616
0
        for (pos = 0, p = tokens[tokmarker+4];
617
0
             lstyle && pos < toklen[tokmarker+4]; pos++, p++)
618
0
        {
619
0
          if (*p == '/')
620
0
          {
621
0
            /* There may be Y2K bugs in the date. Don't simplify to
622
0
             * pos != (len-3) && pos != (len-6) like time is done.
623
0
            */
624
0
            if ((tokens[tokmarker+4][1]) == '/')
625
0
            {
626
0
              if (pos != 1 && pos != 4)
627
0
                lstyle = 0;
628
0
            }
629
0
            else if (pos != 2 && pos != 5)
630
0
              lstyle = 0;
631
0
          }
632
0
          else if (*p != '-' && !IsAsciiDigit(*p))
633
0
            lstyle = 0;
634
0
          else if (*p == '-' && pos != 4 && pos != 7)
635
0
            lstyle = 0;
636
0
        }
637
0
        for (pos = 0, p = tokens[tokmarker+5];
638
0
             lstyle && pos < toklen[tokmarker+5]; pos++, p++)
639
0
        {
640
0
          if (*p != ':' && !IsAsciiDigit(*p))
641
0
            lstyle = 0;
642
0
          else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
643
0
                             && pos != (toklen[tokmarker+5]-6))
644
0
            lstyle = 0;
645
0
        }
646
0
      } /* initial if() */
647
0
648
0
      if (lstyle == 'C')
649
0
      {
650
0
        state->parsed_one = 1;
651
0
        state->lstyle = lstyle;
652
0
653
0
        p = tokens[tokmarker+4];
654
0
        if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */
655
0
        {
656
0
          result->fe_time.tm_year = atoi(p+0);
657
0
          result->fe_time.tm_month  = atoi(p+5) - 1;
658
0
          result->fe_time.tm_mday = atoi(p+8);
659
0
        }
660
0
        else /* oldstyle: [M]M/DD/YY format */
661
0
        {
662
0
          pos = toklen[tokmarker+4];
663
0
          result->fe_time.tm_month  = atoi(p) - 1;
664
0
          result->fe_time.tm_mday = atoi((p+pos)-5);
665
0
          result->fe_time.tm_year = atoi((p+pos)-2);
666
0
          FixupYear(&result->fe_time);
667
0
        }
668
0
669
0
        p = tokens[tokmarker+5];
670
0
        pos = toklen[tokmarker+5];
671
0
        result->fe_time.tm_hour  = atoi(p);
672
0
        result->fe_time.tm_min = atoi((p+pos)-5);
673
0
        result->fe_time.tm_sec = atoi((p+pos)-2);
674
0
675
0
        result->fe_cinfs = 1;
676
0
        result->fe_fname = tokens[0];
677
0
        result->fe_fnlen = toklen[0];
678
0
        result->fe_type  = 'f';
679
0
680
0
        p = tokens[tokmarker];
681
0
        if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
682
0
          result->fe_type  = 'd';
683
0
684
0
        if ((/*newstyle*/ toklen[tokmarker+4] == 10 && tokmarker > 1) ||
685
0
            (/*oldstyle*/ toklen[tokmarker+4] != 10 && tokmarker > 2))
686
0
        {                            /* have a filetype column */
687
0
          char *dot;
688
0
          p = &(tokens[0][toklen[0]]);
689
0
          memcpy( &dot, &p, sizeof(dot) ); /* NASTY! */
690
0
          *dot++ = '.';
691
0
          p = tokens[1];
692
0
          for (pos = 0; pos < toklen[1]; pos++)
693
0
            *dot++ = *p++;
694
0
          result->fe_fnlen += 1 + toklen[1];
695
0
        }
696
0
697
0
        /* oldstyle LISTING:
698
0
         * files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable
699
0
        if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
700
0
          return '?';
701
0
        */
702
0
703
0
        /* VM/CMS LISTings have no usable filesize field.
704
0
         * Have to use the 'SIZE' command for that.
705
0
        */
706
0
        return result->fe_type;
707
0
708
0
      } /* if (lstyle == 'C' && (!state->lstyle || state->lstyle == lstyle)) */
709
0
    } /* VM/CMS */
710
0
#endif
711
0
712
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
713
0
714
0
#if defined(SUPPORT_DOS) /* WinNT DOS dirstyle */
715
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'W'))
716
0
    {
717
0
      /*
718
0
       * "10-23-00  01:27PM       <DIR>          veronist"
719
0
       * "06-15-00  07:37AM       <DIR>          zoe"
720
0
       * "07-14-00  01:35PM              2094926 canprankdesk.tif"
721
0
       * "07-21-00  01:19PM                95077 Jon Kauffman Enjoys the Good Life.jpg"
722
0
       * "07-21-00  01:19PM                52275 Name Plate.jpg"
723
0
       * "07-14-00  01:38PM              2250540 Valentineoffprank-HiRes.jpg"
724
0
      */
725
0
      // Microsoft FTP server with FtpDirBrowseShowLongDate set returns year
726
0
      // in 4-digit format:
727
0
      // "10-10-2014  10:10AM       <DIR>        FTP"
728
0
      // Windows CE FTP server returns time in 24-hour format:
729
0
      // "05-03-13  22:01       <DIR>          APPS"
730
0
      if ((numtoks >= 4) && (toklen[0] == 8 || toklen[0] == 10) &&
731
0
          (toklen[1] == 5 || toklen[1] == 7) &&
732
0
          (*tokens[2] == '<' || IsAsciiDigit(*tokens[2])) )
733
0
      {
734
0
        p = tokens[0];
735
0
        if ( IsAsciiDigit(p[0]) && IsAsciiDigit(p[1]) && p[2]=='-' &&
736
0
             IsAsciiDigit(p[3]) && IsAsciiDigit(p[4]) && p[5]=='-' &&
737
0
             IsAsciiDigit(p[6]) && IsAsciiDigit(p[7]) )
738
0
        {
739
0
          p = tokens[1];
740
0
          if ( IsAsciiDigit(p[0]) && IsAsciiDigit(p[1]) && p[2]==':' &&
741
0
               IsAsciiDigit(p[3]) && IsAsciiDigit(p[4]) &&
742
0
               (toklen[1] == 5 || (toklen[1] == 7 &&
743
0
                                  (p[5]=='A' || p[5]=='P') && p[6]=='M')))
744
0
          {
745
0
            lstyle = 'W';
746
0
            if (!state->lstyle)
747
0
            {
748
0
              p = tokens[2];
749
0
              /* <DIR> or <JUNCTION> */
750
0
              if (*p != '<' || p[toklen[2]-1] != '>')
751
0
              {
752
0
                for (pos = 1; (lstyle && pos < toklen[2]); pos++)
753
0
                {
754
0
                  if (!IsAsciiDigit(*++p))
755
0
                    lstyle = 0;
756
0
                }
757
0
              }
758
0
            }
759
0
          }
760
0
        }
761
0
      }
762
0
763
0
      if (lstyle == 'W')
764
0
      {
765
0
        state->parsed_one = 1;
766
0
        state->lstyle = lstyle;
767
0
768
0
        p = &(line[linelen]); /* line end */
769
0
        result->fe_cinfs = 1;
770
0
        result->fe_fname = tokens[3];
771
0
        result->fe_fnlen = p - tokens[3];
772
0
        result->fe_type = 'd';
773
0
774
0
        if (*tokens[2] != '<') /* not <DIR> or <JUNCTION> */
775
0
        {
776
0
          // try to handle correctly spaces at the beginning of the filename
777
0
          // filesize (token[2]) must end at offset 38
778
0
          if (tokens[2] + toklen[2] - line == 38) {
779
0
            result->fe_fname = &(line[39]);
780
0
            result->fe_fnlen = p - result->fe_fname;
781
0
          }
782
0
          result->fe_type = 'f';
783
0
          pos = toklen[2];
784
0
          while (pos > (sizeof(result->fe_size)-1))
785
0
            pos = (sizeof(result->fe_size)-1);
786
0
          memcpy( result->fe_size, tokens[2], pos );
787
0
          result->fe_size[pos] = '\0';
788
0
        }
789
0
        else {
790
0
          // try to handle correctly spaces at the beginning of the filename
791
0
          // token[2] must begin at offset 24, the length is 5 or 10
792
0
          // token[3] must begin at offset 39 or higher
793
0
          if (tokens[2] - line == 24 && (toklen[2] == 5 || toklen[2] == 10) &&
794
0
              tokens[3] - line >= 39) {
795
0
            result->fe_fname = &(line[39]);
796
0
            result->fe_fnlen = p - result->fe_fname;
797
0
          }
798
0
799
0
          if ((tokens[2][1]) != 'D') /* not <DIR> */
800
0
          {
801
0
            result->fe_type = '?'; /* unknown until junc for sure */
802
0
            if (result->fe_fnlen > 4)
803
0
            {
804
0
              p = result->fe_fname;
805
0
              for (pos = result->fe_fnlen - 4; pos > 0; pos--)
806
0
              {
807
0
                if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
808
0
                    (p[1] == '=' || p[1] == '-'))
809
0
                {
810
0
                  result->fe_type = 'l';
811
0
                  result->fe_fnlen = p - result->fe_fname;
812
0
                  result->fe_lname = p + 4;
813
0
                  result->fe_lnlen = &(line[linelen])
814
0
                                     - result->fe_lname;
815
0
                  break;
816
0
                }
817
0
                p++;
818
0
              }
819
0
            }
820
0
          }
821
0
        }
822
0
823
0
        result->fe_time.tm_month = atoi(tokens[0]+0);
824
0
        if (result->fe_time.tm_month != 0)
825
0
        {
826
0
          result->fe_time.tm_month--;
827
0
          result->fe_time.tm_mday = atoi(tokens[0]+3);
828
0
          result->fe_time.tm_year = atoi(tokens[0]+6);
829
0
          FixupYear(&result->fe_time);
830
0
        }
831
0
832
0
        result->fe_time.tm_hour = atoi(tokens[1]+0);
833
0
        result->fe_time.tm_min = atoi(tokens[1]+3);
834
0
        if (toklen[1] == 7)
835
0
        {
836
0
          if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12)
837
0
            result->fe_time.tm_hour += 12;
838
0
          else if ((tokens[1][5]) == 'A' && result->fe_time.tm_hour == 12)
839
0
            result->fe_time.tm_hour = 0;
840
0
        }
841
0
842
0
        /* the caller should do this (if dropping "." and ".." is desired)
843
0
        if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
844
0
            (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
845
0
                                      result->fe_fname[1] == '.')))
846
0
          return '?';
847
0
        */
848
0
849
0
        return result->fe_type;
850
0
      } /* if (lstyle == 'W' && (!state->lstyle || state->lstyle == lstyle)) */
851
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'W')) */
852
0
#endif
853
0
854
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
855
0
856
0
#if defined(SUPPORT_OS2)
857
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'O')) /* OS/2 test */
858
0
    {
859
0
      /* 220 server IBM TCP/IP for OS/2 - FTP Server ver 23:04:36 on Jan 15 1997 ready.
860
0
      * fixed position, space padded columns. I have only a vague idea
861
0
      * of what the contents between col 18 and 34 might be: All I can infer
862
0
      * is that there may be attribute flags in there and there may be
863
0
      * a " DIR" in there.
864
0
      *
865
0
      *          1         2         3         4         5         6
866
0
      *0123456789012345678901234567890123456789012345678901234567890123456789
867
0
      *----- size -------|??????????????? MM-DD-YY|  HH:MM| nnnnnnnnn....
868
0
      *                 0  DIR            04-11-95   16:26  .
869
0
      *                 0  DIR            04-11-95   16:26  ..
870
0
      *                 0  DIR            04-11-95   16:26  ADDRESS
871
0
      *               612  RHSA           07-28-95   16:45  air_tra1.bag
872
0
      *               195  A              08-09-95   10:23  Alfa1.bag
873
0
      *                 0  RHS   DIR      04-11-95   16:26  ATTACH
874
0
      *               372  A              08-09-95   10:26  Aussie_1.bag
875
0
      *            310992                 06-28-94   09:56  INSTALL.EXE
876
0
      *                            1         2         3         4
877
0
      *                  01234567890123456789012345678901234567890123456789
878
0
      * dirlist from the mirror.pl project, col positions from Mozilla.
879
0
      */
880
0
      p = &(line[toklen[0]]);
881
0
      /* \s(\d\d-\d\d-\d\d)\s+(\d\d:\d\d)\s */
882
0
      if (numtoks >= 4 && toklen[0] <= 18 && IsAsciiDigit(*tokens[0]) &&
883
0
         (linelen - toklen[0]) >= (53-18)                        &&
884
0
         p[18-18] == ' ' && p[34-18] == ' '                      &&
885
0
         p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' '   &&
886
0
         p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' '   &&
887
0
         IsAsciiDigit(p[35-18]) && IsAsciiDigit(p[36-18])                  &&
888
0
         IsAsciiDigit(p[38-18]) && IsAsciiDigit(p[39-18])                  &&
889
0
         IsAsciiDigit(p[41-18]) && IsAsciiDigit(p[42-18])                  &&
890
0
         IsAsciiDigit(p[46-18]) && IsAsciiDigit(p[47-18])                  &&
891
0
         IsAsciiDigit(p[49-18]) && IsAsciiDigit(p[50-18])
892
0
      )
893
0
      {
894
0
        lstyle = 'O'; /* OS/2 */
895
0
        if (!state->lstyle)
896
0
        {
897
0
          for (pos = 1; lstyle && pos < toklen[0]; pos++)
898
0
          {
899
0
            if (!IsAsciiDigit(tokens[0][pos]))
900
0
              lstyle = 0;
901
0
          }
902
0
        }
903
0
      }
904
0
905
0
      if (lstyle == 'O')
906
0
      {
907
0
        state->parsed_one = 1;
908
0
        state->lstyle = lstyle;
909
0
910
0
        p = &(line[toklen[0]]);
911
0
912
0
        result->fe_cinfs = 1;
913
0
        result->fe_fname = &p[53-18];
914
0
        result->fe_fnlen = (&(line[linelen_sans_wsp]))
915
0
                           - (result->fe_fname);
916
0
        result->fe_type = 'f';
917
0
918
0
        /* I don't have a real listing to determine exact pos, so scan. */
919
0
        for (pos = (18-18); pos < ((35-18)-4); pos++)
920
0
        {
921
0
          if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
922
0
              p[pos+2] == 'I' && p[pos+3] == 'R')
923
0
          {
924
0
            result->fe_type = 'd';
925
0
            break;
926
0
          }
927
0
        }
928
0
929
0
        if (result->fe_type != 'd')
930
0
        {
931
0
          pos = toklen[0];
932
0
          if (pos > (sizeof(result->fe_size)-1))
933
0
            pos = (sizeof(result->fe_size)-1);
934
0
          memcpy( result->fe_size, tokens[0], pos );
935
0
          result->fe_size[pos] = '\0';
936
0
        }
937
0
938
0
        result->fe_time.tm_month = atoi(&p[35-18]) - 1;
939
0
        result->fe_time.tm_mday = atoi(&p[38-18]);
940
0
        result->fe_time.tm_year = atoi(&p[41-18]);
941
0
        FixupYear(&result->fe_time);
942
0
        result->fe_time.tm_hour = atoi(&p[46-18]);
943
0
        result->fe_time.tm_min = atoi(&p[49-18]);
944
0
945
0
        /* the caller should do this (if dropping "." and ".." is desired)
946
0
        if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
947
0
            (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
948
0
                                      result->fe_fname[1] == '.')))
949
0
          return '?';
950
0
        */
951
0
952
0
        return result->fe_type;
953
0
      } /* if (lstyle == 'O') */
954
0
955
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'O')) */
956
0
#endif
957
0
958
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
959
0
960
0
#if defined(SUPPORT_LSL)
961
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'U')) /* /bin/ls & co. */
962
0
    {
963
0
      /* UNIX-style listing, without inum and without blocks
964
0
       * "-rw-r--r--   1 root     other        531 Jan 29 03:26 README"
965
0
       * "dr-xr-xr-x   2 root     other        512 Apr  8  1994 etc"
966
0
       * "dr-xr-xr-x   2 root     512 Apr  8  1994 etc"
967
0
       * "lrwxrwxrwx   1 root     other          7 Jan 25 00:17 bin -> usr/bin"
968
0
       * Also produced by Microsoft's FTP servers for Windows:
969
0
       * "----------   1 owner    group         1803128 Jul 10 10:18 ls-lR.Z"
970
0
       * "d---------   1 owner    group               0 May  9 19:45 Softlib"
971
0
       * Also WFTPD for MSDOS:
972
0
       * "-rwxrwxrwx   1 noone    nogroup      322 Aug 19  1996 message.ftp"
973
0
       * Hellsoft for NetWare:
974
0
       * "d[RWCEMFA] supervisor            512       Jan 16 18:53    login"
975
0
       * "-[RWCEMFA] rhesus             214059       Oct 20 15:27    cx.exe"
976
0
       * Newer Hellsoft for NetWare: (netlab2.usu.edu)
977
0
       * - [RWCEAFMS] NFAUUser               192 Apr 27 15:21 HEADER.html
978
0
       * d [RWCEAFMS] jrd                    512 Jul 11 03:01 allupdates
979
0
       * Also NetPresenz for the Mac:
980
0
       * "-------r--         326  1391972  1392298 Nov 22  1995 MegaPhone.sit"
981
0
       * "drwxrwxr-x               folder        2 May 10  1996 network"
982
0
       * Protected directory:
983
0
       * "drwx-wx-wt  2 root  wheel  512 Jul  1 02:15 incoming"
984
0
       * uid/gid instead of username/groupname:
985
0
       * "drwxr-xr-x  2 0  0  512 May 28 22:17 etc"
986
0
      */
987
0
988
0
      bool is_old_Hellsoft = false;
989
0
990
0
      if (numtoks >= 6)
991
0
      {
992
0
        /* there are two perm formats (Hellsoft/NetWare and *IX strmode(3)).
993
0
         * Scan for size column only if the perm format is one or the other.
994
0
         */
995
0
        if (toklen[0] == 1 || (tokens[0][1]) == '[')
996
0
        {
997
0
          if (*tokens[0] == 'd' || *tokens[0] == '-')
998
0
          {
999
0
            pos = toklen[0]-1;
1000
0
            p = tokens[0] + 1;
1001
0
            if (pos == 0)
1002
0
            {
1003
0
              p = tokens[1];
1004
0
              pos = toklen[1];
1005
0
            }
1006
0
            if ((pos == 9 || pos == 10)        &&
1007
0
                (*p == '[' && p[pos-1] == ']') &&
1008
0
                (p[1] == 'R' || p[1] == '-')   &&
1009
0
                (p[2] == 'W' || p[2] == '-')   &&
1010
0
                (p[3] == 'C' || p[3] == '-')   &&
1011
0
                (p[4] == 'E' || p[4] == '-'))
1012
0
            {
1013
0
              /* rest is FMA[S] or AFM[S] */
1014
0
              lstyle = 'U'; /* very likely one of the NetWare servers */
1015
0
              if (toklen[0] == 10)
1016
0
                is_old_Hellsoft = true;
1017
0
            }
1018
0
          }
1019
0
        }
1020
0
        else if ((toklen[0] == 10 || toklen[0] == 11)
1021
0
                   && strchr("-bcdlpsw?DFam", *tokens[0]))
1022
0
        {
1023
0
          p = &(tokens[0][1]);
1024
0
          if ((p[0] == 'r' || p[0] == '-') &&
1025
0
              (p[1] == 'w' || p[1] == '-') &&
1026
0
              (p[3] == 'r' || p[3] == '-') &&
1027
0
              (p[4] == 'w' || p[4] == '-') &&
1028
0
              (p[6] == 'r' || p[6] == '-') &&
1029
0
              (p[7] == 'w' || p[7] == '-'))
1030
0
            /* 'x'/p[9] can be S|s|x|-|T|t or implementation specific */
1031
0
          {
1032
0
            lstyle = 'U'; /* very likely /bin/ls */
1033
0
          }
1034
0
        }
1035
0
      }
1036
0
      if (lstyle == 'U') /* first token checks out */
1037
0
      {
1038
0
        lstyle = 0;
1039
0
        for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
1040
0
        {
1041
0
          /* scan for: (\d+)\s+([A-Z][a-z][a-z])\s+
1042
0
           *  (\d\d\d\d|\d\:\d\d|\d\d\:\d\d|\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
1043
0
           *  \s+(.+)$
1044
0
          */
1045
0
          if (IsAsciiDigit(*tokens[pos]) /* size */
1046
0
              /* (\w\w\w) */
1047
0
           && toklen[pos+1] == 3 && IsAsciiAlpha(*tokens[pos+1]) &&
1048
0
              IsAsciiAlpha(tokens[pos+1][1]) && IsAsciiAlpha(tokens[pos+1][2])
1049
0
              /* (\d|\d\d) */
1050
0
           && IsAsciiDigit(*tokens[pos+2]) &&
1051
0
                (toklen[pos+2] == 1 ||
1052
0
                  (toklen[pos+2] == 2 && IsAsciiDigit(tokens[pos+2][1])))
1053
0
           && toklen[pos+3] >= 4 && IsAsciiDigit(*tokens[pos+3])
1054
0
              /* (\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
1055
0
           && (toklen[pos+3] <= 5 || (
1056
0
               (toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
1057
0
               (tokens[pos+3][toklen[pos+3]-3]) == ':'))
1058
0
           && IsAsciiDigit(tokens[pos+3][toklen[pos+3]-2])
1059
0
           && IsAsciiDigit(tokens[pos+3][toklen[pos+3]-1])
1060
0
           && (
1061
0
              /* (\d\d\d\d) */
1062
0
                 ((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
1063
0
                  IsAsciiDigit(tokens[pos+3][1]) &&
1064
0
                  IsAsciiDigit(tokens[pos+3][2])  )
1065
0
              /* (\d\:\d\d|\d\:\d\d\:\d\d) */
1066
0
              || ((toklen[pos+3] == 4 || toklen[pos+3] == 7) &&
1067
0
                  (tokens[pos+3][1]) == ':' &&
1068
0
                  IsAsciiDigit(tokens[pos+3][2]) && IsAsciiDigit(tokens[pos+3][3]))
1069
0
              /* (\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
1070
0
              || ((toklen[pos+3] == 5 || toklen[pos+3] == 8) &&
1071
0
                  IsAsciiDigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
1072
0
                  IsAsciiDigit(tokens[pos+3][3]) && IsAsciiDigit(tokens[pos+3][4]))
1073
0
              )
1074
0
           )
1075
0
          {
1076
0
            lstyle = 'U'; /* assume /bin/ls or variant format */
1077
0
            tokmarker = pos;
1078
0
1079
0
            /* check that size is numeric */
1080
0
            p = tokens[tokmarker];
1081
0
            unsigned int i;
1082
0
            for (i = 0; i < toklen[tokmarker]; i++)
1083
0
            {
1084
0
              if (!IsAsciiDigit(*p++))
1085
0
              {
1086
0
                lstyle = 0;
1087
0
                break;
1088
0
              }
1089
0
            }
1090
0
            if (lstyle)
1091
0
            {
1092
0
              month_num = 0;
1093
0
              p = tokens[tokmarker+1];
1094
0
              for (i = 0; i < (12*3); i+=3)
1095
0
              {
1096
0
                if (p[0] == month_names[i+0] &&
1097
0
                    p[1] == month_names[i+1] &&
1098
0
                    p[2] == month_names[i+2])
1099
0
                  break;
1100
0
                month_num++;
1101
0
              }
1102
0
              if (month_num >= 12)
1103
0
                lstyle = 0;
1104
0
            }
1105
0
          } /* relative position test */
1106
0
        } /* for (pos = (numtoks-5); !lstyle && pos > 1; pos--) */
1107
0
      } /* if (lstyle == 'U') */
1108
0
1109
0
      if (lstyle == 'U')
1110
0
      {
1111
0
        state->parsed_one = 1;
1112
0
        state->lstyle = lstyle;
1113
0
1114
0
        result->fe_cinfs = 0;
1115
0
        result->fe_type = '?';
1116
0
        if (*tokens[0] == 'd' || *tokens[0] == 'l')
1117
0
          result->fe_type = *tokens[0];
1118
0
        else if (*tokens[0] == 'D')
1119
0
          result->fe_type = 'd';
1120
0
        else if (*tokens[0] == '-' || *tokens[0] == 'F')
1121
0
          result->fe_type = 'f'; /* (hopefully a regular file) */
1122
0
1123
0
        if (result->fe_type != 'd')
1124
0
        {
1125
0
          pos = toklen[tokmarker];
1126
0
          if (pos > (sizeof(result->fe_size)-1))
1127
0
            pos = (sizeof(result->fe_size)-1);
1128
0
          memcpy( result->fe_size, tokens[tokmarker], pos );
1129
0
          result->fe_size[pos] = '\0';
1130
0
        }
1131
0
1132
0
        result->fe_time.tm_month  = month_num;
1133
0
        result->fe_time.tm_mday = atoi(tokens[tokmarker+2]);
1134
0
        if (result->fe_time.tm_mday == 0)
1135
0
          result->fe_time.tm_mday++;
1136
0
1137
0
        p = tokens[tokmarker+3];
1138
0
        pos = (unsigned int)atoi(p);
1139
0
        if (p[1] == ':') /* one digit hour */
1140
0
          p--;
1141
0
        if (p[2] != ':') /* year */
1142
0
        {
1143
0
          result->fe_time.tm_year = pos;
1144
0
        }
1145
0
        else
1146
0
        {
1147
0
          result->fe_time.tm_hour = pos;
1148
0
          result->fe_time.tm_min  = atoi(p+3);
1149
0
          if (p[5] == ':')
1150
0
            result->fe_time.tm_sec = atoi(p+6);
1151
0
1152
0
          if (!state->now_time)
1153
0
          {
1154
0
            state->now_time = nowTimeFn();
1155
0
            PR_ExplodeTime((state->now_time), timeParam, &(state->now_tm) );
1156
0
          }
1157
0
1158
0
          result->fe_time.tm_year = state->now_tm.tm_year;
1159
0
          if ( (( state->now_tm.tm_month << 5) + state->now_tm.tm_mday) <
1160
0
               ((result->fe_time.tm_month << 5) + result->fe_time.tm_mday) )
1161
0
            result->fe_time.tm_year--;
1162
0
1163
0
        } /* time/year */
1164
0
1165
0
        // The length of the whole date string should be 12. On AIX the length
1166
0
        // is only 11 when the year is present in the date string and there is
1167
0
        // 1 padding space at the end of the string. In both cases the filename
1168
0
        // starts at offset 13 from the start of the date string.
1169
0
        // Don't care about leading spaces when the date string has different
1170
0
        // format or when old Hellsoft output was detected.
1171
0
        {
1172
0
          const char *date_start = tokens[tokmarker+1];
1173
0
          const char *date_end = tokens[tokmarker+3] + toklen[tokmarker+3];
1174
0
          if (!is_old_Hellsoft && ((date_end - date_start) == 12 ||
1175
0
              ((date_end - date_start) == 11 && date_end[1] == ' ')))
1176
0
            result->fe_fname = date_start + 13;
1177
0
          else
1178
0
            result->fe_fname = tokens[tokmarker+4];
1179
0
        }
1180
0
1181
0
        result->fe_fnlen = (&(line[linelen]))
1182
0
                           - (result->fe_fname);
1183
0
1184
0
        if (result->fe_type == 'l' && result->fe_fnlen > 4)
1185
0
        {
1186
0
          /* First try to use result->fe_size to find " -> " sequence.
1187
0
             This can give proper result for cases like "aaa -> bbb -> ccc". */
1188
0
          uintptr_t fe_size = atoi(result->fe_size);
1189
0
          CheckedInt<uintptr_t> arrow_start(result->fe_fnlen);
1190
0
          arrow_start -= fe_size;
1191
0
          arrow_start -= 4;
1192
0
1193
0
          if (arrow_start.isValid() &&
1194
0
              PL_strncmp(result->fe_fname + arrow_start.value(), " -> ", 4) == 0)
1195
0
          {
1196
0
            result->fe_lname = result->fe_fname + (result->fe_fnlen - fe_size);
1197
0
            result->fe_lnlen = (&(line[linelen])) - (result->fe_lname);
1198
0
            result->fe_fnlen = arrow_start.value();
1199
0
          }
1200
0
          else
1201
0
          {
1202
0
            /* Search for sequence " -> " from the end for case when there are
1203
0
               more occurrences. F.e. if ftpd returns "a -> b -> c" assume
1204
0
               "a -> b" as a name. Powerusers can remove unnecessary parts
1205
0
               manually but there is no way to follow the link when some
1206
0
               essential part is missing. */
1207
0
            p = result->fe_fname + (result->fe_fnlen - 5);
1208
0
            for (pos = (result->fe_fnlen - 5); pos > 0; pos--)
1209
0
            {
1210
0
              if (PL_strncmp(p, " -> ", 4) == 0)
1211
0
              {
1212
0
                result->fe_lname = p + 4;
1213
0
                result->fe_lnlen = (&(line[linelen]))
1214
0
                                 - (result->fe_lname);
1215
0
                result->fe_fnlen = pos;
1216
0
                break;
1217
0
              }
1218
0
              p--;
1219
0
            }
1220
0
          }
1221
0
        }
1222
0
1223
#if defined(SUPPORT_LSLF) /* some (very rare) servers return ls -lF */
1224
        if (result->fe_fnlen > 1)
1225
        {
1226
          p = result->fe_fname[result->fe_fnlen-1];
1227
          pos = result->fe_type;
1228
          if (pos == 'd') {
1229
             if (*p == '/') result->fe_fnlen--; /* directory */
1230
          } else if (pos == 'l') {
1231
             if (*p == '@') result->fe_fnlen--; /* symlink */
1232
          } else if (pos == 'f') {
1233
             if (*p == '*') result->fe_fnlen--; /* executable */
1234
          } else if (*p == '=' || *p == '%' || *p == '|') {
1235
            result->fe_fnlen--; /* socket, whiteout, fifo */
1236
          }
1237
        }
1238
#endif
1239
1240
0
        /* the caller should do this (if dropping "." and ".." is desired)
1241
0
        if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
1242
0
            (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1243
0
                                      result->fe_fname[1] == '.')))
1244
0
          return '?';
1245
0
        */
1246
0
1247
0
        return result->fe_type;
1248
0
1249
0
      } /* if (lstyle == 'U') */
1250
0
1251
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'U')) */
1252
0
#endif
1253
0
1254
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1255
0
1256
0
#if defined(SUPPORT_W16) /* 16bit Windows */
1257
0
    if (!lstyle && (!state->lstyle || state->lstyle == 'w'))
1258
0
    {       /* old SuperTCP suite FTP server for Win3.1 */
1259
0
            /* old NetManage Chameleon TCP/IP suite FTP server for Win3.1 */
1260
0
      /*
1261
0
      * SuperTCP dirlist from the mirror.pl project
1262
0
      * mon/day/year separator may be '/' or '-'.
1263
0
      * .               <DIR>           11-16-94        17:16
1264
0
      * ..              <DIR>           11-16-94        17:16
1265
0
      * INSTALL         <DIR>           11-16-94        17:17
1266
0
      * CMT             <DIR>           11-21-94        10:17
1267
0
      * DESIGN1.DOC          11264      05-11-95        14:20
1268
0
      * README.TXT            1045      05-10-95        11:01
1269
0
      * WPKIT1.EXE          960338      06-21-95        17:01
1270
0
      * CMT.CSV                  0      07-06-95        14:56
1271
0
      *
1272
0
      * Chameleon dirlist guessed from lynx
1273
0
      * .               <DIR>      Nov 16 1994 17:16
1274
0
      * ..              <DIR>      Nov 16 1994 17:16
1275
0
      * INSTALL         <DIR>      Nov 16 1994 17:17
1276
0
      * CMT             <DIR>      Nov 21 1994 10:17
1277
0
      * DESIGN1.DOC     11264      May 11 1995 14:20   A
1278
0
      * README.TXT       1045      May 10 1995 11:01
1279
0
      * WPKIT1.EXE     960338      Jun 21 1995 17:01   R
1280
0
      * CMT.CSV             0      Jul 06 1995 14:56   RHA
1281
0
      */
1282
0
      if (numtoks >= 4 && toklen[0] < 13 &&
1283
0
          ((toklen[1] == 5 && *tokens[1] == '<') || IsAsciiDigit(*tokens[1])) )
1284
0
      {
1285
0
        if (numtoks == 4
1286
0
         && (toklen[2] == 8 || toklen[2] == 9)
1287
0
         && (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
1288
0
             ((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
1289
0
         && (toklen[3] == 4 || toklen[3] == 5)
1290
0
         && (tokens[3][toklen[3]-3]) == ':'
1291
0
         && IsAsciiDigit(tokens[2][0]) && IsAsciiDigit(tokens[2][1])
1292
0
         && IsAsciiDigit(tokens[2][3]) && IsAsciiDigit(tokens[2][4])
1293
0
         && IsAsciiDigit(tokens[2][6]) && IsAsciiDigit(tokens[2][7])
1294
0
         && (toklen[2] < 9 || IsAsciiDigit(tokens[2][8]))
1295
0
         && IsAsciiDigit(tokens[3][toklen[3]-1]) && IsAsciiDigit(tokens[3][toklen[3]-2])
1296
0
         && IsAsciiDigit(tokens[3][toklen[3]-4]) && IsAsciiDigit(*tokens[3])
1297
0
         )
1298
0
        {
1299
0
          lstyle = 'w';
1300
0
        }
1301
0
        else if ((numtoks == 6 || numtoks == 7)
1302
0
         && toklen[2] == 3 && toklen[3] == 2
1303
0
         && toklen[4] == 4 && toklen[5] == 5
1304
0
         && (tokens[5][2]) == ':'
1305
0
         && IsAsciiAlpha(tokens[2][0]) && IsAsciiAlpha(tokens[2][1])
1306
0
         &&                               IsAsciiAlpha(tokens[2][2])
1307
0
         && IsAsciiDigit(tokens[3][0]) && IsAsciiDigit(tokens[3][1])
1308
0
         && IsAsciiDigit(tokens[4][0]) && IsAsciiDigit(tokens[4][1])
1309
0
         && IsAsciiDigit(tokens[4][2]) && IsAsciiDigit(tokens[4][3])
1310
0
         && IsAsciiDigit(tokens[5][0]) && IsAsciiDigit(tokens[5][1])
1311
0
         && IsAsciiDigit(tokens[5][3]) && IsAsciiDigit(tokens[5][4])
1312
0
         /* could also check that (&(tokens[5][5]) - tokens[2]) == 17 */
1313
0
        )
1314
0
        {
1315
0
          lstyle = 'w';
1316
0
        }
1317
0
        if (lstyle && state->lstyle != lstyle) /* first time */
1318
0
        {
1319
0
          p = tokens[1];
1320
0
          if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
1321
0
                 p[2] != 'I' || p[3] != 'R' || p[4] != '>')
1322
0
          {
1323
0
            for (pos = 0; lstyle && pos < toklen[1]; pos++)
1324
0
            {
1325
0
              if (!IsAsciiDigit(*p++))
1326
0
                lstyle = 0;
1327
0
            }
1328
0
          } /* not <DIR> */
1329
0
        } /* if (first time) */
1330
0
      } /* if (numtoks == ...) */
1331
0
1332
0
      if (lstyle == 'w')
1333
0
      {
1334
0
        state->parsed_one = 1;
1335
0
        state->lstyle = lstyle;
1336
0
1337
0
        result->fe_cinfs = 1;
1338
0
        result->fe_fname = tokens[0];
1339
0
        result->fe_fnlen = toklen[0];
1340
0
        result->fe_type = 'd';
1341
0
1342
0
        p = tokens[1];
1343
0
        if (IsAsciiDigit(*p))
1344
0
        {
1345
0
          result->fe_type = 'f';
1346
0
          pos = toklen[1];
1347
0
          if (pos > (sizeof(result->fe_size)-1))
1348
0
            pos = sizeof(result->fe_size)-1;
1349
0
          memcpy( result->fe_size, p, pos );
1350
0
          result->fe_size[pos] = '\0';
1351
0
        }
1352
0
1353
0
        p = tokens[2];
1354
0
        if (toklen[2] == 3) /* Chameleon */
1355
0
        {
1356
0
          tbuf[0] = ToUpperCaseASCII(p[0]);
1357
0
          tbuf[1] = ToLowerCaseASCII(p[1]);
1358
0
          tbuf[2] = ToLowerCaseASCII(p[2]);
1359
0
          for (pos = 0; pos < (12*3); pos+=3)
1360
0
          {
1361
0
            if (tbuf[0] == month_names[pos+0] &&
1362
0
                tbuf[1] == month_names[pos+1] &&
1363
0
                tbuf[2] == month_names[pos+2])
1364
0
            {
1365
0
              result->fe_time.tm_month = pos/3;
1366
0
              result->fe_time.tm_mday = atoi(tokens[3]);
1367
0
              result->fe_time.tm_year = atoi(tokens[4]);
1368
0
              break;
1369
0
            }
1370
0
          }
1371
0
          pos = 5; /* Chameleon toknum of date field */
1372
0
        }
1373
0
        else
1374
0
        {
1375
0
          result->fe_time.tm_month = atoi(p+0)-1;
1376
0
          result->fe_time.tm_mday = atoi(p+3);
1377
0
          result->fe_time.tm_year = atoi(p+6);
1378
0
          FixupYear(&result->fe_time); /* SuperTCP */
1379
0
1380
0
          pos = 3; /* SuperTCP toknum of date field */
1381
0
        }
1382
0
1383
0
        result->fe_time.tm_hour = atoi(tokens[pos]);
1384
0
        result->fe_time.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
1385
0
1386
0
        /* the caller should do this (if dropping "." and ".." is desired)
1387
0
        if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
1388
0
            (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1389
0
                                      result->fe_fname[1] == '.')))
1390
0
          return '?';
1391
0
        */
1392
0
1393
0
        return result->fe_type;
1394
0
      } /* (lstyle == 'w') */
1395
0
1396
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'w'))  */
1397
0
#endif
1398
0
1399
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1400
0
1401
0
#if defined(SUPPORT_DLS) /* dls -dtR */
1402
0
    if (!lstyle &&
1403
0
       (state->lstyle == 'D' || (!state->lstyle && state->numlines == 1)))
1404
0
       /* /bin/dls lines have to be immediately recognizable (first line) */
1405
0
    {
1406
0
      /* I haven't seen an FTP server that delivers a /bin/dls listing,
1407
0
       * but can infer the format from the lynx and mirror.pl projects.
1408
0
       * Both formats are supported.
1409
0
       *
1410
0
       * Lynx says:
1411
0
       * README              763  Information about this server\0
1412
0
       * bin/                  -  \0
1413
0
       * etc/                  =  \0
1414
0
       * ls-lR                 0  \0
1415
0
       * ls-lR.Z               3  \0
1416
0
       * pub/                  =  Public area\0
1417
0
       * usr/                  -  \0
1418
0
       * morgan               14  -> ../real/morgan\0
1419
0
       * TIMIT.mostlikely.Z\0
1420
0
       *                   79215  \0
1421
0
       *
1422
0
       * mirror.pl says:
1423
0
       * filename:  ^(\S*)\s+
1424
0
       * size:      (\-|\=|\d+)\s+
1425
0
       * month/day: ((\w\w\w\s+\d+|\d+\s+\w\w\w)\s+
1426
0
       * time/year: (\d+:\d+|\d\d\d\d))\s+
1427
0
       * rest:      (.+)
1428
0
       *
1429
0
       * README              763  Jul 11 21:05  Information about this server
1430
0
       * bin/                  -  Apr 28  1994
1431
0
       * etc/                  =  11 Jul 21:04
1432
0
       * ls-lR                 0   6 Aug 17:14
1433
0
       * ls-lR.Z               3  05 Sep 1994
1434
0
       * pub/                  =  Jul 11 21:04  Public area
1435
0
       * usr/                  -  Sep  7 09:39
1436
0
       * morgan               14  Apr 18 09:39  -> ../real/morgan
1437
0
       * TIMIT.mostlikely.Z
1438
0
       *                   79215  Jul 11 21:04
1439
0
      */
1440
0
      if (!state->lstyle && line[linelen-1] == ':' &&
1441
0
          linelen >= 2 && toklen[numtoks-1] != 1)
1442
0
      {
1443
0
        /* code in mirror.pl suggests that a listing may be preceded
1444
0
         * by a PWD line in the form "/some/dir/names/here:"
1445
0
         * but does not necessarily begin with '/'. *sigh*
1446
0
        */
1447
0
        pos = 0;
1448
0
        p = line;
1449
0
        while (pos < (linelen-1))
1450
0
        {
1451
0
          /* illegal (or extremely unusual) chars in a dirspec */
1452
0
          if (*p == '<' || *p == '|' || *p == '>' ||
1453
0
              *p == '?' || *p == '*' || *p == '\\')
1454
0
            break;
1455
0
          if (*p == '/' && pos < (linelen-2) && p[1] == '/')
1456
0
            break;
1457
0
          pos++;
1458
0
          p++;
1459
0
        }
1460
0
        if (pos == (linelen-1))
1461
0
        {
1462
0
          state->lstyle = 'D';
1463
0
          return '?';
1464
0
        }
1465
0
      }
1466
0
1467
0
      if (!lstyle && numtoks >= 2)
1468
0
      {
1469
0
        pos = 22; /* pos of (\d+|-|=) if this is not part of a multiline */
1470
0
        if (state->lstyle && carry_buf_len) /* first is from previous line */
1471
0
          pos = toklen[1]-1; /* and is 'as-is' (may contain whitespace) */
1472
0
1473
0
        if (linelen > pos)
1474
0
        {
1475
0
          p = &line[pos];
1476
0
          if ((*p == '-' || *p == '=' || IsAsciiDigit(*p)) &&
1477
0
              ((linelen == (pos+1)) ||
1478
0
               (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
1479
0
          {
1480
0
            tokmarker = 1;
1481
0
            if (!carry_buf_len)
1482
0
            {
1483
0
              pos = 1;
1484
0
              while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
1485
0
                pos++;
1486
0
              tokmarker = 0;
1487
0
              if ((tokens[pos]+toklen[pos]) == (&line[23]))
1488
0
                tokmarker = pos;
1489
0
            }
1490
0
            if (tokmarker)
1491
0
            {
1492
0
              lstyle = 'D';
1493
0
              if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
1494
0
              {
1495
0
                if (toklen[tokmarker] != 1 ||
1496
0
                   (tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
1497
0
                  lstyle = 0;
1498
0
              }
1499
0
              else
1500
0
              {
1501
0
                for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++)
1502
0
                {
1503
0
                  if (!IsAsciiDigit(tokens[tokmarker][pos]))
1504
0
                    lstyle = 0;
1505
0
                }
1506
0
              }
1507
0
              if (lstyle && !state->lstyle) /* first time */
1508
0
              {
1509
0
                /* scan for illegal (or incredibly unusual) chars in fname */
1510
0
                for (p = tokens[0]; lstyle &&
1511
0
                     p < &(tokens[tokmarker-1][toklen[tokmarker-1]]); p++)
1512
0
                {
1513
0
                  if (*p == '<' || *p == '|' || *p == '>' ||
1514
0
                      *p == '?' || *p == '*' || *p == '/' || *p == '\\')
1515
0
                    lstyle = 0;
1516
0
                }
1517
0
              }
1518
0
1519
0
            } /* size token found */
1520
0
          } /* expected chars behind expected size token */
1521
0
        } /* if (linelen > pos) */
1522
0
      } /* if (!lstyle && numtoks >= 2) */
1523
0
1524
0
      if (!lstyle && state->lstyle == 'D' && !carry_buf_len)
1525
0
      {
1526
0
        /* the filename of a multi-line entry can be identified
1527
0
         * correctly only if dls format had been previously established.
1528
0
         * This should always be true because there should be entries
1529
0
         * for '.' and/or '..' and/or CWD that precede the rest of the
1530
0
         * listing.
1531
0
        */
1532
0
        pos = linelen;
1533
0
        if (pos > (sizeof(state->carry_buf)-1))
1534
0
          pos = sizeof(state->carry_buf)-1;
1535
0
        memcpy( state->carry_buf, line, pos );
1536
0
        state->carry_buf_len = pos;
1537
0
        return '?';
1538
0
      }
1539
0
1540
0
      if (lstyle == 'D')
1541
0
      {
1542
0
        state->parsed_one = 1;
1543
0
        state->lstyle = lstyle;
1544
0
1545
0
        p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
1546
0
        result->fe_fname = tokens[0];
1547
0
        result->fe_fnlen = p - tokens[0];
1548
0
        result->fe_type  = 'f';
1549
0
1550
0
        if (result->fe_fname[result->fe_fnlen-1] == '/')
1551
0
        {
1552
0
          if (result->fe_lnlen == 1)
1553
0
            result->fe_type = '?';
1554
0
          else
1555
0
          {
1556
0
            result->fe_fnlen--;
1557
0
            result->fe_type  = 'd';
1558
0
          }
1559
0
        }
1560
0
        else if (IsAsciiDigit(*tokens[tokmarker]))
1561
0
        {
1562
0
          pos = toklen[tokmarker];
1563
0
          if (pos > (sizeof(result->fe_size)-1))
1564
0
            pos = sizeof(result->fe_size)-1;
1565
0
          memcpy( result->fe_size, tokens[tokmarker], pos );
1566
0
          result->fe_size[pos] = '\0';
1567
0
        }
1568
0
1569
0
        if ((tokmarker+3) < numtoks &&
1570
0
              (&(tokens[numtoks-1][toklen[numtoks-1]]) -
1571
0
               tokens[tokmarker+1]) >= (1+1+3+1+4) )
1572
0
        {
1573
0
          pos = (tokmarker+3);
1574
0
          p = tokens[pos];
1575
0
          pos = toklen[pos];
1576
0
1577
0
          if ((pos == 4 || pos == 5)
1578
0
          &&  IsAsciiDigit(*p) && IsAsciiDigit(p[pos-1]) && IsAsciiDigit(p[pos-2])
1579
0
          &&  ((pos == 5 && p[2] == ':') ||
1580
0
               (pos == 4 && (IsAsciiDigit(p[1]) || p[1] == ':')))
1581
0
             )
1582
0
          {
1583
0
            month_num = tokmarker+1; /* assumed position of month field */
1584
0
            pos = tokmarker+2;       /* assumed position of mday field */
1585
0
            if (IsAsciiDigit(*tokens[month_num])) /* positions are reversed */
1586
0
            {
1587
0
              month_num++;
1588
0
              pos--;
1589
0
            }
1590
0
            p = tokens[month_num];
1591
0
            if (IsAsciiDigit(*tokens[pos])
1592
0
            && (toklen[pos] == 1 ||
1593
0
                  (toklen[pos] == 2 && IsAsciiDigit(tokens[pos][1])))
1594
0
            && toklen[month_num] == 3
1595
0
            && IsAsciiAlpha(*p) && IsAsciiAlpha(p[1]) && IsAsciiAlpha(p[2])  )
1596
0
            {
1597
0
              pos = atoi(tokens[pos]);
1598
0
              if (pos > 0 && pos <= 31)
1599
0
              {
1600
0
                result->fe_time.tm_mday = pos;
1601
0
                month_num = 1;
1602
0
                for (pos = 0; pos < (12*3); pos+=3)
1603
0
                {
1604
0
                  if (p[0] == month_names[pos+0] &&
1605
0
                      p[1] == month_names[pos+1] &&
1606
0
                      p[2] == month_names[pos+2])
1607
0
                    break;
1608
0
                  month_num++;
1609
0
                }
1610
0
                if (month_num > 12)
1611
0
                  result->fe_time.tm_mday = 0;
1612
0
                else
1613
0
                  result->fe_time.tm_month = month_num - 1;
1614
0
              }
1615
0
            }
1616
0
            if (result->fe_time.tm_mday)
1617
0
            {
1618
0
              tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */
1619
0
              p = tokens[tokmarker];
1620
0
1621
0
              pos = atoi(p);
1622
0
              if (pos > 24)
1623
0
                result->fe_time.tm_year = pos;
1624
0
              else
1625
0
              {
1626
0
                if (p[1] == ':')
1627
0
                  p--;
1628
0
                result->fe_time.tm_hour = pos;
1629
0
                result->fe_time.tm_min = atoi(p+3);
1630
0
                if (!state->now_time)
1631
0
                {
1632
0
                  state->now_time = nowTimeFn();
1633
0
                  PR_ExplodeTime((state->now_time), timeParam, &(state->now_tm) );
1634
0
                }
1635
0
                result->fe_time.tm_year = state->now_tm.tm_year;
1636
0
                if ( (( state->now_tm.tm_month  << 4) + state->now_tm.tm_mday) <
1637
0
                     ((result->fe_time.tm_month << 4) + result->fe_time.tm_mday) )
1638
0
                  result->fe_time.tm_year--;
1639
0
              } /* got year or time */
1640
0
            } /* got month/mday */
1641
0
          } /* may have year or time */
1642
0
        } /* enough remaining to possibly have date/time */
1643
0
1644
0
        if (numtoks > (tokmarker+2))
1645
0
        {
1646
0
          pos = tokmarker+1;
1647
0
          p = tokens[pos];
1648
0
          if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
1649
0
          {
1650
0
            p = &(tokens[numtoks-1][toklen[numtoks-1]]);
1651
0
            result->fe_type  = 'l';
1652
0
            result->fe_lname = tokens[pos+1];
1653
0
            result->fe_lnlen = p - result->fe_lname;
1654
0
            if (result->fe_lnlen > 1 &&
1655
0
                result->fe_lname[result->fe_lnlen-1] == '/')
1656
0
              result->fe_lnlen--;
1657
0
          }
1658
0
        } /* if (numtoks > (tokmarker+2)) */
1659
0
1660
0
        /* the caller should do this (if dropping "." and ".." is desired)
1661
0
        if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
1662
0
            (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
1663
0
                                      result->fe_fname[1] == '.')))
1664
0
          return '?';
1665
0
        */
1666
0
1667
0
        return result->fe_type;
1668
0
1669
0
      } /* if (lstyle == 'D') */
1670
0
    } /* if (!lstyle && (!state->lstyle || state->lstyle == 'D')) */
1671
0
#endif
1672
0
1673
0
    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
1674
0
1675
0
  } /* if (linelen > 0) */
1676
0
1677
0
  return ParsingFailed(state);
1678
0
}
1679