编辑代码

#include <stdio.h>
#include<string.h>
#define bool  char


#define OK      0


#define ERROR       (-1)


typedef enum  boolean
{
   FALSE  = 0,
   TRUE   = 1,
}bool_e;
/*-----------------------------------------------------------------------------
 Section: Type Definitions
 ----------------------------------------------------------------------------*/
typedef struct
{
    char     hostname[20];      /**< 文件路径IP地址 */
    char    hostport[6];       /**< 文件路径端口号 */
    char     username[20];      /**< FTP用户名 */
    char     password[20];      /**< FTP密码 */
    char     filepath[40];      /**< 文件路径 */
    char     localname[20];     /**< 本地IP地址*/
    int   localport;         /**< 本地端口号*/

}ftp_info_t;

const char fp[10] = "update.sp4";/*默认文件路径为update.sp4*/
/*-----------------------------------------------------------------------------
 Section: Constant Definitions
 -----------------------------------------------------------------------------*/
static ftp_info_t ftp_info;

/*当前是否处在本地升级中*/
static bool_e ftpupdate = 0;
/*当前是否启动FTP升级*/
static bool_e startftp = 0;
static int ftp_start_tick = 0;
static int ftp_err_cnt = 0;



char ftp_date(char *ftpinfo, int len)
{
    if (1)
    {
        int inlen = 0;
        bool usrflag = FALSE;
        bool pasflag = FALSE;
        char* sslh = NULL;
        char* ccln = NULL;
        char* start = NULL;
        char* end = NULL;
        char* mid = NULL;
        memset((char*) &ftp_info, 0, sizeof(ftp_info));
        start = ftpinfo + 6;/*从"ftp://"之后的位置开始解析*/
        inlen = len;
        end = strchr(start, 0x40);/*查找@*/
        if (end != NULL)
        {/*存在@的情况*/
            usrflag = TRUE;
            mid = strchr(start, 0x3a);/*查找冒号*/
            if ((mid != NULL)&&((end - mid - 1) >= 0))
            {
                memcpy(ftp_info.username, start, mid - start);
                memcpy(ftp_info.password, mid + 1, end - mid -1);
            }
            else
            {
                memcpy(ftp_info.username, start, end - start);
                *ftp_info.password = 0;
            }
            start = end + 1;/*起始指针指到@后面*/
        }
        else
        {/*不存在@的情况*/
            usrflag = FALSE;
        }
        ccln = strchr(start, 0x3a);/*查找冒号*/
        if (ccln != NULL)
        {/*存在冒号*/
            memcpy(ftp_info.hostname, start, ccln - start);
            start = ccln + 1;/*起始指针指到冒号之后*/
        }
        end = strchr(start, 0x2c);/*查找逗号*/
        if (end == NULL)
        {/*没有逗号*/
            pasflag = FALSE;
        }
        else
        {/*有逗号*/
            pasflag = TRUE;
            sslh = strchr(end + 1, 0x2f);/*查找斜杆*/
            if (usrflag == FALSE)
            {
                if (sslh != NULL)
                {
                    memcpy(ftp_info.username, end + 1, sslh - (end + 1));
                    memcpy(ftp_info.password, sslh + 1, inlen - (sslh - ftpinfo) - 1);
                }
                else
                {
                    memcpy(ftp_info.username, end + 1, inlen - (end - ftpinfo) - 1);
                    *ftp_info.password = 0;
                }
            }
        }
        mid = strchr(start, 0x2f);/*查找逗号之前的斜杆*/
        if ((mid == sslh)||(mid == NULL))
        {/*斜杆不存在*/
            if (ccln != NULL)
            {/*冒号存在,获取hostport*/
                if (end != NULL)
                {
                    if (((end - ccln -1) <= 5)&&((end - ccln -1) > 0))
                    memcpy(ftp_info.hostport, ccln + 1, end - ccln -1);
                }
                else
                {
                    if (((inlen - (ccln - ftpinfo) - 1) <= 5)&&((inlen - (ccln - ftpinfo) - 1) > 0))
                    memcpy(ftp_info.hostport, ccln + 1, inlen - (ccln - ftpinfo) - 1);
                }
            }
            else
            {/*冒号不存在,获取hostname*/
                if (end != NULL)
                {
                    memcpy(ftp_info.hostname, start, end - start);
                }
                else
                {
                    memcpy(ftp_info.hostname, start, inlen - (start - ftpinfo));
                }
            }
        }
        else
        {/*斜杆存在*/
            if (ccln != NULL)
            {/*冒号存在,获取hostport*/
                if (((mid - start) <= 5) && ((mid - start) > 0))
                    memcpy(ftp_info.hostport, start, mid - start);
            }
            else
            {/*冒号不存在,获取hostname*/
                memcpy(ftp_info.hostname, start, mid - start);
            }
            if (end != NULL)
            {
                memcpy(ftp_info.filepath, mid + 1, end - (mid + 1));
            }
            else
            {
                memcpy(ftp_info.filepath, mid + 1, inlen - (mid - ftpinfo) - 1);
            }
        }

        extract_filename();

        if ((usrflag == FALSE) && (pasflag == FALSE))
        {/*未对用户名和密码赋值,那么默认用户名密码为空*/
            *ftp_info.username = 0;
            *ftp_info.password = 0;
        }
        if (*ftp_info.filepath == 0)
        {/*未对filepath赋值,那么使用默认文件路径*/
            memcpy(ftp_info.filepath, fp, 10);
        }
        int pos = strlen(ftp_info.filepath);
        if ((pos > 0) && (pos < 30))
        {
            if (ftp_info.filepath[pos - 1] == '/')
            {
                strcpy(ftp_info.filepath + pos, "update.sp4");
            }
        }
        if (*ftp_info.hostport == 0)
        {/*未对端口号赋值,使用默认端口号21*/

            memcpy(ftp_info.hostport, "21", 2);
        }

        printf("ftp_hostname:%s\n,ftp_hostport:%s\n,ftp_filepath:%s\n",ftp_info.hostname,ftp_info.hostport,ftp_info.filepath);
        printf("ftp_usrname:%s\n,ftp_password:%s\n",ftp_info.username,ftp_info.password);
        startftp = 1;
        ftp_err_cnt = 0;
        return OK;
    }
    else
        return ERROR;

}

