1.优化IP获取代码

This commit is contained in:
payton 2024-01-15 15:07:49 +08:00
parent 4ac4de21ba
commit 6345a8233d

View File

@ -2104,6 +2104,7 @@ SINT32 sf_app_to_cardv_hd_ture(void) {
return s32ret;
}
int sf_check_eth0(void)
{
FILE *fp;
@ -2121,7 +2122,16 @@ int sf_check_eth0(void)
free(output);
return 1;
}
fgets(output, len, fp);
// Check if fgets was successful
if (fgets(output, len, fp) == NULL) {
MLOGE("Error reading from ifconfig output\n");
pclose(fp);
free(output);
return 1;
}
// Check if usb0 exists
if (strstr(output, "eth0") == NULL) {
MLOGI("The network card does not exist\n");
pclose(fp);
@ -2129,7 +2139,15 @@ int sf_check_eth0(void)
return 1;
}
fgets(output, len, fp);
// Read the second line
if (fgets(output, len, fp) == NULL) {
MLOGE("Error reading from ifconfig output\n");
pclose(fp);
free(output);
return 1;
}
// Check if an IP address is assigned
if (strstr(output, "inet ") == NULL) {
MLOGI("The network card exists, but no IP address has been assigned\n");
pclose(fp);
@ -2137,11 +2155,17 @@ int sf_check_eth0(void)
return 1;
}
// Extract IP address
char *ip_address_start = strstr(output, "inet ") + strlen("inet ");
char *ip_address_end = strchr(ip_address_start, ' ');
*ip_address_end = '\0';
MLOGI("IP address of network card eth0: %s\n", ip_address_start);
// Check if ip_address_end is NULL before dereferencing
if (ip_address_end != NULL) {
*ip_address_end = '\0';
MLOGI("IP address of network card eth0: %s\n", ip_address_start);
} else {
MLOGE("Error extracting IP address\n");
}
pclose(fp);
free(output);
@ -2164,7 +2188,16 @@ int sf_check_usb0(void)
free(output);
return 1;
}
fgets(output, len, fp);
// Check if fgets was successful
if (fgets(output, len, fp) == NULL) {
MLOGE("Error reading from ifconfig output\n");
pclose(fp);
free(output);
return 1;
}
// Check if usb0 exists
if (strstr(output, "usb0") == NULL) {
MLOGI("The network card does not exist\n");
pclose(fp);
@ -2172,7 +2205,15 @@ int sf_check_usb0(void)
return 1;
}
fgets(output, len, fp);
// Read the second line
if (fgets(output, len, fp) == NULL) {
MLOGE("Error reading from ifconfig output\n");
pclose(fp);
free(output);
return 1;
}
// Check if an IP address is assigned
if (strstr(output, "inet ") == NULL) {
MLOGI("The network card exists, but no IP address has been assigned\n");
pclose(fp);
@ -2180,11 +2221,17 @@ int sf_check_usb0(void)
return 1;
}
// Extract IP address
char *ip_address_start = strstr(output, "inet ") + strlen("inet ");
char *ip_address_end = strchr(ip_address_start, ' ');
*ip_address_end = '\0';
MLOGI("IP address of network card usb0: %s\n", ip_address_start);
// Check if ip_address_end is NULL before dereferencing
if (ip_address_end != NULL) {
*ip_address_end = '\0';
MLOGI("IP address of network card usb0: %s\n", ip_address_start);
} else {
MLOGE("Error extracting IP address\n");
}
pclose(fp);
free(output);