-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
192 lines (168 loc) · 5.1 KB
/
utility.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <proto/dos.h>
#include <proto/utility.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
#include "utility.h"
void dos_debug()
{
if (DEBUG) {
// %m and %n magic modifiers only available in kickstart 51.59+
IDOS->Printf("DOS error message = %m, error code = %n\n",0);
}
}
void dump_path_node(struct PathNode *node)
{
while (node->pn_Next) {
char path[MAX_PATH_BUF];
if(IDOS->NameFromLock(node->pn_Lock, path, MAX_PATH_BUF)) {
IDOS->Printf(" %s\n", path);
}
node = BADDR(node->pn_Next);
}
}
int get_target(const char *cmd, const char *version, char *target)
{
char cmd_dir[MAX_PATH_BUF];
char cmd_version[MAX_PATH_BUF];
char path[MAX_PATH_BUF];
BPTR lock;
int32 rc;
// Check the command directory exists
strcpy(cmd_dir, SETCMD_CMDS);
IDOS->AddPart(cmd_dir, cmd, MAX_PATH_BUF);
if (!can_lock(cmd_dir)) {
if (DEBUG) {
IDOS->Printf("%sERROR %s: Failed to lock the %s directory\n", fmt(FG_RED), fmt(NORMAL), cmd_dir);
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
}
return SETCMD_ERROR;
}
// Now get a lock on the specified version
strcpy(cmd_version, cmd_dir);
IDOS->AddPart(cmd_version, version, MAX_PATH_BUF);
lock = IDOS->Lock(cmd_version, ACCESS_READ);
if (!lock) {
if (DEBUG) {
IDOS->Printf("%sERROR %s: Failed to lock the %s version\n", fmt(FG_RED), fmt(NORMAL), cmd_version);
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
}
return SETCMD_ERROR;
}
rc = IDOS->NameFromLock(lock, target, MAX_PATH_BUF);
if (!rc) {
IDOS->Printf("%sERROR %s: Failed to read the link from %s\n", fmt(FG_RED), fmt(NORMAL), cmd_version);
dos_debug();
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
if (lock) {
IDOS->UnLock(lock);
}
return SETCMD_ERROR;
}
if (lock) {
IDOS->UnLock(lock);
}
return SETCMD_OK;
}
int current_version(const char *cmd, char *version)
{
char link[MAX_PATH_BUF];
char current_version[MAX_PATH_BUF];
char path[MAX_PATH_BUF];
char target[MAX_PATH_BUF];
struct ExamineData *data;
APTR context;
BPTR lock;
BOOL found = FALSE;
int32 rc;
strcpy(path, SETCMD_PATH);
IDOS->AddPart(path, cmd, MAX_PATH_BUF);
lock = IDOS->Lock(path, ACCESS_READ);
if (!lock) {
if (DEBUG) {
IDOS->Printf("%sERROR %s: Failed to lock the %s path\n", fmt(FG_RED), fmt(NORMAL), path);
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
}
dos_debug();
return SETCMD_ERROR;
}
rc = IDOS->NameFromLock(lock, target, MAX_PATH_BUF);
if (!rc) {
if (DEBUG) {
IDOS->Printf("%sERROR %s: Failed to read the link from %s\n", fmt(FG_RED), fmt(NORMAL), path);
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
}
dos_debug();
if (lock) {
IDOS->UnLock(lock);
}
return SETCMD_ERROR;
}
// check if we are just pointing at the stub, if so then return "stub"
if (strcmp(IDOS->FilePart(target), "stub") == 0) {
strcpy(version, "stub");
if (lock) {
IDOS->UnLock(lock);
}
return SETCMD_OK;
}
// OK, so we're not pointing at the stub. Let's move on.
IDOS->UnLock(lock);
lock = IDOS->Lock(SETCMD_PATH, ACCESS_READ);
if (!lock) {
if (DEBUG) {
IDOS->Printf("%sERROR %s: Failed to lock the " SETCMD_PATH " directory\n", fmt(FG_RED), fmt(NORMAL));
IDOS->Printf("Check your installation and make sure the SETCMD: assign is correctly setup.\n");
IDOS->Printf("For more information see the SetCmd manual.\n");
}
return SETCMD_ERROR;
}
context = IDOS->ObtainDirContextTags(EX_LockInput, lock, TAG_END);
while (data = IDOS->ExamineDir(context)) {
strcpy (link, data->Link);
// If the file is not a link, we just ignore it as it's not a valid command
if (strlen(link) > 0 && strcmp(data->Name, cmd) == 0) {
strcpy(current_version, IDOS->FilePart(link));
found = TRUE;
break;
}
}
if (lock) {
IDOS->UnLock(lock);
}
IDOS->ReleaseDirContext(context);
if (!found) {
return SETCMD_ERROR;
}
strcpy(version, current_version);
return SETCMD_OK;
}
BOOL can_lock(const char *path)
{
BPTR lock;
lock = IDOS->Lock(path, ACCESS_READ);
if (lock) {
IDOS->UnLock(lock);
return TRUE;
} else {
return FALSE;
}
}
char *fmt(char *fmt_string)
{
int32 len;
char buf[MAX_PATH_BUF];
len = IDOS->GetVar(SETCMD_NOFORMAT_VAR, buf, MAX_PATH_BUF, LV_VAR);
if (len == -1) {
// NO_FORMAT not set so we just return what was passed
return fmt_string;
} else {
// The env var was set, so return nothing
return "";
}
}