// 提取文件名(如 "upgrade.tar.gz")
void extract_filename(const char *path, char *filename) {
    const char *last_slash = strrchr(path, '/'); // 找到最后一个 '/'
    if (last_slash != NULL) {
        strcpy(filename, last_slash + 1); // 复制最后一个 '/' 之后的内容
    } else {
        strcpy(filename, path); // 如果没有 '/',整个路径就是文件名
    }
}

// 提取目录路径(如 "TEST/TEST/V1.0.0.0/")
void extract_dirpath(const char *path, char *dirpath) {
    const char *last_slash = strrchr(path, '/'); // 找到最后一个 '/'
    if (last_slash != NULL) {
        strncpy(dirpath, path, last_slash - path + 1); // 复制到最后一个 '/' 的位置
        dirpath[last_slash - path + 1] = '\0'; // 确保以 '\0' 结尾
    } else {
        dirpath[0] = '\0'; // 如果没有 '/',目录路径为空
    }
}

int main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 、
    char buf[100] = "ftp://cdpt:cdpt2024@8.210.150.0:21/TEST/TEST/V1.0.0.0/upgrade.tar.gz";
    ftp_date(buf,strlen(buf));
    printf("Hello world!     - c.jsrun.net.");

    const char *path = "TEST/TEST/V1.0.0.0/upgrade.tar.gz";
    char filename[256]; // 假设文件名不超过255个字符
    char filepath[256];
    extract_filename(path, filename);
    extract_dirpath(path, filepath);
    printf("原始路径: %s\n", path);
    printf("提取的文件名: %s\n", filename);
    printf("提取的文件路径: %s\n", filepath);
    return 0;
}