Skip to content

Instantly share code, notes, and snippets.

@dfyz
Created February 24, 2020 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfyz/81fdadfd54bad422c942dfe68892b4a6 to your computer and use it in GitHub Desktop.
Save dfyz/81fdadfd54bad422c942dfe68892b4a6 to your computer and use it in GitHub Desktop.
#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