/* * PUPA -- Preliminary Universal Programming Architecture for GRUB * Copyright (C) 2002 Yoshinori K. Okuji * * PUPA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PUPA; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include char *progname = 0; int verbosity = 0; void pupa_util_info (const char *fmt, ...) { if (verbosity > 0) { va_list ap; fprintf (stderr, "%s: info: ", progname); va_start (ap, fmt); vfprintf (stderr, fmt, ap); va_end (ap); fputc ('\n', stderr); } } void pupa_util_error (const char *fmt, ...) { va_list ap; fprintf (stderr, "%s: error: ", progname); va_start (ap, fmt); vfprintf (stderr, fmt, ap); va_end (ap); fputc ('\n', stderr); exit (1); } void * xmalloc (size_t size) { void *p; p = malloc (size); if (! p) pupa_util_error ("out of memory"); return p; } char * xstrdup (const char *str) { size_t len; char *dup; len = strlen (str); dup = (char *) xmalloc (len + 1); memcpy (dup, str, len + 1); return dup; } char * pupa_util_get_path (const char *dir, const char *file) { char *path; path = (char *) xmalloc (strlen (dir) + 1 + strlen (file) + 1); sprintf (path, "%s/%s", dir, file); return path; } size_t pupa_util_get_image_size (const char *path) { struct stat st; pupa_util_info ("getting the size of %s", path); if (stat (path, &st) == -1) pupa_util_error ("cannot stat %s", path); return st.st_size; } char * pupa_util_read_image (const char *path) { char *img; FILE *fp; size_t size; pupa_util_info ("reading %s", path); size = pupa_util_get_image_size (path); img = (char *) xmalloc (size); fp = fopen (path, "rb"); if (! fp) pupa_util_error ("cannot open %s", path); if (fread (img, 1, size, fp) != size) pupa_util_error ("cannot read %s", path); return img; } void pupa_util_write_image (const char *img, size_t size, FILE *out) { pupa_util_info ("writing 0x%x bytes", size); if (fwrite (img, 1, size, out) != size) pupa_util_error ("write failed"); }