]> git.proxmox.com Git - grub2.git/blame - util/misc.c
2004-03-14 Marco Gerards <metgerards@student.han.nl>
[grub2.git] / util / misc.c
CommitLineData
6a161fa9 1/*
2 * PUPA -- Preliminary Universal Programming Architecture for GRUB
8367695c 3 * Copyright (C) 2002,2003 Free Software Foundation, Inc.
6a161fa9 4 *
5 * PUPA 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 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with PUPA; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdarg.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
8e72a9c0 26#include <sys/times.h>
db1771cf 27#include <malloc.h>
6a161fa9 28
29#include <pupa/util/misc.h>
1cc73a62 30#include <pupa/mm.h>
31#include <pupa/term.h>
6a161fa9 32
33char *progname = 0;
34int verbosity = 0;
35
36void
37pupa_util_info (const char *fmt, ...)
38{
39 if (verbosity > 0)
40 {
41 va_list ap;
42
43 fprintf (stderr, "%s: info: ", progname);
44 va_start (ap, fmt);
45 vfprintf (stderr, fmt, ap);
46 va_end (ap);
47 fputc ('\n', stderr);
48 }
49}
50
51void
52pupa_util_error (const char *fmt, ...)
53{
54 va_list ap;
55
56 fprintf (stderr, "%s: error: ", progname);
57 va_start (ap, fmt);
58 vfprintf (stderr, fmt, ap);
59 va_end (ap);
60 fputc ('\n', stderr);
61 exit (1);
62}
63
64void *
65xmalloc (size_t size)
66{
67 void *p;
68
69 p = malloc (size);
70 if (! p)
71 pupa_util_error ("out of memory");
72
73 return p;
74}
75
1cc73a62 76void *
77xrealloc (void *ptr, size_t size)
78{
79 ptr = realloc (ptr, size);
80 if (! ptr)
81 pupa_util_error ("out of memory");
82
83 return ptr;
84}
85
6a161fa9 86char *
87xstrdup (const char *str)
88{
89 size_t len;
90 char *dup;
91
92 len = strlen (str);
93 dup = (char *) xmalloc (len + 1);
94 memcpy (dup, str, len + 1);
95
96 return dup;
97}
98
99char *
100pupa_util_get_path (const char *dir, const char *file)
101{
102 char *path;
103
104 path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
105 sprintf (path, "%s/%s", dir, file);
106 return path;
107}
108
109size_t
110pupa_util_get_image_size (const char *path)
111{
112 struct stat st;
113
114 pupa_util_info ("getting the size of %s", path);
115
116 if (stat (path, &st) == -1)
117 pupa_util_error ("cannot stat %s", path);
118
119 return st.st_size;
120}
121
122char *
123pupa_util_read_image (const char *path)
124{
125 char *img;
126 FILE *fp;
127 size_t size;
128
129 pupa_util_info ("reading %s", path);
130
131 size = pupa_util_get_image_size (path);
132 img = (char *) xmalloc (size);
133
134 fp = fopen (path, "rb");
135 if (! fp)
136 pupa_util_error ("cannot open %s", path);
137
138 if (fread (img, 1, size, fp) != size)
139 pupa_util_error ("cannot read %s", path);
140
1f5ab428 141 fclose (fp);
142
6a161fa9 143 return img;
144}
145
1f5ab428 146void
147pupa_util_load_image (const char *path, char *buf)
148{
149 FILE *fp;
150 size_t size;
151
152 pupa_util_info ("reading %s", path);
153
154 size = pupa_util_get_image_size (path);
155
156 fp = fopen (path, "rb");
157 if (! fp)
158 pupa_util_error ("cannot open %s", path);
159
160 if (fread (buf, 1, size, fp) != size)
161 pupa_util_error ("cannot read %s", path);
162
163 fclose (fp);
164}
165
6a161fa9 166void
167pupa_util_write_image (const char *img, size_t size, FILE *out)
168{
169 pupa_util_info ("writing 0x%x bytes", size);
170 if (fwrite (img, 1, size, out) != size)
171 pupa_util_error ("write failed");
172}
173
1cc73a62 174void *
175pupa_malloc (unsigned size)
176{
db1771cf 177 return xmalloc (size);
1cc73a62 178}
179
180void
181pupa_free (void *ptr)
182{
183 free (ptr);
184}
185
186void *
187pupa_realloc (void *ptr, unsigned size)
188{
db1771cf 189 return xrealloc (ptr, size);
1cc73a62 190}
191
1f7315a3 192void *
193pupa_memalign (pupa_size_t align, pupa_size_t size)
194{
db1771cf 195 void *p;
196
197 p = memalign (align, size);
198 if (! p)
199 pupa_util_error ("out of memory");
200
201 return p;
1f7315a3 202}
203
204/* Some functions that we don't use. */
1cc73a62 205void
db1771cf 206pupa_mm_init_region (void *addr __attribute__ ((unused)),
207 pupa_size_t size __attribute__ ((unused)))
1cc73a62 208{
1cc73a62 209}
210
211void
1f7315a3 212pupa_register_exported_symbols (void)
1cc73a62 213{
1f7315a3 214}
215
216void
217pupa_stop (void)
218{
219 exit (1);
1cc73a62 220}
8e72a9c0 221
222pupa_uint32_t
223pupa_get_rtc (void)
224{
225 struct tms currtime;
226
227 return times (&currtime);
228}