/* collapse.c
 *
 * Copy stdin to stdout, throwing away backslash newline pairs;
 * all other backslashes and newlines are left alone.  This means
 * that input ending with backslash newline will produce an output
 * that is not newline terminated.
 */
#include <stdio.h>
int
main(int argc, char **argv)
{
    int c;
    while ((c=getchar())!=EOF) {
        if (c=='\\') {
            if ((c=getchar())!='\n') {
                putchar('\\');
                if (c==EOF)
                    break;
                else
                    putchar(c);
            }
        }
        else
            putchar(c);
    }
    fflush(stdout);
    if (ferror(stdout)) {
        perror("stdout");
        return 2;
    }
    else if (ferror(stdin)) {
        perror("stdin");
        return 1;
    }
    else
        return 0;
}

Doing this one character at a time means there are no line length limits
whatsoever.  And converting to wide character support is easy enough:

#include <stdio.h>
#include <locale.h>
#include <wchar.h>
int
main(int argc, char **argv)
{
    wint_t c;

    (void) setlocale(LC_ALL,"");

    while ((c=getwchar())!=WEOF) {
        if (c==L'\\') {
            if ((c=getwchar())!=L'\n') {
                putwchar('\\');
                if (c==WEOF)
                    break;
                else
                    putwchar(c);
            }
        }
        else
            putwchar(c);
    }
    fflush(stdout);
    if (ferror(stdout)) {
        perror("stdout");
        return 2;
    }
    else if (ferror(stdin)) {
        perror("stdin");
        return 1;
    }
    else
        return 0;
}

-- 
mailto:[EMAIL PROTECTED]  http://www.smart.net/~rlhamil

Reply via email to