]>
git.proxmox.com Git - grub2.git/blob - util/misc.c
2 * PUPA -- Preliminary Universal Programming Architecture for GRUB
3 * Copyright (C) 2002,2003 Yoshinori K. Okuji <okuji@enbug.org>
4 * Copyright (C) 2003 Marco Gerards <metgerards@student.han.nl>
6 * PUPA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with PUPA; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <sys/types.h>
27 #include <sys/times.h>
29 #include <pupa/util/misc.h>
31 #include <pupa/term.h>
37 pupa_util_info (const char *fmt
, ...)
43 fprintf (stderr
, "%s: info: ", progname
);
45 vfprintf (stderr
, fmt
, ap
);
52 pupa_util_error (const char *fmt
, ...)
56 fprintf (stderr
, "%s: error: ", progname
);
58 vfprintf (stderr
, fmt
, ap
);
71 pupa_util_error ("out of memory");
77 xrealloc (void *ptr
, size_t size
)
79 ptr
= realloc (ptr
, size
);
81 pupa_util_error ("out of memory");
87 xstrdup (const char *str
)
93 dup
= (char *) xmalloc (len
+ 1);
94 memcpy (dup
, str
, len
+ 1);
100 pupa_util_get_path (const char *dir
, const char *file
)
104 path
= (char *) xmalloc (strlen (dir
) + 1 + strlen (file
) + 1);
105 sprintf (path
, "%s/%s", dir
, file
);
110 pupa_util_get_image_size (const char *path
)
114 pupa_util_info ("getting the size of %s", path
);
116 if (stat (path
, &st
) == -1)
117 pupa_util_error ("cannot stat %s", path
);
123 pupa_util_read_image (const char *path
)
129 pupa_util_info ("reading %s", path
);
131 size
= pupa_util_get_image_size (path
);
132 img
= (char *) xmalloc (size
);
134 fp
= fopen (path
, "rb");
136 pupa_util_error ("cannot open %s", path
);
138 if (fread (img
, 1, size
, fp
) != size
)
139 pupa_util_error ("cannot read %s", path
);
147 pupa_util_load_image (const char *path
, char *buf
)
152 pupa_util_info ("reading %s", path
);
154 size
= pupa_util_get_image_size (path
);
156 fp
= fopen (path
, "rb");
158 pupa_util_error ("cannot open %s", path
);
160 if (fread (buf
, 1, size
, fp
) != size
)
161 pupa_util_error ("cannot read %s", path
);
167 pupa_util_write_image (const char *img
, size_t size
, FILE *out
)
169 pupa_util_info ("writing 0x%x bytes", size
);
170 if (fwrite (img
, 1, size
, out
) != size
)
171 pupa_util_error ("write failed");
175 pupa_malloc (unsigned size
)
177 return malloc (size
);
181 pupa_free (void *ptr
)
187 pupa_realloc (void *ptr
, unsigned size
)
189 return realloc (ptr
, size
);
193 pupa_memalign (pupa_size_t align
, pupa_size_t size
)
195 return memalign (align
, size
);
198 /* Some functions that we don't use. */
200 pupa_mm_init_region (void *addr
, pupa_size_t size
)
205 pupa_register_exported_symbols (void)
220 return times (&currtime
);