Add a wrapper for fopen. On unix-like systems just pass-through. On

windows use unicode version.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-13 20:36:28 +02:00
parent ae5540d3d4
commit bb338aaf24
27 changed files with 97 additions and 41 deletions

View File

@ -1,3 +1,8 @@
2013-10-13 Vladimir Serbinenko <phcoder@gmail.com>
Add a wrapper for fopen. On unix-like systems just pass-through. On
windows use unicode version.
2013-10-13 Vladimir Serbinenko <phcoder@gmail.com> 2013-10-13 Vladimir Serbinenko <phcoder@gmail.com>
Move set_program_name and init_nls to host_init. On windows Move set_program_name and init_nls to host_init. On windows

View File

@ -295,7 +295,7 @@ main (int argc, char **argv)
unsigned char *buf; unsigned char *buf;
if (argc < 2) if (argc < 2)
printf ("Usage: %s FILE\n", argv[0]); printf ("Usage: %s FILE\n", argv[0]);
f = fopen (argv[1], "rb"); f = grub_util_fopen (argv[1], "rb");
if (!f) if (!f)
{ {
printf ("Couldn't open file\n"); printf ("Couldn't open file\n");

View File

@ -435,7 +435,7 @@ read_device_map (const char *dev_map)
return; return;
} }
fp = fopen (dev_map, "r"); fp = grub_util_fopen (dev_map, "r");
if (! fp) if (! fp)
{ {
grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno)); grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno));

View File

@ -109,7 +109,7 @@ grub_hostfs_open (struct grub_file *file, const char *name)
FILE *f; FILE *f;
struct grub_hostfs_data *data; struct grub_hostfs_data *data;
f = fopen (name, "rb"); f = grub_util_fopen (name, "rb");
if (! f) if (! f)
return grub_error (GRUB_ERR_BAD_FILENAME, return grub_error (GRUB_ERR_BAD_FILENAME,
N_("can't open `%s': %s"), name, N_("can't open `%s': %s"), name,

View File

@ -425,7 +425,7 @@ main (int argc, char **argv)
#endif #endif
#ifndef STANDALONE #ifndef STANDALONE
in = fopen ("tst.bin", "rb"); in = grub_util_fopen ("tst.bin", "rb");
if (!in) if (!in)
return 1; return 1;
fseek (in, 0, SEEK_END); fseek (in, 0, SEEK_END);
@ -438,11 +438,11 @@ main (int argc, char **argv)
grub_reed_solomon_add_redundancy (buf, s, rs); grub_reed_solomon_add_redundancy (buf, s, rs);
out = fopen ("tst_rs.bin", "wb"); out = grub_util_fopen ("tst_rs.bin", "wb");
fwrite (buf, 1, s + rs, out); fwrite (buf, 1, s + rs, out);
fclose (out); fclose (out);
#else #else
out = fopen ("tst_rs.bin", "rb"); out = grub_util_fopen ("tst_rs.bin", "rb");
fseek (out, 0, SEEK_END); fseek (out, 0, SEEK_END);
s = ftell (out); s = ftell (out);
fseek (out, 0, SEEK_SET); fseek (out, 0, SEEK_SET);
@ -457,12 +457,12 @@ main (int argc, char **argv)
grub_memset (buf + 512 * 15, 0, 512); grub_memset (buf + 512 * 15, 0, 512);
#endif #endif
out = fopen ("tst_dam.bin", "wb"); out = grub_util_fopen ("tst_dam.bin", "wb");
fwrite (buf, 1, s + rs, out); fwrite (buf, 1, s + rs, out);
fclose (out); fclose (out);
grub_reed_solomon_recover (buf, s, rs); grub_reed_solomon_recover (buf, s, rs);
out = fopen ("tst_rec.bin", "wb"); out = grub_util_fopen ("tst_rec.bin", "wb");
fwrite (buf, 1, s, out); fwrite (buf, 1, s, out);
fclose (out); fclose (out);

View File

@ -499,3 +499,9 @@ grub_util_fd_strerror (void)
return buf + 1; return buf + 1;
return buf; return buf;
} }
FILE *
grub_util_fopen (const char *path, const char *mode)
{
return fopen (path, mode);
}

View File

@ -239,7 +239,7 @@ grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
if (relroot) if (relroot)
*relroot = NULL; *relroot = NULL;
fp = fopen ("/proc/self/mountinfo", "r"); fp = grub_util_fopen ("/proc/self/mountinfo", "r");
if (! fp) if (! fp)
return NULL; /* fall through to other methods */ return NULL; /* fall through to other methods */

View File

