aboutsummaryrefslogtreecommitdiff
path: root/unix/shlib/medit.c
blob: b84b9567185846e825b0e7e88fd9804ad2bf9c82 (plain) (blame)
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
70
71
72
73
74
75
76
77
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>

/*
 * MEDIT -- Replace all occurrences of the given string in a file by a
 * different string of the same length.  Works for binary files as well
 * as text files.  This is a crude but effective way of editing the symbol
 * table of object files, without having to know the data structures.
 * (String *data* will be edited too).
 */

/* Solaris used to need this but it doesn't any longer.
#define bcopy(a,b,n) memmove(b,a,n) */

#define	BUFLEN	16384
static	char	buf[BUFLEN];
static	int	nbytes;

main (argc, argv)
int	argc;
char	*argv[];
{
	register char *ip, *itop, ch;
	char	*fname, *oldname, *newname;
	int	fd, nch, total, nmatch, i;

	if (argc < 4) {
	    fprintf (stderr, "Usage: medit file oldname newname\n");
	    exit (1);
	}

	fname = argv[1];
	if ((fd = open (fname, 2)) == -1) {
	    fprintf (stderr, "Cannot open %s\n", fname);
	    exit (2);
	}

	if ((nbytes = read (fd, buf, BUFLEN)) <= 0) {
	    fprintf (stderr, "Cannot read %s\n", fname);
	    exit (2);
	}

	for (i=2, total=0;  i+1 < argc;  i=i+2, total+=nmatch) {
	    oldname = argv[i];
	    newname = argv[i+1];

	    if (strlen(oldname) != strlen(newname)) {
		fprintf (stderr,
		    "Replacement string must be same length as the original\n");
		exit (3);
	    }

	    ch = oldname[0];
	    nch = strlen (oldname);
	    itop = buf + nbytes - nch;

	    for (ip=buf, nmatch=0;  ip < itop;  ip++)
		if (*ip == ch && (strncmp (ip, oldname, nch) == 0)) {
		    bcopy (newname, ip, nch);
		    ip = ip + nch - 1;
		    nmatch++;
		}

	    printf ("%s, %s -> %s: %d entries edited\n",
		fname, oldname, newname, nmatch);
	}

	if (total) {
	    lseek (fd, 0L, 0);
	    write (fd, buf, nbytes);
	}

	close (fd);
	exit (0);
}