removed some duplicate code

This commit is contained in:
BVK Chaitanya 2010-04-27 20:55:12 +05:30
parent 4c7085f82b
commit f07ccea799
10 changed files with 64 additions and 275 deletions

View file

@ -10,9 +10,9 @@ kernel_img_SOURCES = kern/device.c kern/disk.c kern/dl.c kern/env.c \
kern/parser.c kern/partition.c kern/term.c \ kern/parser.c kern/partition.c kern/term.c \
kern/rescue_reader.c kern/rescue_parser.c \ kern/rescue_reader.c kern/rescue_parser.c \
\ \
kern/emu/main.c kern/emu/misc.c kern/emu/getroot.c \ kern/emu/main.c kern/emu/mm.c kern/emu/misc.c \
kern/emu/time.c kern/emu/hostdisk.c kern/emu/hostfs.c \ kern/emu/getroot.c kern/emu/time.c kern/emu/hostdisk.c \
kern/emu/console.c \ kern/emu/hostfs.c kern/emu/console.c \
\ \
gnulib/progname.c disk/host.c gnulib/progname.c disk/host.c
kernel_img_HEADERS += datetime.h emu/misc.h kernel_img_HEADERS += datetime.h emu/misc.h

View file

@ -6,7 +6,7 @@ script/lexer.c_DEPENDENCIES = grub_script.tab.h
sbin_UTILITIES += grub-mkdevicemap sbin_UTILITIES += grub-mkdevicemap
grub_mkdevicemap_SOURCES = gnulib/progname.c util/grub-mkdevicemap.c \ grub_mkdevicemap_SOURCES = gnulib/progname.c util/grub-mkdevicemap.c \
util/deviceiter.c \ util/deviceiter.c \
util/misc.c util/misc.c kern/emu/misc.c
ifeq ($(target_cpu)-$(platform), sparc64-ieee1275) ifeq ($(target_cpu)-$(platform), sparc64-ieee1275)
grub_mkdevicemap_SOURCES += util/ieee1275/ofpath.c util/ieee1275/devicemap.c grub_mkdevicemap_SOURCES += util/ieee1275/ofpath.c util/ieee1275/devicemap.c
@ -16,16 +16,15 @@ endif
# For grub-mkelfimage. # For grub-mkelfimage.
bin_UTILITIES += grub-mkelfimage bin_UTILITIES += grub-mkelfimage
grub_mkelfimage_SOURCES = gnulib/progname.c \ grub_mkelfimage_SOURCES = gnulib/progname.c util/elf/grub-mkimage.c \
util/elf/grub-mkimage.c util/misc.c \ util/misc.c kern/emu/misc.c util/resolve.c
util/resolve.c
util/elf/grub-mkimage.c_DEPENDENCIES = Makefile util/elf/grub-mkimage.c_DEPENDENCIES = Makefile
# For grub-probe. # For grub-probe.
sbin_UTILITIES += grub-probe sbin_UTILITIES += grub-probe
util/grub-probe.c_DEPENDENCIES = grub_probe_init.h util/grub-probe.c_DEPENDENCIES = grub_probe_init.h
grub_probe_SOURCES = gnulib/progname.c util/grub-probe.c \ grub_probe_SOURCES = gnulib/progname.c util/grub-probe.c \
kern/emu/hostdisk.c util/misc.c kern/emu/getroot.c util/mm.c \ kern/emu/hostdisk.c util/misc.c kern/emu/misc.c kern/emu/getroot.c kern/emu/mm.c \
kern/device.c kern/disk.c kern/err.c kern/misc.c \ kern/device.c kern/disk.c kern/err.c kern/misc.c \
kern/parser.c kern/partition.c kern/file.c kern/list.c \ kern/parser.c kern/partition.c kern/file.c kern/list.c \
\ \
@ -61,7 +60,7 @@ grub_mkisofs_CFLAGS = -D_FILE_OFFSET_BITS=64 \
# For grub-fstest. # For grub-fstest.
util/grub-fstest.c_DEPENDENCIES = grub_fstest_init.h util/grub-fstest.c_DEPENDENCIES = grub_fstest_init.h
grub_fstest_SOURCES = gnulib/progname.c util/grub-fstest.c kern/emu/hostfs.c \ grub_fstest_SOURCES = gnulib/progname.c util/grub-fstest.c kern/emu/hostfs.c \
util/misc.c util/mm.c \ util/misc.c kern/emu/misc.c kern/emu/mm.c \
kern/file.c kern/device.c kern/disk.c kern/err.c kern/misc.c \ kern/file.c kern/device.c kern/disk.c kern/err.c kern/misc.c \
disk/host.c disk/loopback.c kern/list.c kern/command.c \ disk/host.c disk/loopback.c kern/list.c kern/command.c \
lib/arg.c commands/extcmd.c normal/datetime.c normal/misc.c \ lib/arg.c commands/extcmd.c normal/datetime.c normal/misc.c \
@ -83,14 +82,14 @@ grub_fstest_SOURCES = gnulib/progname.c util/grub-fstest.c kern/emu/hostfs.c \
# For grub-mkfont. # For grub-mkfont.
ifeq ($(enable_grub_mkfont), yes) ifeq ($(enable_grub_mkfont), yes)
bin_UTILITIES += grub-mkfont bin_UTILITIES += grub-mkfont
grub_mkfont_SOURCES = gnulib/progname.c util/grub-mkfont.c util/misc.c grub_mkfont_SOURCES = gnulib/progname.c util/grub-mkfont.c util/misc.c kern/emu/misc.c
grub_mkfont_CFLAGS = $(freetype_cflags) grub_mkfont_CFLAGS = $(freetype_cflags)
grub_mkfont_LDFLAGS = $(freetype_libs) grub_mkfont_LDFLAGS = $(freetype_libs)
endif endif
# For grub-mkrelpath. # For grub-mkrelpath.
bin_UTILITIES += grub-mkrelpath bin_UTILITIES += grub-mkrelpath
grub_mkrelpath_SOURCES = gnulib/progname.c util/grub-mkrelpath.c util/misc.c grub_mkrelpath_SOURCES = gnulib/progname.c util/grub-mkrelpath.c util/misc.c kern/emu/misc.c
bin_UTILITIES += grub-bin2h bin_UTILITIES += grub-bin2h
grub_bin2h_SOURCES = gnulib/progname.c util/bin2h.c grub_bin2h_SOURCES = gnulib/progname.c util/bin2h.c
@ -104,7 +103,7 @@ DISTCLEANFILES += grub_script.yy.c grub_script.yy.h
bin_UTILITIES += grub-script-check bin_UTILITIES += grub-script-check
util/grub-script-check.c_DEPENDENCIES = grub_script_check_init.h util/grub-script-check.c_DEPENDENCIES = grub_script_check_init.h
grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c \ grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c \
util/grub-script-check.c util/misc.c util/mm.c \ util/grub-script-check.c util/misc.c kern/emu/misc.c kern/emu/mm.c \
script/main.c script/script.c script/function.c script/lexer.c \ script/main.c script/script.c script/function.c script/lexer.c \
kern/handler.c kern/err.c kern/parser.c kern/list.c \ kern/handler.c kern/err.c kern/parser.c kern/list.c \
kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \ kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \
@ -187,7 +186,7 @@ DISTCLEANFILES += grub_fstest_init.c
# for grub-editenv # for grub-editenv
bin_UTILITIES += grub-editenv bin_UTILITIES += grub-editenv
grub_editenv_SOURCES = gnulib/progname.c util/grub-editenv.c lib/envblk.c util/misc.c util/mm.c kern/misc.c kern/err.c grub_editenv_SOURCES = gnulib/progname.c util/grub-editenv.c lib/envblk.c util/misc.c kern/emu/misc.c kern/emu/mm.c kern/misc.c kern/err.c
CLEANFILES += grub-editenv CLEANFILES += grub-editenv
# Needed for genmk.rb to work # Needed for genmk.rb to work
@ -195,7 +194,7 @@ ifeq (0,1)
bin_UTILITIES += grub-macho2img grub-pe2elf bin_UTILITIES += grub-macho2img grub-pe2elf
endif endif
grub_pe2elf_SOURCES = gnulib/progname.c util/grub-pe2elf.c util/misc.c grub_pe2elf_SOURCES = gnulib/progname.c util/grub-pe2elf.c util/misc.c kern/emu/misc.c
CLEANFILES += grub-pe2elf CLEANFILES += grub-pe2elf
grub_macho2img_SOURCES = util/grub-macho2img.c grub_macho2img_SOURCES = util/grub-macho2img.c
@ -855,7 +854,7 @@ boot_mod_CFLAGS = $(COMMON_CFLAGS)
boot_mod_LDFLAGS = $(COMMON_LDFLAGS) boot_mod_LDFLAGS = $(COMMON_LDFLAGS)
bin_UTILITIES += grub-mkpasswd-pbkdf2 bin_UTILITIES += grub-mkpasswd-pbkdf2
grub_mkpasswd_pbkdf2_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c util/grub-mkpasswd-pbkdf2.c lib/crypto.c lib/libgcrypt-grub/cipher/sha512.c lib/pbkdf2.c util/misc.c util/mm.c kern/err.c grub_mkpasswd_pbkdf2_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c util/grub-mkpasswd-pbkdf2.c lib/crypto.c lib/libgcrypt-grub/cipher/sha512.c lib/pbkdf2.c util/misc.c kern/emu/misc.c kern/emu/mm.c kern/err.c
grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(srcdir)/lib/libgcrypt_wrap -DGRUB_MKPASSWD=1 grub_mkpasswd_pbkdf2_CFLAGS += -Wno-missing-field-initializers -Wno-error -I$(srcdir)/lib/libgcrypt_wrap -DGRUB_MKPASSWD=1
include $(srcdir)/conf/gcry.mk include $(srcdir)/conf/gcry.mk