@ -721,7 +721,7 @@ grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
if (stat (dir, &st) != 0) if (stat (dir, &st) != 0)
return; return;
FILE *mnttab = fopen ("/etc/mnttab", "r"); FILE *mnttab = grub_util_fopen ("/etc/mnttab", "r");
if (! mnttab) if (! mnttab)
return; return;

View File

@ -211,4 +211,10 @@ canonicalize_file_name (const char *path)
#endif #endif
} }
FILE *
grub_util_fopen (const char *path, const char *mode)
{
return fopen (path, mode);
}
#endif #endif

View File

@ -36,7 +36,7 @@ grub_get_random (void *out, grub_size_t len)
FILE *f; FILE *f;
size_t rd; size_t rd;
f = fopen ("/dev/urandom", "rb"); f = grub_util_fopen ("/dev/urandom", "rb");
if (!f) if (!f)
return 1; return 1;
rd = fread (out, 1, len, f); rd = fread (out, 1, len, f);

View File

@ -291,9 +291,35 @@ canonicalize_file_name (const char *path)
#ifdef __MINGW32__ #ifdef __MINGW32__
FILE *
grub_util_fopen (const char *path, const char *mode)
{
LPTSTR tpath;
FILE *ret;
tpath = grub_util_get_windows_path (path);
#if SIZEOF_TCHAR == 1
ret = fopen (tpath, tmode);
#else
LPTSTR tmode;
tmode = grub_util_utf8_to_tchar (mode);
ret = _wfopen (tpath, tmode);
free (tmode);
#endif
free (tpath);
return ret;
}
int fsync (int fno __attribute__ ((unused))) int fsync (int fno __attribute__ ((unused)))
{ {
return 0; return 0;
} }
#else
FILE *
grub_util_fopen (const char *path, const char *mode)
{
return fopen (path, mode);
}
#endif #endif

View File

@ -22,6 +22,8 @@
#include <config.h> #include <config.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <grub/symbol.h> #include <grub/symbol.h>
#include <grub/types.h> #include <grub/types.h>
@ -54,4 +56,11 @@ extern char * canonicalize_file_name (const char *path);
int grub_device_mapper_supported (void); int grub_device_mapper_supported (void);
#endif #endif
#ifdef GRUB_BUILD
#define grub_util_fopen fopen
#else
FILE *
grub_util_fopen (const char *path, const char *mode);
#endif
#endif /* GRUB_EMU_MISC_H */ #endif /* GRUB_EMU_MISC_H */

View File

