1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ----- BEGIN LICENSE BLOCK -----
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Initial Developer of the Original Code is
16
* Cyrus Patel <cyp@fb14.uni-mainz.de>
17
* Portions created by the Initial Developer are Copyright (C) 2002
18
* the Initial Developer. All Rights Reserved.
20
* Contributor(s): Doug Turner <dougt@netscape.com>
22
* Alternatively, the contents of this file may be used under the terms of
23
* either of the GNU General Public License Version 2 or later (the "GPL"),
24
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25
* in which case the provisions of the GPL or the LGPL are applicable instead
26
* of those above. If you wish to allow use of your version of this file only
27
* under the terms of either the GPL or the LGPL, and not to allow others to
28
* use your version of this file under the terms of the MPL, indicate your
29
* decision by deleting the provisions above and replace them with the notice
30
* and other provisions required by the LGPL or the GPL. If you do not delete
31
* the provisions above, a recipient may use your version of this file under
32
* the terms of any one of the MPL, the GPL or the LGPL.
34
* ----- END LICENSE BLOCK ----- */
37
/* ParseFTPList() parses lines from an FTP LIST command.
39
** Written July 2002 by Cyrus Patel <cyp@fb14.uni-mainz.de>
40
** with acknowledgements to squid, lynx, wget and ftpmirror.
43
** 'line': line of FTP data connection output. The line is assumed
44
** to end at the first '\0' or '\n' or '\r\n'.
45
** 'state': a structure used internally to track state between
46
** lines. Needs to be bzero()'d at LIST begin.
47
** 'result': where ParseFTPList will store the results of the parse
48
** if 'line' is not a comment and is not junk.
50
** Returns one of the following:
51
** 'd' - LIST line is a directory entry ('result' is valid)
52
** 'f' - LIST line is a file's entry ('result' is valid)
53
** 'l' - LIST line is a symlink's entry ('result' is valid)
54
** '?' - LIST line is junk. (cwd, non-file/dir/link, etc)
55
** '"' - its not a LIST line (its a "comment")
57
** It may be advisable to let the end-user see "comments" (particularly when
58
** the listing results in ONLY such lines) because such a listing may be:
59
** - an unknown LIST format (NLST or "custom" format for example)
60
** - an error msg (EPERM,ENOENT,ENFILE,EMFILE,ENOTDIR,ENOTBLK,EEXDEV etc).
61
** - an empty directory and the 'comment' is a "total 0" line or similar.
62
** (warning: a "total 0" can also mean the total size is unknown).
64
** ParseFTPList() supports all known FTP LISTing formats:
65
** - '/bin/ls -l' and all variants (including Hellsoft FTP for NetWare);
66
** - EPLF (Easily Parsable List Format);
67
** - Windows NT's default "DOS-dirstyle";
68
** - OS/2 basic server format LIST format;
69
** - VMS (MultiNet, UCX, and CMU) LIST format (including multi-line format);
70
** - IBM VM/CMS, VM/ESA LIST format (two known variants);
71
** - SuperTCP FTP Server for Win16 LIST format;
72
** - NetManage Chameleon (NEWT) for Win16 LIST format;
73
** - '/bin/dls' (two known variants, plus multi-line) LIST format;
74
** If there are others, then I'd like to hear about them (send me a sample).
76
** NLSTings are not supported explicitely because they cannot be machine
77
** parsed consistantly: NLSTings do not have unique characteristics - even
78
** the assumption that there won't be whitespace on the line does not hold
79
** because some nlistings have more than one filename per line and/or
80
** may have filenames that have spaces in them. Moreover, distinguishing
81
** between an error message and an NLST line would require ParseList() to
82
** recognize all the possible strerror() messages in the world.
86
/* #undef anything you don't want to support */
87
#define SUPPORT_LSL /* /bin/ls -l and dozens of variations therof */
88
#define SUPPORT_DLS /* /bin/dls format (very, Very, VERY rare) */
89
#define SUPPORT_EPLF /* Extraordinarily Pathetic List Format */
90
#define SUPPORT_DOS /* WinNT server in 'site dirstyle' dos */
91
#define SUPPORT_VMS /* VMS (all: MultiNet, UCX, CMU-IP) */
92
#define SUPPORT_CMS /* IBM VM/CMS,VM/ESA (z/VM and LISTING forms) */
93
#define SUPPORT_OS2 /* IBM TCP/IP for OS/2 - FTP Server */
94
#define SUPPORT_W16 /* win16 hosts: SuperTCP or NetManage Chameleon */
98
void *magic; /* to determine if previously initialized */
99
PRTime now_time; /* needed for year determination */
100
PRExplodedTime now_tm; /* needed for year determination */
101
PRInt32 lstyle; /* LISTing style */
102
PRInt32 parsed_one; /* returned anything yet? */
103
char carry_buf[84]; /* for VMS multiline */
104
PRUint32 carry_buf_len; /* length of name in carry_buf */
105
PRUint32 numlines; /* number of lines seen */
110
PRInt32 fe_type; /* 'd'(dir) or 'l'(link) or 'f'(file) */
111
const char * fe_fname; /* pointer to filename */
112
PRUint32 fe_fnlen; /* length of filename */
113
const char * fe_lname; /* pointer to symlink name */
114
PRUint32 fe_lnlen; /* length of symlink name */
115
char fe_size[40]; /* size of file in bytes (<= (2^128 - 1)) */
116
PRExplodedTime fe_time; /* last-modified time */
117
PRInt32 fe_cinfs; /* file system is definitely case insensitive */
118
/* (converting all-upcase names may be desirable) */
121
int ParseFTPList(const char *line,
122
struct list_state *state,
123
struct list_result *result );