/*****************************************************************************/ /* */ /* comment.c removes comments from the source file. */ /* */ /*****************************************************************************/ #include #include #include /*****************************************************************************/ /* */ /* when the str has only white spaces, do not print it. */ /* */ /*****************************************************************************/ str_print( char *fmt, ... ) { va_list argp; char str[BUFSIZ]; va_start(argp, fmt); vsprintf(str, fmt, argp); va_end(argp); if ( strspn(str, " \t\n") != strlen( str ) ) printf("%s", str); } /*****************************************************************************/ /* */ /* main() */ /* */ /*****************************************************************************/ main() { enum {in, out} status; char buf[BUFSIZ]; char *ptr1, *ptr2; status = out; while ( fgets(buf, sizeof buf, stdin) != NULL ) { ptr1 = strstr(buf, "/*"); ptr2 = strstr(buf, "*/"); if (ptr2 != NULL) ptr2 = ptr2 + 2; if (status == out && ptr1 == NULL) printf("%s", buf); if (status == out && ptr1 != NULL) if (ptr2 != NULL) { *ptr1 = '\0'; str_print("%s%s", buf, ptr2); } else { *ptr1 = '\0'; str_print("%s", buf); status = in; } if (status == in && ptr2 != NULL) { str_print("%s", ptr2); status = out; } } exit(0); }