#include <stdio.h>
#include<string.h>
#define bool char
#define OK 0
#define ERROR (-1)
typedef enum boolean
{
FALSE = 0,
TRUE = 1,
}bool_e;
typedef struct
{
char hostname[20];
char hostport[6];
char username[20];
char password[20];
char filepath[40];
char localname[20];
int localport;
}ftp_info_t;
const char fp[10] = "update.sp4";
static ftp_info_t ftp_info;
static bool_e ftpupdate = 0;
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;
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)
{
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
{
if (end != NULL)
{
memcpy(ftp_info.hostname, start, end - start);
}
else
{
memcpy(ftp_info.hostname, start, inlen - (start - ftpinfo));
}
}
}
else
{
if (ccln != NULL)
{
if (((mid - start) <= 5) && ((mid - start) > 0))
memcpy(ftp_info.hostport, start, mid - start);
}
else
{
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)
{
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)
{
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;
}
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);
}
}
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';
} else {
dirpath[0] = '\0';
}
}
int main () {
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];
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;
}