Skip to content

Instantly share code, notes, and snippets.

@mrdomino
Created September 29, 2014 00:21
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 mrdomino/480167b59dd649eff742 to your computer and use it in GitHub Desktop.
Save mrdomino/480167b59dd649eff742 to your computer and use it in GitHub Desktop.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
static void
usage(const char* argv0)
{
fprintf(stderr,
"Usage: %s\n"
" Passes spans of non-ASCII characters from stdin to stdout, "
"separated by newlines.\n",
argv0);
exit(EXIT_FAILURE);
}
static void
filter()
{
char cs[BUFSIZ];
int r;
size_t i;
i = 0;
while ((r = getchar()) != EOF) {
if (0 == (r & 0x80)) {
if (i != 0) {
cs[i] = '\0';
printf("%s\n", cs);
i = 0;
}
}
else {
if (i == BUFSIZ - 1) {
cs[i] = '\0';
printf("%s", cs);
i = 0;
}
cs[i++] = (char)r;
}
}
if (i != 0) {
cs[i] = '\0';
printf("%s\n", cs);
}
}
int
main(int argc, char* argv[])
{
if (argc > 1) {
usage(*argv);
}
else filter();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment