Commit | Line | Data |
---|---|---|
761330fe DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
9953372d | 13 | #ifndef lint |
761330fe DF |
14 | static char sccsid[] = "@(#)arff.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
ae07d59d | 16 | |
9323c1d3 BJ |
17 | #include <sys/types.h> |
18 | #include <sys/stat.h> | |
ce4fd43b | 19 | #include <sys/time.h> |
9323c1d3 BJ |
20 | #include <signal.h> |
21 | #include <stdio.h> | |
670b62a8 | 22 | #include <sys/file.h> |
ae07d59d | 23 | |
9323c1d3 | 24 | #define dbprintf printf |
ae07d59d | 25 | |
9323c1d3 | 26 | struct rt_dat { |
ae07d59d SL |
27 | u_short rt_yr:5; /* year-1972 */ |
28 | u_short rt_dy:5; /* day */ | |
29 | u_short rt_mo:5; /* month */ | |
9323c1d3 | 30 | }; |
ae07d59d | 31 | |
9323c1d3 BJ |
32 | struct rt_axent { |
33 | char rt_sent[14]; | |
34 | }; | |
35 | ||
36 | struct rt_ent { | |
ae07d59d SL |
37 | char rt_pad; /* unusued */ |
38 | char rt_stat; /* type of entry, or end of seg */ | |
39 | u_short rt_name[3]; /* name, 3 words in rad50 form */ | |
98242db9 | 40 | u_short rt_len; /* length of file */ |
ae07d59d SL |
41 | char rt_chan; /* only used in temporary files */ |
42 | char rt_job; /* only used in temporary files */ | |
9953372d | 43 | struct rt_dat rt_date; /* creation date */ |
9323c1d3 | 44 | }; |
ae07d59d SL |
45 | |
46 | #define RT_TEMP 1 | |
47 | #define RT_NULL 2 | |
48 | #define RT_FILE 4 | |
49 | #define RT_ESEG 8 | |
50 | ||
51 | #define RT_BLOCK 512 /* block size */ | |
52 | #define RT_DIRSIZE 31 /* max # of directory segments */ | |
53 | ||
9323c1d3 | 54 | struct rt_head { |
ae07d59d SL |
55 | short rt_numseg; /* # of segments available */ |
56 | short rt_nxtseg; /* # of next logical segment */ | |
57 | short rt_lstseg; /* highest seg currently open */ | |
58 | u_short rt_entpad; /* extra words/directory entry */ | |
59 | short rt_stfile; /* block # where files begin */ | |
9323c1d3 | 60 | }; |
ae07d59d | 61 | |
9323c1d3 BJ |
62 | struct rt_dir { |
63 | struct rt_head rt_axhead; | |
64 | struct rt_ent rt_ents[72]; | |
ae07d59d | 65 | char _dirpad[6]; |
9323c1d3 | 66 | }; |
ae07d59d | 67 | |
9323c1d3 BJ |
68 | typedef struct fldope { |
69 | int startad; | |
70 | int count; | |
71 | struct rt_ent *rtdope; | |
72 | } FLDOPE; | |
ae07d59d | 73 | |
9323c1d3 | 74 | FLDOPE *lookup(); |
ae07d59d | 75 | |
9953372d SL |
76 | #define rt(p) ((struct rt_ent *) p ) |
77 | #define Ain1 03100 | |
78 | #define Ain2 050 | |
79 | #define flag(c) (flg[('c') - 'a']) | |
9323c1d3 | 80 | |
9953372d SL |
81 | char *man = "rxtd"; |
82 | char zeroes[512]; | |
ae07d59d | 83 | |
9323c1d3 BJ |
84 | extern char *val; |
85 | extern char table[256]; | |
ae07d59d | 86 | struct rt_dir rt_dir[RT_DIRSIZE] = { |
9953372d SL |
87 | { 4, 0, 1, 0, 14 }, |
88 | { { 0, RT_NULL, { 0, 0, 0 }, 494, 0 }, | |
89 | { 0, RT_ESEG } } | |
ae07d59d SL |
90 | }; |
91 | ||
92 | int rt_entsiz; | |
93 | int rt_nleft; | |
94 | struct rt_ent *rt_curend[RT_DIRSIZE]; | |
95 | int floppydes; | |
96 | int dirdirty; | |
97 | char *rt_last; | |
98 | char *defdev = "/dev/floppy"; | |
99 | ||
100 | char *opt = "vf"; | |
9323c1d3 BJ |
101 | |
102 | int signum[] = {SIGHUP, SIGINT, SIGQUIT, 0}; | |
ae07d59d SL |
103 | extern long lseek(); |
104 | int rcmd(), dcmd(), xcmd(), tcmd(); | |
105 | ||
9323c1d3 BJ |
106 | int (*comfun)(); |
107 | char flg[26]; | |
108 | char **namv; | |
109 | int namc; | |
110 | int file; | |
111 | ||
9323c1d3 | 112 | main(argc, argv) |
ae07d59d | 113 | char *argv[]; |
9323c1d3 BJ |
114 | { |
115 | register char *cp; | |
116 | ||
ae07d59d | 117 | if (argc < 2) |
9323c1d3 | 118 | usage(); |
ae07d59d SL |
119 | for (cp = argv[1]; *cp; cp++) |
120 | switch (*cp) { | |
121 | ||
122 | case 'm': | |
123 | case 'v': | |
124 | case 'u': | |
125 | case 'w': | |
126 | flg[*cp-'a']++; | |
127 | continue; | |
128 | case 'c': | |
670b62a8 | 129 | flag(c)++; |
ae07d59d SL |
130 | dirdirty++; |
131 | continue; | |
132 | ||
133 | case 'r': | |
134 | setcom(rcmd); | |
135 | flag(r)++; | |
136 | continue; | |
137 | ||
138 | case 'd': | |
139 | setcom(dcmd); | |
140 | flag(d)++; | |
141 | continue; | |
142 | ||
143 | case 'x': | |
144 | setcom(xcmd); | |
145 | continue; | |
146 | ||
147 | case 't': | |
148 | setcom(tcmd); | |
149 | continue; | |
150 | ||
151 | case 'f': | |
152 | defdev = argv[2]; | |
153 | argv++; | |
154 | argc--; | |
155 | continue; | |
156 | ||
157 | default: | |
158 | fprintf(stderr, "arff: bad option `%c'\n", *cp); | |
159 | exit(1); | |
9323c1d3 | 160 | } |
9323c1d3 | 161 | |
9323c1d3 BJ |
162 | namv = argv+2; |
163 | namc = argc-2; | |
ae07d59d SL |
164 | if (comfun == 0) { |
165 | if (flag(u) == 0) { | |
166 | fprintf(stderr, "arff: one of [%s] must be specified\n", | |
167 | man); | |
9323c1d3 BJ |
168 | exit(1); |
169 | } | |
170 | setcom(rcmd); | |
171 | } | |
172 | (*comfun)(); | |
173 | exit(notfound()); | |
174 | } | |
175 | ||
176 | setcom(fun) | |
ae07d59d | 177 | int (*fun)(); |
9323c1d3 | 178 | { |
ae07d59d | 179 | if (comfun != 0) { |
9323c1d3 BJ |
180 | fprintf(stderr, "arff: only one of [%s] allowed\n", man); |
181 | exit(1); | |
182 | } | |
183 | comfun = fun; | |
184 | } | |
185 | ||
9323c1d3 BJ |
186 | usage() |
187 | { | |
565c6f97 | 188 | fprintf(stderr, "usage: ar [%s][%s] archive files ...\n", opt, man); |
9323c1d3 BJ |
189 | exit(1); |
190 | } | |
191 | ||
9323c1d3 BJ |
192 | notfound() |
193 | { | |
ae07d59d | 194 | register i, n = 0; |
9323c1d3 | 195 | |
ae07d59d SL |
196 | for (i = 0; i < namc; i++) |
197 | if (namv[i]) { | |
9323c1d3 BJ |
198 | fprintf(stderr, "arff: %s not found\n", namv[i]); |
199 | n++; | |
200 | } | |
9953372d | 201 | return (n); |
9323c1d3 BJ |
202 | } |
203 | ||
9323c1d3 BJ |
204 | mesg(c) |
205 | { | |
ae07d59d SL |
206 | if (flag(v)) |
207 | if (c != 'c' || flag(v) > 1) | |
9323c1d3 BJ |
208 | printf("%c - %s\n", c, file); |
209 | } | |
210 | ||
211 | tcmd() | |
212 | { | |
ae07d59d | 213 | register char *de, *last; |
9323c1d3 | 214 | FLDOPE *lookup(), *dope; |
ae07d59d SL |
215 | int segnum, nleft; |
216 | register i; | |
9323c1d3 BJ |
217 | register struct rt_ent *rde; |
218 | ||
219 | rt_init(); | |
9953372d | 220 | if (namc != 0) { |
ae07d59d SL |
221 | for (i = 0; i < namc; i++) |
222 | if (dope = lookup(namv[i])) { | |
9323c1d3 BJ |
223 | rde = dope->rtdope; |
224 | rtls(rde); | |
225 | namv[i] = 0; | |
226 | } | |
9953372d SL |
227 | return; |
228 | } | |
229 | for (segnum = 0; segnum != -1; | |
230 | segnum = rt_dir[segnum].rt_axhead.rt_nxtseg - 1) { | |
231 | last = rt_last + segnum*2*RT_BLOCK; | |
232 | for (de = ((char *)&rt_dir[segnum])+10; de <= last; | |
233 | de += rt_entsiz) | |
234 | if (rtls(rt(de))) { | |
235 | nleft = (last-de)/rt_entsiz; | |
236 | #define ENTRIES "\n%d entries remaining in directory segment %d.\n" | |
237 | printf(ENTRIES, nleft, segnum+1); | |
238 | break; | |
239 | } | |
240 | } | |
9323c1d3 | 241 | } |
ae07d59d | 242 | |
9323c1d3 | 243 | rtls(de) |
ae07d59d | 244 | register struct rt_ent *de; |
9323c1d3 | 245 | { |
ae07d59d | 246 | int month, day, year; |
9323c1d3 BJ |
247 | char name[12], ext[4]; |
248 | ||
ae07d59d | 249 | switch (de->rt_stat) { |
9323c1d3 | 250 | |
ae07d59d SL |
251 | case RT_TEMP: |
252 | if (flag(v)) | |
253 | printf("Tempfile:\n"); | |
254 | /* fall thru...*/ | |
9323c1d3 | 255 | |
ae07d59d SL |
256 | case RT_FILE: |
257 | if (!flag(v)) { | |
258 | sunrad50(name, de->rt_name); | |
259 | printf("%s\n", name); | |
9323c1d3 | 260 | break; |
9323c1d3 | 261 | } |
ae07d59d SL |
262 | unrad50(2, de->rt_name, name); |
263 | unrad50(1, &(de->rt_name[2]), ext); | |
264 | day = de->rt_date.rt_dy; | |
265 | year = de->rt_date.rt_yr+72; | |
266 | month = de->rt_date.rt_mo; | |
267 | printf("%6.6s %3.3s %02d/%02d/%02d %d\n",name, | |
268 | ext, month, day, year, de->rt_len); | |
269 | break; | |
270 | ||
271 | case RT_NULL: | |
272 | printf("%-25.9s %d\n","<UNUSED>", de->rt_len); | |
273 | break; | |
274 | ||
275 | case RT_ESEG: | |
9953372d | 276 | return (1); |
9323c1d3 | 277 | } |
9953372d | 278 | return (0); |
9323c1d3 | 279 | } |
ae07d59d | 280 | |
9323c1d3 BJ |
281 | xcmd() |
282 | { | |
ae07d59d | 283 | register char *de, *last; |
fa61ea79 | 284 | int segnum; |
9323c1d3 BJ |
285 | char name[12]; |
286 | register int i; | |
287 | ||
288 | rt_init(); | |
9953372d | 289 | if (namc != 0) { |
ae07d59d SL |
290 | for (i = 0; i < namc; i++) |
291 | if (rtx(namv[i]) == 0) | |
292 | namv[i] = 0; | |
9953372d SL |
293 | return; |
294 | } | |
295 | for (segnum = 0; segnum != -1; | |
296 | segnum = rt_dir[segnum].rt_axhead.rt_nxtseg-1) | |
297 | for (last = rt_last+(segnum*2*RT_BLOCK), | |
298 | de = ((char *)&rt_dir[segnum])+10; de <= last; | |
9957da67 | 299 | de += rt_entsiz) { |
9953372d SL |
300 | switch (rt(de)->rt_stat) { |
301 | ||
302 | case RT_ESEG: | |
9957da67 | 303 | break; /* exit loop and try next segment */ |
9953372d SL |
304 | |
305 | case RT_TEMP: | |
306 | case RT_FILE: | |
307 | sunrad50(name,rt(de)->rt_name); | |
308 | rtx(name); | |
309 | ||
310 | case RT_NULL: | |
9957da67 RC |
311 | default: |
312 | continue; | |
9953372d | 313 | } |
9957da67 RC |
314 | break; |
315 | } | |
9323c1d3 | 316 | } |
ae07d59d | 317 | |
9323c1d3 | 318 | rtx(name) |
ae07d59d | 319 | char *name; |
9323c1d3 BJ |
320 | { |
321 | register FLDOPE *dope; | |
322 | FLDOPE *lookup(); | |
323 | register startad, count; | |
ae07d59d SL |
324 | int file; |
325 | char buff[512]; | |
9323c1d3 BJ |
326 | |
327 | ||
ae07d59d SL |
328 | if (dope = lookup(name)) { |
329 | if (flag(v)) | |
9323c1d3 BJ |
330 | rtls(dope->rtdope); |
331 | else | |
332 | printf("x - %s\n",name); | |
333 | ||
ae07d59d | 334 | if ((file = creat(name, 0666)) < 0) |
9953372d | 335 | return (1); |
9323c1d3 BJ |
336 | count = dope->count; |
337 | startad = dope->startad; | |
338 | for( ; count > 0 ; count -= 512) { | |
ae07d59d SL |
339 | lread(startad, 512, buff); |
340 | write(file, buff, 512); | |
9323c1d3 BJ |
341 | startad += 512; |
342 | } | |
343 | close(file); | |
9953372d | 344 | return (0); |
9323c1d3 | 345 | } |
9953372d | 346 | return (1); |
9323c1d3 | 347 | } |
ae07d59d | 348 | |
9323c1d3 BJ |
349 | rt_init() |
350 | { | |
351 | static initized = 0; | |
ae07d59d | 352 | register char *de, *last; |
fa61ea79 | 353 | register i; |
ffa51da0 KH |
354 | int dirnum; |
355 | char *mode; | |
ffa51da0 | 356 | FILE *temp_floppydes; |
9323c1d3 | 357 | |
ae07d59d SL |
358 | if (initized) |
359 | return; | |
9323c1d3 | 360 | initized = 1; |
670b62a8 SL |
361 | if (flag(c)) { |
362 | struct stat sb; | |
363 | char response[128]; | |
364 | int tty; | |
365 | ||
366 | if (stat(defdev, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG) | |
367 | goto ignore; | |
368 | tty = open("/dev/tty", O_RDWR); | |
369 | #define SURE "Are you sure you want to clobber the floppy? " | |
370 | write(tty, SURE, sizeof (SURE)); | |
371 | read(tty, response, sizeof (response)); | |
372 | if (*response != 'y') | |
373 | exit(50); | |
374 | close(tty); | |
375 | ignore: | |
376 | ; | |
377 | } | |
ae07d59d | 378 | if (flag(c) || flag(d) || flag(r)) |
ffa51da0 | 379 | mode = "r+"; |
9323c1d3 | 380 | else |
ffa51da0 | 381 | mode = "r"; |
ae07d59d | 382 | if ((temp_floppydes = fopen(defdev, mode)) == NULL) { |
ffa51da0 | 383 | perror(defdev); |
565c6f97 | 384 | exit(1); |
ffa51da0 KH |
385 | } else |
386 | floppydes = fileno(temp_floppydes); | |
ae07d59d SL |
387 | if (!flag(c)) { |
388 | lread(6*RT_BLOCK, 2*RT_BLOCK, (char *)&rt_dir[0]); | |
fa61ea79 | 389 | dirnum = rt_dir[0].rt_axhead.rt_numseg; |
45ee91c7 HS |
390 | /* check for blank/uninitialized diskette */ |
391 | if (dirnum <= 0) { | |
392 | fprintf(stderr,"arff: bad directory format\n"); | |
393 | exit(1); | |
394 | } | |
fa61ea79 | 395 | if (dirnum > RT_DIRSIZE) { |
ae07d59d SL |
396 | fprintf(stderr,"arff: too many directory segments\n"); |
397 | exit(1); | |
fa61ea79 | 398 | } |
ae07d59d SL |
399 | for (i = 1; i < dirnum; i++) |
400 | lread((6+2*i)*RT_BLOCK, 2*RT_BLOCK, (char *)&rt_dir[i]); | |
0dfff170 BJ |
401 | } else |
402 | dirnum = 1; | |
9323c1d3 | 403 | |
fa61ea79 BJ |
404 | rt_entsiz = 2*rt_dir[0].rt_axhead.rt_entpad + 14; |
405 | rt_entsiz = 14; /* assume rt_entpad = 0 ??? */ | |
406 | rt_last = ((char *) &rt_dir[0]) + 10 + 1014/rt_entsiz*rt_entsiz; | |
407 | rt_nleft = 0; | |
408 | ||
ae07d59d SL |
409 | for (i = 0; i < dirnum; i++) { |
410 | last = rt_last + i*2*RT_BLOCK; | |
411 | for (de = ((char *)&rt_dir[i])+10; de <= last; de += rt_entsiz) | |
412 | if (rt(de)->rt_stat == RT_ESEG) | |
413 | break; | |
414 | rt_curend[i] = rt(de); | |
415 | rt_nleft += (last-de)/rt_entsiz; | |
9323c1d3 | 416 | } |
9323c1d3 BJ |
417 | } |
418 | ||
419 | static FLDOPE result; | |
ae07d59d | 420 | |
9323c1d3 BJ |
421 | FLDOPE * |
422 | lookup(name) | |
ae07d59d | 423 | char *name; |
9323c1d3 BJ |
424 | { |
425 | unsigned short rname[3]; | |
ae07d59d | 426 | register char *de, *last; |
fa61ea79 | 427 | int segnum; |
9323c1d3 BJ |
428 | register index; |
429 | ||
430 | srad50(name,rname); | |
431 | ||
565c6f97 | 432 | /* |
9323c1d3 BJ |
433 | * Search for name, accumulate blocks in index |
434 | */ | |
435 | rt_init(); | |
ae07d59d SL |
436 | for (segnum = 0; segnum != -1; |
437 | segnum = rt_dir[segnum].rt_axhead.rt_nxtseg - 1) | |
438 | { | |
439 | index = 0; | |
440 | last = rt_last + segnum*2*RT_BLOCK; | |
441 | for (de=((char *)&rt_dir[segnum])+10; | |
442 | rt(de)->rt_stat != RT_ESEG; de += rt_entsiz) | |
443 | switch(rt(de)->rt_stat) { | |
444 | ||
445 | case RT_FILE: | |
446 | case RT_TEMP: | |
447 | if(samename(rname,rt(de)->rt_name)) { | |
448 | result.count = rt(de)->rt_len * 512; | |
449 | result.startad = 512* | |
450 | (rt_dir[segnum].rt_axhead.rt_stfile + index); | |
451 | result.rtdope = (struct rt_ent *) de; | |
9953372d | 452 | return (&result); |
ae07d59d SL |
453 | } |
454 | ||
455 | case RT_NULL: | |
456 | index += rt(de)->rt_len; | |
457 | } | |
fa61ea79 | 458 | } |
9953372d | 459 | return ((FLDOPE *) 0); |
ae07d59d | 460 | |
9323c1d3 | 461 | } |
ae07d59d | 462 | |
9323c1d3 | 463 | static |
ae07d59d SL |
464 | samename(a, b) |
465 | u_short a[], b[]; | |
9323c1d3 | 466 | { |
9953372d | 467 | return (*a == *b && a[1] == b[1] && a[2] == b[2] ); |
9323c1d3 BJ |
468 | } |
469 | ||
ae07d59d SL |
470 | rad50(cp, out) |
471 | register u_char *cp; | |
472 | u_short *out; | |
9323c1d3 | 473 | { |
ae07d59d | 474 | register index, temp; |
9323c1d3 | 475 | |
ae07d59d | 476 | for (index = 0; *cp; index++) { |
9323c1d3 | 477 | temp = Ain1 * table[*cp++]; |
ae07d59d | 478 | if (*cp!=0) { |
9323c1d3 | 479 | temp += Ain2 * table[*cp++]; |
9323c1d3 BJ |
480 | if(*cp!=0) |
481 | temp += table[*cp++]; | |
482 | } | |
9323c1d3 BJ |
483 | out[index] = temp; |
484 | } | |
485 | } | |
9323c1d3 | 486 | |
ae07d59d SL |
487 | #define reduce(x, p, q) (x = v[p/q], p %= q); |
488 | ||
489 | unrad50(count, in, cp) | |
490 | u_short *in; | |
491 | register char *cp; | |
9323c1d3 | 492 | { |
ae07d59d SL |
493 | register i, temp; |
494 | register u_char *v = (u_char *) val; | |
9323c1d3 | 495 | |
ae07d59d | 496 | for (i = 0; i < count; i++) { |
9323c1d3 | 497 | temp = in[i]; |
ae07d59d SL |
498 | reduce(*cp++, temp, Ain1); |
499 | reduce(*cp++, temp, Ain2); | |
500 | reduce(*cp++, temp, 1); | |
9323c1d3 BJ |
501 | } |
502 | *cp=0; | |
503 | } | |
504 | ||
ae07d59d SL |
505 | srad50(name, rname) |
506 | register char *name; | |
507 | register u_short *rname; | |
9323c1d3 | 508 | { |
ae07d59d SL |
509 | register index; |
510 | register char *cp; | |
511 | char file[7], ext[4]; | |
512 | ||
565c6f97 | 513 | /* |
9323c1d3 BJ |
514 | * Find end of pathname |
515 | */ | |
ae07d59d SL |
516 | for (cp = name; *cp++; ) |
517 | ; | |
518 | while (cp >= name && *--cp != '/') | |
519 | ; | |
9323c1d3 | 520 | cp++; |
565c6f97 | 521 | /* |
9323c1d3 | 522 | * Change to rad50 |
9323c1d3 | 523 | */ |
ae07d59d | 524 | for (index = 0; *cp; ) { |
9323c1d3 | 525 | file[index++] = *cp++; |
ae07d59d | 526 | if (*cp == '.') { |
9323c1d3 BJ |
527 | cp++; |
528 | break; | |
529 | } | |
ae07d59d | 530 | if (index >= 6) { |
9323c1d3 BJ |
531 | break; |
532 | } | |
533 | } | |
534 | file[index] = 0; | |
ae07d59d | 535 | for (index = 0; *cp; ) { |
9323c1d3 | 536 | ext[index++] = *cp++; |
ae07d59d | 537 | if (*cp == '.' || index >= 3) |
9323c1d3 | 538 | break; |
9323c1d3 BJ |
539 | } |
540 | ext[index]=0; | |
ae07d59d SL |
541 | rname[0] = rname[1] = rname[2] = 0; |
542 | rad50((u_char *)file, rname); | |
543 | rad50((u_char *)ext, rname+2); | |
9323c1d3 | 544 | } |
ae07d59d SL |
545 | |
546 | sunrad50(name, rname) | |
547 | u_short rname[]; | |
548 | register char *name; | |
9323c1d3 BJ |
549 | { |
550 | register char *cp, *cp2; | |
551 | char ext[4]; | |
552 | ||
ae07d59d SL |
553 | unrad50(2, rname, name); |
554 | unrad50(1, rname + 2, ext); | |
555 | /* | |
556 | * Jam name and extension together with a dot | |
557 | * deleting white space | |
558 | */ | |
559 | for (cp = name; *cp++;) | |
560 | ; | |
561 | --cp; | |
562 | while (*--cp == ' ' && cp >= name) | |
563 | ; | |
564 | *++cp = '.'; | |
565 | cp++; | |
566 | for (cp2 = ext; *cp2 != ' ' && cp2 < ext+3;) | |
9323c1d3 | 567 | *cp++ = *cp2++; |
9323c1d3 | 568 | *cp=0; |
ae07d59d SL |
569 | if (cp[-1] == '.') |
570 | cp[-1] = 0; | |
9323c1d3 BJ |
571 | } |
572 | ||
573 | static char *oval = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.@0123456789"; | |
574 | static char *val = " abcdefghijklmnopqrstuvwxyz$.@0123456789"; | |
ae07d59d | 575 | |
9323c1d3 BJ |
576 | static char table[256] = { |
577 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, | |
578 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, | |
579 | 0, 29, 29, 29, 27, 29, 29, 29, 29, 29, 29, 29, 29, 29, 28, 29, | |
580 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 29, 29, 29, 29, 29, 29, | |
581 | 29, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
582 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 29, 29, 29, 29, 29, | |
583 | 29, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
584 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 29, 29, 29, 29, 29, | |
585 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, | |
586 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, | |
587 | 0, 29, 29, 29, 27, 29, 29, 29, 29, 29, 29, 29, 29, 29, 28, 29, | |
588 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 29, 29, 29, 29, 29, 29, | |
589 | 29, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
590 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 29, 29, 29, 29, 29, | |
591 | 29, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
592 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 29, 29, 29, 29 }; | |
593 | ||
ae07d59d SL |
594 | /* |
595 | * Logical to physical adress translation | |
596 | */ | |
597 | long | |
598 | trans(logical) | |
599 | register int logical; | |
9323c1d3 | 600 | { |
9323c1d3 BJ |
601 | register int sector, bytes, track; |
602 | ||
ae07d59d SL |
603 | logical += 26*128; |
604 | bytes = (logical&127); | |
9323c1d3 | 605 | logical >>= 7; |
ae07d59d | 606 | sector = logical%26; |
9323c1d3 | 607 | if(sector >= 13) |
ae07d59d | 608 | sector = sector*2+1; |
9323c1d3 BJ |
609 | else |
610 | sector *= 2; | |
ae07d59d | 611 | sector += 26 + ((track = (logical/26))-1)*6; |
9323c1d3 | 612 | sector %= 26; |
9953372d | 613 | return ((((track*26)+sector) << 7) + bytes); |
9323c1d3 | 614 | } |
ae07d59d SL |
615 | |
616 | lread(startad, count, obuff) | |
617 | register startad, count; | |
618 | register char *obuff; | |
9323c1d3 BJ |
619 | { |
620 | long trans(); | |
621 | extern floppydes; | |
ae07d59d SL |
622 | register int size = flag(m) ? 512 : 128; |
623 | ||
9323c1d3 | 624 | rt_init(); |
ae07d59d SL |
625 | while ((count -= size) >= 0) { |
626 | lseek(floppydes, flag(m) ? | |
627 | (long)startad : trans(startad), 0); | |
628 | if (read(floppydes, obuff, size) != size) | |
629 | fprintf(stderr, "arff: read error block %d\n", | |
630 | startad/size); | |
631 | obuff += size; | |
632 | startad += size; | |
633 | } | |
9323c1d3 | 634 | } |
ae07d59d SL |
635 | |
636 | lwrite(startad, count, obuff) | |
637 | register startad, count; | |
638 | register char *obuff; | |
9323c1d3 BJ |
639 | { |
640 | long trans(); | |
641 | extern floppydes; | |
ae07d59d SL |
642 | register int size = flag(m) ? 512 : 128; |
643 | ||
9323c1d3 | 644 | rt_init(); |
ae07d59d SL |
645 | while ((count -= size) >= 0) { |
646 | lseek(floppydes, flag(m) ? | |
647 | (long)startad : trans(startad), 0); | |
648 | if (write(floppydes, obuff, size) != size) | |
649 | fprintf(stderr, "arff: write error block %d\n", | |
650 | startad/size); | |
651 | obuff += size; | |
652 | startad += size; | |
653 | } | |
9323c1d3 BJ |
654 | } |
655 | ||
656 | rcmd() | |
657 | { | |
658 | register int i; | |
659 | ||
660 | rt_init(); | |
ae07d59d SL |
661 | if (namc > 0) |
662 | for (i = 0; i < namc; i++) | |
663 | if (rtr(namv[i]) == 0) | |
664 | namv[i] = 0; | |
9323c1d3 BJ |
665 | } |
666 | ||
667 | rtr(name) | |
ae07d59d | 668 | char *name; |
9323c1d3 | 669 | { |
ae07d59d SL |
670 | register FLDOPE *dope; |
671 | register struct rt_ent *de; | |
672 | struct stat buf; | |
673 | register struct stat *bufp = &buf; | |
fa61ea79 BJ |
674 | int segnum; |
675 | register char *last; | |
9323c1d3 | 676 | |
ae07d59d | 677 | if (stat(name, bufp) < 0) { |
0dfff170 | 678 | perror(name); |
9953372d | 679 | return (-1); |
0dfff170 | 680 | } |
ae07d59d | 681 | if (dope = lookup(name)) { |
9323c1d3 BJ |
682 | /* can replace, no problem */ |
683 | de = dope->rtdope; | |
ae07d59d | 684 | if (bufp->st_size <= (de->rt_len * 512)) |
9323c1d3 | 685 | printf("r - %s\n",name), |
ae07d59d | 686 | toflop(name, bufp->st_size, dope); |
9323c1d3 | 687 | else { |
9953372d SL |
688 | fprintf(stderr, |
689 | "%s will not fit in currently used file on floppy\n", | |
690 | name); | |
691 | return (-1); | |
9323c1d3 | 692 | } |
9953372d SL |
693 | goto found; |
694 | } | |
695 | /* | |
696 | * Search for vacant spot | |
697 | */ | |
698 | for (segnum = 0; segnum != -1; | |
699 | segnum = rt_dir[segnum].rt_axhead.rt_nxtseg - 1) | |
700 | { | |
701 | last = rt_last + segnum*2*RT_BLOCK; | |
702 | for (de = rt_dir[segnum].rt_ents; | |
703 | rt(de)->rt_stat != RT_ESEG; de++) | |
704 | if ((de)->rt_stat == RT_NULL) { | |
705 | if (bufp->st_size <= (de->rt_len*512)) { | |
706 | printf("a - %s\n",name), | |
707 | mkent(de, segnum, bufp,name); | |
708 | goto found; | |
9323c1d3 | 709 | } |
9953372d SL |
710 | continue; |
711 | } | |
9323c1d3 | 712 | } |
9953372d SL |
713 | printf("%s: no slot for file\n", name); |
714 | return (-1); | |
ae07d59d SL |
715 | |
716 | found: | |
717 | if (dope = lookup(name)) { | |
718 | toflop(name, bufp->st_size, dope); | |
0dfff170 | 719 | return (0); |
9323c1d3 | 720 | } |
0dfff170 BJ |
721 | printf("%s: internal error, added then not found\n", name); |
722 | return (-1); | |
9323c1d3 | 723 | } |
ae07d59d SL |
724 | |
725 | mkent(de, segnum, bufp, name) | |
726 | register struct rt_ent *de; | |
727 | int segnum; | |
728 | register struct stat *bufp; | |
729 | char *name; | |
9323c1d3 | 730 | { |
ae07d59d SL |
731 | struct tm *localtime(); |
732 | register struct tm *timp; | |
733 | register struct rt_ent *workp; | |
734 | int count; | |
9323c1d3 BJ |
735 | |
736 | count = (((bufp->st_size -1) >>9) + 1); | |
ae07d59d SL |
737 | /* make sure there is room */ |
738 | if (de->rt_len == count) | |
9323c1d3 | 739 | goto overwrite; |
ae07d59d SL |
740 | if ((char *)rt_curend[segnum] == (rt_last + (segnum*2*RT_BLOCK))) { |
741 | /* no entries left on segment */ | |
742 | if (flag(o)) | |
9323c1d3 | 743 | goto overwrite; |
9953372d SL |
744 | fprintf(stderr, "Directory segment #%d full on %s\n", |
745 | segnum+1, defdev); | |
9323c1d3 BJ |
746 | exit(1); |
747 | } | |
ae07d59d SL |
748 | /* copy directory entries up */ |
749 | for (workp = rt_curend[segnum]+1; workp > de; workp--) | |
9323c1d3 BJ |
750 | *workp = workp[-1]; |
751 | de[1].rt_len -= count; | |
752 | de->rt_len = count; | |
fa61ea79 | 753 | rt_curend[segnum]++; |
9323c1d3 | 754 | rt_nleft--; |
ae07d59d | 755 | |
9323c1d3 BJ |
756 | overwrite: |
757 | srad50(name,de->rt_name); | |
758 | timp = localtime(&bufp->st_mtime); | |
db34e2f8 | 759 | de->rt_date.rt_dy = timp->tm_mday; |
9323c1d3 BJ |
760 | de->rt_date.rt_mo = timp->tm_mon + 1; |
761 | de->rt_date.rt_yr = timp->tm_year - 72; | |
762 | de->rt_stat = RT_FILE; | |
763 | de->rt_pad = 0; | |
764 | de->rt_chan = 0; | |
765 | de->rt_job = 0; | |
ae07d59d | 766 | lwrite((6+segnum*2)*RT_BLOCK, 2*RT_BLOCK, (char *)&rt_dir[segnum]); |
9323c1d3 BJ |
767 | } |
768 | ||
ae07d59d SL |
769 | toflop(name, ocount, dope) |
770 | char *name; | |
771 | register FLDOPE *dope; | |
772 | long ocount; | |
9323c1d3 BJ |
773 | { |
774 | register file, n, startad = dope->startad, count = ocount; | |
775 | char buff[512]; | |
776 | ||
ae07d59d SL |
777 | file = open(name, 0); |
778 | if (file < 0) { | |
779 | fprintf(stderr, "arff: couldn't open %s\n",name); | |
780 | exit(1); | |
781 | } | |
9323c1d3 | 782 | for( ; count >= 512; count -= 512) { |
ae07d59d SL |
783 | read(file, buff, 512); |
784 | lwrite(startad, 512, buff); | |
9323c1d3 BJ |
785 | startad += 512; |
786 | } | |
ae07d59d | 787 | read(file, buff, count); |
9323c1d3 | 788 | close(file); |
ae07d59d SL |
789 | if (count <= 0) |
790 | return; | |
791 | for (n = count; n < 512; n ++) | |
792 | buff[n] = 0; | |
793 | lwrite(startad, 512, buff); | |
794 | count = (dope->rtdope->rt_len*512-ocount)/512 ; | |
795 | if (count <= 0) | |
796 | return; | |
797 | for ( ; count > 0 ; count--) { | |
9323c1d3 | 798 | startad += 512; |
ae07d59d | 799 | lwrite(startad, 512, zeroes); |
9323c1d3 | 800 | } |
9323c1d3 | 801 | } |
ae07d59d | 802 | |
9323c1d3 BJ |
803 | dcmd() |
804 | { | |
805 | register int i; | |
806 | ||
807 | rt_init(); | |
ae07d59d SL |
808 | if (namc) |
809 | for (i = 0; i < namc; i++) | |
810 | if (rtk(namv[i])==0) | |
811 | namv[i]=0; | |
812 | if (dirdirty) | |
9323c1d3 | 813 | scrunch(); |
9323c1d3 | 814 | } |
ae07d59d | 815 | |
9323c1d3 | 816 | rtk(name) |
ae07d59d | 817 | char *name; |
9323c1d3 BJ |
818 | { |
819 | register FLDOPE *dope; | |
820 | register struct rt_ent *de; | |
821 | FLDOPE *lookup(); | |
822 | ||
ae07d59d | 823 | if (dope = lookup(name)) { |
9323c1d3 BJ |
824 | printf("d - %s\n",name); |
825 | de = dope->rtdope; | |
826 | de->rt_stat = RT_NULL; | |
827 | de->rt_name[0] = 0; | |
828 | de->rt_name[1] = 0; | |
829 | de->rt_name[2] = 0; | |
9953372d | 830 | *((u_short *)&(de->rt_date)) = 0; |
9323c1d3 | 831 | dirdirty = 1; |
9953372d | 832 | return (0); |
9323c1d3 | 833 | } |
9953372d | 834 | return (1); |
9323c1d3 | 835 | } |
ae07d59d SL |
836 | |
837 | scrunch() | |
838 | { | |
fa61ea79 BJ |
839 | register struct rt_ent *de , *workp; |
840 | register segnum; | |
ae07d59d SL |
841 | |
842 | for (segnum = 0; segnum != -1; | |
fa61ea79 | 843 | segnum = rt_dir[segnum].rt_axhead.rt_nxtseg - 1) { |
ae07d59d | 844 | for (de = rt_dir[segnum].rt_ents; de <= rt_curend[segnum]; de++) |
9953372d | 845 | if (de->rt_stat == RT_NULL && |
45ee91c7 | 846 | (de+1)->rt_stat == RT_NULL) { |
ae07d59d | 847 | (de+1)->rt_len += de->rt_len; |
45ee91c7 | 848 | for (workp=de; workp<rt_curend[segnum]; workp++) |
ae07d59d SL |
849 | *workp = workp[1]; |
850 | de--; | |
851 | rt_curend[segnum]--; | |
852 | rt_nleft++; | |
ae07d59d | 853 | } |
45ee91c7 HS |
854 | lwrite((6+segnum*2)*RT_BLOCK, 2*RT_BLOCK, |
855 | (char *)&rt_dir[segnum]); | |
9323c1d3 | 856 | } |
45ee91c7 | 857 | dirdirty = 0; |
9323c1d3 | 858 | } |