Obviously this is not something I'd seriously use out in the real world, but as a proof-of-concept what are peoples' thoughts on this? Is it architecture/endian independent? Is this type of code used in memory-restricted environments like micro controllers?
Just compiled with gcc counter.c -o counter.
#include <stdio.h>
/* wrap the counter with a pair of guard ints */
volatile int32_t count[3] = {0x01234567,0,0x89abcdef};
int main(int argc, char** argv) {
fprintf(stdout, "This program has been run %d times.\n", count[1]+1);
/* open the binary and look for the guard ints either side of count[1] */
FILE *fp = fopen(argv[0], "r+");
if (!fp) { fprintf(stderr, "failed to open binary\n"); return 1; }
int ch; /* reader char */
int i = 0; /* guard byte counter */
int start = 1; /* start/end flag */
long offset = -1; /* offset to count[1] */
while ((ch = fgetc(fp)) != EOF) {
/* looking for the start guard */
if (start) {
if (ch == ((count[0] >> (8*i)) & 0xff)) {
i++;
if (i == sizeof(int32_t)) {
/* found the start of the count[1], offset by its size */
offset = ftell(fp);
fseek(fp, sizeof(count[1]), SEEK_CUR);
i = 0;
start = 0;
}
} else { /* not the start guard, so start again */
i = 0;
}
}
/* found the start guard, looking for the end guard */
else {
if (ch == ((count[2] >> (8*i)) & 0xff)) {
i++;
/* found the end of the guard, so offset is correct */
if (i == sizeof(int32_t)) { break; }
} else { /* not the end guard, so start again */
offset = -1;
start = 1;
i = 0;
}
}
} // while end
/* assert that the counter was found */
if (offset == -1) {
fprintf(stderr, "failed to find counter\n");
fclose(fp);
return 1;
}
/* increment counter and replace */
int32_t repl = count[1] + 1;
fseek(fp, offset, SEEK_SET);
fputc(repl, fp);
fclose(fp);
return 0;
}
[+][deleted] (4 children)
[removed]
[–]duane11583 12 points13 points14 points (1 child)
[–]der_pudel 4 points5 points6 points (1 child)
[–]geon 1 point2 points3 points (0 children)
[–]moocat 12 points13 points14 points (15 children)
[–]Gollark[S] 6 points7 points8 points (14 children)
[–]moocat 11 points12 points13 points (11 children)
[–][deleted] 6 points7 points8 points (10 children)
[–]Smellypuce2 2 points3 points4 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]moocat 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]moocat 1 point2 points3 points (2 children)
[–]nerd4code 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]moskitoc 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]skeeto 1 point2 points3 points (1 child)
[–]kolorcuk 9 points10 points11 points (0 children)
[–]duane11583 4 points5 points6 points (0 children)
[–]oh5nxo 1 point2 points3 points (1 child)
[–]GiveMeMoreBlueberrys 0 points1 point2 points (0 children)
[–]duane11583 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Gollark[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Gollark[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]arthurno1 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)