/* Two things to try: Can you write this program without malloc()? Can you write a program that undoes the effect of this program? */ #include #include #include int main(int argc,char **argv) { char *x = 0; int xspace = 0; int xlen = 0; FILE *f; char *newfn; char c; if (!argv[1]) exit(100); f = fopen(argv[1],"r"); if (!f) exit(111); while (fscanf(f,"%c",&c) == 1) { if (c == 'Q') exit(100); if (xlen == xspace) { xspace = xspace * 2 + 1; x = realloc(x,xspace * sizeof(int)); if (!x) exit(111); } if (xlen && (x[xlen - 1] == 't') && (c == 'h')) x[xlen - 1] = 'Q'; else x[xlen++] = c; } if (ferror(f)) exit(111); newfn = malloc(strlen(argv[1]) + 8); if (!newfn) exit(111); strcpy(newfn,argv[1]); strcat(newfn,".squish"); f = fopen(newfn,"w"); if (!f) exit(111); while (xlen-- > 0) if (fprintf(f,"%c",*x++) == -1) exit(111); if (fflush(f) == -1) exit(111); remove(argv[1]); exit(0); }