From 2c9d2b6933d5e3434fb9474aa513fd613a449881 Mon Sep 17 00:00:00 2001 From: yang-nanqian <786058091@qq.com> Date: Sat, 28 Dec 2024 14:50:30 +0800 Subject: [PATCH 1/3] Add file name after my Gitee ID --- yang-nanqian.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 yang-nanqian.txt diff --git a/yang-nanqian.txt b/yang-nanqian.txt new file mode 100644 index 0000000..e69de29 -- Gitee From 0b90f2fd7e5b6e097aa6632d252de872a4db09ca Mon Sep 17 00:00:00 2001 From: Serien <786058091@qq.com> Date: Mon, 30 Dec 2024 14:55:29 +0000 Subject: [PATCH 2/3] update primary/pr/src/main.c. Signed-off-by: Serien <786058091@qq.com> --- primary/pr/src/main.c | 117 +++++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 29 deletions(-) diff --git a/primary/pr/src/main.c b/primary/pr/src/main.c index 10bc340..fb4073f 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(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; +// } -- Gitee From 2b0ba31280d781f09ea7b77bd081de2762e1c698 Mon Sep 17 00:00:00 2001 From: Serien <786058091@qq.com> Date: Mon, 30 Dec 2024 14:59:57 +0000 Subject: [PATCH 3/3] update primary/pr/src/main.c. Signed-off-by: Serien <786058091@qq.com> --- primary/pr/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primary/pr/src/main.c b/primary/pr/src/main.c index fb4073f..c9c9c73 100644 --- a/primary/pr/src/main.c +++ b/primary/pr/src/main.c @@ -19,7 +19,7 @@ int is_regular_file(const char *path) { // Function to validate Gitee ID int isValidGiteeID(const char *name) { - if (strlen(name) < 2 || !isalpha(name[0])) { + if (strlen(name) < 2 || !isalpha((unsigned char)name[0])) { return 0; } for (size_t i = 1; i < strlen(name); ++i) { -- Gitee