View file

@ -25,7 +25,7 @@
#include <grub/lvm.h> #include <grub/lvm.h>
#ifdef GRUB_UTIL #ifdef GRUB_UTIL
#include <grub/util/misc.h> #include <grub/emu/misc.h>
#endif #endif
static struct grub_lvm_vg *vg_list; static struct grub_lvm_vg *vg_list;

View file

@ -28,4 +28,14 @@ void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...);
void EXPORT_FUNC(grub_util_info) (const char *fmt, ...); void EXPORT_FUNC(grub_util_info) (const char *fmt, ...);
void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((noreturn)); void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((noreturn));
#ifndef HAVE_VASPRINTF
int EXPORT_FUNC(vasprintf) (char **buf, const char *fmt, va_list ap);
#endif
#ifndef HAVE_ASPRINTF
int EXPORT_FUNC(asprintf) (char **buf, const char *fmt, ...);
#endif
char * EXPORT_FUNC(xasprintf) (const char *fmt, ...);
#endif /* GRUB_EMU_MISC_H */ #endif /* GRUB_EMU_MISC_H */

View file

@ -28,26 +28,7 @@
#include <config.h> #include <config.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/symbol.h> #include <grub/symbol.h>
#include <grub/emu/misc.h>
#ifdef __NetBSD__
/* NetBSD uses /boot for its boot block. */
# define DEFAULT_DIRECTORY "/grub"
#else
# define DEFAULT_DIRECTORY "/boot/grub"
#endif
#define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map"
extern char *progname;
extern int verbosity;
void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...);
void EXPORT_FUNC(grub_util_info) (const char *fmt, ...);
void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((noreturn));
void *xmalloc (size_t size);
void *xrealloc (void *ptr, size_t size);
char *xstrdup (const char *str);
char *grub_util_get_path (const char *dir, const char *file); char *grub_util_get_path (const char *dir, const char *file);
size_t grub_util_get_fp_size (FILE *fp); size_t grub_util_get_fp_size (FILE *fp);
@ -59,20 +40,6 @@ 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
int asprintf (char **buf, const char *fmt, ...);
#endif
char *xasprintf (const char *fmt, ...);
#ifdef __MINGW32__ #ifdef __MINGW32__
#define fseeko fseeko64 #define fseeko fseeko64

