~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to util/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2002,2003,2005  Free Software Foundation, Inc.
 
4
 *
 
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 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 GRUB; 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>
 
26
#include <sys/time.h>
 
27
#include <malloc.h>
 
28
#include <unistd.h>
 
29
 
 
30
#include <grub/util/misc.h>
 
31
#include <grub/mm.h>
 
32
#include <grub/term.h>
 
33
#include <grub/machine/time.h>
 
34
 
 
35
char *progname = 0;
 
36
int verbosity = 0;
 
37
 
 
38
void
 
39
grub_util_info (const char *fmt, ...)
 
40
{
 
41
  if (verbosity > 0)
 
42
    {
 
43
      va_list ap;
 
44
      
 
45
      fprintf (stderr, "%s: info: ", progname);
 
46
      va_start (ap, fmt);
 
47
      vfprintf (stderr, fmt, ap);
 
48
      va_end (ap);
 
49
      fputc ('\n', stderr);
 
50
    }
 
51
}
 
52
 
 
53
void
 
54
grub_util_error (const char *fmt, ...)
 
55
{
 
56
  va_list ap;
 
57
  
 
58
  fprintf (stderr, "%s: error: ", progname);
 
59
  va_start (ap, fmt);
 
60
  vfprintf (stderr, fmt, ap);
 
61
  va_end (ap);
 
62
  fputc ('\n', stderr);
 
63
  exit (1);
 
64
}
 
65
 
 
66
void *
 
67
xmalloc (size_t size)
 
68
{
 
69
  void *p;
 
70
  
 
71
  p = malloc (size);
 
72
  if (! p)
 
73
    grub_util_error ("out of memory");
 
74
 
 
75
  return p;
 
76
}
 
77
 
 
78
void *
 
79
xrealloc (void *ptr, size_t size)
 
80
{
 
81
  ptr = realloc (ptr, size);
 
82
  if (! ptr)
 
83
    grub_util_error ("out of memory");
 
84
 
 
85
  return ptr;
 
86
}
 
87
 
 
88
char *
 
89
xstrdup (const char *str)
 
90
{
 
91
  size_t len;
 
92
  char *dup;
 
93
  
 
94
  len = strlen (str);
 
95
  dup = (char *) xmalloc (len + 1);
 
96
  memcpy (dup, str, len + 1);
 
97
 
 
98
  return dup;
 
99
}
 
100
 
 
101
char *
 
102
grub_util_get_path (const char *dir, const char *file)
 
103
{
 
104
  char *path;
 
105
  
 
106
  path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1);
 
107
  sprintf (path, "%s/%s", dir, file);
 
108
  return path;
 
109
}
 
110
 
 
111
size_t
 
112
grub_util_get_fp_size (FILE *fp)
 
113
{
 
114
  struct stat st;
 
115
  
 
116
  if (fflush (fp) == EOF)
 
117
    grub_util_error ("fflush failed");
 
118
 
 
119
  if (fstat (fileno (fp), &st) == -1)
 
120
    grub_util_error ("fstat failed");
 
121
  
 
122
  return st.st_size;
 
123
}
 
124
 
 
125
size_t
 
126
grub_util_get_image_size (const char *path)
 
127
{
 
128
  struct stat st;
 
129
  
 
130
  grub_util_info ("getting the size of %s", path);
 
131
  
 
132
  if (stat (path, &st) == -1)
 
133
    grub_util_error ("cannot stat %s", path);
 
134
  
 
135
  return st.st_size;
 
136
}
 
137
 
 
138
void
 
139
grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp)
 
140
{
 
141
  if (fseek (fp, offset, SEEK_SET) == -1)
 
142
    grub_util_error ("fseek failed");
 
143
 
 
144
  if (fread (img, 1, size, fp) != size)
 
145
    grub_util_error ("read failed");
 
146
}
 
147
 
 
148
char *
 
149
grub_util_read_image (const char *path)
 
150
{
 
151
  char *img;
 
152
  FILE *fp;
 
153
  size_t size;
 
154
  
 
155
  grub_util_info ("reading %s", path);
 
156
 
 
157
  size = grub_util_get_image_size (path);
 
158
  img = (char *) xmalloc (size);
 
159
 
 
160
  fp = fopen (path, "rb");
 
161
  if (! fp)
 
162
    grub_util_error ("cannot open %s", path);
 
163
 
 
164
  grub_util_read_at (img, size, 0, fp);
 
165
 
 
166
  fclose (fp);
 
167
  
 
168
  return img;
 
169
}
 
170
 
 
171
void
 
172
grub_util_load_image (const char *path, char *buf)
 
173
{
 
174
  FILE *fp;
 
175
  size_t size;
 
176
  
 
177
  grub_util_info ("reading %s", path);
 
178
 
 
179
  size = grub_util_get_image_size (path);
 
180
  
 
181
  fp = fopen (path, "rb");
 
182
  if (! fp)
 
183
    grub_util_error ("cannot open %s", path);
 
184
 
 
185
  if (fread (buf, 1, size, fp) != size)
 
186
    grub_util_error ("cannot read %s", path);
 
187
 
 
188
  fclose (fp);
 
189
}
 
190
 
 
191
void
 
192
grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out)
 
193
{
 
194
  grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset);
 
195
  if (fseek (out, offset, SEEK_SET) == -1)
 
196
    grub_util_error ("seek failed");
 
197
  if (fwrite (img, 1, size, out) != size)
 
198
    grub_util_error ("write failed");
 
199
}
 
200
 
 
201
void
 
202
grub_util_write_image (const char *img, size_t size, FILE *out)
 
203
{
 
204
  grub_util_info ("writing 0x%x bytes", size);
 
205
  if (fwrite (img, 1, size, out) != size)
 
206
    grub_util_error ("write failed");
 
207
}
 
208
 
 
209
void *
 
210
grub_malloc (grub_size_t size)
 
211
{
 
212
  return xmalloc (size);
 
213
}
 
214
 
 
215
void
 
216
grub_free (void *ptr)
 
217
{
 
218
  free (ptr);
 
219
}
 
220
 
 
221
void *
 
222
grub_realloc (void *ptr, grub_size_t size)
 
223
{
 
224
  return xrealloc (ptr, size);
 
225
}
 
226
 
 
227
void *
 
228
grub_memalign (grub_size_t align, grub_size_t size)
 
229
{
 
230
  void *p;
 
231
  
 
232
  p = memalign (align, size);
 
233
  if (! p)
 
234
    grub_util_error ("out of memory");
 
235
  
 
236
  return p;
 
237
}
 
238
 
 
239
/* Some functions that we don't use.  */
 
240
void
 
241
grub_mm_init_region (void *addr __attribute__ ((unused)),
 
242
                     grub_size_t size __attribute__ ((unused)))
 
243
{
 
244
}
 
245
 
 
246
void
 
247
grub_register_exported_symbols (void)
 
248
{
 
249
}
 
250
 
 
251
void
 
252
grub_stop (void)
 
253
{
 
254
  exit (1);
 
255
}
 
256
 
 
257
grub_uint32_t
 
258
grub_get_rtc (void)
 
259
{
 
260
  struct timeval tv;
 
261
 
 
262
  gettimeofday (&tv, 0);
 
263
  
 
264
  return (tv.tv_sec * GRUB_TICKS_PER_SECOND
 
265
          + (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
 
266
             * GRUB_TICKS_PER_SECOND / 1000000));
 
267
}
 
268
 
 
269
void 
 
270
grub_arch_sync_caches (void *address __attribute__ ((unused)),
 
271
                       grub_size_t len __attribute__ ((unused)))
 
272
{
 
273
}