2009-12-07 Colin Watson <cjwatson@ubuntu.com>

* configure.ac: Check for vasprintf.
	* util/misc.c (asprintf): Move allocation from here ...
	(vasprintf): ... to here.  New function.
	(xasprintf): New function.
	* include/grub/util/misc.h (vasprintf, xasprintf): Add
	prototypes.
	* util/getroot.c (grub_util_get_grub_dev): Use xasprintf.
	* util/grub-mkfont.c (write_font): Likewise.
	* util/grub-probe.c (probe): Likewise.
	* util/hostdisk.c (make_device_name): Likewise.
This commit is contained in:
Colin Watson 2009-12-07 16:46:24 +00:00
parent de6daa8b56
commit d6ceebf1d9
9 changed files with 68 additions and 18 deletions

View file

@ -1,3 +1,16 @@
2009-12-07 Colin Watson <cjwatson@ubuntu.com>
* configure.ac: Check for vasprintf.
* util/misc.c (asprintf): Move allocation from here ...
(vasprintf): ... to here. New function.
(xasprintf): New function.
* include/grub/util/misc.h (vasprintf, xasprintf): Add
prototypes.
* util/getroot.c (grub_util_get_grub_dev): Use xasprintf.
* util/grub-mkfont.c (write_font): Likewise.
* util/grub-probe.c (probe): Likewise.
* util/hostdisk.c (make_device_name): Likewise.
2009-12-06 David S. Miller <davem@sunset.davemloft.net> 2009-12-06 David S. Miller <davem@sunset.davemloft.net>
* disk/ieee1275/ofdisk.c (grub_ofdisk_iterate): Recognize * disk/ieee1275/ofdisk.c (grub_ofdisk_iterate): Recognize

View file

@ -194,7 +194,7 @@ else
fi fi
# Check for functions. # Check for functions.
AC_CHECK_FUNCS(posix_memalign memalign asprintf) AC_CHECK_FUNCS(posix_memalign memalign asprintf vasprintf)
# For grub-mkisofs # For grub-mkisofs
AC_HEADER_MAJOR AC_HEADER_MAJOR

View file

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include <setjmp.h> #include <setjmp.h>
#include <unistd.h> #include <unistd.h>
@ -57,12 +58,20 @@ void grub_util_write_image (const char *img, size_t size, FILE *out);
void grub_util_write_image_at (const void *img, size_t size, off_t offset, void grub_util_write_image_at (const void *img, size_t size, off_t offset,
FILE *out); FILE *out);
#ifndef HAVE_VASPRINTF
int vasprintf (char **buf, const char *fmt, va_list ap);
#endif
#ifndef HAVE_ASPRINTF #ifndef HAVE_ASPRINTF
int asprintf (char **buf, const char *fmt, ...); int asprintf (char **buf, const char *fmt, ...);
#endif #endif
char *xasprintf (const char *fmt, ...);
#ifdef __MINGW32__ #ifdef __MINGW32__
#define fseeko fseeko64 #define fseeko fseeko64

View file

@ -546,7 +546,7 @@ grub_util_get_grub_dev (const char *os_dev)
if (q) if (q)
*q = ','; *q = ',';
asprintf (&grub_dev, "md%s", p); grub_dev = xasprintf ("md%s", p);
free (p); free (p);
} }
else if (os_dev[7] == '/' && os_dev[8] == 'd') else if (os_dev[7] == '/' && os_dev[8] == 'd')
@ -561,7 +561,7 @@ grub_util_get_grub_dev (const char *os_dev)
if (q) if (q)
*q = ','; *q = ',';
asprintf (&grub_dev, "md%s", p); grub_dev = xasprintf ("md%s", p);
free (p); free (p);
} }
else if (os_dev[7] >= '0' && os_dev[7] <= '9') else if (os_dev[7] >= '0' && os_dev[7] <= '9')
@ -574,7 +574,7 @@ grub_util_get_grub_dev (const char *os_dev)
if (q) if (q)
*q = ','; *q = ',';
asprintf (&grub_dev, "md%s", p); grub_dev = xasprintf ("md%s", p);
free (p); free (p);
} }
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9') else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
@ -587,7 +587,7 @@ grub_util_get_grub_dev (const char *os_dev)
if (q) if (q)
*q = ','; *q = ',';
asprintf (&grub_dev, "md%s", p); grub_dev = xasprintf ("md%s", p);
free (p); free (p);
} }
else else

