transfer file from windows

This commit is contained in:
Your Name 2021-07-19 21:12:53 +08:00
parent 6e9efcbfcc
commit 5cecf3b5ba

64
src/main1.c Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <dirent.h> // read dir
#include <regex.h> // regular expression
#include <string.h>
/*
* dirent的属性d_name里存储了app的名字
* c语言没有字符串数组app名dirent*
* app_list[index]->d_name就可以获取文件名
*/
int main() {
// variable declaration for reading directory
DIR* dir = NULL;
int app_max = 50; // c doesn't have dynamic array like "std::vector" in c++, so I set a large size for array
int app_index = 0;
struct dirent* read_result;
struct dirent* app_list[app_max];
const char* app_path = "/home/wwd/CLionProjects/AndroidTest/data/data"; // test, should be /data/data in android
// variable declaration for regular expression
regex_t reg;
int err_code;
int compression_flags = REG_EXTENDED | REG_ICASE;
char err_buff[256];
char* reg_str = "com.android.*";
// set regular expression and handle error
if ((err_code = regcomp(&reg, reg_str, compression_flags)) != 0){
printf("set regular expression failed:\n");
regerror(err_code, &reg, err_buff, sizeof(err_buff));
fprintf(stderr, "%s\n", err_buff);
return -1;
}
// read dir
if ((dir = opendir(app_path)) == NULL){
printf("open dir failed\n");
return -1;
}
// get result using loop
while ((read_result = readdir(dir)) != NULL){
// filter, ignore current dir + parent dir + com.android.* app
if (strcmp(read_result->d_name, ".") == 0
|| strcmp(read_result->d_name, "..") == 0
|| (err_code = regexec(&reg, read_result->d_name, 0, NULL, 0)) == 0)
continue;
// d_type = 4 means it's a directory
if (read_result->d_type == 4){
app_list[app_index] = read_result;
app_index++;
}
}
// print apps' names
printf("we have %d apps\n", app_index);
for (int i = 0; i < app_index; i++){
printf("app %d : %s\n", i, app_list[i]->d_name);
}
regfree(&reg);
return 0;
}