23 int delete_empty_lines =
false;
25 int patchmode =
false;
27 if (argc <= 1 || (argc >= 2 && strcmp(argv[1],
"--help") == 0)) {
28 printf(
"syntax: arb_replace [-l/L/p] \"old=newdata\" [filepattern]\n");
29 printf(
" -l linemode, parse each line separately\n");
30 printf(
" -L linemode, parse each line separately, delete empty lines\n");
31 printf(
" -p patchmode, (no wildcards allowed, rightside<leftside)\n");
34 if (!strcmp(argv[1],
"-l")) {
39 else if (!strcmp(argv[1],
"-L")) {
42 delete_empty_lines =
true;
45 else if (!strcmp(argv[1],
"-p")) {
60 for (arg = startarg; arg<argc; arg++) {
70 char *evaldup = strdup(eval);
71 char *right = strchr(evaldup,
'=');
75 fprintf(stderr,
"'=' not found in replace string\n");
79 if (strlen(right) > strlen(evaldup)) {
80 fprintf(stderr,
"You cannot replace a shorter string by a longer one!!!\n");
86 int leftsize = strlen(evaldup);
90 for (
unsigned long i=0; i<size; i++) {
91 if (!strncmp(data+i, evaldup, leftsize)) {
92 strcpy(data+i, right);
97 out = fopen(fname,
"w");
99 if (fwrite(data, (
unsigned int)size, 1, out) != 1) {
100 fprintf(stderr,
"Write failed %s\n", fname);
104 fprintf(out,
"%s", data);
105 printf(
"File %s parsed\n", fname);
110 fprintf(stderr,
"Write failed %s\n", fname);
125 while ((nextp = strchr(p,
'\n'))) {
134 fprintf(stdout,
"%s", h);
135 if (h[0] || !delete_empty_lines) {
136 fprintf(stdout,
"\n");
141 if (h[0] || !delete_empty_lines) {
163 if (strcmp(data, ndata)) {
165 fprintf(stdout,
"%s", ndata);
168 out = fopen(fname,
"w");
170 fprintf(out,
"%s", ndata);
172 printf(
"File %s parsed\n", fname);
175 printf(
"cannot write %s\n", fname);
char * GBS_string_eval(const char *insource, const char *icommand)
void cat(const char *from)
long GB_size_of_file(const char *path)
GB_ERROR GB_await_error()
int ARB_main(int argc, char *argv[])
char * GB_read_file(const char *path)