Вот так делаю я:
Код
/*******************************************************************************
List current directory (or a specified file/directory)
*******************************************************************************/
BOOL FTPS_CmdLIST(CHAR *args)
{
/* if not PASV mode, we need a PORT comand first */
if((FALSE == g_PassiveMode) &&
((0 == g_DestDataPort) || (0 == g_DestDataIP)))
{
FTPS_SendResponse("503 Bad sequence of commands.");
return TRUE;
}
/* if client did not specify a directory, use current dir */
if(0 == *args)
args = g_CurrPath;
/* create file system directory object and try to open args dir */
FRESULT res;
if(FR_OK != (res = f_opendir(&g_Dir, args)))
{
FTPS_SendResponse("550 Can not open directory. FS error %d", res);
return TRUE;
}
/* Create data connection to transfer directory list */
TCP_SOCKET *socket;
g_ServerError = FTPS_RES_FAILED_CREATE_DATA_SOCKET;
if(NULL == (socket = FTPS_CreateDataConnection(FTPS_XFER_ASCII)))
return FALSE; /* Close session */
g_ServerError = FTPS_RES_OK;
/* Fill upper dir entry */
g_FileInfo.fattrib = AM_DIR;
g_FileInfo.fsize = 0;
strcpy(g_FileInfo.fname, "..");
/* Send directory list */
do
{
/* Prepare buffer */
CHAR *pp = g_DataTxBuf;
UINT16 total_len = 0;
/* put file object attribute (dir/file) */
if(g_FileInfo.fattrib & AM_DIR)
pp[0] = 'd';
else
pp[0] = '-';
pp++;
total_len++;
/* put file access attributes */
UINT16 i = sprintf(pp, "rw-rw-rw- 1 0 0 %6lu ", g_FileInfo.fsize);
pp += i;
total_len += i;
/* Put file creation date */
i = sprintf(pp, "%.3s %02u %02u:%02u ", "Mar", 26, 0, 0);
pp += i;
total_len += i;
/* Put file name */
i = sprintf(pp, "%s\r\n", g_FileInfo.fname);
pp += i;
total_len += i;
/* Send item in directory list */
UINT16 transmitted;
if(NET_RES_OK != TCP_Transmit(socket, 1000, g_DataTxBuf, total_len, &transmitted))
{
FTPS_SendResponse("421 Failed to transmit data socket!");
FTPS_CloseDataConnection();
return TRUE;
}
}
while((FR_OK == f_readdir(&g_Dir, &g_FileInfo)) && g_FileInfo.fname[0]);
/* Send end of list */
sprintf(g_DataTxBuf, "\r\n\r\n");
UINT16 transmitted;
if(NET_RES_OK != TCP_Transmit(socket, 1000, g_DataTxBuf, 4, &transmitted))
{
FTPS_SendResponse("421 Failed to transmit data socket!");
FTPS_CloseDataConnection();
return TRUE;
}
/* OK */
FTPS_CloseDataConnection();
FTPS_SendResponse("220 OK!");
return TRUE;
}
Это код из готового проекта с ФТП сервером. Но смысл, думаю понятен.