Post B6t4q8LapJVd1giJge by ns@sacred.harpy.faith
(DIR) More posts by ns@sacred.harpy.faith
(DIR) Post #B6t4q8LapJVd1giJge by ns@sacred.harpy.faith
1 likes, 0 repeats
#include <u.h>#include <libc.h>#include <bio.h>voidmain (int argc, char* argv[]){ Biobuf in, out; int lenin, stalin, c, i; if (argc != 3) sysfatal ("expected two arguments"); lenin = strlen (argv[1]); stalin = strlen (argv[2]); if (lenin == 0) sysfatal ("cannot replace empty string"); Binit (&in, 0, OREAD); Binit (&out, 1, OWRITE); while ((c = Bgetc (&in)) >= 0) { if (c != argv[1][0]) { Bputc(&out, c); goto Next; } for (i = 1; i < lenin; i++) { c = Bgetc (&in); if (c < 0 || c != argv[1][i]) { Bputc (&out, argv[1][0]); if (c >=0) Bungetc (&in); if (i > 1) Bwrite (&out, argv[1]+1, i-1); goto Next; } } Bwrite (&out, argv[2], stalin); Next:; } Bterm (&in); Bterm (&out); exits(0);}this may or may not work. the point of this was twofold. the first is that sed s/// used without a regular expression in it is both an absurdly common usecase and akin to using a flamethrower to make toast. the second is that i wanted to get my hands dirty with buffer stuff.