Detabbed
[AROS.git] / rom / dos / loadseg.c
blob5d75e7af3c530f6b53bfdc385233b33f1e8905d2
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: DOS function LoadSeg()
6 Lang: english
7 */
9 #include <aros/asmcall.h>
10 #include <aros/config.h>
11 #include <dos/dos.h>
12 #include <dos/stdio.h>
13 #include <proto/dos.h>
14 #include <aros/debug.h>
15 #include "dos_intern.h"
17 static AROS_UFH4(LONG, ReadFunc,
18 AROS_UFHA(BPTR, file, D1),
19 AROS_UFHA(APTR, buffer, D2),
20 AROS_UFHA(LONG, length, D3),
21 AROS_UFHA(struct DosLibrary *, DOSBase, A6)
24 AROS_USERFUNC_INIT
26 return FRead(file, buffer, 1, length);
28 AROS_USERFUNC_EXIT
31 static AROS_UFH4(LONG, SeekFunc,
32 AROS_UFHA(BPTR, file, D1),
33 AROS_UFHA(LONG, pos, D2),
34 AROS_UFHA(LONG, mode, D3),
35 AROS_UFHA(struct DosLibrary *, DOSBase, A6)
38 AROS_USERFUNC_INIT
40 return Seek(file, pos, mode);
42 AROS_USERFUNC_EXIT
46 static AROS_UFH3(APTR, AllocFunc,
47 AROS_UFHA(ULONG, length, D0),
48 AROS_UFHA(ULONG, flags, D1),
49 AROS_UFHA(struct ExecBase *, SysBase, A6)
52 AROS_USERFUNC_INIT
54 return AllocMem(length, flags);
56 AROS_USERFUNC_EXIT
59 static AROS_UFH3(void, FreeFunc,
60 AROS_UFHA(APTR, buffer, A1),
61 AROS_UFHA(ULONG, length, D0),
62 AROS_UFHA(struct ExecBase *, SysBase, A6)
65 AROS_USERFUNC_INIT
67 FreeMem(buffer, length);
69 AROS_USERFUNC_EXIT
72 /*****************************************************************************
74 NAME */
75 #include <proto/dos.h>
77 AROS_LH1(BPTR, LoadSeg,
79 /* SYNOPSIS */
80 AROS_LHA(CONST_STRPTR, name, D1),
82 /* LOCATION */
83 struct DosLibrary *, DOSBase, 25, Dos)
85 /* FUNCTION
86 Loads an executable file into memory. Each hunk of the loadfile
87 is loaded into its own memory section and a handle on all of them
88 is returned. The segments can be freed with UnLoadSeg().
90 INPUTS
91 name - NUL terminated name of the file.
93 RESULT
94 Handle to the loaded executable or NULL if the load failed.
95 IoErr() gives additional information in that case.
97 NOTES
98 This function is built on top of InternalLoadSeg()
100 EXAMPLE
102 BUGS
104 SEE ALSO
105 UnLoadSeg()
107 INTERNALS
109 *****************************************************************************/
111 AROS_LIBFUNC_INIT
113 BPTR file, segs=0;
114 SIPTR err;
115 LONG_FUNC FunctionArray[] = {
116 (LONG_FUNC)ReadFunc,
117 (LONG_FUNC)AllocFunc,
118 (LONG_FUNC)FreeFunc,
119 (LONG_FUNC)SeekFunc, /* Only needed for ELF */
122 /* Open the file */
123 D(bug("[LoadSeg] Opening '%s'...\n", name));
124 file = Open (name, MODE_OLDFILE);
126 if (file)
128 D(bug("[LoadSeg] Loading '%s'...\n", name));
130 SetVBuf(file, NULL, BUF_FULL, 4096);
131 segs = InternalLoadSeg(file, BNULL, FunctionArray, NULL);
132 /* We cache the IoErr(), since Close() will alter it */
133 err = IoErr();
135 D(if (segs == BNULL)
136 bug("[LoadSeg] Failed to load '%s'\n", name));
137 #if (AROS_FLAVOUR & AROS_FLAVOUR_BINCOMPAT)
138 /* overlayed executables return -segs and handle must not be closed */
139 if ((LONG)segs > 0)
140 Close(file);
141 else
142 segs = (BPTR)-((LONG)segs);
143 #else
144 Close(file);
145 #endif
146 SetIoErr(err);
149 D(bug("[LoadSeg] Returns '%p', IoErr=%d\n", segs, IoErr()));
152 /* And return */
153 return segs;
155 AROS_LIBFUNC_EXIT
156 } /* LoadSeg */