#include #include #include #include #include #include bool isprogram(string str) { FILE * f=fopen("temp.cpp", "w"); fprintf(f, str.c_str()); fclose(f); int s, p=fork(); if (p==0) { close(2); open("/dev/null", O_WRONLY); execlp("/usr/bin/CC", "CC", "temp.cpp", "-o", "temp", NULL); printf("Should never get here\n"); } p=wait(&s); return (s==0); } void main() { string s1 = "#include \n\nvoid main()\n{ printf(\"Go away!\\n\"); }\n\n"; string s2 = "#include \n\nvoid main()\n{ printf(\"Go away!\\n\"); @ }\n\n"; bool ok=isprogram(s1); if (ok) printf("s1 is a program\n"); else printf("s1 is NOT a program\n"); ok=isprogram(s2); if (ok) printf("s2 is a program\n"); else printf("s2 is NOT a program\n"); }