Skip to content

Instantly share code, notes, and snippets.

@trn1ty
Last active May 2, 2022 22:40
Show Gist options
  • Save trn1ty/dd574d704c6e98e9f664d38f96224328 to your computer and use it in GitHub Desktop.
Save trn1ty/dd574d704c6e98e9f664d38f96224328 to your computer and use it in GitHub Desktop.
Quick test to make sure your compiler knows trigraphs.
#include <unistd.h>
static char *program_name =
"trigraphs.c";
/* Trinity BLAKE 2020-2022
* Public domain */
char trigraphs[] =
/* From the C89 draft specification, section 2.2.1.1:
* All occurrences in a source file of the following sequences of
* three characters (called trigraph sequences[5]) are replaced
* with the corresponding single character. */
"??=" /* # */
"??(" /* [ */
"??/??/" /* \\ - escaped backslash; will print '\' */
"??)" /* ] */
"??'" /* ^ */
"??<" /* { */
"??!" /* | */
"??>" /* } */
"??-" /* ~ */
;
/* No other trigraph sequences exist. */
int trigraphs_s = 10; /* (9 trigraphs + nul terminator) */
/* Each ? that does not begin one of the trigraphs listed above is not
* changed. */
int main(int argc, char *argv[]){
int i;
char *argv0;
if(sizeof(trigraphs) / sizeof(trigraphs[0]) != trigraphs_s){
argv0 = argv[0] != NULL ? argv[0] : program_name;
for(i = 0; argv[0][i] != '\0'; ++i);
write(1, argv0, i);
write(1,
": Trigraphs were not parsed correctly by the"
" compiler.\n", 55
);
i = 71; /* sysexits(3) EX_OSERR */
}else{
trigraphs[trigraphs_s - 1] = '\n';
write(1, trigraphs, trigraphs_s);
i = 0;
}
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment