From 981a1931d454d42efd748f7160fce370ad3af3db Mon Sep 17 00:00:00 2001 From: Emil Date: Sat, 7 Oct 2023 18:14:49 +0000 Subject: [PATCH] Forgot --- baked.c | 57 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/baked.c b/baked.c index 6df4ef0..ffca5b2 100644 --- a/baked.c +++ b/baked.c @@ -57,48 +57,51 @@ find_region(const char * fn) fd = open(fn, O_RDONLY); - if ( fd != -1 - && !fstat(fd,&s) - && s.st_mode & S_IFREG - && s.st_size) + if (fd != -1) { - char * start, * stop, * addr; - addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (addr != MAP_FAILED) + if (!fstat(fd,&s) + && s.st_mode & S_IFREG + && s.st_size) { - for (start = addr; *start; ++start) + char * start, * stop, * addr; + addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (addr != MAP_FAILED) { - if (s.st_size - (start - addr) > SLEN) + for (start = addr; *start; ++start) { - if (!strncmp(start,START,SLEN)) + if (s.st_size - (start - addr) > SLEN) { - start += strlen(START); - for (stop = start; *stop; ++stop) + if (!strncmp(start,START,SLEN)) { - if (s.st_size - (stop - addr) > SLEN) + start += strlen(START); + for (stop = start; *stop; ++stop) { - if (!strncmp(stop,STOP,SLEN)) + if (s.st_size - (stop - addr) > SLEN) { - size_t len = (stop - addr) - (start - addr); - buf = malloc(len + 1); - assert(buf); - if (!buf) - { goto stop; } - strncpy(buf, start, len); - buf[len] = '\0'; - goto stop; + if (!strncmp(stop,STOP,SLEN)) + { + size_t len = (stop - addr) - (start - addr); + buf = malloc(len + 1); + assert(buf); + if (!buf) + { goto stop; } + strncpy(buf, start, len); + buf[len] = '\0'; + goto stop; + } } + else goto stop; } - else goto stop; + goto stop; } - goto stop; } + else goto stop; } - else goto stop; + stop: + munmap(addr, s.st_size); } - stop: - munmap(addr, s.st_size); } + close(fd); } return buf; }