Created
February 24, 2020 23:38
-
-
Save dfyz/81fdadfd54bad422c942dfe68892b4a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctype.h> | |
#include <stdio.h> | |
int openbsd_check(const char* line) { | |
if (isdigit((int)*line) && *(line + 1) == '.' && | |
isdigit((int)*line+2) && *(line + 3) == '.' && | |
isdigit((int)*line+4) && isspace((int)*(line + 5))) | |
return 1; | |
else | |
return 0; | |
} | |
int regular_check(const char* line) { | |
if (isdigit((int)*line) && *(line + 1) == '.' && | |
isdigit((int)*(line+2)) && *(line + 3) == '.' && | |
isdigit((int)*(line+4)) && isspace((int)*(line + 5))) | |
return 1; | |
else | |
return 0; | |
} | |
int main() { | |
const char* valid_line = "5.4.0 "; | |
const char* nasty_line = "5.?.! "; | |
printf("OpenBSD check for a valid line: %d\n", openbsd_check(valid_line)); | |
printf("OpenBSD check for an invalid line: %d\n", openbsd_check(nasty_line)); | |
printf("Regular check for a valid line: %d\n", regular_check(valid_line)); | |
printf("Regular check for an invalid line: %d\n", regular_check(nasty_line)); | |
/* | |
* $ gcc -O2 openbsd.c -o openbsd | |
* $ ./openbsd | |
* OpenBSD check for a valid line: 1 | |
* OpenBSD check for an invalid line: 1 | |
* Regular check for a valid line: 1 | |
* Regular check for an invalid line: 0 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment