diff --git a/src/Colleen.c b/src/Colleen.c index f35a527..79750b3 100644 --- a/src/Colleen.c +++ b/src/Colleen.c @@ -1,4 +1,5 @@ #include +#include #include void @@ -11,28 +12,34 @@ int main(void) { // One of the comments must be present in the main function - char *src = "#include \n#include \n\nvoid\nfoo(void)\n{\n\twrite(2, \"Hello world!\\n\", 13);\n}\n\nint\nmain(void)\n{\n\t// One of the comments must be present in the main function\n\tchar\t*src = \"$\";\n\tfor (size_t i = 0; src[i]; ++i)\n\t{\n\t\tif (36 == src[i])\n\t\t{\n\t\t\tfor (size_t j = 0; src[j]; ++j)\n\t\t\t{\n\t\t\t\tswitch (src[j])\n\t\t\t\t{\n\t\t\t\t\tcase 10:\twrite(1, \"\\\\n\", 2); break;\n\t\t\t\t\tcase 9:\t\twrite(1, \"\\\\t\", 2); break;\n\t\t\t\t\tcase '\\\\':\twrite(1, \"\\\\\\\\\", 2); break;\n\t\t\t\t\tcase '\"':\twrite(1, \"\\\\\\\"\", 2); break;\n\t\t\t\t\tdefault:\twrite(1, &src[j], 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\twrite(1, &src[i], 1);\n\t\t}\n\t}\n\n\tfoo();\n\treturn 0;\n}\n// One of the comments must be present outside of your program.\n"; + char *src = "#include \n#include \n#include \n\nvoid\nfoo(void)\n{\n\twrite(2, \"Hello world!\\n\", 13);\n}\n\nint\nmain(void)\n{\n\t// One of the comments must be present in the main function\n\tchar\t*src = \"$\";\n\tchar\tbuff[1024];\n\tsize_t\tlen = 0;\n\tsize_t\tbuff_max = sizeof(buff) - 1;\n\tfor (size_t i = 0; src[i]; ++i)\n\t{\n\t\tif (36 == src[i])\n\t\t{\n\t\t\tfor (size_t j = 0; src[j]; ++j)\n\t\t\t{\n\t\t\t\tif (len + 2 > buff_max) { write(1, buff, len); len = 0; }\n\t\t\t\tswitch (src[j])\n\t\t\t\t{\n\t\t\t\t\tcase 10:\tmemcpy(buff + len, \"\\\\n\", 2); len += 2; break;\n\t\t\t\t\tcase 9:\t\tmemcpy(buff + len, \"\\\\t\", 2); len += 2; break;\n\t\t\t\t\tcase '\\\\':\tmemcpy(buff + len, \"\\\\\\\\\", 2); len += 2; break;\n\t\t\t\t\tcase '\"':\tmemcpy(buff + len, \"\\\\\\\"\", 2); len += 2; break;\n\t\t\t\t\tdefault:\tbuff[len++] = src[j]; break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (len + 1 > buff_max) { write(1, buff, len); len = 0; }\n\t\t\tbuff[len++] = src[i];\n\t\t}\n\t}\n\tif (len > 0) write(1, buff, len);\n\n\tfoo();\n\treturn 0;\n}\n// One of the comments must be present outside of your program.\n"; + char buff[1024]; + size_t len = 0; + size_t buff_max = sizeof(buff) - 1; for (size_t i = 0; src[i]; ++i) { if (36 == src[i]) { for (size_t j = 0; src[j]; ++j) { + if (len + 2 > buff_max) { write(1, buff, len); len = 0; } switch (src[j]) { - case 10: write(1, "\\n", 2); break; - case 9: write(1, "\\t", 2); break; - case '\\': write(1, "\\\\", 2); break; - case '"': write(1, "\\\"", 2); break; - default: write(1, &src[j], 1); + case 10: memcpy(buff + len, "\\n", 2); len += 2; break; + case 9: memcpy(buff + len, "\\t", 2); len += 2; break; + case '\\': memcpy(buff + len, "\\\\", 2); len += 2; break; + case '"': memcpy(buff + len, "\\\"", 2); len += 2; break; + default: buff[len++] = src[j]; break; } } } else { - write(1, &src[i], 1); + if (len + 1 > buff_max) { write(1, buff, len); len = 0; } + buff[len++] = src[i]; } } + if (len > 0) write(1, buff, len); foo(); return 0;