File: fixup.c

package info (click to toggle)
sqliteodbc 0.70-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,700 kB
  • ctags: 1,040
  • sloc: ansic: 21,609; sh: 6,574; makefile: 104
file content (69 lines) | stat: -rw-r--r-- 1,311 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Simple string replacement utility
 *
 * $Id: fixup.c,v 1.2 2005/06/14 06:07:43 chw Exp chw $
 */

#include <string.h>
#include <stdio.h>

int
main(int argc, char **argv)
{
    int i;
    char line[512];

    if (argc < 2 || argc % 2 != 1) {
	fprintf(stderr, "usage: %s search1 replace1 ..\n", argv[0]);
	exit(1);
    }
    for (i = 1 + 1; i < argc; i += 2) {
	if (argv[i][0] == '@') {
	    int len, off = 1;
	    FILE *f;

	    if (argv[i][1] == '@') {
		off++;
	    }
	    f = fopen(argv[i] + off, "r");
	    if (f == NULL) {
		fprintf(stderr, "unable to read %s\n", argv[i] + off);
		exit(1);
	    }
	    line[0] = '\0';
	    fgets(line, sizeof (line), f);
	    fclose(f);
	    len = strlen(line);
	    if (len) {
		line[len - 1] = '\0';
	    }
	    if (off > 1) {
		int x = 0, y = 0, z = 0;

		sscanf(line, "%d.%d.%d", &x, &y, &z);
		sprintf(line, "%d", x * 100000 + y * 1000 + z);
	    }
	    argv[i] = strdup(line);
	}
    }
    while (fgets(line, sizeof (line), stdin) != NULL) {
	int found = 0;

	for (i = 1; i < argc; i += 2) {
	    char *p = strstr(line, argv[i]);

	    if (p != NULL) {
		fwrite(line, p - line, 1, stdout);
		fputs(argv[i + 1], stdout);
		p += strlen(argv[i]);
		fputs(p, stdout);
		found = 1;
		break;
	    }
	}
	if (!found) {
	    fputs(line, stdout);
	}
    }
    exit(0);
}