C正则表达式验证文件夹下的文件名
C regex validate filename under a folder
我不熟悉 C 中的正则表达式,我正在尝试使用 regex.h
库使用正则表达式查找给定文件名是否在文件夹下。这是我试过的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
int checkregex(char regex_str[100], char test[100]) {
regex_t regex;
printf("regex_str: %s\n\n", regex_str);
int reti = regcomp(®ex, regex_str, REG_EXTENDED | REG_ICASE);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, test, 0, NULL, REG_EXTENDED | REG_ICASE);
regfree(®ex);
return reti;
}
void main(int argc, char *argv[]) {
const char *safepath = "/home";
size_t spl = strlen(safepath);
char *fn = argv[1];
int noDoubleDots = checkregex("[^..\/]", fn);
int allowedChars = checkregex("^[[:alnum:]\/._ -]*$", fn);
int backslashWithSpace = checkregex(".*(\ ).*", fn);
puts("noDoubleDots");
puts((noDoubleDots == 0 ? "Match\n" : "No Match\n"));
puts("allowedChars");
puts((allowedChars == 0 ? "Match\n" : "No Match\n"));
puts("backslashWithSpace");
puts((backslashWithSpace == 0 ? "Match\n" : "No Match\n"));
return;
}
我的第一次尝试是不匹配,如果它包含 ..
(我什至无法做到)与 noDubleDots
。但是后来我测试了一下,发现文件名和文件夹名可以有..
,比如folder..name/
。所以我想排除带有 /..
或 ../
的那些。但是,如果文件夹名称类似于 folder ..
,并且其中有另一个名为 folder2/
的文件夹,则路径将为 folder\ ../folder2
,排除 ../
将导致错误输出。
在代码中,allowedChars 工作正常。我想如果我也检查文件名是否有 ..
、\ ..
或 \ ([:alnum:])*
来验证文件路径,它就会完成。但是我的正则表达式似乎不起作用。例如,backslashWithSpace 匹配 asd /
和 asd\ /
.
如何使用正则表达式检查并确保给定路径位于文件夹下?提前致谢。
POSIX 提供一个不错的功能 realpath()
realpath() expands all symbolic links and resolves references to /./,
/../ and extra '/' characters in the null-terminated string named by
path to produce a canonicalized absolute pathname. The resulting
pathname is stored as a null-terminated string, up to a maximum of
PATH_MAX bytes, in the buffer pointed to by resolved_path. The
resulting path will have no symbolic link, /./ or /../ components.
如果你会用,我认为它能满足你的需要,如果不会,你可以复制源代码。
我不熟悉 C 中的正则表达式,我正在尝试使用 regex.h
库使用正则表达式查找给定文件名是否在文件夹下。这是我试过的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
int checkregex(char regex_str[100], char test[100]) {
regex_t regex;
printf("regex_str: %s\n\n", regex_str);
int reti = regcomp(®ex, regex_str, REG_EXTENDED | REG_ICASE);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, test, 0, NULL, REG_EXTENDED | REG_ICASE);
regfree(®ex);
return reti;
}
void main(int argc, char *argv[]) {
const char *safepath = "/home";
size_t spl = strlen(safepath);
char *fn = argv[1];
int noDoubleDots = checkregex("[^..\/]", fn);
int allowedChars = checkregex("^[[:alnum:]\/._ -]*$", fn);
int backslashWithSpace = checkregex(".*(\ ).*", fn);
puts("noDoubleDots");
puts((noDoubleDots == 0 ? "Match\n" : "No Match\n"));
puts("allowedChars");
puts((allowedChars == 0 ? "Match\n" : "No Match\n"));
puts("backslashWithSpace");
puts((backslashWithSpace == 0 ? "Match\n" : "No Match\n"));
return;
}
我的第一次尝试是不匹配,如果它包含 ..
(我什至无法做到)与 noDubleDots
。但是后来我测试了一下,发现文件名和文件夹名可以有..
,比如folder..name/
。所以我想排除带有 /..
或 ../
的那些。但是,如果文件夹名称类似于 folder ..
,并且其中有另一个名为 folder2/
的文件夹,则路径将为 folder\ ../folder2
,排除 ../
将导致错误输出。
在代码中,allowedChars 工作正常。我想如果我也检查文件名是否有 ..
、\ ..
或 \ ([:alnum:])*
来验证文件路径,它就会完成。但是我的正则表达式似乎不起作用。例如,backslashWithSpace 匹配 asd /
和 asd\ /
.
如何使用正则表达式检查并确保给定路径位于文件夹下?提前致谢。
POSIX 提供一个不错的功能 realpath()
realpath() expands all symbolic links and resolves references to /./, /../ and extra '/' characters in the null-terminated string named by path to produce a canonicalized absolute pathname. The resulting pathname is stored as a null-terminated string, up to a maximum of PATH_MAX bytes, in the buffer pointed to by resolved_path. The resulting path will have no symbolic link, /./ or /../ components.
如果你会用,我认为它能满足你的需要,如果不会,你可以复制源代码。