11 ** grok -- program to a alter bytes at an offset
13 ** Sometimes it useful to alter bytes in a binary file.
14 ** To those who would 'just use emacs', the binary is not always a file on disk.
18 ** License: Public Domain
22 main(int argc, char **argv)
27 unsigned char *buf, *data;
28 /* grok fn offset data */
31 fputs("usage: grok fn offset [cdata]+\n", stderr);
35 offset = strtol(argv[2], NULL, 16);
36 data = (unsigned char *) calloc(argc - 2, sizeof(unsigned char));
37 for (i = 3; i < argc; i++) {
39 x = strtol(argv[i], NULL, 16);
40 data[dc++] = 0xff & x;
43 if (0 > (fd = open(fn, O_RDWR))) {
44 fprintf(stderr, "cannot open %s, errno %d\n", fn, errno);
48 if (0 > lseek(fd, offset, SEEK_SET)) {
49 fprintf(stderr, "cannot seek to %x, errno %d\n", offset, errno);
53 buf = (unsigned char *) calloc(dc, sizeof(unsigned char));
55 if (0 > (cnt = read(fd, buf, dc))) {
56 fprintf(stderr, "cannot read %d bytes at %x, errno %d\n",
60 printf("read %d of %d bytes\n", cnt, dc);
61 /* dumpBuf(buf, cnt); */
63 if (0 > lseek(fd, offset, SEEK_SET)) {
64 fprintf(stderr, "cannot seek to %x, errno %d\n", offset, errno);
68 if (0 > (sz = write(fd, data, cnt))) {
69 fprintf(stderr, "cannot write %d bytes at %x, errno %d\n",
73 printf("wrote %d of %d bytes\n", sz, cnt);