FTP列表格式

fri*_*iwi 5 ftp

我正在编写一个嵌入式ftp服务器,但我无法正确获取列表格式.服务器完全可以工作,只有像FileZilla这样的程序无法解释列表格式.这是一个示例列表:

-rwxr--r--  1   owner   group 640   1970 01 01  test
-rwxr--r--  1   owner   group 13440 1970 01 01  test.html
-rwxr--r--  1   owner   group 512   1970 01 01  test2.txt
Run Code Online (Sandbox Code Playgroud)

基本上是:

permissions[tab]number?[tab]owner[tab]group[tab]filesize[tab]date[tab]filename 
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢,Yvan

t0m*_*13b 7

既然你没有指定编程语言,我想我会给我的2cents ....

permissions[tab]number?[tab]owner[tab]group[tab]filesize[tab]date[tab]filename 
                ^^^^^^^                                      ^^^^
             no of inodes                      Dates can vary, it can be year on its own or Month, Day

我决定在下面展示C#regexp,这可以根据您的需求进行调整,

            private Regex ftpUnixListInfo = new Regex(
                @"(?" +
                @"(?[-|d|r|w|x]+)\s+" +
                @"(?\d+)\s*" +
                @"(?\w+)?\s+" +
                @"(?\w+)\s*" +
                @"(?\d+)\s+" +
                @"(?\w+)\s+" +
                @"(?\d{1,2})\s+" +
                @"(?:(?\d{2}\:\d{2})|(?\d{4}))\s+" +
                @"(?.+))",
                RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled);

            // Regex for Microsoft FTP Server
            private Regex ftpMsListInfo = new Regex(
                @"(?" +
                @"(?\d+-\d+-\d+)\s+" +
                @"(?\d+\:\d+(AM|PM))\s*" +
                @"(?((?\)|(?\d+))\s*)" +
                @"(?\w+))",
                RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled);

注意事实,没有标签,它纯粹是空格......并且要小心,一些FTP客户端可以将列表读作MSDOS或Unix ...


tom*_*gic 7

正如其他人已经提到的那样,您需要使用空格而不是制表符.这是来自另一个应该工作的嵌入式FTP服务器的sprintf:

sprintf(line, "%s   1 %-10s %-10s %10lu Jan  1  1980 %s\r\n",
    permstr, username, username,
    length,
    filename);
Run Code Online (Sandbox Code Playgroud)

permstr设置为类似的字符串"-rw-rw-rw-".

至于日期格式,这两个应该有效,如果日期超过6个月,则使用顶部:

if (dfmt)
    sprintf(buf, "%3.3s %2d  %04d", month_name, month_num, year);
else
    sprintf(buf, "%3.3s %2d %02d:%02d", month_name, month_num, hour, minute);
Run Code Online (Sandbox Code Playgroud)