bus_load_file.c revision 223534
11927Swollman/* $FreeBSD: head/tools/tools/bus_autoconf/bus_load_file.c 223534 2011-06-25 13:44:05Z hselasky $ */
21927Swollman
31927Swollman/*-
41927Swollman * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
51927Swollman *
61927Swollman * Redistribution and use in source and binary forms, with or without
71927Swollman * modification, are permitted provided that the following conditions
81927Swollman * are met:
91927Swollman * 1. Redistributions of source code must retain the above copyright
101927Swollman *    notice, this list of conditions and the following disclaimer.
111927Swollman * 2. Redistributions in binary form must reproduce the above copyright
121927Swollman *    notice, this list of conditions and the following disclaimer in the
131927Swollman *    documentation and/or other materials provided with the distribution.
141927Swollman *
151927Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161927Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171927Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181927Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191927Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201927Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211927Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221927Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231927Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241927Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251927Swollman * SUCH DAMAGE.
261927Swollman */
271927Swollman
281927Swollman#include <stdio.h>
291927Swollman#include <stdint.h>
3030762Scharnier#include <stdlib.h>
3130762Scharnier#include <fcntl.h>
3250479Speter#include <err.h>
3330762Scharnier#include <sysexits.h>
341927Swollman#include <unistd.h>
351927Swollman
361927Swollman#include "bus_load_file.h"
378091Swpaul
381927Swollmanvoid
391927Swollmanload_file(const char *fname, uint8_t **pptr, uint32_t *plen)
401927Swollman{
411927Swollman	uint8_t *ptr;
421927Swollman	uint32_t len;
438091Swpaul	off_t off;
441927Swollman	int f;
451927Swollman
461927Swollman	f = open(fname, O_RDONLY);
4730762Scharnier	if (f < 0)
4830762Scharnier		err(EX_NOINPUT, "Cannot open file '%s'", fname);
491927Swollman
5030762Scharnier	off = lseek(f, 0, SEEK_END);
5130762Scharnier	if (off <= 0)
5230762Scharnier		err(EX_NOINPUT, "Cannot seek to end of file");
531927Swollman
5430762Scharnier	if (lseek(f, 0, SEEK_SET) < 0)
5530762Scharnier		err(EX_NOINPUT, "Cannot seek to beginning of file");
561927Swollman
571927Swollman	len = off;
581927Swollman	if (len != off)
596732Swpaul		err(EX_NOINPUT, "File '%s' is too big", fname);
601927Swollman
611927Swollman	ptr = malloc(len);
621927Swollman	if (ptr == NULL)
631927Swollman		errx(EX_SOFTWARE, "Out of memory");
6430762Scharnier
6512862Swpaul	if (read(f, ptr, len) != len)
6612862Swpaul		err(EX_NOINPUT, "Cannot read all data");
671927Swollman
6826135Swpaul	close(f);
691927Swollman
701927Swollman	*pptr = ptr;
711927Swollman	*plen = len;
721927Swollman}
731927Swollman