Reduced bloat around how mmap is handled, fixes to nobloat

This commit is contained in:
Emil 2023-10-10 03:37:26 +00:00
parent bb9ee9dcb2
commit a02b196b21
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A
2 changed files with 98 additions and 102 deletions

View File

@ -27,7 +27,7 @@
"\t$@ returns target-file (abc.x.txt)\n" \ "\t$@ returns target-file (abc.x.txt)\n" \
"\t$* returns target-file without suffix (^-> abc.x)\n" "\t$* returns target-file without suffix (^-> abc.x)\n"
static char * g_filename, * g_short; char * g_filename, * g_short;
static char * static char *
map(const char * fn, size_t * len) map(const char * fn, size_t * len)
@ -65,60 +65,42 @@ find(const char * x, const char * buf, const size_t max, const size_t min)
} }
static char * static char *
find_region(char * addr, size_t len) find_region(const char * fn, const char * start, const char * stop)
{ {
char * buf = NULL; size_t len = 0;
const char * start, * stop; char * buf = NULL, * addr;
if ((start = find(START, addr, len, strlen(START)))) const char * pb, * pe;
addr = map(fn, &len);
if (addr != MAP_FAILED)
{ {
start += strlen(START); if ((pb = find(start, addr, len, strlen(start))))
stop = find(STOP, start, len - (start - addr), strlen(STOP));
if (!stop)
{ {
stop = start; pb += strlen(start);
while (*stop && *stop != '\n') pe = find(stop, pb, len - (pb - addr), strlen(stop));
if (!pe)
{ {
if (stop[0] == '\\' && stop[1] == '\n') pe = pb;
{ stop += 2; } while (*pe && *pe != '\n')
++stop; {
if (pe[0] == '\\' && pe[1] == '\n')
{ pe += 2; }
++pe;
}
} }
if (pe)
{ buf = strndup(pb, (pe - addr) - (pb - addr)); }
} }
if (stop) munmap(addr, len);
{ buf = strndup(start, (stop - addr) - (start - addr)); }
} }
return buf; return buf;
} }
static void
swap(char * a, char * b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
static int
root(char * root)
{
int ret;
char x[1] = "\0";
size_t len = strlen(root);
while (len && root[len] != '/')
{ --len; }
if (!len)
{ return 0; }
swap(root + len, x);
ret = chdir(root);
swap(root + len, x);
return ret;
}
static char * static char *
insert(const char * new, char * str, size_t offset, size_t shift) insert(const char * new, char * str, size_t offset, size_t shift)
{ {
size_t len, max; size_t len, max;
if (!new) { return str; } if (!new || !str) { return NULL; }
if (!str) { return NULL; }
len = strlen(new); len = strlen(new);
max = (strlen(str) + 1 - offset - shift); max = (strlen(str) + 1 - offset - shift);
memmove(str + offset + len, str + offset + shift, max); memmove(str + offset + len, str + offset + shift, max);
@ -202,29 +184,38 @@ expand(char * buf)
return buf; return buf;
} }
static int static void
run(const char * buf) swap(char * a, char * b)
{ {
fputs("Output:\n", stderr); *a ^= *b;
root(g_filename); *b ^= *a;
return system(buf); *a ^= *b;
}
static int
root(char * root)
{
int ret;
char x[1] = "\0";
size_t len = strlen(root);
while (len && root[len] != '/')
{ --len; }
if (!len)
{ return 0; }
swap(root + len, x);
ret = chdir(root);
swap(root + len, x);
return ret;
} }
int int
main(int argc, char ** argv) main(int argc, char ** argv)
{ {
int ret = 0; char * buf;
char * buf = NULL, * addr;
size_t len;
setlocale(LC_ALL, "C"); setlocale(LC_ALL, "C");
if (argc < 2) if (argc < 2) { goto help; }
{ goto help; }
g_filename = argv[1]; g_filename = argv[1];
if ((addr = map(g_filename, &len))) buf = find_region(g_filename, START, STOP);
{
buf = find_region(addr, len);
munmap(addr, len);
}
if (!buf) if (!buf)
{ {
if (errno) if (errno)
@ -234,11 +225,14 @@ main(int argc, char ** argv)
return 1; return 1;
} }
buf = expand(buf); buf = expand(buf);
fprintf(stderr, "Exec: %s\n", buf ? buf + 1 : buf); root(argv[0]);
if ((ret = ret ? 0 : run(buf))) fprintf(stderr, "Exec: %s\n", buf);
{ fprintf(stderr, "Result: %d\n", ret); } if (!buf) { return 1; }
fputs("Output:\n", stderr);
fflush(stderr);
system(buf);
free(buf); free(buf);
return ret; return 0;
help: help:
fprintf(stderr, "%s: %s", argv[0], HELP DESC); fprintf(stderr, "%s: %s", argv[0], HELP DESC);
return 1; return 1;

88
baked.c
View File

@ -83,57 +83,63 @@ find(const char * x, const char * buf, const size_t max, const size_t min)
} }
static char * static char *
find_region(char * addr, size_t len) find_region(const char * fn)
{ {
char * buf = NULL; size_t len = 0;
char * buf = NULL, * addr;
const char * start, * stop; const char * start, * stop;
start = find(START, addr, len, strlen(START)); addr = map(fn, &len);
if (addr != MAP_FAILED)
{
start = find(START, addr, len, strlen(START));
#ifdef OTHER_START #ifdef OTHER_START
if (!start) if (!start)
{ {
start = find(OTHER_START, addr, len, strlen(OTHER_START)); start = find(OTHER_START, addr, len, strlen(OTHER_START));
start = (const char *) /* DON'T QUESTION IT */ start = (const char *) /* DON'T QUESTION IT */
((ptrdiff_t) (start - strlen(START) + strlen(OTHER_START)) * (start != 0)); ((ptrdiff_t) (start - strlen(START) + strlen(OTHER_START)) * (start != 0));
} }
#endif /* OTHER_START */ #endif /* OTHER_START */
if (start) if (start)
{ {
start += strlen(START); start += strlen(START);
#ifdef REQUIRE_SPACE #ifdef REQUIRE_SPACE
if (!isspace(*start)) if (!isspace(*start))
{
fprintf(stderr, "ERROR: Found start without suffix spacing.\n");
return NULL;
}
#endif
stop = find(STOP, start, len - (start - addr), strlen(STOP));
if (!stop)
{
stop = start;
while (*stop && *stop != '\n')
{ {
if (stop[0] == '\\' && stop[1] == '\n') fprintf(stderr, "ERROR: Found start without suffix spacing.\n");
{ stop += 2; } goto stop;
++stop; }
#endif
stop = find(STOP, start, len - (start - addr), strlen(STOP));
if (!stop)
{
stop = start;
while (*stop && *stop != '\n')
{
if (stop[0] == '\\' && stop[1] == '\n')
{ stop += 2; }
++stop;
}
} }
}
#ifdef REQUIRE_SPACE #ifdef REQUIRE_SPACE
else else
{
if (!isspace(*(stop - 1)))
{ {
fprintf(stderr, "ERROR: Found stop without prefixing spacing.\n"); if (!isspace(*(stop - 1)))
return NULL; {
fprintf(stderr, "ERROR: Found stop without prefixing spacing.\n");
goto stop;
}
} }
}
#endif #endif
if (stop) if (stop)
{ buf = strndup(start, (stop - addr) - (start - addr)); } { buf = strndup(start, (stop - addr) - (start - addr)); }
}
stop:
munmap(addr, len);
} }
return buf; return buf;
} }
static void static void
swap(char * a, char * b) swap(char * a, char * b)
{ {
@ -303,8 +309,8 @@ int
main(int argc, char ** argv) main(int argc, char ** argv)
{ {
int ret = 0; int ret = 0;
char * buf = NULL, * addr; char * buf;
size_t len;
assert(setlocale(LC_ALL, "C")); assert(setlocale(LC_ALL, "C"));
if (argc < 2 if (argc < 2
@ -323,11 +329,7 @@ main(int argc, char ** argv)
{ goto help; } { goto help; }
} }
if ((addr = map(g_filename, &len))) buf = find_region(g_filename);
{
buf = find_region(addr, len);
munmap(addr, len);
}
if (!buf) if (!buf)
{ {