Forgot tar.c
[grub2/phcoder.git] / util / misc.c
blob559022ff19cb89ca66814d1f7f9ce6cbca626a90
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2006,2007,2008 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 <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <unistd.h>
30 #include <grub/kernel.h>
31 #include <grub/misc.h>
32 #include <grub/cache.h>
33 #include <grub/util/misc.h>
34 #include <grub/mm.h>
35 #include <grub/term.h>
36 #include <grub/time.h>
37 #include <grub/machine/time.h>
39 /* Include malloc.h, only if memalign is available. It is known that
40 memalign is declared in malloc.h in all systems, if present. */
41 #ifdef HAVE_MEMALIGN
42 # include <malloc.h>
43 #endif
45 char *progname = 0;
46 int verbosity = 0;
48 void
49 grub_util_info (const char *fmt, ...)
51 if (verbosity > 0)
53 va_list ap;
55 fprintf (stderr, "%s: info: ", progname);
56 va_start (ap, fmt);
57 vfprintf (stderr, fmt, ap);
58 va_end (ap);
59 fputc ('\n', stderr);
60 fflush (stderr);
64 void
65 grub_util_error (const char *fmt, ...)
67 va_list ap;
69 fprintf (stderr, "%s: error: ", progname);
70 va_start (ap, fmt);
71 vfprintf (stderr, fmt, ap);
72 va_end (ap);
73 fputc ('\n', stderr);
74 exit (1);
77 int
78 grub_err_printf (const char *fmt, ...)
80 va_list ap;
81 int ret;
83 va_start (ap, fmt);
84 ret = vfprintf (stderr, fmt, ap);
85 va_end (ap);
87 return ret;
90 void *
91 xmalloc (size_t size)
93 void *p;
95 p = malloc (size);
96 if (! p)
97 grub_util_error ("out of memory");
99 return p;
102 void *
103 xrealloc (void *ptr, size_t size)
105 ptr = realloc (ptr, size);
106 if (! ptr)
107 grub_util_error ("out of memory");
109 return ptr;
112 char *
113 xstrdup (const char *str)
115 size_t len;
116 char *dup;
118 len = strlen (str);
119 dup = (char *) xmalloc (len + 1);
120 memcpy (dup, str, len + 1);
122 return dup;
125 char *
126 grub_util_get_path (const char *dir, const char *file)
128 char *path;
130 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
131 sprintf (path, "%s/%s", dir, file);
132 return path;
135 size_t
136 grub_util_get_fp_size (FILE *fp)
138 struct stat st;
140 if (fflush (fp) == EOF)
141 grub_util_error ("fflush failed");
143 if (fstat (fileno (fp), &st) == -1)
144 grub_util_error ("fstat failed");
146 return st.st_size;
149 size_t
150 grub_util_get_image_size (const char *path)
152 struct stat st;
154 grub_util_info ("getting the size of %s", path);
156 if (stat (path, &st) == -1)
157 grub_util_error ("cannot stat %s", path);
159 return st.st_size;
162 void
163 grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
165 if (fseeko (fp, offset, SEEK_SET) == -1)
166 grub_util_error ("seek failed");
168 if (fread (img, 1, size, fp) != size)
169 grub_util_error ("read failed");
172 char *
173 grub_util_read_image (const char *path)
175 char *img;
176 FILE *fp;
177 size_t size;
179 grub_util_info ("reading %s", path);
181 size = grub_util_get_image_size (path);
182 img = (char *) xmalloc (size);
184 fp = fopen (path, "rb");
185 if (! fp)
186 grub_util_error ("cannot open %s", path);
188 grub_util_read_at (img, size, 0, fp);
190 fclose (fp);
192 return img;
195 void
196 grub_util_load_image (const char *path, char *buf)
198 FILE *fp;
199 size_t size;
201 grub_util_info ("reading %s", path);
203 size = grub_util_get_image_size (path);
205 fp = fopen (path, "rb");
206 if (! fp)
207 grub_util_error ("cannot open %s", path);
209 if (fread (buf, 1, size, fp) != size)
210 grub_util_error ("cannot read %s", path);
212 fclose (fp);
215 void
216 grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
218 grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
219 if (fseeko (out, offset, SEEK_SET) == -1)
220 grub_util_error ("seek failed");
221 if (fwrite (img, 1, size, out) != size)
222 grub_util_error ("write failed");
225 void
226 grub_util_write_image (const char *img, size_t size, FILE *out)
228 grub_util_info ("writing 0x%x bytes", size);
229 if (fwrite (img, 1, size, out) != size)
230 grub_util_error ("write failed");
233 void *
234 grub_malloc (grub_size_t size)
236 return xmalloc (size);
239 void
240 grub_free (void *ptr)
242 free (ptr);
245 void *
246 grub_realloc (void *ptr, grub_size_t size)
248 return xrealloc (ptr, size);
251 void *
252 grub_memalign (grub_size_t align, grub_size_t size)
254 void *p;
256 #if defined(HAVE_POSIX_MEMALIGN)
257 if (posix_memalign (&p, align, size) != 0)
258 p = 0;
259 #elif defined(HAVE_MEMALIGN)
260 p = memalign (align, size);
261 #else
262 (void) align;
263 (void) size;
264 grub_util_error ("grub_memalign is not supported");
265 #endif
267 if (! p)
268 grub_util_error ("out of memory");
270 return p;
273 /* Some functions that we don't use. */
274 void
275 grub_mm_init_region (void *addr __attribute__ ((unused)),
276 grub_size_t size __attribute__ ((unused)))
280 void
281 grub_register_exported_symbols (void)
285 void
286 grub_exit (void)
288 exit (1);
291 grub_uint32_t
292 grub_get_rtc (void)
294 struct timeval tv;
296 gettimeofday (&tv, 0);
298 return (tv.tv_sec * GRUB_TICKS_PER_SECOND
299 + (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
300 * GRUB_TICKS_PER_SECOND / 1000000));
303 grub_uint64_t
304 grub_get_time_ms (void)
306 struct timeval tv;
308 gettimeofday (&tv, 0);
310 return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
313 void
314 grub_arch_sync_caches (void *address __attribute__ ((unused)),
315 grub_size_t len __attribute__ ((unused)))
319 #ifndef HAVE_ASPRINTF
322 asprintf (char **buf, const char *fmt, ...)
324 int status;
325 va_list ap;
327 /* Should be large enough. */
328 *buf = xmalloc (512);
330 va_start (ap, fmt);
331 status = vsprintf (*buf, fmt, ap);
332 va_end (ap);
334 return status;
337 #endif
339 #ifdef __MINGW32__
341 #include <windows.h>
342 #include <winioctl.h>
344 void sync (void)
348 void sleep (int s)
350 Sleep (s * 1000);
353 grub_int64_t
354 grub_util_get_disk_size (char *name)
356 HANDLE hd;
357 grub_int64_t size = -1LL;
359 hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
360 0, OPEN_EXISTING, 0, 0);
362 if (hd == INVALID_HANDLE_VALUE)
363 return size;
365 if (((name[0] == '/') || (name[0] == '\\')) &&
366 ((name[1] == '/') || (name[1] == '\\')) &&
367 (name[2] == '.') &&
368 ((name[3] == '/') || (name[3] == '\\')) &&
369 (! strncasecmp (name + 4, "PHYSICALDRIVE", 13)))
371 DWORD nr;
372 DISK_GEOMETRY g;
374 if (! DeviceIoControl (hd, IOCTL_DISK_GET_DRIVE_GEOMETRY,
375 0, 0, &g, sizeof (g), &nr, 0))
376 goto fail;
378 size = g.Cylinders.QuadPart;
379 size *= g.TracksPerCylinder * g.SectorsPerTrack * g.BytesPerSector;
381 else
383 LARGE_INTEGER s;
385 s.LowPart = GetFileSize (hd, &s.HighPart);
386 size = s.QuadPart;
389 fail:
391 CloseHandle (hd);
393 return size;
396 #endif