View file

@ -62,70 +62,14 @@ grub_util_error (const char *fmt, ...)
exit (1); exit (1);
} }
void *
grub_malloc (grub_size_t size)
{
return malloc (size);
}
void *
grub_zalloc (grub_size_t size)
{
void *ret;
ret = malloc (size);
memset (ret, 0, size);
return ret;
}
void
grub_free (void *ptr)
{
free (ptr);
}
void *
grub_realloc (void *ptr, grub_size_t size)
{
return realloc (ptr, size);
}
void *
grub_memalign (grub_size_t align, grub_size_t size)
{
void *p;
#if defined(HAVE_POSIX_MEMALIGN)
if (align < sizeof (void *))
align = sizeof (void *);
else if (align % sizeof (void *))
grub_fatal ("bad alignment");
if (posix_memalign (&p, align, size) != 0)
p = 0;
#elif defined(HAVE_MEMALIGN)
p = memalign (align, size);
#else
(void) align;
(void) size;
grub_fatal ("grub_memalign is not supported");
#endif
if (! p)
grub_fatal ("out of memory");
return p;
}
void * void *
xmalloc (grub_size_t size) xmalloc (grub_size_t size)
{ {
void *p; void *p;
p = grub_malloc (size); p = malloc (size);
if (! p) if (! p)
grub_fatal ("out of memory"); grub_util_error ("out of memory");
return p; return p;
} }
@ -133,9 +77,9 @@ xmalloc (grub_size_t size)
void * void *
xrealloc (void *ptr, grub_size_t size) xrealloc (void *ptr, grub_size_t size)
{ {
ptr = grub_realloc (ptr, size); ptr = realloc (ptr, size);
if (! ptr) if (! ptr)
grub_fatal ("out of memory"); grub_util_error ("out of memory");
return ptr; return ptr;
} }
@ -146,13 +90,43 @@ xstrdup (const char *str)
size_t len; size_t len;
char *newstr; char *newstr;
len = grub_strlen (str); len = strlen (str);
newstr = (char *) xmalloc (len + 1); newstr = (char *) xmalloc (len + 1);
grub_memcpy (newstr, str, len + 1); memcpy (newstr, str, len + 1);
return newstr; return newstr;
} }
#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
int
asprintf (char **buf, const char *fmt, ...)
{
int status;
va_list ap;
va_start (ap, fmt);
status = vasprintf (*buf, fmt, ap);
va_end (ap);
return status;
}
#endif
char * char *
xasprintf (const char *fmt, ...) xasprintf (const char *fmt, ...)
{ {

View file

@ -55,8 +55,6 @@ grub_realloc (void *ptr, grub_size_t size)
{ {
void *ret; void *ret;
ret = realloc (ptr, size); ret = realloc (ptr, size);
if (!ret)
grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
return ret; return ret;
} }
@ -78,8 +76,5 @@ grub_memalign (grub_size_t align, grub_size_t size)
grub_util_error ("grub_memalign is not supported"); grub_util_error ("grub_memalign is not supported");
#endif #endif
if (!p)
grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
return p; return p;
} }

