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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const MAXLUN: i32 = 200;
//$Procedure T_TSTRLN (Test RESLUN)
pub fn T_TSTRLN(LUN: i32, RESRVD: &mut bool, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut LUNARY = StackArray::<i32, 200>::new(1..=MAXLUN);
let mut IOSTAT: i32 = 0;
let mut I: i32 = 0;
let mut DONE: bool = false;
let mut OPENED: bool = false;
let mut LUNFND: bool = false;
let mut FNAME = [b' '; 128 as usize];
//
// SPICELIB Functions
//
//
// Local Variables
//
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(b"T_TSTRLN", ctx)?;
}
//
// Assume LUN is not reserved until proven otherwise.
//
*RESRVD = false;
//
// INQUIRE on LUN to see if it is attached to a file.
//
{
use f2rust_std::io;
let specs = io::InquireSpecs {
unit: Some(LUN),
opened: Some(&mut OPENED),
..Default::default()
};
IOSTAT = io::capture_iostat(|| ctx.inquire(specs))?;
}
//
// Check IOSTAT for troubles.
//
if (IOSTAT != 0) {
spicelib::SETMSG(
b"Attempt to perform INQUIRE on logical unit, \'#\', failed. IOSTAT = #.",
ctx,
);
spicelib::ERRINT(b"#", LUN, ctx);
spicelib::ERRINT(b"#", IOSTAT, ctx);
spicelib::SIGERR(b"SPICE(INQUIREFAILED)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
//
// Now check to see if LUN is attached to a file.
//
if OPENED {
spicelib::SETMSG(b"The logical unit, \'#\', is currently attached to a file. This routine can only verify the reserved state of units not currently attached to files.", ctx);
spicelib::ERRINT(b"#", LUN, ctx);
spicelib::SIGERR(b"SPICE(LUNINUSE)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
//
// This is a total kludge, but it will work. We are going to
// call FNDLUN until we run out of space for storing units or
// we have exhausted all possible logical units.
//
LUNFND = false;
DONE = false;
I = 0;
while !DONE {
//
// Increment I.
//
I = (I + 1);
//
// Fetch the next unit from FNDLUN.
//
spicelib::FNDLUN(&mut LUNARY[I], ctx)?;
//
// Check to see if the INQUIRE buried in FNDLUN failed.
//
if (LUNARY[I] < 0) {
spicelib::SETMSG(b"INQUIRE failed. IOSTAT = #.", ctx);
spicelib::ERRINT(b"#", -LUNARY[I], ctx);
spicelib::SIGERR(b"SPICE(INQUIREFAILED)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
//
// Now see if LUN is LUNARY(I). If it is stop, because
// clearly LUN isn't reserved.
//
if (LUN == LUNARY[I]) {
DONE = true;
LUNFND = true;
//
// Then check to see if LUNARY(I) is 0, indicating that
// FNDLUN was unable to locate a new logical unit, in
// which case, we are finished.
//
} else if (LUNARY[I] == 0) {
DONE = true;
//
// Otherwise open a scratch file on LUNARY(I) so FNDLUN
// won't return it again.
//
} else {
//
// Create a temporary, unused file, at logical unit
// LUNARY(I).
//
fstr::assign(&mut FNAME, b"tmp.#");
spicelib::REPMI(&FNAME.clone(), b"#", I, &mut FNAME, ctx);
{
use f2rust_std::io;
let specs = io::OpenSpecs {
unit: Some(LUNARY[I]),
file: Some(&FNAME),
form: Some(b"FORMATTED"),
access: Some(b"SEQUENTIAL"),
status: Some(b"NEW"),
..Default::default()
};
IOSTAT = io::capture_iostat(|| ctx.open(specs))?;
}
if (IOSTAT != 0) {
spicelib::SETMSG(b"Could not open file #. IOSTAT was #. ", ctx);
spicelib::ERRCH(b"#", &FNAME, ctx);
spicelib::ERRINT(b"#", IOSTAT, ctx);
spicelib::SIGERR(b"SPICE(FILEOPENFAILED)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
}
//
// Now see if entering the loop again will push I past
// MAXLUN. This should never happen since MAXLUN is
// greater than the number of units for any platform.
//
if ((I == MAXLUN) || (LUNARY[I] == 0)) {
DONE = true;
}
}
//
// Wrap up our analysis. Clean up the open units and delete the
// corresponding files.
//
for J in 1..=(I - 1) {
{
use f2rust_std::io;
let specs = io::CloseSpecs {
unit: Some(LUNARY[J]),
..Default::default()
};
ctx.close(specs)?;
}
fstr::assign(&mut FNAME, b"tmp.#");
spicelib::REPMI(&FNAME.clone(), b"#", J, &mut FNAME, ctx);
spicelib::DELFIL(&FNAME, ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
}
//
// If we didn't find LUN in the list, then see if we exhausted
// all possible logical units.
//
if !LUNFND {
//
// First check to see if we exhausted the available space to
// store units. If we did, signal an error to warn the
// caller that the test results are inconclusive.
//
if ((I == MAXLUN) && (LUNARY[I] > 0)) {
{
use f2rust_std::io;
let specs = io::CloseSpecs {
unit: Some(LUNARY[I]),
..Default::default()
};
ctx.close(specs)?;
}
spicelib::SETMSG(b"FNDLUN returned too many logical units for the test to be conclusive. Increase the parameter MAXLUN and recompile this module.", ctx);
spicelib::SIGERR(b"SPICE(PARAMTOOSMALL)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
//
// If we make it this far, we did not use all of the available
// space to store units. Just for safety, check to see that
// LUNARY(I) is 0.
//
if (LUNARY[I] != 0) {
{
use f2rust_std::io;
let specs = io::CloseSpecs {
unit: Some(LUNARY[I]),
..Default::default()
};
ctx.close(specs)?;
}
//
// This should never happen, signal SPICE(BUG) and return.
//
spicelib::SETMSG(b"All of the unit storage space was not exhausted, and the last logical unit returned by FNDLUN was not zero. This should never happen.", ctx);
spicelib::SIGERR(b"SPICE(BUG)", ctx)?;
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
return Ok(());
}
//
// All that remains is to set RESRVD to TRUE, since we have
// exhausted all of the units in FNDLUN and LUN did not turn
// up.
//
*RESRVD = true;
}
spicelib::CHKOUT(b"T_TSTRLN", ctx)?;
Ok(())
}