ParseFTPList.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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. #ifndef ParseRTPList_h___
  6. #define ParseRTPList_h___
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include "prtime.h"
  10. /* ParseFTPList() parses lines from an FTP LIST command.
  11. **
  12. ** Written July 2002 by Cyrus Patel <cyp@fb14.uni-mainz.de>
  13. ** with acknowledgements to squid, lynx, wget and ftpmirror.
  14. **
  15. ** Arguments:
  16. ** 'line': line of FTP data connection output. The line is assumed
  17. ** to end at the first '\0' or '\n' or '\r\n'.
  18. ** 'state': a structure used internally to track state between
  19. ** lines. Needs to be bzero()'d at LIST begin.
  20. ** 'result': where ParseFTPList will store the results of the parse
  21. ** if 'line' is not a comment and is not junk.
  22. **
  23. ** Returns one of the following:
  24. ** 'd' - LIST line is a directory entry ('result' is valid)
  25. ** 'f' - LIST line is a file's entry ('result' is valid)
  26. ** 'l' - LIST line is a symlink's entry ('result' is valid)
  27. ** '?' - LIST line is junk. (cwd, non-file/dir/link, etc)
  28. ** '"' - its not a LIST line (its a "comment")
  29. **
  30. ** It may be advisable to let the end-user see "comments" (particularly when
  31. ** the listing results in ONLY such lines) because such a listing may be:
  32. ** - an unknown LIST format (NLST or "custom" format for example)
  33. ** - an error msg (EPERM,ENOENT,ENFILE,EMFILE,ENOTDIR,ENOTBLK,EEXDEV etc).
  34. ** - an empty directory and the 'comment' is a "total 0" line or similar.
  35. ** (warning: a "total 0" can also mean the total size is unknown).
  36. **
  37. ** ParseFTPList() supports all known FTP LISTing formats:
  38. ** - '/bin/ls -l' and all variants (including Hellsoft FTP for NetWare);
  39. ** - EPLF (Easily Parsable List Format);
  40. ** - Windows NT's default "DOS-dirstyle";
  41. ** - OS/2 basic server format LIST format;
  42. ** - VMS (MultiNet, UCX, and CMU) LIST format (including multi-line format);
  43. ** - IBM VM/CMS, VM/ESA LIST format (two known variants);
  44. ** - SuperTCP FTP Server for Win16 LIST format;
  45. ** - NetManage Chameleon (NEWT) for Win16 LIST format;
  46. ** - '/bin/dls' (two known variants, plus multi-line) LIST format;
  47. ** If there are others, then I'd like to hear about them (send me a sample).
  48. **
  49. ** NLSTings are not supported explicitely because they cannot be machine
  50. ** parsed consistently: NLSTings do not have unique characteristics - even
  51. ** the assumption that there won't be whitespace on the line does not hold
  52. ** because some nlistings have more than one filename per line and/or
  53. ** may have filenames that have spaces in them. Moreover, distinguishing
  54. ** between an error message and an NLST line would require ParseList() to
  55. ** recognize all the possible strerror() messages in the world.
  56. */
  57. /* #undef anything you don't want to support */
  58. #define SUPPORT_LSL /* /bin/ls -l and dozens of variations therof */
  59. #define SUPPORT_DLS /* /bin/dls format (very, Very, VERY rare) */
  60. #define SUPPORT_EPLF /* Extraordinarily Pathetic List Format */
  61. #define SUPPORT_DOS /* WinNT server in 'site dirstyle' dos */
  62. #define SUPPORT_VMS /* VMS (all: MultiNet, UCX, CMU-IP) */
  63. #define SUPPORT_CMS /* IBM VM/CMS,VM/ESA (z/VM and LISTING forms) */
  64. #define SUPPORT_OS2 /* IBM TCP/IP for OS/2 - FTP Server */
  65. #define SUPPORT_W16 /* win16 hosts: SuperTCP or NetManage Chameleon */
  66. struct list_state
  67. {
  68. list_state() {
  69. memset(this, 0, sizeof(*this));
  70. }
  71. PRTime now_time; /* needed for year determination */
  72. PRExplodedTime now_tm; /* needed for year determination */
  73. int32_t lstyle; /* LISTing style */
  74. int32_t parsed_one; /* returned anything yet? */
  75. char carry_buf[84]; /* for VMS multiline */
  76. uint32_t carry_buf_len; /* length of name in carry_buf */
  77. uint32_t numlines; /* number of lines seen */
  78. };
  79. struct list_result
  80. {
  81. int32_t fe_type; /* 'd'(dir) or 'l'(link) or 'f'(file) */
  82. const char * fe_fname; /* pointer to filename */
  83. uint32_t fe_fnlen; /* length of filename */
  84. const char * fe_lname; /* pointer to symlink name */
  85. uint32_t fe_lnlen; /* length of symlink name */
  86. char fe_size[40]; /* size of file in bytes (<= (2^128 - 1)) */
  87. PRExplodedTime fe_time; /* last-modified time */
  88. int32_t fe_cinfs; /* file system is definitely case insensitive */
  89. /* (converting all-upcase names may be desirable) */
  90. };
  91. int ParseFTPList(const char *line,
  92. struct list_state *state,
  93. struct list_result *result );
  94. #endif /* !ParseRTPList_h___ */