0

Everytime I enter a wrong filename, its going to Regular Expression mismatched. Where am I doing wrong is it the regular expression or something else. Why r is always matching REG_NOMATCH

char fname[128], rlpath[PATH_MAX-1];
int r;
regex_t regex;
struct stat buf;

printf("Enter filename: ");
scanf("%s", fname);
if(fname == NULL){
printf("please enter the filename");
}
else {
if(realpath(fname,rlpath)!=NULL){
printf("The path for source file is %s \n", rlpath);
}
}
r = regcomp(&regex, "/^[a-zA-Z0-9/-/._/]+$/", REG_EXTENDED|REG_NOSUB);

r = regexec(&regex, fname, 0, NULL, 0);


if(r)
{    
if(realpath(fname,rlpath)!= NULL)
{

    if(sizeof(rlpath) < strlen(rlpath)){ //to check max rlpath length
    printf("File name entered is too long");
    exit(1);
    }

    stat(rlpath, &buf);//to examine the attributes of file, I used the stat function
    if(S_ISREG(buf.st_mode))
    {
        printf("real path %s \n", rlpath);
        printf("File Name is legit \n");
    }   
    else{
        printf("File name is not legit, it may be a device name, please check \n");
        exit(1);
    }   
}   
}
else if(r == REG_NOMATCH)
    printf("Regular expression mismatch\n");
return 0;
user3192813
  • 3
  • 2
  • 6

0 Answers0