View file

@ -17,6 +17,7 @@
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include <grub/util/misc.h> #include <grub/util/misc.h>
#include <grub/i18n.h> #include <grub/i18n.h>
#include <getopt.h> #include <getopt.h>

View file

@ -19,7 +19,7 @@
#include <config.h> #include <config.h>
#include <grub/types.h> #include <grub/types.h>
#include <grub/emu/misc.h> #include <grub/util/misc.h>
#include <grub/util/misc.h> #include <grub/util/misc.h>
#include <grub/device.h> #include <grub/device.h>
#include <grub/disk.h> #include <grub/disk.h>

View file

@ -58,53 +58,6 @@
#include <winioctl.h> #include <winioctl.h>
#endif #endif
int verbosity = 0;
void
grub_util_warn (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, _("%s: warn:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
fflush (stderr);
}
void
grub_util_info (const char *fmt, ...)
{
if (verbosity > 0)
{
va_list ap;
fprintf (stderr, _("%s: info:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
fflush (stderr);
}
}
void
grub_util_error (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, _("%s: error:"), program_name);
fprintf (stderr, " ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, ".\n");
exit (1);
}
#ifdef GRUB_UTIL #ifdef GRUB_UTIL
int int
grub_err_printf (const char *fmt, ...) grub_err_printf (const char *fmt, ...)
@ -120,41 +73,6 @@ grub_err_printf (const char *fmt, ...)
} }
#endif #endif
void *
xmalloc (size_t size)
{
void *p;
p = malloc (size);
if (! p)
grub_util_error ("out of memory");
return p;
}
void *
xrealloc (void *ptr, size_t size)
{
ptr = realloc (ptr, size);
if (! ptr)
grub_util_error ("out of memory");
return ptr;
}
char *
xstrdup (const char *str)
{
size_t len;
char *newstr;
len = strlen (str);
newstr = (char *) xmalloc (len + 1);
memcpy (newstr, str, len + 1);
return newstr;
}
char * char *
grub_util_get_path (const char *dir, const char *file) grub_util_get_path (const char *dir, const char *file)
{ {
@ -277,34 +195,6 @@ grub_register_exported_symbols (void)
} }
#endif #endif
void
grub_exit (void)
{
exit (1);
}
grub_uint32_t
grub_get_rtc (void)
{
struct timeval tv;
gettimeofday (&tv, 0);
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
* GRUB_TICKS_PER_SECOND / 1000000));
}
grub_uint64_t
grub_get_time_ms (void)
{
struct timeval tv;
gettimeofday (&tv, 0);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
#ifdef __MINGW32__ #ifdef __MINGW32__
void void
@ -335,53 +225,6 @@ grub_arch_sync_caches (void *address __attribute__ ((unused)),
} }
#endif #endif
#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
int
asprintf (char **buf, const char *fmt, ...)
{
int status;
va_list ap;
va_start (ap, fmt);
status = vasprintf (*buf, fmt, ap);
va_end (ap);
return status;
}
#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)