Introduce fdloadfile() to load an image from an open file descriptor;

implement loadfile() in terms of it.

This allows clients to open a file once and "load" it multiple times (e.g.
first with COUNT_KERNEL, then with LOAD_KERNEL) without the side-effects
of multiple open calls.
This commit is contained in:
pk 2003-02-24 10:51:05 +00:00
parent d82babe73c
commit d71686ab26
2 changed files with 37 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: loadfile.c,v 1.21 2002/12/11 09:55:20 pk Exp $ */
/* $NetBSD: loadfile.c,v 1.22 2003/02/24 10:51:05 pk Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
@ -103,6 +103,35 @@ loadfile(fname, marks, flags)
const char *fname;
u_long *marks;
int flags;
{
int fd, error;
/* Open the file. */
if ((fd = open(fname, 0)) < 0) {
WARN(("open %s", fname ? fname : "<default>"));
return (-1);
}
/* Load it; save the value of errno across the close() call */
if ((error = fdloadfile(fd, marks, flags)) != 0) {
(void)close(fd);
errno = error;
return (-1);
}
return (fd);
}
/*
* Read in program from the given file descriptor.
* Return error code (0 on success).
* Fill in marks.
*/
int
fdloadfile(fd, marks, flags)
int fd;
u_long *marks;
int flags;
{
union {
#ifdef BOOT_ECOFF
@ -119,15 +148,11 @@ loadfile(fname, marks, flags)
#endif
} hdr;
ssize_t nr;
int fd, rval;
/* Open the file. */
if ((fd = open(fname, 0)) < 0) {
WARN(("open %s", fname ? fname : "<default>"));
return -1;
}
int rval;
/* Read the exec header. */
if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
goto err;
if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
WARN(("read header"));
goto err;
@ -162,16 +187,14 @@ loadfile(fname, marks, flags)
{
rval = 1;
errno = EFTYPE;
WARN(("%s", fname ? fname : "<default>"));
}
if (rval == 0) {
if ((flags & LOAD_ALL) != 0)
PROGRESS(("=0x%lx\n",
marks[MARK_END] - marks[MARK_START]));
return fd;
return (0);
}
err:
(void)close(fd);
return -1;
return (errno);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: loadfile.h,v 1.3 2001/10/31 17:20:50 thorpej Exp $ */
/* $NetBSD: loadfile.h,v 1.4 2003/02/24 10:51:05 pk Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@ -66,6 +66,7 @@
#define COUNT_ALL 0x3f00
int loadfile(const char *, u_long *, int);
int fdloadfile(int fd, u_long *, int);
#include "machine/loadfile_machdep.h"