@ -119,7 +119,7 @@ create_envblk_file (const char *name)
buf = xmalloc (DEFAULT_ENVBLK_SIZE); buf = xmalloc (DEFAULT_ENVBLK_SIZE);
namenew = xasprintf ("%s.new", name); namenew = xasprintf ("%s.new", name);
fp = fopen (namenew, "wb"); fp = grub_util_fopen (namenew, "wb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), namenew, grub_util_error (_("cannot open `%s': %s"), namenew,
strerror (errno)); strerror (errno));
@ -149,12 +149,12 @@ open_envblk_file (const char *name)
size_t size; size_t size;
grub_envblk_t envblk; grub_envblk_t envblk;
fp = fopen (name, "rb"); fp = grub_util_fopen (name, "rb");
if (! fp) if (! fp)
{ {
/* Create the file implicitly. */ /* Create the file implicitly. */
create_envblk_file (name); create_envblk_file (name);
fp = fopen (name, "rb"); fp = grub_util_fopen (name, "rb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), name, grub_util_error (_("cannot open `%s': %s"), name,
strerror (errno)); strerror (errno));
@ -208,7 +208,7 @@ write_envblk (const char *name, grub_envblk_t envblk)
{ {
FILE *fp; FILE *fp;
fp = fopen (name, "wb"); fp = grub_util_fopen (name, "wb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), name, grub_util_error (_("cannot open `%s': %s"), name,
strerror (errno)); strerror (errno));

View File

@ -200,7 +200,7 @@ cmd_cp (char *src, const char *dest)
.dest = dest .dest = dest
}; };
ctx.ff = fopen (dest, "wb"); ctx.ff = grub_util_fopen (dest, "wb");
if (ctx.ff == NULL) if (ctx.ff == NULL)
{ {
grub_util_error (_("cannot open OS file `%s': %s"), dest, grub_util_error (_("cannot open OS file `%s': %s"), dest,
@ -310,7 +310,7 @@ cmd_cmp (char *src, char *dest)
return; return;
} }
ff = fopen (dest, "rb"); ff = grub_util_fopen (dest, "rb");
if (ff == NULL) if (ff == NULL)
{ {
grub_util_error (_("OS file %s open error: %s"), dest, grub_util_error (_("OS file %s open error: %s"), dest,
@ -562,7 +562,7 @@ argp_parser (int key, char *arg, struct argp_state *state)
FILE *f; FILE *f;
ssize_t real_size; ssize_t real_size;
grub_uint8_t buf[1024]; grub_uint8_t buf[1024];
f = fopen (arg, "rb"); f = grub_util_fopen (arg, "rb");
if (!f) if (!f)
{ {
printf (_("%s: error:"), program_name); printf (_("%s: error:"), program_name);

View File

@ -184,19 +184,19 @@ main (int argc, char *argv[])
exit(1); exit(1);
} }
in32 = fopen (arguments.input32, "r"); in32 = grub_util_fopen (arguments.input32, "r");
if (!in32) if (!in32)
grub_util_error (_("cannot open `%s': %s"), arguments.input32, grub_util_error (_("cannot open `%s': %s"), arguments.input32,
strerror (errno)); strerror (errno));
in64 = fopen (arguments.input64, "r"); in64 = grub_util_fopen (arguments.input64, "r");
if (!in64) if (!in64)
grub_util_error (_("cannot open `%s': %s"), arguments.input64, grub_util_error (_("cannot open `%s': %s"), arguments.input64,
strerror (errno)); strerror (errno));
if (arguments.output) if (arguments.output)
out = fopen (arguments.output, "wb"); out = grub_util_fopen (arguments.output, "wb");
else else
out = stdout; out = stdout;

View File

@ -52,13 +52,13 @@ main (int argc, char **argv)
"Convert Mach-O into raw image\n", argv[0]); "Convert Mach-O into raw image\n", argv[0]);
return 0; return 0;
} }
in = fopen (argv[1 + do_bss], "rb"); in = grub_util_fopen (argv[1 + do_bss], "rb");
if (! in) if (! in)
{ {
printf ("Couldn't open %s\n", argv[1 + do_bss]); printf ("Couldn't open %s\n", argv[1 + do_bss]);
return 1; return 1;
} }
out = fopen (argv[2 + do_bss], "wb"); out = grub_util_fopen (argv[2 + do_bss], "wb");
if (! out) if (! out)
{ {
fclose (in); fclose (in);

View File

@ -47,7 +47,7 @@ main (int argc, char **argv)
if (argc >= 2) if (argc >= 2)
{ {
in = fopen (argv[1], "r"); in = grub_util_fopen (argv[1], "r");
if (!in) if (!in)
{ {
fprintf (stderr, _("cannot open `%s': %s"), fprintf (stderr, _("cannot open `%s': %s"),
@ -60,7 +60,7 @@ main (int argc, char **argv)
if (argc >= 3) if (argc >= 3)
{ {
out = fopen (argv[2], "w"); out = grub_util_fopen (argv[2], "w");
if (!out) if (!out)
{ {
if (in != stdin) if (in != stdin)

View File

@ -55,6 +55,10 @@
#include "progname.h" #include "progname.h"
#endif #endif
#ifdef GRUB_BUILD
#define grub_util_fopen fopen
#endif
#define GRUB_FONT_DEFAULT_SIZE 16 #define GRUB_FONT_DEFAULT_SIZE 16
#define GRUB_FONT_RANGE_BLOCK 1024 #define GRUB_FONT_RANGE_BLOCK 1024
@ -785,7 +789,7 @@ write_font_pf2 (struct grub_font_info *font_info, char *output_file)
int offset; int offset;
struct grub_glyph_info *cur; struct grub_glyph_info *cur;
file = fopen (output_file, "wb"); file = grub_util_fopen (output_file, "wb");
if (! file) if (! file)
grub_util_error (_("cannot write to `%s': %s"), output_file, grub_util_error (_("cannot write to `%s': %s"), output_file,
strerror (errno)); strerror (errno));

View File

@ -2052,7 +2052,7 @@ main (int argc, char *argv[])
if (arguments.output) if (arguments.output)
{ {
fp = fopen (arguments.output, "wb"); fp = grub_util_fopen (arguments.output, "wb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), arguments.output, grub_util_error (_("cannot open `%s': %s"), arguments.output,
strerror (errno)); strerror (errno));

View File

@ -482,7 +482,7 @@ main (int argc, char *argv[])
} }
if (arguments.input) if (arguments.input)
in = fopen (arguments.input, "r"); in = grub_util_fopen (arguments.input, "r");
else else
in = stdin; in = stdin;
@ -491,7 +491,7 @@ main (int argc, char *argv[])
strerror (errno)); strerror (errno));
if (arguments.output) if (arguments.output)
out = fopen (arguments.output, "wb"); out = grub_util_fopen (arguments.output, "wb");
else else
out = stdout; out = stdout;

View File

@ -474,7 +474,7 @@ argp_parser (int key, char *arg, struct argp_state *state)
FILE *f; FILE *f;
ssize_t real_size; ssize_t real_size;
grub_uint8_t buf[1024]; grub_uint8_t buf[1024];
f = fopen (arg, "rb"); f = grub_util_fopen (arg, "rb");
if (!f) if (!f)
{ {
printf (_("%s: error:"), program_name); printf (_("%s: error:"), program_name);

View File

@ -478,7 +478,7 @@ main (int argc, char *argv[])
out = in; out = in;
image = grub_util_read_image (in); image = grub_util_read_image (in);
fp = fopen (out, "wb"); fp = grub_util_fopen (out, "wb");
if (! fp) if (! fp)
grub_util_error ("cannot open %s", out); grub_util_error ("cannot open %s", out);

View File

@ -236,7 +236,7 @@ main (int argc, char *argv[])
text = arguments.text; text = arguments.text;
else else
{ {
FILE *in = fopen (arguments.input, "r"); FILE *in = grub_util_fopen (arguments.input, "r");
size_t s; size_t s;
if (!in) if (!in)
grub_util_error (_("cannot open `%s': %s"), arguments.input, grub_util_error (_("cannot open `%s': %s"), arguments.input,
@ -253,7 +253,7 @@ main (int argc, char *argv[])
} }
if (arguments.output) if (arguments.output)
out = fopen (arguments.output, "wb"); out = grub_util_fopen (arguments.output, "wb");
else else
out = stdout; out = stdout;
if (!out) if (!out)

View File

@ -164,7 +164,7 @@ main (int argc, char *argv[])
} }
else else
{ {
ctx.file = fopen (ctx.arguments.filename, "r"); ctx.file = grub_util_fopen (ctx.arguments.filename, "r");
if (! ctx.file) if (! ctx.file)
{ {
char *program = xstrdup(program_name); char *program = xstrdup(program_name);

View File

@ -662,14 +662,14 @@ unable_to_embed:
FILE *dump; FILE *dump;
FILE *dump2; FILE *dump2;
dump = fopen ("dump.img", "wb"); dump = grub_util_fopen ("dump.img", "wb");
if (dump) if (dump)
{ {
fwrite (tmp_img, 1, core_size, dump); fwrite (tmp_img, 1, core_size, dump);
fclose (dump); fclose (dump);
} }
dump2 = fopen ("dump2.img", "wb"); dump2 = grub_util_fopen ("dump2.img", "wb");
if (dump2) if (dump2)
{ {
fwrite (core_img, 1, core_size, dump2); fwrite (core_img, 1, core_size, dump2);
@ -731,7 +731,7 @@ unable_to_embed:
/* Write the first two sectors of the core image onto the disk. */ /* Write the first two sectors of the core image onto the disk. */
grub_util_info ("opening the core image `%s'", core_path); grub_util_info ("opening the core image `%s'", core_path);
fp = fopen (core_path, "rb"); fp = grub_util_fopen (core_path, "rb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), core_path, grub_util_error (_("cannot open `%s': %s"), core_path,
strerror (errno)); strerror (errno));
@ -893,7 +893,7 @@ unable_to_embed:
/* Write the first two sectors of the core image onto the disk. */ /* Write the first two sectors of the core image onto the disk. */
grub_util_info ("opening the core image `%s'", core_path); grub_util_info ("opening the core image `%s'", core_path);
fp = fopen (core_path, "r+b"); fp = grub_util_fopen (core_path, "r+b");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), core_path, grub_util_error (_("cannot open `%s': %s"), core_path,
strerror (errno)); strerror (errno));

View File

@ -101,7 +101,7 @@ grub_util_read_image (const char *path)
size = grub_util_get_image_size (path); size = grub_util_get_image_size (path);
img = (char *) xmalloc (size); img = (char *) xmalloc (size);
fp = fopen (path, "rb"); fp = grub_util_fopen (path, "rb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), path, grub_util_error (_("cannot open `%s': %s"), path,
strerror (errno)); strerror (errno));
@ -125,7 +125,7 @@ grub_util_load_image (const char *path, char *buf)
size = grub_util_get_image_size (path); size = grub_util_get_image_size (path);
fp = fopen (path, "rb"); fp = grub_util_fopen (path, "rb");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), path, grub_util_error (_("cannot open `%s': %s"), path,
strerror (errno)); strerror (errno));

View File

@ -241,7 +241,7 @@ grub_util_resolve_dependencies (const char *prefix,
struct grub_util_path_list *path_list = 0; struct grub_util_path_list *path_list = 0;
path = grub_util_get_path (prefix, dep_list_file); path = grub_util_get_path (prefix, dep_list_file);
fp = fopen (path, "r"); fp = grub_util_fopen (path, "r");
if (! fp) if (! fp)
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno)); grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));