ref: 91fc7321c2b7df934036f7b88ffdd5d6c826e941
dir: /ex/ex1-20.c/
#include <u.h> #include <libc.h> #include <bio.h> void usage(void) { fprint(2, "usage: %s [-t tabstop]", argv0); exits("usage"); } /* Replace tabs with 'tabstop' number of spaces. */ void main(int argc, char *argv[]) { int tabstop = 4; Biobuf *in, *out; char *aux; ARGBEGIN{ case 't': aux = EARGF(usage()); tabstop = atoi(aux); break; default: usage(); }ARGEND in = Bfdopen(0, OREAD); out = Bfdopen(1, OWRITE); while(1){ char *line; int len, n, i; // Read line = Brdline(in, '\n'); len = Blinelen(in); if(line == 0) break; if(len < 0) sysfatal("err: could not read line → %r\n"); int ind = -1; for(i = 0; i < len-1; i++){ if(line[i] != ' ' && line[i] != ' '){ ind = i; break; } if(line[i] == ' '){ int j; for(j = 0; j < tabstop; j++) Bwrite(out, " ", 1); } } // Write char *begin = line; int end = len; if(ind > 0){ ind++; begin = line+ind-1; end = len-ind+1; } n = Bwrite(out, begin, end); if(n < 0) sysfatal("err: could not write line → %r\n"); } Bterm(in); Bterm(out); exits(nil); }