- Include aros/config.h at almost all places where AROS_FLAVOUR is used.
[AROS.git] / rom / dos / loadseg.c
blob2161882cfe91247d4e454b32cf1b46b2c575c286
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: DOS function LoadSeg()
6 Lang: english
7 */
8 #define DEBUG 0
10 #include <aros/asmcall.h>
11 #include <aros/config.h>
12 #include <dos/dos.h>
13 #include <dos/dosextens.h>
14 #include <dos/stdio.h>
15 #include <proto/dos.h>
16 #include <aros/debug.h>
17 #include "dos_intern.h"
19 static AROS_UFH4(LONG, ReadFunc,
20 AROS_UFHA(BPTR, file, D1),
21 AROS_UFHA(APTR, buffer, D2),
22 AROS_UFHA(LONG, length, D3),
23 AROS_UFHA(struct DosLibrary *, DOSBase, A6)
26 AROS_USERFUNC_INIT
28 return FRead(file, buffer, 1, length);
30 AROS_USERFUNC_EXIT
33 static AROS_UFH4(LONG, SeekFunc,
34 AROS_UFHA(BPTR, file, D1),
35 AROS_UFHA(LONG, pos, D2),
36 AROS_UFHA(LONG, mode, D3),
37 AROS_UFHA(struct DosLibrary *, DOSBase, A6)
40 AROS_USERFUNC_INIT
42 return Seek(file, pos, mode);
44 AROS_USERFUNC_EXIT
48 static AROS_UFH3(APTR, AllocFunc,
49 AROS_UFHA(ULONG, length, D0),
50 AROS_UFHA(ULONG, flags, D1),
51 AROS_UFHA(struct ExecBase *, SysBase, A6)
54 AROS_USERFUNC_INIT
56 return AllocMem(length, flags);
58 AROS_USERFUNC_EXIT
61 static AROS_UFH3(void, FreeFunc,
62 AROS_UFHA(APTR, buffer, A1),
63 AROS_UFHA(ULONG, length, D0),
64 AROS_UFHA(struct ExecBase *, SysBase, A6)
67 AROS_USERFUNC_INIT
69 FreeMem(buffer, length);
71 AROS_USERFUNC_EXIT
74 #ifdef __mc68000
75 static AROS_ENTRY(LONG, SetPatch_noop,
76 AROS_UFHA(char *, argstr, A0),
77 AROS_UFHA(ULONG, argsize, D0),
78 struct ExecBase *, SysBase)
80 AROS_USERFUNC_INIT
82 APTR DOSBase = TaggedOpenLibrary(TAGGEDOPEN_DOS);
84 if (DOSBase) {
85 struct CommandLineInterface *cli = Cli();
86 if (cli && cli->cli_Interactive) {
87 Printf("SetPatch is a reserved program name.\n");
89 CloseLibrary(DOSBase);
92 return RETURN_WARN;
94 AROS_USERFUNC_EXIT
96 #endif
98 /*****************************************************************************
100 NAME */
101 #include <proto/dos.h>
103 AROS_LH1(BPTR, LoadSeg,
105 /* SYNOPSIS */
106 AROS_LHA(CONST_STRPTR, name, D1),
108 /* LOCATION */
109 struct DosLibrary *, DOSBase, 25, Dos)
111 /* FUNCTION
112 Loads an executable file into memory. Each hunk of the loadfile
113 is loaded into its own memory section and a handle on all of them
114 is returned. The segments can be freed with UnLoadSeg().
116 INPUTS
117 name - NUL terminated name of the file.
119 RESULT
120 Handle to the loaded executable or NULL if the load failed.
121 IoErr() gives additional information in that case.
123 NOTES
124 This function is built on top of InternalLoadSeg()
126 EXAMPLE
128 BUGS
130 SEE ALSO
131 UnLoadSeg()
133 INTERNALS
135 *****************************************************************************/
137 AROS_LIBFUNC_INIT
139 BPTR file, segs=0;
140 SIPTR err;
141 LONG_FUNC FunctionArray[] = {
142 (LONG_FUNC)ReadFunc,
143 (LONG_FUNC)AllocFunc,
144 (LONG_FUNC)FreeFunc,
145 (LONG_FUNC)SeekFunc, /* Only needed for ELF */
148 #ifdef __mc68000
149 /* On m68k, we map SetPatch to a no-op seglist,
150 * due to the fact that OS 3.x and higher's SetPatch
151 * blindly patches without checking OS versions.
153 if (Stricmp(FilePart(name),"SetPatch") == 0)
154 return CreateSegList(SetPatch_noop);
155 #endif
157 /* Open the file */
158 D(bug("[LoadSeg] Opening '%s'...\n", name));
159 file = Open (name, MODE_OLDFILE);
161 if (file)
163 D(bug("[LoadSeg] Loading '%s'...\n", name));
165 SetVBuf(file, NULL, BUF_FULL, 4096);
166 segs = InternalLoadSeg(file, BNULL, FunctionArray, NULL);
167 /* We cache the IoErr(), since Close() will alter it */
168 err = IoErr();
170 D(if (segs == BNULL)
171 bug("[LoadSeg] Failed to load '%s'\n", name));
172 #if (AROS_FLAVOUR & AROS_FLAVOUR_BINCOMPAT)
173 /* overlayed executables return -segs and handle must not be closed */
174 if ((LONG)segs > 0)
175 Close(file);
176 else
177 segs = (BPTR)-((LONG)segs);
178 #else
179 Close(file);
180 #endif
181 SetIoErr(err);
183 D(else
184 bug("[LoadSeg] Failed to open '%s'\n", name));
187 /* And return */
188 return segs;
190 AROS_LIBFUNC_EXIT
191 } /* LoadSeg */