Home > OS >  How to create an allow-list in C language
How to create an allow-list in C language

Time:11-27

I am a newbie in C. I am trying to build a whitelist program that returns 0 if a character in the given string (user_data) is not in the list of 'ok_chars'

  #include <stdio.h>
  #include <string.h>


static char ok_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.";
char user_data[] = "Bad &";
char * cp = user_data; 
const char * end = user_data   strlen (user_data);
for (cp  = strspn(cp , ok_chars); cp != end; cp  = strspn (cp , ok_chars)) {
//*cp = '_';
  
if(user_data not in ok_chars){ //I don't know how to implement this
     return 0;
 }

}

Kindly assist.

CodePudding user response:

int isOK(const char *needles, const char *haystack)
{
    int result = 1;
    while(*needles)
        if(!strchr(haystack, *needles  ))
        {
            result = 0;
            break;
        }
    return result;
}


/* .... */

if(!isOK(user_data, ok_chars))
     return 0;

/* .... */
  • Related