View file

@ -107,7 +107,7 @@ create_envblk_file (const char *name)
if (! buf) if (! buf)
grub_util_error ("out of memory"); grub_util_error ("out of memory");
asprintf (&namenew, "%s.new", name); namenew = xasprintf ("%s.new", name);
fp = fopen (namenew, "wb"); fp = fopen (namenew, "wb");
if (! fp) if (! fp)
grub_util_error ("cannot open the file %s", namenew); grub_util_error ("cannot open the file %s", namenew);

View file

@ -366,7 +366,7 @@ write_font (struct grub_font_info *font_info, char *output_file)
if (! style_name[0]) if (! style_name[0])
strcpy (style_name, " Regular"); strcpy (style_name, " Regular");
asprintf (&font_name, "%s %s %d", font_info->name, &style_name[1], font_name = xasprintf ("%s %s %d", font_info->name, &style_name[1],
font_info->size); font_info->size);
write_string_section ("NAME", font_name, &offset, file); write_string_section ("NAME", font_name, &offset, file);

View file

@ -254,7 +254,7 @@ probe (const char *path, char *device_name)
filebuf_via_sys = grub_util_read_image (path); filebuf_via_sys = grub_util_read_image (path);
rel_path = make_system_path_relative_to_its_root (path); rel_path = make_system_path_relative_to_its_root (path);
asprintf (&grub_path, "(%s)%s", drive_name, rel_path); grub_path = xasprintf ("(%s)%s", drive_name, rel_path);
free (rel_path); free (rel_path);
grub_util_info ("reading %s via GRUB facilities", grub_path); grub_util_info ("reading %s via GRUB facilities", grub_path);
file = grub_file_open (grub_path); file = grub_file_open (grub_path);

View file

@ -679,12 +679,12 @@ make_device_name (int drive, int dos_part, int bsd_part)
char *bsd_part_str = NULL; char *bsd_part_str = NULL;
if (dos_part >= 0) if (dos_part >= 0)
asprintf (&dos_part_str, ",%d", dos_part + 1); dos_part_str = xasprintf (",%d", dos_part + 1);
if (bsd_part >= 0) if (bsd_part >= 0)
asprintf (&bsd_part_str, ",%c", dos_part + 'a'); bsd_part_str = xasprintf (",%c", dos_part + 'a');
asprintf (&ret, "%s%s%s", map[drive].drive, ret = xasprintf ("%s%s%s", map[drive].drive,
dos_part_str ? : "", dos_part_str ? : "",
bsd_part_str ? : ""); bsd_part_str ? : "");

View file

@ -30,6 +30,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <errno.h>
#include <grub/kernel.h> #include <grub/kernel.h>
#include <grub/misc.h> #include <grub/misc.h>
@ -370,6 +371,19 @@ grub_arch_sync_caches (void *address __attribute__ ((unused)),
{ {
} }
#ifndef HAVE_VASPRINTF
int
vasprintf (char **buf, const char *fmt, va_list ap)
{
/* Should be large enough. */
*buf = xmalloc (512);
return vsprintf (*buf, fmt, ap);
}
#endif
#ifndef HAVE_ASPRINTF #ifndef HAVE_ASPRINTF
int int
@ -378,11 +392,8 @@ asprintf (char **buf, const char *fmt, ...)
int status; int status;
va_list ap; va_list ap;
/* Should be large enough. */
*buf = xmalloc (512);
va_start (ap, fmt); va_start (ap, fmt);
status = vsprintf (*buf, fmt, ap); status = vasprintf (*buf, fmt, ap);
va_end (ap); va_end (ap);
return status; return status;
@ -390,6 +401,23 @@ asprintf (char **buf, const char *fmt, ...)
#endif #endif
char *
xasprintf (const char *fmt, ...)
{
va_list ap;
char *result;
va_start (ap, fmt);
if (vasprintf (&result, fmt, ap) < 0)
{
if (errno == ENOMEM)
grub_util_error ("out of memory");
return NULL;
}
return result;
}
#ifdef __MINGW32__ #ifdef __MINGW32__
void sync (void) void sync (void)