This commit is contained in:
Emil 2023-10-07 18:14:49 +00:00
parent 0d7605a43f
commit 981a1931d4
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A

57
baked.c
View File

@ -57,48 +57,51 @@ find_region(const char * fn)
fd = open(fn, O_RDONLY); fd = open(fn, O_RDONLY);
if ( fd != -1 if (fd != -1)
&& !fstat(fd,&s)
&& s.st_mode & S_IFREG
&& s.st_size)
{ {
char * start, * stop, * addr; if (!fstat(fd,&s)
addr = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0); && s.st_mode & S_IFREG
if (addr != MAP_FAILED) && 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); if (!strncmp(start,START,SLEN))
for (stop = start; *stop; ++stop)
{ {
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); if (!strncmp(stop,STOP,SLEN))
buf = malloc(len + 1); {
assert(buf); size_t len = (stop - addr) - (start - addr);
if (!buf) buf = malloc(len + 1);
{ goto stop; } assert(buf);
strncpy(buf, start, len); if (!buf)
buf[len] = '\0'; { goto stop; }
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; return buf;
} }