Back to index

lightning-sunbird  0.9+nobinonly
ParseFTPList.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include "nspr.h"
00039 
00040 /* ParseFTPList() parses lines from an FTP LIST command.
00041 **
00042 ** Written July 2002 by Cyrus Patel <cyp@fb14.uni-mainz.de>
00043 ** with acknowledgements to squid, lynx, wget and ftpmirror.
00044 **
00045 ** Arguments:
00046 **   'line':       line of FTP data connection output. The line is assumed
00047 **                 to end at the first '\0' or '\n' or '\r\n'. 
00048 **   'state':      a structure used internally to track state between 
00049 **                 lines. Needs to be bzero()'d at LIST begin.
00050 **   'result':     where ParseFTPList will store the results of the parse
00051 **                 if 'line' is not a comment and is not junk.
00052 **
00053 ** Returns one of the following:
00054 **    'd' - LIST line is a directory entry ('result' is valid)
00055 **    'f' - LIST line is a file's entry ('result' is valid)
00056 **    'l' - LIST line is a symlink's entry ('result' is valid)
00057 **    '?' - LIST line is junk. (cwd, non-file/dir/link, etc)
00058 **    '"' - its not a LIST line (its a "comment")
00059 **
00060 ** It may be advisable to let the end-user see "comments" (particularly when 
00061 ** the listing results in ONLY such lines) because such a listing may be:
00062 ** - an unknown LIST format (NLST or "custom" format for example)
00063 ** - an error msg (EPERM,ENOENT,ENFILE,EMFILE,ENOTDIR,ENOTBLK,EEXDEV etc).
00064 ** - an empty directory and the 'comment' is a "total 0" line or similar.
00065 **   (warning: a "total 0" can also mean the total size is unknown).
00066 **
00067 ** ParseFTPList() supports all known FTP LISTing formats:
00068 ** - '/bin/ls -l' and all variants (including Hellsoft FTP for NetWare); 
00069 ** - EPLF (Easily Parsable List Format); 
00070 ** - Windows NT's default "DOS-dirstyle";
00071 ** - OS/2 basic server format LIST format;  
00072 ** - VMS (MultiNet, UCX, and CMU) LIST format (including multi-line format);
00073 ** - IBM VM/CMS, VM/ESA LIST format (two known variants);  
00074 ** - SuperTCP FTP Server for Win16 LIST format;  
00075 ** - NetManage Chameleon (NEWT) for Win16 LIST format;  
00076 ** - '/bin/dls' (two known variants, plus multi-line) LIST format;
00077 ** If there are others, then I'd like to hear about them (send me a sample).
00078 **
00079 ** NLSTings are not supported explicitely because they cannot be machine 
00080 ** parsed consistantly: NLSTings do not have unique characteristics - even 
00081 ** the assumption that there won't be whitespace on the line does not hold
00082 ** because some nlistings have more than one filename per line and/or
00083 ** may have filenames that have spaces in them. Moreover, distinguishing
00084 ** between an error message and an NLST line would require ParseList() to
00085 ** recognize all the possible strerror() messages in the world.
00086 */
00087 
00088 
00089 /* #undef anything you don't want to support */
00090 #define SUPPORT_LSL  /* /bin/ls -l and dozens of variations therof */
00091 #define SUPPORT_DLS  /* /bin/dls format (very, Very, VERY rare) */
00092 #define SUPPORT_EPLF /* Extraordinarily Pathetic List Format */
00093 #define SUPPORT_DOS  /* WinNT server in 'site dirstyle' dos */
00094 #define SUPPORT_VMS  /* VMS (all: MultiNet, UCX, CMU-IP) */
00095 #define SUPPORT_CMS  /* IBM VM/CMS,VM/ESA (z/VM and LISTING forms) */
00096 #define SUPPORT_OS2  /* IBM TCP/IP for OS/2 - FTP Server */
00097 #define SUPPORT_W16  /* win16 hosts: SuperTCP or NetManage Chameleon */
00098 
00099 struct list_state
00100 {
00101   void           *magic;        /* to determine if previously initialized */
00102   PRTime         now_time;      /* needed for year determination */
00103   PRExplodedTime now_tm;        /* needed for year determination */
00104   PRInt32        lstyle;        /* LISTing style */
00105   PRInt32        parsed_one;    /* returned anything yet? */
00106   char           carry_buf[84]; /* for VMS multiline */
00107   PRUint32       carry_buf_len; /* length of name in carry_buf */
00108   PRUint32       numlines;      /* number of lines seen */
00109 };
00110 
00111 struct list_result
00112 {
00113   PRInt32           fe_type;      /* 'd'(dir) or 'l'(link) or 'f'(file) */
00114   const char *      fe_fname;     /* pointer to filename */
00115   PRUint32          fe_fnlen;     /* length of filename */
00116   const char *      fe_lname;     /* pointer to symlink name */
00117   PRUint32          fe_lnlen;     /* length of symlink name */
00118   char              fe_size[40];  /* size of file in bytes (<= (2^128 - 1)) */
00119   PRExplodedTime    fe_time;      /* last-modified time */
00120   PRInt32           fe_cinfs;     /* file system is definitely case insensitive */
00121                                   /* (converting all-upcase names may be desirable) */
00122 };
00123 
00124 int ParseFTPList(const char *line, 
00125                  struct list_state *state,
00126                  struct list_result *result );
00127