Back to index

lightning-sunbird  0.9+nobinonly
ParseFTPList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org Code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Cyrus Patel <cyp@fb14.uni-mainz.de>.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Doug Turner <dougt@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdlib.h>
00040 #include <string.h>
00041 #include <ctype.h>
00042 
00043 #include "ParseFTPList.h"
00044 
00045 /* ==================================================================== */
00046 
00047 int ParseFTPList(const char *line, struct list_state *state,
00048                  struct list_result *result )
00049 {
00050   unsigned int carry_buf_len; /* copy of state->carry_buf_len */
00051   unsigned int linelen, pos;
00052   const char *p;
00053 
00054   if (!line || !state || !result)
00055     return 0;
00056 
00057   memset( result, 0, sizeof(*result) );
00058   if (state->magic != ((void *)ParseFTPList))
00059   {
00060     memset( state, 0, sizeof(*state) );
00061     state->magic = ((void *)ParseFTPList);
00062   }
00063   state->numlines++;
00064 
00065   /* carry buffer is only valid from one line to the next */
00066   carry_buf_len = state->carry_buf_len;
00067   state->carry_buf_len = 0;
00068 
00069   linelen = 0;
00070 
00071   /* strip leading whitespace */
00072   while (*line == ' ' || *line == '\t')
00073     line++;
00074     
00075   /* line is terminated at first '\0' or '\n' */
00076   p = line;
00077   while (*p && *p != '\n')
00078     p++;
00079   linelen = p - line;
00080 
00081   if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
00082     linelen--;
00083 
00084   /* DONT strip trailing whitespace. */
00085 
00086   if (linelen > 0)
00087   {
00088     static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
00089     const char *tokens[16]; /* 16 is more than enough */
00090     unsigned int toklen[(sizeof(tokens)/sizeof(tokens[0]))];
00091     unsigned int linelen_sans_wsp;  // line length sans whitespace
00092     unsigned int numtoks = 0;
00093     unsigned int tokmarker = 0; /* extra info for lstyle handler */
00094     unsigned int month_num = 0;
00095     char tbuf[4];
00096     int lstyle = 0;
00097 
00098     if (carry_buf_len) /* VMS long filename carryover buffer */
00099     {
00100       tokens[0] = state->carry_buf;
00101       toklen[0] = carry_buf_len;
00102       numtoks++;
00103     }
00104 
00105     pos = 0;
00106     while (pos < linelen && numtoks < (sizeof(tokens)/sizeof(tokens[0])) )
00107     {
00108       while (pos < linelen && 
00109             (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
00110         pos++;
00111       if (pos < linelen)
00112       {
00113         tokens[numtoks] = &line[pos];
00114         while (pos < linelen && 
00115            (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
00116           pos++;
00117         if (tokens[numtoks] != &line[pos])
00118         {
00119           toklen[numtoks] = (&line[pos] - tokens[numtoks]);
00120           numtoks++;  
00121         }
00122       }
00123     }    
00124 
00125     linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
00126     if (numtoks == (sizeof(tokens)/sizeof(tokens[0])) )
00127     {
00128       pos = linelen;
00129       while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
00130         pos--;
00131       linelen_sans_wsp = pos;
00132     }
00133 
00134     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00135 
00136 #if defined(SUPPORT_EPLF)
00137     /* EPLF handling must come somewhere before /bin/dls handling. */
00138     if (!lstyle && (!state->lstyle || state->lstyle == 'E'))
00139     {
00140       if (*line == '+' && linelen > 4 && numtoks >= 2)
00141       {
00142         pos = 1;
00143         while (pos < (linelen-1))
00144         {
00145           p = &line[pos++];
00146           if (*p == '/') 
00147             result->fe_type = 'd'; /* its a dir */
00148           else if (*p == 'r')
00149             result->fe_type = 'f'; /* its a file */
00150           else if (*p == 'm')
00151           {
00152             if (isdigit(line[pos]))
00153             {
00154               while (pos < linelen && isdigit(line[pos]))
00155                 pos++;
00156               if (pos < linelen && line[pos] == ',')
00157               {
00158                 PRTime t;
00159                 PRTime seconds;
00160                 PR_sscanf(p+1, "%llu", &seconds);
00161                 LL_MUL(t, seconds, PR_USEC_PER_SEC);
00162                 PR_ExplodeTime(t, PR_LocalTimeParameters, &(result->fe_time) );
00163               }
00164             }
00165           }
00166           else if (*p == 's')
00167           {
00168             if (isdigit(line[pos]))
00169             {
00170               while (pos < linelen && isdigit(line[pos]))
00171                 pos++;
00172               if (pos < linelen && line[pos] == ',' &&
00173                  ((&line[pos]) - (p+1)) < int(sizeof(result->fe_size)-1) )
00174               {
00175                 memcpy( result->fe_size, p+1, (unsigned)(&line[pos] - (p+1)) );
00176                 result->fe_size[(&line[pos] - (p+1))] = '\0';
00177               }
00178             }
00179           }
00180           else if (isalpha(*p)) /* 'i'/'up' or unknown "fact" (property) */
00181           {
00182             while (pos < linelen && *++p != ',')
00183               pos++;
00184           }
00185           else if (*p != '\t' || (p+1) != tokens[1])
00186           {
00187             break; /* its not EPLF after all */
00188           }
00189           else
00190           {
00191             state->parsed_one = 1;
00192             state->lstyle = lstyle = 'E';
00193 
00194             p = &(line[linelen_sans_wsp]);
00195             result->fe_fname = tokens[1];
00196             result->fe_fnlen = p - tokens[1];
00197 
00198             if (!result->fe_type) /* access denied */
00199             {
00200               result->fe_type = 'f'; /* is assuming 'f'ile correct? */
00201               return '?';            /* NO! junk it. */
00202             }
00203             return result->fe_type;
00204           }
00205           if (pos >= (linelen-1) || line[pos] != ',')
00206             break;
00207           pos++;
00208         } /* while (pos < linelen) */
00209         memset( result, 0, sizeof(*result) );
00210       } /* if (*line == '+' && linelen > 4 && numtoks >= 2) */
00211     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'E')) */
00212 #endif /* SUPPORT_EPLF */
00213 
00214     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00215 
00216 #if defined(SUPPORT_VMS)
00217     if (!lstyle && (!state->lstyle || state->lstyle == 'V'))
00218     {                          /* try VMS Multinet/UCX/CMS server */
00219       /*
00220        * Legal characters in a VMS file/dir spec are [A-Z0-9$.-_~].
00221        * '$' cannot begin a filename and `-' cannot be used as the first 
00222        * or last character. '.' is only valid as a directory separator 
00223        * and <file>.<type> separator. A canonical filename spec might look 
00224        * like this: DISK$VOL:[DIR1.DIR2.DIR3]FILE.TYPE;123
00225        * All VMS FTP servers LIST in uppercase.
00226        *
00227        * We need to be picky about this in order to support
00228        * multi-line listings correctly.
00229       */
00230       if (!state->parsed_one &&
00231           (numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
00232                             memcmp(tokens[0], "Directory", 9)==0 )))
00233       {
00234         /* If no dirstyle has been detected yet, and this line is a 
00235          * VMS list's dirname, then turn on VMS dirstyle.
00236          * eg "ACA:[ANONYMOUS]", "DISK$FTP:[ANONYMOUS]", "SYS$ANONFTP:" 
00237         */
00238         p = tokens[0];
00239         pos = toklen[0];
00240         if (numtoks == 2)
00241         {
00242           p = tokens[1];
00243           pos = toklen[1];
00244         }
00245         pos--;
00246         if (pos >= 3)
00247         {
00248           while (pos > 0 && p[pos] != '[')
00249           {
00250             pos--;
00251             if (p[pos] == '-' || p[pos] == '$')
00252             {
00253               if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
00254                   (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
00255                 break;
00256             }
00257             else if (p[pos] != '.' && p[pos] != '~' && 
00258                      !isdigit(p[pos]) && !isalpha(p[pos]))
00259               break;
00260             else if (isalpha(p[pos]) && p[pos] != toupper(p[pos]))
00261               break;
00262           }
00263           if (pos > 0)
00264           {
00265             pos--;
00266             if (p[pos] != ':' || p[pos+1] != '[')
00267               pos = 0;
00268           }
00269         }
00270         if (pos > 0 && p[pos] == ':')
00271         {
00272           while (pos > 0)
00273           {
00274             pos--;
00275             if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
00276                 p[pos] != '~' && !isdigit(p[pos]) && !isalpha(p[pos]))
00277               break;
00278             else if (isalpha(p[pos]) && p[pos] != toupper(p[pos]))
00279               break;
00280           }
00281           if (pos == 0)
00282           {  
00283             state->lstyle = 'V';
00284             return '?'; /* its junk */
00285           }
00286         }
00287         /* fallthrough */ 
00288       }
00289       else if ((tokens[0][toklen[0]-1]) != ';')
00290       {
00291         if (numtoks == 1 && (state->lstyle == 'V' && !carry_buf_len))
00292           lstyle = 'V';
00293         else if (numtoks < 4)
00294           ;
00295         else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
00296           lstyle = 'V';
00297         else if ((&line[linelen] - tokens[1]) >= 22 &&
00298                   memcmp(tokens[1], "insufficient privilege", 22) == 0)
00299           lstyle = 'V';
00300         else if (numtoks != 4 && numtoks != 6)
00301           ;
00302         else if (numtoks == 6 && (
00303                  toklen[5] < 4 || *tokens[5] != '(' ||        /* perms */
00304                            (tokens[5][toklen[5]-1]) != ')'  ))
00305           ;
00306         else if (  (toklen[2] == 10 || toklen[2] == 11) &&      
00307                         (tokens[2][toklen[2]-5]) == '-' &&
00308                         (tokens[2][toklen[2]-9]) == '-' &&
00309         (((toklen[3]==4 || toklen[3]==5 || toklen[3]==7 || toklen[3]==8) &&
00310                         (tokens[3][toklen[3]-3]) == ':' ) ||
00311          ((toklen[3]==10 || toklen[3]==11 ) &&
00312                         (tokens[3][toklen[3]-3]) == '.' )
00313         ) &&  /* time in [H]H:MM[:SS[.CC]] format */
00314                                     isdigit(*tokens[1]) && /* size */
00315                                     isdigit(*tokens[2]) && /* date */
00316                                     isdigit(*tokens[3])    /* time */
00317                 )
00318         {
00319           lstyle = 'V';
00320         }
00321         if (lstyle == 'V')
00322         {
00323           /* 
00324           * MultiNet FTP:
00325           *   LOGIN.COM;2                 1   4-NOV-1994 04:09 [ANONYMOUS] (RWE,RWE,,)
00326           *   PUB.DIR;1                   1  27-JAN-1994 14:46 [ANONYMOUS] (RWE,RWE,RE,RWE)
00327           *   README.FTP;1        %RMS-E-PRV, insufficient privilege or file protection violation
00328           *   ROUSSOS.DIR;1               1  27-JAN-1994 14:48 [CS,ROUSSOS] (RWE,RWE,RE,R)
00329           *   S67-50903.JPG;1           328  22-SEP-1998 16:19 [ANONYMOUS] (RWED,RWED,,)
00330           * UCX FTP: 
00331           *   CII-MANUAL.TEX;1  213/216  29-JAN-1996 03:33:12  [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
00332           * CMU/VMS-IP FTP
00333           *   [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
00334           * TCPware FTP
00335           *   FOO.BAR;1 4 5-MAR-1993 18:09:01.12
00336           * Long filename example:
00337           *   THIS-IS-A-LONG-VMS-FILENAME.AND-THIS-IS-A-LONG-VMS-FILETYPE\r\n
00338           *                    213[/nnn]  29-JAN-1996 03:33[:nn]  [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
00339           */
00340           tokmarker = 0;
00341           p = tokens[0];
00342           pos = 0;
00343           if (*p == '[' && toklen[0] >= 4) /* CMU style */
00344           {
00345             if (p[1] != ']') 
00346             {
00347               p++;
00348               pos++;
00349             }
00350             while (lstyle && pos < toklen[0] && *p != ']')
00351             {
00352               if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
00353                   *p != '~' && !isdigit(*p) && !isalpha(*p))              
00354                 lstyle = 0;
00355               pos++;
00356               p++;
00357             }
00358             if (lstyle && pos < (toklen[0]-1) && *p == ']')
00359             {
00360               pos++;
00361               p++;
00362               tokmarker = pos; /* length of leading "[DIR1.DIR2.etc]" */
00363             }
00364           }
00365           while (lstyle && pos < toklen[0] && *p != ';')
00366           {
00367             if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
00368                 *p != '~' && !isdigit(*p) && !isalpha(*p))
00369               lstyle = 0;
00370             else if (isalpha(*p) && *p != toupper(*p))
00371               lstyle = 0;
00372             p++;
00373             pos++;
00374           }
00375           if (lstyle && *p == ';')
00376           {
00377             if (pos == 0 || pos == (toklen[0]-1))
00378               lstyle = 0;
00379             for (pos++;lstyle && pos < toklen[0];pos++)
00380             {
00381               if (!isdigit(tokens[0][pos]))
00382                 lstyle = 0;
00383             }
00384           }
00385           pos = (p - tokens[0]); /* => fnlength sans ";####" */
00386           pos -= tokmarker;      /* => fnlength sans "[DIR1.DIR2.etc]" */
00387           p = &(tokens[0][tokmarker]); /* offset of basename */
00388 
00389           if (!lstyle || pos > 80) /* VMS filenames can't be longer than that */
00390           {
00391             lstyle = 0;
00392           }
00393           else if (numtoks == 1)
00394           { 
00395             /* if VMS has been detected and there is only one token and that 
00396              * token was a VMS filename then this is a multiline VMS LIST entry.
00397             */
00398             if (pos >= (sizeof(state->carry_buf)-1))
00399               pos = (sizeof(state->carry_buf)-1); /* shouldn't happen */
00400             memcpy( state->carry_buf, p, pos );
00401             state->carry_buf_len = pos;
00402             return '?'; /* tell caller to treat as junk */
00403           }
00404           else if (isdigit(*tokens[1])) /* not no-privs message */
00405           {
00406             for (pos = 0; lstyle && pos < (toklen[1]); pos++)
00407             {
00408               if (!isdigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
00409                 lstyle = 0;
00410             }
00411             if (lstyle && numtoks > 4) /* Multinet or UCX but not CMU */
00412             {
00413               for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
00414               {
00415                 p = &(tokens[5][pos]);
00416                 if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
00417                   lstyle = 0;
00418               }
00419             }
00420           }
00421         } /* passed initial tests */
00422       } /* else if ((tokens[0][toklen[0]-1]) != ';') */    
00423 
00424       if (lstyle == 'V')
00425       {
00426         state->parsed_one = 1;
00427         state->lstyle = lstyle;
00428 
00429         if (isdigit(*tokens[1]))  /* not permission denied etc */
00430         {
00431           /* strip leading directory name */
00432           if (*tokens[0] == '[') /* CMU server */
00433           {
00434             pos = toklen[0]-1;
00435             p = tokens[0]+1;
00436             while (*p != ']')
00437             {
00438               p++;
00439               pos--;
00440             }
00441             toklen[0] = --pos;
00442             tokens[0] = ++p;
00443           }
00444           pos = 0;
00445           while (pos < toklen[0] && (tokens[0][pos]) != ';')
00446             pos++;
00447        
00448           result->fe_cinfs = 1;
00449           result->fe_type = 'f';
00450           result->fe_fname = tokens[0];
00451           result->fe_fnlen = pos;
00452 
00453           if (pos > 4)
00454           {
00455             p = &(tokens[0][pos-4]);
00456             if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
00457             {
00458               result->fe_fnlen -= 4;
00459               result->fe_type = 'd';
00460             }
00461           }
00462 
00463           if (result->fe_type != 'd')
00464           {
00465             /* #### or used/allocated form. If used/allocated form, then
00466              * 'used' is the size in bytes if and only if 'used'<=allocated.
00467              * If 'used' is size in bytes then it can be > 2^32
00468              * If 'used' is not size in bytes then it is size in blocks.
00469             */
00470             pos = 0;
00471             while (pos < toklen[1] && (tokens[1][pos]) != '/')
00472               pos++;
00473             
00474 /*
00475  * I've never seen size come back in bytes, its always in blocks, and 
00476  * the following test fails. So, always perform the "size in blocks".
00477  * I'm leaving the "size in bytes" code if'd out in case we ever need
00478  * to re-instate it.
00479 */
00480 #if 0
00481             if (pos < toklen[1] && ( (pos<<1) > (toklen[1]-1) ||
00482                  (strtoul(tokens[1], (char **)0, 10) > 
00483                   strtoul(tokens[1]+pos+1, (char **)0, 10))        ))
00484             {                                   /* size is in bytes */
00485               if (pos > (sizeof(result->fe_size)-1))
00486                 pos = sizeof(result->fe_size)-1;
00487               memcpy( result->fe_size, tokens[1], pos );
00488               result->fe_size[pos] = '\0';
00489             }
00490             else /* size is in blocks */
00491 #endif
00492             {
00493               /* size requires multiplication by blocksize. 
00494                *
00495                * We could assume blocksize is 512 (like Lynx does) and
00496                * shift by 9, but that might not be right. Even if it 
00497                * were, doing that wouldn't reflect what the file's 
00498                * real size was. The sanest thing to do is not use the
00499                * LISTing's filesize, so we won't (like ftpmirror).
00500                *
00501                * ulltoa(((unsigned long long)fsz)<<9, result->fe_size, 10);
00502                *
00503                * A block is always 512 bytes on OpenVMS, compute size.
00504                * So its rounded up to the next block, so what, its better
00505                * than not showing the size at all.
00506                * A block is always 512 bytes on OpenVMS, compute size.
00507                * So its rounded up to the next block, so what, its better
00508                * than not showing the size at all.
00509               */
00510               PRUint64 fsz, factor;
00511               LL_UI2L(fsz, strtoul(tokens[1], (char **)0, 10));
00512               LL_UI2L(factor, 512);
00513               LL_MUL(fsz, fsz, factor);
00514               PR_snprintf(result->fe_size, sizeof(result->fe_size), 
00515                           "%lld", fsz);
00516             } 
00517 
00518           } /* if (result->fe_type != 'd') */
00519 
00520           p = tokens[2] + 2;
00521           if (*p == '-')
00522             p++;
00523           tbuf[0] = p[0];
00524           tbuf[1] = tolower(p[1]);
00525           tbuf[2] = tolower(p[2]);
00526           month_num = 0;
00527           for (pos = 0; pos < (12*3); pos+=3)
00528           {
00529             if (tbuf[0] == month_names[pos+0] && 
00530                 tbuf[1] == month_names[pos+1] && 
00531                 tbuf[2] == month_names[pos+2])
00532               break;
00533             month_num++;
00534           }
00535           if (month_num >= 12)
00536             month_num = 0;
00537           result->fe_time.tm_month = month_num;
00538           result->fe_time.tm_mday = atoi(tokens[2]);
00539           result->fe_time.tm_year = atoi(p+4); // NSPR wants year as XXXX
00540 
00541           p = tokens[3] + 2;
00542           if (*p == ':')
00543             p++;
00544           if (p[2] == ':')
00545             result->fe_time.tm_sec = atoi(p+3);
00546           result->fe_time.tm_hour = atoi(tokens[3]);
00547           result->fe_time.tm_min  = atoi(p);
00548       
00549           return result->fe_type;
00550 
00551         } /* if (isdigit(*tokens[1])) */
00552 
00553         return '?'; /* junk */
00554 
00555       } /* if (lstyle == 'V') */
00556     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'V')) */
00557 #endif
00558 
00559     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00560 
00561 #if defined(SUPPORT_CMS)
00562     /* Virtual Machine/Conversational Monitor System (IBM Mainframe) */
00563     if (!lstyle && (!state->lstyle || state->lstyle == 'C'))  /* VM/CMS */
00564     {
00565       /* LISTing according to mirror.pl
00566        * Filename FileType  Fm Format Lrecl  Records Blocks Date      Time
00567        * LASTING  GLOBALV   A1 V      41     21     1       9/16/91   15:10:32
00568        * J43401   NETLOG    A0 V      77     1      1       9/12/91   12:36:04
00569        * PROFILE  EXEC      A1 V      17     3      1       9/12/91   12:39:07
00570        * DIRUNIX  SCRIPT    A1 V      77     1216   17      1/04/93   20:30:47
00571        * MAIL     PROFILE   A2 F      80     1      1       10/14/92  16:12:27
00572        * BADY2K   TEXT      A0 V      1      1      1       1/03/102  10:11:12
00573        * AUTHORS            A1 DIR    -      -      -       9/20/99   10:31:11
00574        *
00575        * LISTing from vm.marist.edu and vm.sc.edu
00576        * 220-FTPSERVE IBM VM Level 420 at VM.MARIST.EDU, 04:58:12 EDT WEDNESDAY 2002-07-10
00577        * AUTHORS           DIR        -          -          - 1999-09-20 10:31:11 -
00578        * HARRINGTON        DIR        -          -          - 1997-02-12 15:33:28 -
00579        * PICS              DIR        -          -          - 2000-10-12 15:43:23 -
00580        * SYSFILE           DIR        -          -          - 2000-07-20 17:48:01 -
00581        * WELCNVT  EXEC     V         72          9          1 1999-09-20 17:16:18 -
00582        * WELCOME  EREADME  F         80         21          1 1999-12-27 16:19:00 -
00583        * WELCOME  README   V         82         21          1 1999-12-27 16:19:04 -
00584        * README   ANONYMOU V         71         26          1 1997-04-02 12:33:20 TCP291
00585        * README   ANONYOLD V         71         15          1 1995-08-25 16:04:27 TCP291
00586       */
00587       if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
00588       {
00589         for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
00590         {
00591           p = tokens[pos];
00592           if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
00593               (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
00594           {
00595             if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
00596                                       (tokens[pos+5][5]) == ':'   )
00597             {
00598               p = tokens[pos+4];
00599               if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
00600                   (toklen[pos+4] >= 7 && toklen[pos+4] <= 9 && 
00601                             p[((p[1]!='/')?(2):(1))] == '/' && 
00602                             p[((p[1]!='/')?(5):(4))] == '/'))
00603                /* Y2K bugs possible ("7/06/102" or "13/02/101") */
00604               {
00605                 if ( (*tokens[pos+1] == '-' &&
00606                       *tokens[pos+2] == '-' &&
00607                       *tokens[pos+3] == '-')  ||
00608                       (isdigit(*tokens[pos+1]) &&
00609                        isdigit(*tokens[pos+2]) &&
00610                        isdigit(*tokens[pos+3])) )
00611                 {
00612                   lstyle = 'C';
00613                   tokmarker = pos;
00614                 }
00615               }
00616             }
00617           }
00618         } /* for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) */
00619       } /* if (numtoks >= 7) */
00620 
00621       /* extra checking if first pass */
00622       if (lstyle && !state->lstyle) 
00623       {
00624         for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
00625         {  
00626           if (isalpha(*p) && toupper(*p) != *p)
00627             lstyle = 0;
00628         } 
00629         for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
00630         {
00631           if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
00632           {
00633             for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
00634             {
00635               if (!isdigit(*p))
00636                 lstyle = 0;
00637             }
00638           }
00639         }
00640         for (pos = 0, p = tokens[tokmarker+4]; 
00641              lstyle && pos < toklen[tokmarker+4]; pos++, p++)
00642         {
00643           if (*p == '/')
00644           { 
00645             /* There may be Y2K bugs in the date. Don't simplify to
00646              * pos != (len-3) && pos != (len-6) like time is done.
00647             */             
00648             if ((tokens[tokmarker+4][1]) == '/')
00649             {
00650               if (pos != 1 && pos != 4)
00651                 lstyle = 0;
00652             }
00653             else if (pos != 2 && pos != 5)
00654               lstyle = 0;
00655           }
00656           else if (*p != '-' && !isdigit(*p))
00657             lstyle = 0;
00658           else if (*p == '-' && pos != 4 && pos != 7)
00659             lstyle = 0;
00660         }
00661         for (pos = 0, p = tokens[tokmarker+5]; 
00662              lstyle && pos < toklen[tokmarker+5]; pos++, p++)
00663         {
00664           if (*p != ':' && !isdigit(*p))
00665             lstyle = 0;
00666           else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
00667                              && pos != (toklen[tokmarker+5]-6))
00668             lstyle = 0;
00669         }
00670       } /* initial if() */
00671 
00672       if (lstyle == 'C')
00673       {
00674         state->parsed_one = 1;
00675         state->lstyle = lstyle;
00676 
00677         p = tokens[tokmarker+4];
00678         if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */
00679         {
00680           result->fe_time.tm_year = atoi(p+0) - 1900;
00681           result->fe_time.tm_month  = atoi(p+5) - 1;
00682           result->fe_time.tm_mday = atoi(p+8);
00683         }
00684         else /* oldstyle: [M]M/DD/YY format */
00685         {
00686           pos = toklen[tokmarker+4];
00687           result->fe_time.tm_month  = atoi(p) - 1;
00688           result->fe_time.tm_mday = atoi((p+pos)-5);
00689           result->fe_time.tm_year = atoi((p+pos)-2);
00690           if (result->fe_time.tm_year < 70)
00691             result->fe_time.tm_year += 100;
00692         }
00693 
00694         p = tokens[tokmarker+5];
00695         pos = toklen[tokmarker+5];
00696         result->fe_time.tm_hour  = atoi(p);
00697         result->fe_time.tm_min = atoi((p+pos)-5);
00698         result->fe_time.tm_sec = atoi((p+pos)-2);
00699 
00700         result->fe_cinfs = 1;
00701         result->fe_fname = tokens[0];
00702         result->fe_fnlen = toklen[0];
00703         result->fe_type  = 'f';
00704 
00705         p = tokens[tokmarker];
00706         if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
00707           result->fe_type  = 'd';
00708 
00709         if ((/*newstyle*/ toklen[tokmarker+4] == 10 && tokmarker > 1) ||
00710             (/*oldstyle*/ toklen[tokmarker+4] != 10 && tokmarker > 2))
00711         {                            /* have a filetype column */
00712           char *dot;
00713           p = &(tokens[0][toklen[0]]);
00714           memcpy( &dot, &p, sizeof(dot) ); /* NASTY! */
00715           *dot++ = '.';
00716           p = tokens[1];
00717           for (pos = 0; pos < toklen[1]; pos++)
00718             *dot++ = *p++;
00719           result->fe_fnlen += 1 + toklen[1];
00720         }
00721 
00722         /* oldstyle LISTING: 
00723          * files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable 
00724         if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
00725           return '?';
00726         */
00727         
00728         /* VM/CMS LISTings have no usable filesize field. 
00729          * Have to use the 'SIZE' command for that.
00730         */
00731         return result->fe_type;
00732 
00733       } /* if (lstyle == 'C' && (!state->lstyle || state->lstyle == lstyle)) */
00734     } /* VM/CMS */
00735 #endif
00736 
00737     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00738 
00739 #if defined(SUPPORT_DOS) /* WinNT DOS dirstyle */
00740     if (!lstyle && (!state->lstyle || state->lstyle == 'W'))
00741     {
00742       /*
00743        * "10-23-00  01:27PM       <DIR>          veronist"
00744        * "06-15-00  07:37AM       <DIR>          zoe"
00745        * "07-14-00  01:35PM              2094926 canprankdesk.tif"
00746        * "07-21-00  01:19PM                95077 Jon Kauffman Enjoys the Good Life.jpg"
00747        * "07-21-00  01:19PM                52275 Name Plate.jpg"
00748        * "07-14-00  01:38PM              2250540 Valentineoffprank-HiRes.jpg"
00749       */
00750       if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 && 
00751           (*tokens[2] == '<' || isdigit(*tokens[2])) )
00752       {
00753         p = tokens[0];
00754         if ( isdigit(p[0]) && isdigit(p[1]) && p[2]=='-' && 
00755              isdigit(p[3]) && isdigit(p[4]) && p[5]=='-' &&
00756              isdigit(p[6]) && isdigit(p[7]) )
00757         {
00758           p = tokens[1];
00759           if ( isdigit(p[0]) && isdigit(p[1]) && p[2]==':' && 
00760                isdigit(p[3]) && isdigit(p[4]) && 
00761                (p[5]=='A' || p[5]=='P') && p[6]=='M')
00762           {
00763             lstyle = 'W';
00764             if (!state->lstyle)
00765             {            
00766               p = tokens[2];
00767               /* <DIR> or <JUNCTION> */
00768               if (*p != '<' || p[toklen[2]-1] != '>')
00769               {
00770                 for (pos = 1; (lstyle && pos < toklen[2]); pos++)
00771                 {
00772                   if (!isdigit(*++p))
00773                     lstyle = 0;
00774                 }
00775               }
00776             }
00777           }
00778         }
00779       }
00780 
00781       if (lstyle == 'W')
00782       {
00783         state->parsed_one = 1;
00784         state->lstyle = lstyle;
00785 
00786         p = &(line[linelen_sans_wsp]); /* line end sans wsp */
00787         result->fe_cinfs = 1;
00788         result->fe_fname = tokens[3];
00789         result->fe_fnlen = p - tokens[3];
00790         result->fe_type = 'd';
00791 
00792         if (*tokens[2] != '<') /* not <DIR> or <JUNCTION> */
00793         {
00794           result->fe_type = 'f';
00795           pos = toklen[2];
00796           while (pos > (sizeof(result->fe_size)-1))
00797             pos = (sizeof(result->fe_size)-1);
00798           memcpy( result->fe_size, tokens[2], pos );
00799           result->fe_size[pos] = '\0';
00800         }
00801         else if ((tokens[2][1]) != 'D') /* not <DIR> */
00802         {
00803           result->fe_type = '?'; /* unknown until junc for sure */
00804           if (result->fe_fnlen > 4)
00805           {
00806             p = result->fe_fname;
00807             for (pos = result->fe_fnlen - 4; pos > 0; pos--)
00808             {
00809               if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
00810                   (p[1] == '=' || p[1] == '-'))
00811               {
00812                 result->fe_type = 'l';
00813                 result->fe_fnlen = p - result->fe_fname;
00814                 result->fe_lname = p + 4;
00815                 result->fe_lnlen = &(line[linelen_sans_wsp]) 
00816                                    - result->fe_lname;
00817                 break;
00818               }
00819               p++;
00820             }    
00821           }
00822         }
00823       
00824         result->fe_time.tm_month = atoi(tokens[0]+0);
00825         if (result->fe_time.tm_month != 0)
00826         {
00827           result->fe_time.tm_month--;
00828           result->fe_time.tm_mday = atoi(tokens[0]+3);
00829           result->fe_time.tm_year = atoi(tokens[0]+6);
00830           if (result->fe_time.tm_year < 80)
00831             result->fe_time.tm_year += 100;
00832         }
00833 
00834         result->fe_time.tm_hour = atoi(tokens[1]+0);
00835         result->fe_time.tm_min = atoi(tokens[1]+3);
00836         if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12)
00837           result->fe_time.tm_hour += 12;
00838 
00839         /* the caller should do this (if dropping "." and ".." is desired)
00840         if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
00841             (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
00842                                       result->fe_fname[1] == '.')))
00843           return '?';
00844         */
00845 
00846         return result->fe_type;  
00847       } /* if (lstyle == 'W' && (!state->lstyle || state->lstyle == lstyle)) */
00848     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'W')) */
00849 #endif
00850 
00851     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00852 
00853 #if defined(SUPPORT_OS2)
00854     if (!lstyle && (!state->lstyle || state->lstyle == 'O')) /* OS/2 test */
00855     {
00856       /* 220 server IBM TCP/IP for OS/2 - FTP Server ver 23:04:36 on Jan 15 1997 ready.
00857       * fixed position, space padded columns. I have only a vague idea 
00858       * of what the contents between col 18 and 34 might be: All I can infer
00859       * is that there may be attribute flags in there and there may be 
00860       * a " DIR" in there.
00861       *
00862       *          1         2         3         4         5         6
00863       *0123456789012345678901234567890123456789012345678901234567890123456789
00864       *----- size -------|??????????????? MM-DD-YY|  HH:MM| nnnnnnnnn....
00865       *                 0  DIR            04-11-95   16:26  .
00866       *                 0  DIR            04-11-95   16:26  ..
00867       *                 0  DIR            04-11-95   16:26  ADDRESS
00868       *               612  RHSA           07-28-95   16:45  air_tra1.bag
00869       *               195  A              08-09-95   10:23  Alfa1.bag
00870       *                 0  RHS   DIR      04-11-95   16:26  ATTACH
00871       *               372  A              08-09-95   10:26  Aussie_1.bag
00872       *            310992                 06-28-94   09:56  INSTALL.EXE
00873       *                            1         2         3         4
00874       *                  01234567890123456789012345678901234567890123456789
00875       * dirlist from the mirror.pl project, col positions from Mozilla.
00876       */
00877       p = &(line[toklen[0]]);
00878       /* \s(\d\d-\d\d-\d\d)\s+(\d\d:\d\d)\s */
00879       if (numtoks >= 4 && toklen[0] <= 18 && isdigit(*tokens[0]) &&
00880          (linelen - toklen[0]) >= (53-18)                        &&
00881          p[18-18] == ' ' && p[34-18] == ' '                      &&
00882          p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' '   &&
00883          p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' '   &&
00884          isdigit(p[35-18]) && isdigit(p[36-18])                  &&
00885          isdigit(p[38-18]) && isdigit(p[39-18])                  &&
00886          isdigit(p[41-18]) && isdigit(p[42-18])                  &&
00887          isdigit(p[46-18]) && isdigit(p[47-18])                  &&
00888          isdigit(p[49-18]) && isdigit(p[50-18])
00889       )
00890       {
00891         lstyle = 'O'; /* OS/2 */
00892         if (!state->lstyle)
00893         {            
00894           for (pos = 1; lstyle && pos < toklen[0]; pos++)
00895           {
00896             if (!isdigit(tokens[0][pos]))
00897               lstyle = 0;
00898           }
00899         }
00900       }
00901 
00902       if (lstyle == 'O')
00903       {
00904         state->parsed_one = 1;
00905         state->lstyle = lstyle;
00906 
00907         p = &(line[toklen[0]]);
00908 
00909         result->fe_cinfs = 1;
00910         result->fe_fname = &p[53-18];
00911         result->fe_fnlen = (&(line[linelen_sans_wsp]))
00912                            - (result->fe_fname);
00913         result->fe_type = 'f';
00914 
00915         /* I don't have a real listing to determine exact pos, so scan. */
00916         for (pos = (18-18); pos < ((35-18)-4); pos++)
00917         {
00918           if (p[pos+0] == ' ' && p[pos+1] == 'D' && 
00919               p[pos+2] == 'I' && p[pos+3] == 'R')
00920           {
00921             result->fe_type = 'd';
00922             break;
00923           }
00924         }
00925     
00926         if (result->fe_type != 'd')
00927         {
00928           pos = toklen[0];
00929           if (pos > (sizeof(result->fe_size)-1))
00930             pos = (sizeof(result->fe_size)-1);
00931           memcpy( result->fe_size, tokens[0], pos );
00932           result->fe_size[pos] = '\0';
00933         }  
00934     
00935         result->fe_time.tm_month = atoi(&p[35-18]) - 1;
00936         result->fe_time.tm_mday = atoi(&p[38-18]);
00937         result->fe_time.tm_year = atoi(&p[41-18]);
00938         if (result->fe_time.tm_year < 80)
00939           result->fe_time.tm_year += 100;
00940         result->fe_time.tm_hour = atoi(&p[46-18]);
00941         result->fe_time.tm_min = atoi(&p[49-18]);
00942    
00943         /* the caller should do this (if dropping "." and ".." is desired)
00944         if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
00945             (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
00946                                       result->fe_fname[1] == '.')))
00947           return '?';
00948         */
00949 
00950         return result->fe_type;
00951       } /* if (lstyle == 'O') */
00952 
00953     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'O')) */
00954 #endif
00955 
00956     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
00957     
00958 #if defined(SUPPORT_LSL)
00959     if (!lstyle && (!state->lstyle || state->lstyle == 'U')) /* /bin/ls & co. */
00960     {
00961       /* UNIX-style listing, without inum and without blocks
00962        * "-rw-r--r--   1 root     other        531 Jan 29 03:26 README"
00963        * "dr-xr-xr-x   2 root     other        512 Apr  8  1994 etc"
00964        * "dr-xr-xr-x   2 root     512 Apr  8  1994 etc"
00965        * "lrwxrwxrwx   1 root     other          7 Jan 25 00:17 bin -> usr/bin"
00966        * Also produced by Microsoft's FTP servers for Windows:
00967        * "----------   1 owner    group         1803128 Jul 10 10:18 ls-lR.Z"
00968        * "d---------   1 owner    group               0 May  9 19:45 Softlib"
00969        * Also WFTPD for MSDOS:
00970        * "-rwxrwxrwx   1 noone    nogroup      322 Aug 19  1996 message.ftp"
00971        * Hellsoft for NetWare:
00972        * "d[RWCEMFA] supervisor            512       Jan 16 18:53    login"
00973        * "-[RWCEMFA] rhesus             214059       Oct 20 15:27    cx.exe"
00974        * Newer Hellsoft for NetWare: (netlab2.usu.edu)
00975        * - [RWCEAFMS] NFAUUser               192 Apr 27 15:21 HEADER.html
00976        * d [RWCEAFMS] jrd                    512 Jul 11 03:01 allupdates
00977        * Also NetPresenz for the Mac:
00978        * "-------r--         326  1391972  1392298 Nov 22  1995 MegaPhone.sit"
00979        * "drwxrwxr-x               folder        2 May 10  1996 network"
00980        * Protected directory:
00981        * "drwx-wx-wt  2 root  wheel  512 Jul  1 02:15 incoming"
00982        * uid/gid instead of username/groupname:
00983        * "drwxr-xr-x  2 0  0  512 May 28 22:17 etc"
00984       */
00985     
00986       if (numtoks >= 6)
00987       {
00988         /* there are two perm formats (Hellsoft/NetWare and *IX strmode(3)).
00989          * Scan for size column only if the perm format is one or the other.
00990          */
00991         if (toklen[0] == 1 || (tokens[0][1]) == '[')
00992         {
00993           if (*tokens[0] == 'd' || *tokens[0] == '-')
00994           {
00995             pos = toklen[0]-1;
00996             p = tokens[0] + 1;
00997             if (pos == 0)
00998             {
00999               p = tokens[1];
01000               pos = toklen[1];
01001             }
01002             if ((pos == 9 || pos == 10)        && 
01003                 (*p == '[' && p[pos-1] == ']') &&
01004                 (p[1] == 'R' || p[1] == '-')   &&
01005                 (p[2] == 'W' || p[2] == '-')   &&
01006                 (p[3] == 'C' || p[3] == '-')   &&
01007                 (p[4] == 'E' || p[4] == '-'))
01008             {
01009               /* rest is FMA[S] or AFM[S] */
01010               lstyle = 'U'; /* very likely one of the NetWare servers */
01011             }
01012           }
01013         }
01014         else if ((toklen[0] == 10 || toklen[0] == 11) 
01015                    && strchr("-bcdlpsw?DFam", *tokens[0]))
01016         {
01017           p = &(tokens[0][1]);
01018           if ((p[0] == 'r' || p[0] == '-') &&
01019               (p[1] == 'w' || p[1] == '-') &&
01020               (p[3] == 'r' || p[3] == '-') &&
01021               (p[4] == 'w' || p[4] == '-') &&
01022               (p[6] == 'r' || p[6] == '-') &&
01023               (p[7] == 'w' || p[7] == '-'))
01024             /* 'x'/p[9] can be S|s|x|-|T|t or implementation specific */
01025           {
01026             lstyle = 'U'; /* very likely /bin/ls */
01027           }
01028         }
01029       }
01030       if (lstyle == 'U') /* first token checks out */
01031       {
01032         lstyle = 0;
01033         for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
01034         {
01035           /* scan for: (\d+)\s+([A-Z][a-z][a-z])\s+
01036            *  (\d\d\d\d|\d\:\d\d|\d\d\:\d\d|\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
01037            *  \s+(.+)$
01038           */
01039           if (isdigit(*tokens[pos]) /* size */
01040               /* (\w\w\w) */
01041            && toklen[pos+1] == 3 && isalpha(*tokens[pos+1]) &&
01042               isalpha(tokens[pos+1][1]) && isalpha(tokens[pos+1][2])
01043               /* (\d|\d\d) */
01044            && isdigit(*tokens[pos+2]) &&
01045                 (toklen[pos+2] == 1 || 
01046                   (toklen[pos+2] == 2 && isdigit(tokens[pos+2][1])))
01047            && toklen[pos+3] >= 4 && isdigit(*tokens[pos+3]) 
01048               /* (\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
01049            && (toklen[pos+3] <= 5 || (
01050                (toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
01051                (tokens[pos+3][toklen[pos+3]-3]) == ':'))
01052            && isdigit(tokens[pos+3][toklen[pos+3]-2])
01053            && isdigit(tokens[pos+3][toklen[pos+3]-1])
01054            && (
01055               /* (\d\d\d\d) */
01056                  ((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
01057                   isdigit(tokens[pos+3][1]) &&
01058                   isdigit(tokens[pos+3][2])  )
01059               /* (\d\:\d\d|\d\:\d\d\:\d\d) */
01060               || ((toklen[pos+3] == 4 || toklen[pos+3] == 7) && 
01061                   (tokens[pos+3][1]) == ':' &&
01062                   isdigit(tokens[pos+3][2]) && isdigit(tokens[pos+3][3]))
01063               /* (\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
01064               || ((toklen[pos+3] == 5 || toklen[pos+3] == 8) && 
01065                   isdigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
01066                   isdigit(tokens[pos+3][3]) && isdigit(tokens[pos+3][4])) 
01067               )
01068            )
01069           {
01070             lstyle = 'U'; /* assume /bin/ls or variant format */
01071             tokmarker = pos;
01072 
01073             /* check that size is numeric */
01074             p = tokens[tokmarker];
01075             unsigned int i;
01076             for (i = 0; i < toklen[tokmarker]; i++)
01077             {
01078               if (!isdigit(*p++))
01079               {
01080                 lstyle = 0;
01081                 break;
01082               }
01083             }
01084             if (lstyle)
01085             {
01086               month_num = 0;
01087               p = tokens[tokmarker+1];
01088               for (i = 0; i < (12*3); i+=3)
01089               {
01090                 if (p[0] == month_names[i+0] && 
01091                     p[1] == month_names[i+1] && 
01092                     p[2] == month_names[i+2])
01093                   break;
01094                 month_num++;
01095               }
01096               if (month_num >= 12)
01097                 lstyle = 0;
01098             }
01099           } /* relative position test */
01100         } /* for (pos = (numtoks-5); !lstyle && pos > 1; pos--) */
01101       } /* if (lstyle == 'U') */
01102 
01103       if (lstyle == 'U')
01104       {
01105         state->parsed_one = 1;
01106         state->lstyle = lstyle;
01107     
01108         result->fe_cinfs = 0;
01109         result->fe_type = '?';
01110         if (*tokens[0] == 'd' || *tokens[0] == 'l')
01111           result->fe_type = *tokens[0];
01112         else if (*tokens[0] == 'D')
01113           result->fe_type = 'd';
01114         else if (*tokens[0] == '-' || *tokens[0] == 'F')
01115           result->fe_type = 'f'; /* (hopefully a regular file) */
01116 
01117         if (result->fe_type != 'd')
01118         {
01119           pos = toklen[tokmarker];
01120           if (pos > (sizeof(result->fe_size)-1))
01121             pos = (sizeof(result->fe_size)-1);
01122           memcpy( result->fe_size, tokens[tokmarker], pos );
01123           result->fe_size[pos] = '\0';
01124         }
01125 
01126         result->fe_time.tm_month  = month_num;
01127         result->fe_time.tm_mday = atoi(tokens[tokmarker+2]);
01128         if (result->fe_time.tm_mday == 0)
01129           result->fe_time.tm_mday++;
01130 
01131         p = tokens[tokmarker+3];
01132         pos = (unsigned int)atoi(p);
01133         if (p[1] == ':') /* one digit hour */
01134           p--;
01135         if (p[2] != ':') /* year */
01136         {
01137           result->fe_time.tm_year = pos;
01138         }
01139         else
01140         {
01141           result->fe_time.tm_hour = pos;
01142           result->fe_time.tm_min  = atoi(p+3);
01143           if (p[5] == ':')
01144             result->fe_time.tm_sec = atoi(p+6);
01145        
01146           if (!state->now_time)
01147           {
01148             state->now_time = PR_Now();
01149             PR_ExplodeTime((state->now_time), PR_LocalTimeParameters, &(state->now_tm) );
01150           }
01151 
01152           result->fe_time.tm_year = state->now_tm.tm_year;
01153           if ( (( state->now_tm.tm_month << 5) + state->now_tm.tm_mday) <
01154                ((result->fe_time.tm_month << 5) + result->fe_time.tm_mday) )
01155             result->fe_time.tm_year--;
01156        
01157         } /* time/year */
01158         
01159         result->fe_fname = tokens[tokmarker+4];
01160         result->fe_fnlen = (&(line[linelen_sans_wsp]))
01161                            - (result->fe_fname);
01162 
01163         if (result->fe_type == 'l' && result->fe_fnlen > 4)
01164         {
01165           p = result->fe_fname + 1;
01166           for (pos = 1; pos < (result->fe_fnlen - 4); pos++)
01167           {
01168             if (*p == ' ' && p[1] == '-' && p[2] == '>' && p[3] == ' ')
01169             {
01170               result->fe_lname = p + 4;
01171               result->fe_lnlen = (&(line[linelen_sans_wsp]))
01172                                - (result->fe_lname);
01173               result->fe_fnlen = pos;
01174               break;
01175             }
01176             p++;
01177           }
01178         }
01179 
01180 #if defined(SUPPORT_LSLF) /* some (very rare) servers return ls -lF */
01181         if (result->fe_fnlen > 1)
01182         {
01183           p = result->fe_fname[result->fe_fnlen-1];
01184           pos = result->fe_type;
01185           if (pos == 'd') { 
01186              if (*p == '/') result->fe_fnlen--; /* directory */
01187           } else if (pos == 'l') { 
01188              if (*p == '@') result->fe_fnlen--; /* symlink */
01189           } else if (pos == 'f') { 
01190              if (*p == '*') result->fe_fnlen--; /* executable */
01191           } else if (*p == '=' || *p == '%' || *p == '|') {
01192             result->fe_fnlen--; /* socket, whiteout, fifo */
01193           }
01194         }
01195 #endif
01196      
01197         /* the caller should do this (if dropping "." and ".." is desired)
01198         if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
01199             (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
01200                                       result->fe_fname[1] == '.')))
01201           return '?';
01202         */
01203 
01204         return result->fe_type;  
01205 
01206       } /* if (lstyle == 'U') */
01207 
01208     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'U')) */
01209 #endif
01210 
01211     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
01212 
01213 #if defined(SUPPORT_W16) /* 16bit Windows */
01214     if (!lstyle && (!state->lstyle || state->lstyle == 'w'))
01215     {       /* old SuperTCP suite FTP server for Win3.1 */
01216             /* old NetManage Chameleon TCP/IP suite FTP server for Win3.1 */
01217       /*
01218       * SuperTCP dirlist from the mirror.pl project
01219       * mon/day/year separator may be '/' or '-'.
01220       * .               <DIR>           11-16-94        17:16
01221       * ..              <DIR>           11-16-94        17:16
01222       * INSTALL         <DIR>           11-16-94        17:17
01223       * CMT             <DIR>           11-21-94        10:17
01224       * DESIGN1.DOC          11264      05-11-95        14:20
01225       * README.TXT            1045      05-10-95        11:01
01226       * WPKIT1.EXE          960338      06-21-95        17:01
01227       * CMT.CSV                  0      07-06-95        14:56
01228       *
01229       * Chameleon dirlist guessed from lynx
01230       * .               <DIR>      Nov 16 1994 17:16   
01231       * ..              <DIR>      Nov 16 1994 17:16   
01232       * INSTALL         <DIR>      Nov 16 1994 17:17
01233       * CMT             <DIR>      Nov 21 1994 10:17
01234       * DESIGN1.DOC     11264      May 11 1995 14:20   A
01235       * README.TXT       1045      May 10 1995 11:01
01236       * WPKIT1.EXE     960338      Jun 21 1995 17:01   R
01237       * CMT.CSV             0      Jul 06 1995 14:56   RHA
01238       */
01239       if (numtoks >= 4 && toklen[0] < 13 && 
01240           ((toklen[1] == 5 && *tokens[1] == '<') || isdigit(*tokens[1])) )
01241       {
01242         if (numtoks == 4
01243          && (toklen[2] == 8 || toklen[2] == 9)
01244          && (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
01245              ((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
01246          && (toklen[3] == 4 || toklen[3] == 5)
01247          && (tokens[3][toklen[3]-3]) == ':'
01248          && isdigit(tokens[2][0]) && isdigit(tokens[2][1])
01249          && isdigit(tokens[2][3]) && isdigit(tokens[2][4])
01250          && isdigit(tokens[2][6]) && isdigit(tokens[2][7])
01251          && (toklen[2] < 9 || isdigit(tokens[2][8]))
01252          && isdigit(tokens[3][toklen[3]-1]) && isdigit(tokens[3][toklen[3]-2])
01253          && isdigit(tokens[3][toklen[3]-4]) && isdigit(*tokens[3]) 
01254          )
01255         {
01256           lstyle = 'w';
01257         }
01258         else if ((numtoks == 6 || numtoks == 7)
01259          && toklen[2] == 3 && toklen[3] == 2
01260          && toklen[4] == 4 && toklen[5] == 5
01261          && (tokens[5][2]) == ':'
01262          && isalpha(tokens[2][0]) && isalpha(tokens[2][1])
01263          &&                          isalpha(tokens[2][2])
01264          && isdigit(tokens[3][0]) && isdigit(tokens[3][1])
01265          && isdigit(tokens[4][0]) && isdigit(tokens[4][1])
01266          && isdigit(tokens[4][2]) && isdigit(tokens[4][3])
01267          && isdigit(tokens[5][0]) && isdigit(tokens[5][1])
01268          && isdigit(tokens[5][3]) && isdigit(tokens[5][4])
01269          /* could also check that (&(tokens[5][5]) - tokens[2]) == 17 */
01270         )
01271         {
01272           lstyle = 'w';
01273         }
01274         if (lstyle && state->lstyle != lstyle) /* first time */
01275         {
01276           p = tokens[1];   
01277           if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' || 
01278                  p[2] != 'I' || p[3] != 'R' || p[4] != '>')
01279           {
01280             for (pos = 0; lstyle && pos < toklen[1]; pos++)
01281             {
01282               if (!isdigit(*p++))
01283                 lstyle = 0;
01284             }
01285           } /* not <DIR> */
01286         } /* if (first time) */
01287       } /* if (numtoks == ...) */
01288 
01289       if (lstyle == 'w')
01290       {
01291         state->parsed_one = 1;
01292         state->lstyle = lstyle;
01293 
01294         result->fe_cinfs = 1;
01295         result->fe_fname = tokens[0];
01296         result->fe_fnlen = toklen[0];
01297         result->fe_type = 'd';
01298 
01299         p = tokens[1];
01300         if (isdigit(*p))
01301         {
01302           result->fe_type = 'f';
01303           pos = toklen[1];
01304           if (pos > (sizeof(result->fe_size)-1))
01305             pos = sizeof(result->fe_size)-1;
01306           memcpy( result->fe_size, p, pos );
01307           result->fe_size[pos] = '\0';
01308         }
01309 
01310         p = tokens[2];
01311         if (toklen[2] == 3) /* Chameleon */
01312         {
01313           tbuf[0] = toupper(p[0]);
01314           tbuf[1] = tolower(p[1]);
01315           tbuf[2] = tolower(p[2]);
01316           for (pos = 0; pos < (12*3); pos+=3)
01317           {
01318             if (tbuf[0] == month_names[pos+0] &&
01319                 tbuf[1] == month_names[pos+1] && 
01320                 tbuf[2] == month_names[pos+2])
01321             {
01322               result->fe_time.tm_month = pos/3;
01323               result->fe_time.tm_mday = atoi(tokens[3]);
01324               result->fe_time.tm_year = atoi(tokens[4]) - 1900;
01325               break;
01326             }
01327           }          
01328           pos = 5; /* Chameleon toknum of date field */
01329         }
01330         else
01331         {
01332           result->fe_time.tm_month = atoi(p+0)-1;
01333           result->fe_time.tm_mday = atoi(p+3);
01334           result->fe_time.tm_year = atoi(p+6);
01335           if (result->fe_time.tm_year < 80) /* SuperTCP */
01336             result->fe_time.tm_year += 100;
01337 
01338           pos = 3; /* SuperTCP toknum of date field */
01339         }
01340 
01341         result->fe_time.tm_hour = atoi(tokens[pos]);
01342         result->fe_time.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
01343 
01344         /* the caller should do this (if dropping "." and ".." is desired)
01345         if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
01346             (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
01347                                       result->fe_fname[1] == '.')))
01348           return '?';
01349         */
01350 
01351         return result->fe_type;
01352       } /* (lstyle == 'w') */
01353 
01354     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'w'))  */
01355 #endif
01356 
01357     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
01358 
01359 #if defined(SUPPORT_DLS) /* dls -dtR */
01360     if (!lstyle && 
01361        (state->lstyle == 'D' || (!state->lstyle && state->numlines == 1)))
01362        /* /bin/dls lines have to be immediately recognizable (first line) */
01363     {
01364       /* I haven't seen an FTP server that delivers a /bin/dls listing,
01365        * but can infer the format from the lynx and mirror.pl projects.
01366        * Both formats are supported.
01367        *
01368        * Lynx says:
01369        * README              763  Information about this server\0
01370        * bin/                  -  \0
01371        * etc/                  =  \0
01372        * ls-lR                 0  \0
01373        * ls-lR.Z               3  \0
01374        * pub/                  =  Public area\0
01375        * usr/                  -  \0
01376        * morgan               14  -> ../real/morgan\0
01377        * TIMIT.mostlikely.Z\0
01378        *                   79215  \0
01379        *
01380        * mirror.pl says:
01381        * filename:  ^(\S*)\s+
01382        * size:      (\-|\=|\d+)\s+
01383        * month/day: ((\w\w\w\s+\d+|\d+\s+\w\w\w)\s+
01384        * time/year: (\d+:\d+|\d\d\d\d))\s+
01385        * rest:      (.+) 
01386        *
01387        * README              763  Jul 11 21:05  Information about this server
01388        * bin/                  -  Apr 28  1994
01389        * etc/                  =  11 Jul 21:04
01390        * ls-lR                 0   6 Aug 17:14
01391        * ls-lR.Z               3  05 Sep 1994
01392        * pub/                  =  Jul 11 21:04  Public area
01393        * usr/                  -  Sep  7 09:39
01394        * morgan               14  Apr 18 09:39  -> ../real/morgan
01395        * TIMIT.mostlikely.Z
01396        *                   79215  Jul 11 21:04
01397       */
01398       if (!state->lstyle && line[linelen-1] == ':' && 
01399           linelen >= 2 && toklen[numtoks-1] != 1)
01400       { 
01401         /* code in mirror.pl suggests that a listing may be preceded
01402          * by a PWD line in the form "/some/dir/names/here:"
01403          * but does not necessarily begin with '/'. *sigh*
01404         */
01405         pos = 0;
01406         p = line;
01407         while (pos < (linelen-1))
01408         {
01409           /* illegal (or extremely unusual) chars in a dirspec */
01410           if (*p == '<' || *p == '|' || *p == '>' ||
01411               *p == '?' || *p == '*' || *p == '\\')
01412             break;
01413           if (*p == '/' && pos < (linelen-2) && p[1] == '/')
01414             break;
01415           pos++;
01416           p++;
01417         }
01418         if (pos == (linelen-1))
01419         {
01420           state->lstyle = 'D';
01421           return '?';
01422         }
01423       }
01424 
01425       if (!lstyle && numtoks >= 2)
01426       {
01427         pos = 22; /* pos of (\d+|-|=) if this is not part of a multiline */
01428         if (state->lstyle && carry_buf_len) /* first is from previous line */
01429           pos = toklen[1]-1; /* and is 'as-is' (may contain whitespace) */
01430 
01431         if (linelen > pos)
01432         {
01433           p = &line[pos];
01434           if ((*p == '-' || *p == '=' || isdigit(*p)) &&
01435               ((linelen == (pos+1)) || 
01436                (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
01437           {
01438             tokmarker = 1;
01439             if (!carry_buf_len)
01440             {
01441               pos = 1;
01442               while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
01443                 pos++;
01444               tokmarker = 0;
01445               if ((tokens[pos]+toklen[pos]) == (&line[23]))
01446                 tokmarker = pos;
01447             }
01448             if (tokmarker)  
01449             {
01450               lstyle = 'D';
01451               if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
01452               {
01453                 if (toklen[tokmarker] != 1 ||
01454                    (tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
01455                   lstyle = 0;
01456               }              
01457               else
01458               {
01459                 for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++) 
01460                 {
01461                   if (!isdigit(tokens[tokmarker][pos]))
01462                     lstyle = 0; 
01463                 }
01464               }
01465               if (lstyle && !state->lstyle) /* first time */
01466               {
01467                 /* scan for illegal (or incredibly unusual) chars in fname */
01468                 for (p = tokens[0]; lstyle &&
01469                      p < &(tokens[tokmarker-1][toklen[tokmarker-1]]); p++)
01470                 {
01471                   if (*p == '<' || *p == '|' || *p == '>' || 
01472                       *p == '?' || *p == '*' || *p == '/' || *p == '\\')
01473                     lstyle = 0;
01474                 }
01475               }
01476 
01477             } /* size token found */
01478           } /* expected chars behind expected size token */
01479         } /* if (linelen > pos) */
01480       } /* if (!lstyle && numtoks >= 2) */
01481 
01482       if (!lstyle && state->lstyle == 'D' && !carry_buf_len)
01483       {
01484         /* the filename of a multi-line entry can be identified
01485          * correctly only if dls format had been previously established.
01486          * This should always be true because there should be entries
01487          * for '.' and/or '..' and/or CWD that precede the rest of the
01488          * listing.
01489         */
01490         pos = linelen;
01491         if (pos > (sizeof(state->carry_buf)-1))
01492           pos = sizeof(state->carry_buf)-1;
01493         memcpy( state->carry_buf, line, pos );
01494         state->carry_buf_len = pos;
01495         return '?';
01496       }
01497 
01498       if (lstyle == 'D')
01499       {
01500         state->parsed_one = 1;
01501         state->lstyle = lstyle;
01502 
01503         p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
01504         result->fe_fname = tokens[0];
01505         result->fe_fnlen = p - tokens[0];
01506         result->fe_type  = 'f';
01507 
01508         if (result->fe_fname[result->fe_fnlen-1] == '/')
01509         {
01510           if (result->fe_lnlen == 1)
01511             result->fe_type = '?';
01512           else
01513           {
01514             result->fe_fnlen--;
01515             result->fe_type  = 'd';
01516           }
01517         }
01518         else if (isdigit(*tokens[tokmarker]))
01519         {
01520           pos = toklen[tokmarker];
01521           if (pos > (sizeof(result->fe_size)-1))
01522             pos = sizeof(result->fe_size)-1;
01523           memcpy( result->fe_size, tokens[tokmarker], pos );
01524           result->fe_size[pos] = '\0';
01525         }
01526 
01527         if ((tokmarker+3) < numtoks && 
01528               (&(tokens[numtoks-1][toklen[numtoks-1]]) - 
01529                tokens[tokmarker+1]) >= (1+1+3+1+4) )
01530         {
01531           pos = (tokmarker+3);
01532           p = tokens[pos];
01533           pos = toklen[pos];
01534 
01535           if ((pos == 4 || pos == 5)
01536           &&  isdigit(*p) && isdigit(p[pos-1]) && isdigit(p[pos-2])
01537           &&  ((pos == 5 && p[2] == ':') ||  
01538                (pos == 4 && (isdigit(p[1]) || p[1] == ':')))
01539              )
01540           {
01541             month_num = tokmarker+1; /* assumed position of month field */
01542             pos = tokmarker+2;       /* assumed position of mday field */
01543             if (isdigit(*tokens[month_num])) /* positions are reversed */
01544             {
01545               month_num++;
01546               pos--;
01547             }
01548             p = tokens[month_num];
01549             if (isdigit(*tokens[pos]) 
01550             && (toklen[pos] == 1 || 
01551                   (toklen[pos] == 2 && isdigit(tokens[pos][1])))
01552             && toklen[month_num] == 3
01553             && isalpha(*p) && isalpha(p[1]) && isalpha(p[2])  )
01554             {
01555               pos = atoi(tokens[pos]);
01556               if (pos > 0 && pos <= 31)
01557               {
01558                 result->fe_time.tm_mday = pos;
01559                 month_num = 1;
01560                 for (pos = 0; pos < (12*3); pos+=3)
01561                 {
01562                   if (p[0] == month_names[pos+0] &&
01563                       p[1] == month_names[pos+1] &&
01564                       p[2] == month_names[pos+2])
01565                     break;
01566                   month_num++;
01567                 }
01568                 if (month_num > 12)
01569                   result->fe_time.tm_mday = 0;
01570                 else
01571                   result->fe_time.tm_month = month_num - 1;
01572               }
01573             }
01574             if (result->fe_time.tm_mday)
01575             {
01576               tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */
01577               p = tokens[tokmarker];
01578 
01579               pos = atoi(p);
01580               if (pos > 24)
01581                 result->fe_time.tm_year = pos-1900;
01582               else
01583               {
01584                 if (p[1] == ':')
01585                   p--;
01586                 result->fe_time.tm_hour = pos;
01587                 result->fe_time.tm_min = atoi(p+3);
01588                 if (!state->now_time)
01589                 {
01590                   state->now_time = PR_Now();
01591                   PR_ExplodeTime((state->now_time), PR_LocalTimeParameters, &(state->now_tm) );
01592                 }
01593                 result->fe_time.tm_year = state->now_tm.tm_year;
01594                 if ( (( state->now_tm.tm_month  << 4) + state->now_tm.tm_mday) <
01595                      ((result->fe_time.tm_month << 4) + result->fe_time.tm_mday) )
01596                   result->fe_time.tm_year--;
01597               } /* got year or time */
01598             } /* got month/mday */
01599           } /* may have year or time */
01600         } /* enough remaining to possibly have date/time */
01601 
01602         if (numtoks > (tokmarker+2))
01603         {
01604           pos = tokmarker+1;
01605           p = tokens[pos];
01606           if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
01607           {
01608             p = &(tokens[numtoks-1][toklen[numtoks-1]]);
01609             result->fe_type  = 'l';
01610             result->fe_lname = tokens[pos+1];
01611             result->fe_lnlen = p - result->fe_lname;
01612             if (result->fe_lnlen > 1 &&
01613                 result->fe_lname[result->fe_lnlen-1] == '/')
01614               result->fe_lnlen--;
01615           }
01616         } /* if (numtoks > (tokmarker+2)) */
01617 
01618         /* the caller should do this (if dropping "." and ".." is desired)
01619         if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
01620             (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
01621                                       result->fe_fname[1] == '.')))
01622           return '?';
01623         */
01624 
01625         return result->fe_type;
01626 
01627       } /* if (lstyle == 'D') */
01628     } /* if (!lstyle && (!state->lstyle || state->lstyle == 'D')) */
01629 #endif
01630 
01631     /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
01632 
01633   } /* if (linelen > 0) */
01634 
01635   if (state->parsed_one || state->lstyle) /* junk if we fail to parse */
01636     return '?';      /* this time but had previously parsed sucessfully */
01637   return '"';        /* its part of a comment or error message */
01638 }
01639 
01640 /* ==================================================================== */
01641 /* standalone testing                                                   */
01642 /* ==================================================================== */
01643 #if 0
01644 
01645 #include <stdio.h>
01646 
01647 static int do_it(FILE *outfile, 
01648                  char *line, size_t linelen, struct list_state *state,
01649                  char **cmnt_buf, unsigned int *cmnt_buf_sz,
01650                  char **list_buf, unsigned int *list_buf_sz )
01651 {
01652   struct list_result result;
01653   char *p;
01654   int rc;
01655 
01656   rc = ParseFTPLIST( line, state, &result );
01657 
01658   if (!outfile)
01659   {
01660     outfile = stdout;
01661     if (rc == '?')
01662       fprintf(outfile, "junk: %.*s\n", (int)linelen, line );
01663     else if (rc == '"')
01664       fprintf(outfile, "cmnt: %.*s\n", (int)linelen, line );
01665     else
01666       fprintf(outfile, 
01667               "list: %02u-%02u-%02u  %02u:%02u%cM %20s %.*s%s%.*s\n",
01668               (result.fe_time.tm_mday ? (result.fe_time.tm_month + 1) : 0),
01669               result.fe_time.tm_mday,
01670               (result.fe_time.tm_mday ? (result.fe_time.tm_year % 100) : 0),
01671               result.fe_time.tm_hour - 
01672                 ((result.fe_time.tm_hour > 12)?(12):(0)),
01673               result.fe_time.tm_min,
01674               ((result.fe_time.tm_hour >= 12) ? 'P' : 'A'),
01675               (rc == 'd' ? "<DIR>         " : 
01676               (rc == 'l' ? "<JUNCTION>    " : result.fe_size)),
01677               (int)result.fe_fnlen, result.fe_fname,
01678               ((rc == 'l' && result.fe_lnlen) ? " -> " : ""),
01679               (int)((rc == 'l' && result.fe_lnlen) ? result.fe_lnlen : 0),
01680               ((rc == 'l' && result.fe_lnlen) ? result.fe_lname : "") );
01681   }
01682   else if (rc != '?') /* NOT junk */
01683   { 
01684     char **bufp = list_buf;
01685     unsigned int *bufz = list_buf_sz;
01686     
01687     if (rc == '"') /* comment - make it a 'result' */
01688     {
01689       memset( &result, 0, sizeof(result));
01690       result.fe_fname = line;
01691       result.fe_fnlen = linelen;
01692       result.fe_type = 'f';
01693       if (line[linelen-1] == '/')
01694       {
01695         result.fe_type = 'd';
01696         result.fe_fnlen--; 
01697       }
01698       bufp = cmnt_buf;
01699       bufz = cmnt_buf_sz;
01700       rc = result.fe_type;
01701     }  
01702 
01703     linelen = 80 + result.fe_fnlen + result.fe_lnlen;
01704     p = (char *)realloc( *bufp, *bufz + linelen );
01705     if (!p)
01706       return -1;
01707     sprintf( &p[*bufz], 
01708              "%02u-%02u-%04u  %02u:%02u:%02u %20s %.*s%s%.*s\n",
01709               (result.fe_time.tm_mday ? (result.fe_time.tm_month + 1) : 0),
01710               result.fe_time.tm_mday,
01711               (result.fe_time.tm_mday ? (result.fe_time.tm_year + 1900) : 0),
01712               result.fe_time.tm_hour,
01713               result.fe_time.tm_min,
01714               result.fe_time.tm_sec,
01715               (rc == 'd' ? "<DIR>         " : 
01716               (rc == 'l' ? "<JUNCTION>    " : result.fe_size)),
01717               (int)result.fe_fnlen, result.fe_fname,
01718               ((rc == 'l' && result.fe_lnlen) ? " -> " : ""),
01719               (int)((rc == 'l' && result.fe_lnlen) ? result.fe_lnlen : 0),
01720               ((rc == 'l' && result.fe_lnlen) ? result.fe_lname : "") );
01721     linelen = strlen(&p[*bufz]);
01722     *bufp = p;
01723     *bufz = *bufz + linelen;
01724   }
01725   return 0;
01726 }
01727 
01728 int main(int argc, char *argv[])
01729 {
01730   FILE *infile = (FILE *)0;
01731   FILE *outfile = (FILE *)0;
01732   int need_close_in = 0;
01733   int need_close_out = 0;
01734 
01735   if (argc > 1)
01736   {
01737     infile = stdin;
01738     if (strcmp(argv[1], "-") == 0)
01739       need_close_in = 0;
01740     else if ((infile = fopen(argv[1], "r")) != ((FILE *)0))
01741       need_close_in = 1;
01742     else
01743       fprintf(stderr, "Unable to open input file '%s'\n", argv[1]);
01744   }
01745   if (infile && argc > 2)
01746   {
01747     outfile = stdout;
01748     if (strcmp(argv[2], "-") == 0)
01749       need_close_out = 0;
01750     else if ((outfile = fopen(argv[2], "w")) != ((FILE *)0))
01751       need_close_out = 1;
01752     else
01753     {
01754       fprintf(stderr, "Unable to open output file '%s'\n", argv[2]);
01755       fclose(infile);
01756       infile = (FILE *)0;
01757     }
01758   }
01759 
01760   if (!infile)
01761   {
01762     char *appname = &(argv[0][strlen(argv[0])]);
01763     while (appname > argv[0])
01764     {
01765       appname--;
01766       if (*appname == '/' || *appname == '\\' || *appname == ':')
01767       {
01768         appname++;
01769         break;
01770       } 
01771     }
01772     fprintf(stderr, 
01773         "Usage: %s <inputfilename> [<outputfilename>]\n"
01774         "\nIf an outout file is specified the results will be"
01775         "\nbe post-processed, and only the file entries will appear"
01776         "\n(or all comments if there are no file entries)."
01777         "\nNot specifying an output file causes %s to run in \"debug\""
01778         "\nmode, ie results are printed as lines are parsed."
01779         "\nIf a filename is a single dash ('-'), stdin/stdout is used."
01780         "\n", appname, appname );
01781   }
01782   else
01783   {
01784     char *cmnt_buf = (char *)0;
01785     unsigned int cmnt_buf_sz = 0;
01786     char *list_buf = (char *)0;
01787     unsigned int list_buf_sz = 0;
01788 
01789     struct list_state state;
01790     char line[512];
01791 
01792     memset( &state, 0, sizeof(state) );
01793     while (fgets(line, sizeof(line), infile))
01794     {
01795       size_t linelen = strlen(line);
01796       if (linelen < (sizeof(line)-1))
01797       {
01798         if (linelen > 0 && line[linelen-1] == '\n')
01799           linelen--;
01800         if (do_it( outfile, line, linelen, &state, 
01801                    &cmnt_buf, &cmnt_buf_sz, &list_buf, &list_buf_sz) != 0)
01802         {
01803           fprintf(stderr, "Insufficient memory. Listing may be incomplete.\n"); 
01804           break;
01805         }
01806       }
01807       else
01808       {
01809         /* no '\n' found. drop this and everything up to the next '\n' */
01810         fprintf(stderr, "drop: %.*s", (int)linelen, line );
01811         while (linelen == sizeof(line))
01812         {
01813           if (!fgets(line, sizeof(line), infile))
01814             break;
01815           linelen = 0;
01816           while (linelen < sizeof(line) && line[linelen] != '\n')
01817             linelen++;
01818           fprintf(stderr, "%.*s", (int)linelen, line );
01819         }  
01820         fprintf(stderr, "\n");
01821       }
01822     }
01823     if (outfile)
01824     {
01825       if (list_buf)
01826         fwrite( list_buf, 1, list_buf_sz, outfile );
01827       else if (cmnt_buf)
01828         fwrite( cmnt_buf, 1, cmnt_buf_sz, outfile );
01829     }
01830     if (list_buf) 
01831       free(list_buf);
01832     if (cmnt_buf)
01833       free(cmnt_buf);
01834 
01835     if (need_close_in)
01836       fclose(infile);
01837     if (outfile && need_close_out)
01838       fclose(outfile);
01839   }
01840 
01841   return 0;
01842 }
01843 #endif