revert between 56095 -> 55830 in arch
[AROS.git] / workbench / c / LoadResource / main.c
blob9045c9e1fea5d2ec891a5c9dd9b113126d9ccff0
1 /*
2 Copyright © 2004-2013, The AROS Development Team. All rights reserved.
3 This file is part of the LoadResource program, which is distributed under
4 the terms of version 2 of the GNU General Public License.
6 FIXME:
7 + Implement LOCK and UNLOCK options. Argument template should then be
8 changed to "NAME/M,LOCK/S,UNLOCK/S"; if the user doesn't provide NAME,
9 then all currently locked resources shall be listed.
10 + Implement support for loading devices, fonts and catalogs.
13 #include <exec/io.h>
14 #include <proto/exec.h>
15 #include <proto/dos.h>
17 #include <string.h>
19 #include "locale.h"
21 #if defined(__AROSPLATFORM_SMP__)
22 #include <aros/types/spinlock_s.h>
23 #include <proto/execlock.h>
24 #include <resources/execlock.h>
25 #endif
27 enum
29 ARG_NAME,
30 ARG_COUNT
33 #define TEMPLATE "NAME/M/A"
34 #define ERROR_HEADER "LoadResource"
36 BOOL process(CONST_STRPTR name);
37 BOOL dev_process(CONST_STRPTR name);
39 int main(void)
41 int rc = RETURN_OK;
42 struct RDArgs *rdargs = NULL;
43 IPTR args[ARG_COUNT] = { 0 };
45 if ((rdargs = ReadArgs(TEMPLATE, args, NULL)) != NULL)
47 if (args[ARG_NAME] != 0)
49 CONST_STRPTR *names = (CONST_STRPTR *) args[ARG_NAME],
50 name = NULL;
52 while ((name = *names++) != NULL)
54 BOOL res;
55 int len = strlen(name);
57 if ((len > 7) && (!strcmp(&name[len-7], ".device")))
58 res = dev_process(name);
59 else
60 res = process(name);
62 if (!res)
64 rc = RETURN_WARN;
68 else
70 // FIXME: List currently locked resources.
73 FreeArgs(rdargs);
75 else
77 PrintFault(IoErr(), ERROR_HEADER);
78 rc = RETURN_FAIL;
81 return rc;
84 BOOL process(CONST_STRPTR name)
86 struct Library *lb = OpenLibrary(name, 0L);
88 if (lb != NULL)
90 CloseLibrary(lb);
91 return TRUE;
93 else
95 PutStr(ERROR_HEADER": ");
96 Printf(_(MSG_ERROR_OPEN_LIBRARY), name);
97 PutStr("\n");
98 return FALSE;
102 BOOL dev_process(CONST_STRPTR name)
104 struct IORequest req;
105 BOOL retval = FALSE;
106 memset(&req, 0, sizeof(req));
107 req.io_Message.mn_Length = sizeof(req);
108 #if defined(__AROSPLATFORM_SMP__)
109 void *ExecLockBase = OpenResource("execlock.resource");
110 #endif
112 if (!OpenDevice(name, 0, &req, 0))
114 CloseDevice(&req);
115 retval = TRUE;
117 else
119 #if defined(__AROSPLATFORM_SMP__)
120 if (ExecLockBase)
121 ObtainSystemLock(&SysBase->DeviceList, SPINLOCK_MODE_READ, LOCKF_FORBID);
122 else
123 Forbid();
124 #else
125 Forbid();
126 #endif
127 /* There can different errors, but if the device was loaded, it's OK */
128 if (FindName(&SysBase->DeviceList, name))
129 retval = TRUE;
131 #if defined(__AROSPLATFORM_SMP__)
132 if (ExecLockBase)
133 ReleaseSystemLock(&SysBase->DeviceList, LOCKF_FORBID);
134 else
135 Permit();
136 #else
137 Permit();
138 #endif
139 if (retval == FALSE)
141 PutStr(ERROR_HEADER": ");
142 Printf(_(MSG_ERROR_OPEN_DEVICE), name);
143 PutStr("\n");
147 return retval;