
#include <stdio.h>
#include <stdlib.h>

main(int argc, char **argv)
{
    FILE    *fp, *wp;
    int     tabsize;
    int     cpos,c;

    if(argc < 2) {
        fprintf(stderr, "Usage; %s <tab size> <filename>\n", argv[0]);
        exit(2);
    } else if(!(fp=fopen(argv[2], "rb"))) {
        perror(argv[2]);
        exit(1);
    } else if(!(wp=fopen("/tmp/conv","w+b"))) {
        perror("/tmp/conv");
        exit(1);
    }

    tabsize = atoi(argv[1]);
    cpos = 1;
    while((c = fgetc(fp)) != EOF)
    {
        if(c == '\t') {
            if(((cpos / tabsize) * tabsize) == cpos) {
                fputc(' ', wp);
                cpos++;
            } else {
                for(c = ((cpos / tabsize)+1) * tabsize;cpos <= c;cpos++)
                    fputc(' ', wp);
            }
        } else {
            if(c == '\n')
                cpos = 1;
            else
                cpos++;
            fputc(c, wp);
        }
    }
    fclose(fp);
    fclose(wp);
}

