diff --git a/primary/pr/src/main.c b/primary/pr/src/main.c index 10bc340c9c6b67bc60b883e97e8ad8ff09edd94d..c9c9c734a83e54548a31be5bf3dd9e50c43501f4 100644 --- a/primary/pr/src/main.c +++ b/primary/pr/src/main.c @@ -1,42 +1,101 @@ -/* - * main.c - * - * This program runs on Linux and UNIX-like macOS. - * - * Compile and run the program: - * gcc this-file - * ./a.out - */ - -#include +#include #include -#include +#include +#include #include +#include +#include // For isalpha and isalnum +#include // For stat -int main() { - // 其名称设置成Gitee-ID的文件将置于本程序的上级目录 - DIR *dir = opendir(".."); - if (dir == NULL) { - perror("Unable to open parent directory"); - return 1; +// Function to check if a file is a regular file using stat +int is_regular_file(const char *path) { + struct stat path_stat; + if (stat(path, &path_stat) != 0) { + perror("stat"); + return 0; } + return S_ISREG(path_stat.st_mode); +} + +// Function to validate Gitee ID +int isValidGiteeID(const char *name) { + if (strlen(name) < 2 || !isalpha((unsigned char)name[0])) { + return 0; + } + for (size_t i = 1; i < strlen(name); ++i) { + if (!isalnum((unsigned char)name[i]) && name[i] != '_' && name[i] != '-') { + return 0; + } + } + return 1; +} +int main() { + DIR *dir; struct dirent *entry; + char path[PATH_MAX]; + + dir = opendir("."); + if (dir == NULL) { + perror("opendir"); + return EXIT_FAILURE; + } while ((entry = readdir(dir)) != NULL) { - // 0. Gitee-ID命名规则:只允许字母、数字或者下划线(_)、 - // 中划线(-),至少 2 个字符,必须以字母开头, - // 不能以特殊字符结尾。 - // 1. 本程序只处理常规文件(排除目录、软链接等)。 - // 2. 本程序会排除“.”和“..”目录项,也会排除带有扩展名的文件。 - if (entry->d_type == DT_REG && - strchr(entry->d_name, '.') == NULL) { - printf("Greetings from %s!\n", entry->d_name); + snprintf(path, sizeof(path), "%s/%s", ".", entry->d_name); + + // Use is_regular_file instead of checking d_type + if (is_regular_file(path)) { + if (strchr(entry->d_name, '.') == NULL && isValidGiteeID(entry->d_name)) { + printf("Valid Gitee ID file: %s\n", entry->d_name); + // Process the file... + } } } closedir(dir); - - printf(":)\n"); - return 0; + return EXIT_SUCCESS; } + +// /* +// * main.c +// * +// * This program runs on Linux and UNIX-like macOS. +// * +// * Compile and run the program: +// * gcc this-file +// * ./a.out +// */ + +// #include +// #include +// #include +// #include + +// int main() { +// // 其名称设置成Gitee-ID的文件将置于本程序的上级目录 +// DIR *dir = opendir(".."); +// if (dir == NULL) { +// perror("Unable to open parent directory"); +// return 1; +// } + +// struct dirent *entry; + +// while ((entry = readdir(dir)) != NULL) { +// // 0. Gitee-ID命名规则:只允许字母、数字或者下划线(_)、 +// // 中划线(-),至少 2 个字符,必须以字母开头, +// // 不能以特殊字符结尾。 +// // 1. 本程序只处理常规文件(排除目录、软链接等)。 +// // 2. 本程序会排除“.”和“..”目录项,也会排除带有扩展名的文件。 +// if (entry->d_type == DT_REG && +// strchr(entry->d_name, '.') == NULL) { +// printf("Greetings from %s!\n", entry->d_name); +// } +// } + +// closedir(dir); + +// printf(":)\n"); +// return 0; +// } diff --git a/yang-nanqian.txt b/yang-nanqian.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391