/* * Program to expand tabs in input. * * Command-line argument specifies infile * (later we can maybe figure out how to also specify spaces per tab). */ #include #include #define LINE_WIDTH 80 void print_header(int spaces_per_tab); int main(int argc, char * argv[]) { if (argc < 2) { fprintf(stderr, "usage: %s infile spaces_per_tab\n", argv[0]); return EXIT_FAILURE; } FILE * infile = fopen(argv[1], "r"); if (infile == NULL) { fprintf(stderr, "cannot open input file %s\n", argv[2]); return EXIT_FAILURE; } int spaces_per_tab = 8; print_header(spaces_per_tab); int ch; int tab_count = 0; int position_in_line = 0; while ((ch = fgetc(infile)) != EOF) { if (ch == '\t') { tab_count += 1; do { fputc(' ', stdout); position_in_line += 1; } while ((position_in_line % spaces_per_tab) != 0); } else if (ch == '\n') { fputc(ch, stdout); position_in_line = 0; } else { fputc(ch, stdout); position_in_line += 1; } } fclose(infile); printf("%d tabs found\n", tab_count); return EXIT_SUCCESS; } void print_header(int spaces_per_tab) { for (int i = 0; i < LINE_WIDTH; ++i) { if ((i % spaces_per_tab) == 0) { fputc('+', stdout); } else { fputc('-', stdout); } } fputc('\n', stdout); }