* grub-core/osdep/unix/platform.c (get_ofpathname): Trim ending newline.

Don't rely on PATH_MAX.
This commit is contained in:
Vladimir Serbinenko 2013-11-25 07:34:49 +01:00
parent c98dd165b0
commit 61e1b9a49d
2 changed files with 27 additions and 6 deletions

View file

@ -1,3 +1,8 @@
2013-11-25 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/osdep/unix/platform.c (get_ofpathname): Trim ending newline.
Don't rely on PATH_MAX.
2013-11-25 Vladimir Serbinenko <phcoder@gmail.com> 2013-11-25 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/genmoddep.awk: Use more portable && rather than and. * grub-core/genmoddep.awk: Use more portable && rather than and.

View file

@ -31,11 +31,11 @@
static char * static char *
get_ofpathname (const char *dev) get_ofpathname (const char *dev)
{ {
char *ret = xmalloc (2 * PATH_MAX); size_t alloced = 4096;
char *end = ret + 2 * PATH_MAX - 1; char *ret = xmalloc (alloced);
size_t offset = 0;
int fd; int fd;
pid_t pid; pid_t pid;
char *ptr = ret;
pid = grub_util_exec_pipe ((const char * []){ "ofpathname", dev, NULL }, &fd); pid = grub_util_exec_pipe ((const char * []){ "ofpathname", dev, NULL }, &fd);
if (!pid) if (!pid)
@ -45,13 +45,29 @@ get_ofpathname (const char *dev)
if (!fp) if (!fp)
goto fail; goto fail;
while (!feof (fp) && ptr < end) while (!feof (fp))
{ {
size_t r; size_t r;
r = fread (ptr, 1, end - ptr, fp); if (alloced == offset)
ptr += r; {
alloced *= 2;
ret = xrealloc (ret, alloced);
}
r = fread (ret + offset, 1, alloced - offset, fp);
offset += r;
} }
if (offset > 0 && ret[offset - 1] == '\n')
offset--;
if (offset > 0 && ret[offset - 1] == '\r')
offset--;
if (alloced == offset)
{
alloced++;
ret = xrealloc (ret, alloced);
}
ret[offset] = '\0';
fclose (fp); fclose (fp);
return ret; return ret;