]> git.proxmox.com Git - grub2.git/blob - util/misc.c
2003-11-17 Marco Gerards <metgerards@student.han.nl>
[grub2.git] / util / misc.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
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/times.h>
28
29 #include <pupa/util/misc.h>
30 #include <pupa/mm.h>
31 #include <pupa/term.h>
32
33 char *progname = 0;
34 int verbosity = 0;
35
36 void
37 pupa_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
51 void
52 pupa_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
64 void *
65 xmalloc (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
76 void *
77 xrealloc (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
86 char *
87 xstrdup (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
99 char *
100 pupa_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
109 size_t
110 pupa_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
122 char *
123 pupa_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
141 fclose (fp);
142
143 return img;
144 }
145
146 void
147 pupa_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
166 void
167 pupa_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
174 void *
175 pupa_malloc (unsigned size)
176 {
177 return malloc (size);
178 }
179
180 void
181 pupa_free (void *ptr)
182 {
183 free (ptr);
184 }
185
186 void *
187 pupa_realloc (void *ptr, unsigned size)
188 {
189 return realloc (ptr, size);
190 }
191
192 void *
193 pupa_memalign (pupa_size_t align, pupa_size_t size)
194 {
195 return memalign (align, size);
196 }
197
198 /* Some functions that we don't use. */
199 void
200 pupa_mm_init_region (void *addr, pupa_size_t size)
201 {
202 }
203
204 void
205 pupa_register_exported_symbols (void)
206 {
207 }
208
209 void
210 pupa_stop (void)
211 {
212 exit (1);
213 }
214
215 pupa_uint32_t
216 pupa_get_rtc (void)
217 {
218 struct tms currtime;
219
220 return times (&currtime);
221 }