Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / misc.c
blobb8ec69108cbc91d44ac75a44625879f43dfd925b
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include <errno.h>
22 #include <setjmp.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <time.h>
32 #include <grub/kernel.h>
33 #include <grub/dl.h>
34 #include <grub/misc.h>
35 #include <grub/cache.h>
36 #include <grub/emu/misc.h>
37 #include <grub/util/misc.h>
38 #include <grub/mm.h>
39 #include <grub/term.h>
40 #include <grub/time.h>
41 #include <grub/i18n.h>
42 #include <grub/script_sh.h>
43 #include <grub/emu/hostfile.h>
45 #define ENABLE_RELOCATABLE 0
46 #ifdef GRUB_BUILD
47 const char *program_name = GRUB_BUILD_PROGRAM_NAME;
48 #else
49 #include "progname.h"
50 #endif
52 #ifdef GRUB_UTIL
53 int
54 grub_err_printf (const char *fmt, ...)
56 va_list ap;
57 int ret;
59 va_start (ap, fmt);
60 ret = vfprintf (stderr, fmt, ap);
61 va_end (ap);
63 return ret;
65 #endif
67 char *
68 grub_util_get_path (const char *dir, const char *file)
70 char *path;
72 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
73 sprintf (path, "%s/%s", dir, file);
74 return path;
77 size_t
78 grub_util_get_image_size (const char *path)
80 FILE *f;
81 size_t ret;
82 off_t sz;
84 f = grub_util_fopen (path, "rb");
86 if (!f)
87 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
89 fseeko (f, 0, SEEK_END);
91 sz = ftello (f);
92 if (sz < 0)
93 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
94 if (sz != (size_t) sz)
95 grub_util_error (_("file `%s' is too big"), path);
96 ret = (size_t) sz;
98 fclose (f);
100 return ret;
103 char *
104 grub_util_read_image (const char *path)
106 char *img;
107 FILE *fp;
108 size_t size;
110 grub_util_info ("reading %s", path);
112 size = grub_util_get_image_size (path);
113 img = (char *) xmalloc (size);
115 fp = grub_util_fopen (path, "rb");
116 if (! fp)
117 grub_util_error (_("cannot open `%s': %s"), path,
118 strerror (errno));
120 if (fread (img, 1, size, fp) != size)
121 grub_util_error (_("cannot read `%s': %s"), path,
122 strerror (errno));
124 fclose (fp);
126 return img;
129 void
130 grub_util_load_image (const char *path, char *buf)
132 FILE *fp;
133 size_t size;
135 grub_util_info ("reading %s", path);
137 size = grub_util_get_image_size (path);
139 fp = grub_util_fopen (path, "rb");
140 if (! fp)
141 grub_util_error (_("cannot open `%s': %s"), path,
142 strerror (errno));
144 if (fread (buf, 1, size, fp) != size)
145 grub_util_error (_("cannot read `%s': %s"), path,
146 strerror (errno));
148 fclose (fp);
151 void
152 grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out,
153 const char *name)
155 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes at offset 0x%"
156 GRUB_HOST_PRIxLONG_LONG,
157 (unsigned long long) size, (unsigned long long) offset);
158 if (fseeko (out, offset, SEEK_SET) == -1)
159 grub_util_error (_("cannot seek `%s': %s"),
160 name, strerror (errno));
161 if (fwrite (img, 1, size, out) != size)
162 grub_util_error (_("cannot write to `%s': %s"),
163 name, strerror (errno));
166 void
167 grub_util_write_image (const char *img, size_t size, FILE *out,
168 const char *name)
170 grub_util_info ("writing 0x%" GRUB_HOST_PRIxLONG_LONG " bytes", (unsigned long long) size);
171 if (fwrite (img, 1, size, out) != size)
173 if (!name)
174 grub_util_error (_("cannot write to the stdout: %s"),
175 strerror (errno));
176 else
177 grub_util_error (_("cannot write to `%s': %s"),
178 name, strerror (errno));
182 grub_err_t
183 grub_script_execute_cmdline (struct grub_script_cmd *cmd __attribute__ ((unused)))
185 return 0;
188 grub_err_t
189 grub_script_execute_cmdlist (struct grub_script_cmd *cmd __attribute__ ((unused)))
191 return 0;
194 grub_err_t
195 grub_script_execute_cmdif (struct grub_script_cmd *cmd __attribute__ ((unused)))
197 return 0;
200 grub_err_t
201 grub_script_execute_cmdfor (struct grub_script_cmd *cmd __attribute__ ((unused)))
203 return 0;
206 grub_err_t
207 grub_script_execute_cmdwhile (struct grub_script_cmd *cmd __attribute__ ((unused)))
209 return 0;
212 grub_err_t
213 grub_script_execute (struct grub_script *script)
215 if (script == 0 || script->cmd == 0)
216 return 0;
218 return script->cmd->exec (script->cmd);
222 grub_getkey (void)
224 return -1;
227 void
228 grub_refresh (void)
230 fflush (stdout);
233 static void
234 grub_xputs_real (const char *str)
236 fputs (str, stdout);
239 void (*grub_xputs) (const char *str) = grub_xputs_real;
242 grub_dl_ref (grub_dl_t mod)
244 (void) mod;
245 return 0;
249 grub_dl_unref (grub_dl_t mod)
251 (void) mod;
252 return 0;
255 /* Some functions that we don't use. */
256 void
257 grub_mm_init_region (void *addr __attribute__ ((unused)),
258 grub_size_t size __attribute__ ((unused)))
262 void
263 grub_register_exported_symbols (void)
267 /* Used in comparison of arrays of strings with qsort */
269 grub_qsort_strcmp (const void *p1, const void *p2)
271 return strcmp(*(char *const *)p1, *(char *const *)p2);